diff --git a/pulsar-broker-common/src/main/java/com/yahoo/pulsar/broker/ServiceConfiguration.java b/pulsar-broker-common/src/main/java/com/yahoo/pulsar/broker/ServiceConfiguration.java index 11c2220101743..e1071572f012b 100644 --- a/pulsar-broker-common/src/main/java/com/yahoo/pulsar/broker/ServiceConfiguration.java +++ b/pulsar-broker-common/src/main/java/com/yahoo/pulsar/broker/ServiceConfiguration.java @@ -22,13 +22,15 @@ import com.google.common.collect.Sets; import com.yahoo.pulsar.client.impl.auth.AuthenticationDisabled; +import com.yahoo.pulsar.common.configuration.FieldContext; +import com.yahoo.pulsar.common.configuration.PulsarConfiguration; /** * * Pulsar service configuration object. * */ -public class ServiceConfiguration { +public class ServiceConfiguration implements PulsarConfiguration{ /***** --- pulsar configuration --- ****/ // Zookeeper quorum connection string diff --git a/pulsar-broker-common/src/main/java/com/yahoo/pulsar/broker/FieldContext.java b/pulsar-broker-common/src/main/java/com/yahoo/pulsar/common/configuration/FieldContext.java similarity index 97% rename from pulsar-broker-common/src/main/java/com/yahoo/pulsar/broker/FieldContext.java rename to pulsar-broker-common/src/main/java/com/yahoo/pulsar/common/configuration/FieldContext.java index 77550b635ccfa..cf693239b5484 100644 --- a/pulsar-broker-common/src/main/java/com/yahoo/pulsar/broker/FieldContext.java +++ b/pulsar-broker-common/src/main/java/com/yahoo/pulsar/common/configuration/FieldContext.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.yahoo.pulsar.broker; +package com.yahoo.pulsar.common.configuration; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/pulsar-broker-common/src/main/java/com/yahoo/pulsar/common/configuration/PulsarConfiguration.java b/pulsar-broker-common/src/main/java/com/yahoo/pulsar/common/configuration/PulsarConfiguration.java new file mode 100644 index 0000000000000..b8552b929e8e6 --- /dev/null +++ b/pulsar-broker-common/src/main/java/com/yahoo/pulsar/common/configuration/PulsarConfiguration.java @@ -0,0 +1,25 @@ +/** + * Copyright 2016 Yahoo Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.yahoo.pulsar.common.configuration; + +import java.util.Properties; + +public interface PulsarConfiguration { + + public Properties getProperties(); + + public void setProperties(Properties properties); +} \ No newline at end of file diff --git a/pulsar-broker-common/src/main/java/com/yahoo/pulsar/broker/ServiceConfigurationLoader.java b/pulsar-broker-common/src/main/java/com/yahoo/pulsar/common/configuration/PulsarConfigurationLoader.java similarity index 74% rename from pulsar-broker-common/src/main/java/com/yahoo/pulsar/broker/ServiceConfigurationLoader.java rename to pulsar-broker-common/src/main/java/com/yahoo/pulsar/common/configuration/PulsarConfigurationLoader.java index a22c810425088..78e84f41e2072 100644 --- a/pulsar-broker-common/src/main/java/com/yahoo/pulsar/broker/ServiceConfigurationLoader.java +++ b/pulsar-broker-common/src/main/java/com/yahoo/pulsar/common/configuration/PulsarConfigurationLoader.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.yahoo.pulsar.broker; +package com.yahoo.pulsar.common.configuration; import static com.google.common.base.Preconditions.checkNotNull; import static com.yahoo.pulsar.common.util.FieldParser.update; @@ -30,22 +30,23 @@ * * */ -public class ServiceConfigurationLoader { +public class PulsarConfigurationLoader { /** - * Creates ServiceConfiguration and loads it with populated attribute values loaded from provided property file. + * Creates PulsarConfiguration and loads it with populated attribute values loaded from provided property file. * * @param configFile * @throws IOException * @throws IllegalArgumentException */ - public static ServiceConfiguration create(String configFile) throws IOException, IllegalArgumentException { + public static T create(String configFile, + Class clazz) throws IOException, IllegalArgumentException { checkNotNull(configFile); - return create(new FileInputStream(configFile)); + return create(new FileInputStream(configFile), clazz); } /** - * Creates ServiceConfiguration and loads it with populated attribute values loaded from provided inputstream + * Creates PulsarConfiguration and loads it with populated attribute values loaded from provided inputstream * property file. * * @param inStream @@ -54,12 +55,13 @@ public static ServiceConfiguration create(String configFile) throws IOException, * @throws IllegalArgumentException * if the input stream contains incorrect value type */ - public static ServiceConfiguration create(InputStream inStream) throws IOException, IllegalArgumentException { + public static T create(InputStream inStream, + Class clazz) throws IOException, IllegalArgumentException { try { checkNotNull(inStream); Properties properties = new Properties(); properties.load(inStream); - return (create(properties)); + return (create(properties, clazz)); } finally { if (inStream != null) { inStream.close(); @@ -68,11 +70,17 @@ public static ServiceConfiguration create(InputStream inStream) throws IOExcepti } @SuppressWarnings({ "rawtypes", "unchecked" }) - public static ServiceConfiguration create(Properties properties) throws IOException, IllegalArgumentException { + protected static T create(Properties properties, + Class clazz) throws IOException, IllegalArgumentException { checkNotNull(properties); - ServiceConfiguration configuration = new ServiceConfiguration(); - configuration.setProperties(properties); - update((Map) properties, configuration); + T configuration = null; + try { + configuration = (T) clazz.newInstance(); + configuration.setProperties(properties); + update((Map) properties, configuration); + } catch (InstantiationException | IllegalAccessException e) { + throw new IllegalArgumentException("Failed to instantiate " + clazz.getName(), e); + } return configuration; } diff --git a/pulsar-broker/src/test/java/com/yahoo/pulsar/common/naming/ServiceConfigurationLoaderTest.java b/pulsar-broker-common/src/test/java/com/yahoo/pulsar/common/configuration/PulsarConfigurationLoaderTest.java similarity index 55% rename from pulsar-broker/src/test/java/com/yahoo/pulsar/common/naming/ServiceConfigurationLoaderTest.java rename to pulsar-broker-common/src/test/java/com/yahoo/pulsar/common/configuration/PulsarConfigurationLoaderTest.java index c060f87130bb6..918444d119d58 100644 --- a/pulsar-broker/src/test/java/com/yahoo/pulsar/common/naming/ServiceConfigurationLoaderTest.java +++ b/pulsar-broker-common/src/test/java/com/yahoo/pulsar/common/configuration/PulsarConfigurationLoaderTest.java @@ -13,50 +13,83 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.yahoo.pulsar.common.naming; +package com.yahoo.pulsar.common.configuration; -import static com.yahoo.pulsar.broker.ServiceConfigurationLoader.isComplete; +import static com.yahoo.pulsar.common.configuration.PulsarConfigurationLoader.isComplete; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.InputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; import java.util.Properties; import org.testng.annotations.Test; -import com.yahoo.pulsar.broker.FieldContext; import com.yahoo.pulsar.broker.ServiceConfiguration; -import com.yahoo.pulsar.broker.ServiceConfigurationLoader; -public class ServiceConfigurationLoaderTest { +public class PulsarConfigurationLoaderTest { @Test - public void testServiceConfiguraitonLoadingStream() throws Exception { - final String fileName = "configurations/pulsar_broker_test.conf"; // test-resource file - InputStream stream = this.getClass().getClassLoader().getResourceAsStream(fileName); - final ServiceConfiguration serviceConfig = ServiceConfigurationLoader.create(stream); + public void testPulsarConfiguraitonLoadingStream() throws Exception { + File testConfigFile = new File("tmp." + System.currentTimeMillis() + ".properties"); + if (testConfigFile.exists()) { + testConfigFile.delete(); + } + final String zkServer = "z1.example.com,z2.example.com,z3.example.com"; + PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(testConfigFile))); + printWriter.println("zookeeperServers=" + zkServer); + printWriter.println("globalZookeeperServers=gz1.example.com,gz2.example.com,gz3.example.com/foo"); + printWriter.println("brokerDeleteInactiveTopicsEnabled=true"); + printWriter.println("statusFilePath=/tmp/status.html"); + printWriter.println("managedLedgerDefaultEnsembleSize=1"); + printWriter.println("backlogQuotaDefaultLimitGB=18"); + printWriter.println("clusterName=usc"); + printWriter.println("brokerClientAuthenticationPlugin=test.xyz.client.auth.plugin"); + printWriter.println("brokerClientAuthenticationParameters=role:my-role"); + printWriter.println("superUserRoles=appid1,appid2"); + printWriter.println("brokerServicePort=7777"); + printWriter.println("managedLedgerDefaultMarkDeleteRateLimit=5.0"); + printWriter.close(); + testConfigFile.deleteOnExit(); + InputStream stream = new FileInputStream(testConfigFile); + final ServiceConfiguration serviceConfig = PulsarConfigurationLoader.create(stream, ServiceConfiguration.class); assertNotNull(serviceConfig); + assertEquals(serviceConfig.getZookeeperServers(), zkServer); + assertEquals(serviceConfig.isBrokerDeleteInactiveTopicsEnabled(), true); + assertEquals(serviceConfig.getBacklogQuotaDefaultLimitGB(), 18); + assertEquals(serviceConfig.getClusterName(), "usc"); + assertEquals(serviceConfig.getBrokerClientAuthenticationParameters(), "role:my-role"); + assertEquals(serviceConfig.getBrokerServicePort(), 7777); } @Test - public void testServiceConfiguraitonLoadingProp() throws Exception { + public void testPulsarConfiguraitonLoadingProp() throws Exception { final String zk = "localhost:2184"; final Properties prop = new Properties(); prop.setProperty("zookeeperServers", zk); - final ServiceConfiguration serviceConfig = ServiceConfigurationLoader.create(prop); + final ServiceConfiguration serviceConfig = PulsarConfigurationLoader.create(prop, ServiceConfiguration.class); assertNotNull(serviceConfig); assertEquals(serviceConfig.getZookeeperServers(), zk); } @Test - public void testServiceConfiguraitonComplete() throws Exception { + public void testPulsarConfiguraitonComplete() throws Exception { final String zk = "localhost:2184"; final Properties prop = new Properties(); prop.setProperty("zookeeperServers", zk); - final ServiceConfiguration serviceConfig = ServiceConfigurationLoader.create(prop); - assertEquals(serviceConfig.getZookeeperServers(), zk); + final ServiceConfiguration serviceConfig = PulsarConfigurationLoader.create(prop, ServiceConfiguration.class); + try { + isComplete(serviceConfig); + fail("it should fail as config is not complete"); + } catch (IllegalArgumentException e) { + // Ok + } } @Test diff --git a/pulsar-broker/src/main/java/com/yahoo/pulsar/PulsarBrokerStarter.java b/pulsar-broker/src/main/java/com/yahoo/pulsar/PulsarBrokerStarter.java index 287e7adbbf423..aa167ec3cc9bf 100644 --- a/pulsar-broker/src/main/java/com/yahoo/pulsar/PulsarBrokerStarter.java +++ b/pulsar-broker/src/main/java/com/yahoo/pulsar/PulsarBrokerStarter.java @@ -15,8 +15,8 @@ */ package com.yahoo.pulsar; -import static com.yahoo.pulsar.broker.ServiceConfigurationLoader.create; -import static com.yahoo.pulsar.broker.ServiceConfigurationLoader.isComplete; +import static com.yahoo.pulsar.common.configuration.PulsarConfigurationLoader.create; +import static com.yahoo.pulsar.common.configuration.PulsarConfigurationLoader.isComplete; import java.io.FileInputStream; @@ -33,7 +33,7 @@ public class PulsarBrokerStarter { private static ServiceConfiguration loadConfig(String configFile) throws Exception { SLF4JBridgeHandler.removeHandlersForRootLogger(); SLF4JBridgeHandler.install(); - ServiceConfiguration config = create((new FileInputStream(configFile))); + ServiceConfiguration config = create((new FileInputStream(configFile)), ServiceConfiguration.class); // it validates provided configuration is completed isComplete(config); return config; diff --git a/pulsar-broker/src/main/java/com/yahoo/pulsar/PulsarStandaloneStarter.java b/pulsar-broker/src/main/java/com/yahoo/pulsar/PulsarStandaloneStarter.java index e9cba99415cc8..af1b6af0db629 100644 --- a/pulsar-broker/src/main/java/com/yahoo/pulsar/PulsarStandaloneStarter.java +++ b/pulsar-broker/src/main/java/com/yahoo/pulsar/PulsarStandaloneStarter.java @@ -29,9 +29,9 @@ import com.google.common.collect.Sets; import com.yahoo.pulsar.broker.PulsarService; import com.yahoo.pulsar.broker.ServiceConfiguration; -import com.yahoo.pulsar.broker.ServiceConfigurationLoader; import com.yahoo.pulsar.client.admin.PulsarAdmin; import com.yahoo.pulsar.client.admin.PulsarAdminException; +import com.yahoo.pulsar.common.configuration.PulsarConfigurationLoader; import com.yahoo.pulsar.common.policies.data.ClusterData; import com.yahoo.pulsar.common.policies.data.PropertyAdmin; import com.yahoo.pulsar.zookeeper.LocalBookkeeperEnsemble; @@ -96,8 +96,8 @@ public PulsarStandaloneStarter(String[] args) throws Exception { return; } - this.config = ServiceConfigurationLoader.create((new FileInputStream(configFile))); - ServiceConfigurationLoader.isComplete(config); + this.config = PulsarConfigurationLoader.create((new FileInputStream(configFile)), ServiceConfiguration.class); + PulsarConfigurationLoader.isComplete(config); // Set ZK server's host to localhost config.setZookeeperServers("127.0.0.1:" + zkPort); config.setGlobalZookeeperServers("127.0.0.1:" + zkPort); diff --git a/pulsar-broker/src/test/java/com/yahoo/pulsar/broker/service/BacklogQuotaManagerTest.java b/pulsar-broker/src/test/java/com/yahoo/pulsar/broker/service/BacklogQuotaManagerTest.java index bd7e7199d3c09..c825150fbb08b 100644 --- a/pulsar-broker/src/test/java/com/yahoo/pulsar/broker/service/BacklogQuotaManagerTest.java +++ b/pulsar-broker/src/test/java/com/yahoo/pulsar/broker/service/BacklogQuotaManagerTest.java @@ -15,12 +15,10 @@ */ package com.yahoo.pulsar.broker.service; -import static com.yahoo.pulsar.broker.ServiceConfigurationLoader.create; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; import java.net.URL; -import java.util.Properties; import java.util.concurrent.CountDownLatch; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.TimeUnit; @@ -77,7 +75,7 @@ void setup() throws Exception { bkEnsemble.start(); // start pulsar service - config = create(new Properties(System.getProperties())); + config = new ServiceConfiguration(); config.setZookeeperServers("127.0.0.1" + ":" + ZOOKEEPER_PORT); config.setWebServicePort(BROKER_WEBSERVICE_PORT); config.setClusterName("usc"); diff --git a/pulsar-broker/src/test/java/com/yahoo/pulsar/broker/service/ReplicatorTestBase.java b/pulsar-broker/src/test/java/com/yahoo/pulsar/broker/service/ReplicatorTestBase.java index f6a7a1802a58f..88349f5567fae 100644 --- a/pulsar-broker/src/test/java/com/yahoo/pulsar/broker/service/ReplicatorTestBase.java +++ b/pulsar-broker/src/test/java/com/yahoo/pulsar/broker/service/ReplicatorTestBase.java @@ -15,13 +15,11 @@ */ package com.yahoo.pulsar.broker.service; -import static com.yahoo.pulsar.broker.ServiceConfigurationLoader.create; import static org.testng.Assert.assertEquals; import java.net.URL; import java.util.ArrayList; import java.util.List; -import java.util.Properties; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingQueue; @@ -104,7 +102,7 @@ void setup() throws Exception { // NOTE: we have to instantiate a new copy of System.getProperties() to make sure pulsar1 and pulsar2 have // completely // independent config objects instead of referring to the same properties object - ServiceConfiguration config1 = create(new Properties(System.getProperties())); + ServiceConfiguration config1 = new ServiceConfiguration(); config1.setClusterName("r1"); config1.setWebServicePort(webServicePort1); config1.setZookeeperServers("127.0.0.1:" + zkPort1); @@ -128,7 +126,7 @@ void setup() throws Exception { bkEnsemble2.start(); int webServicePort2 = PortManager.nextFreePort(); - config2 = create(new Properties(System.getProperties())); + config2 = new ServiceConfiguration(); config2.setClusterName("r2"); config2.setWebServicePort(webServicePort2); config2.setZookeeperServers("127.0.0.1:" + zkPort2); @@ -152,7 +150,7 @@ void setup() throws Exception { bkEnsemble3.start(); int webServicePort3 = PortManager.nextFreePort(); - config3 = create(new Properties(System.getProperties())); + config3 = new ServiceConfiguration(); config3.setClusterName("r3"); config3.setWebServicePort(webServicePort3); config3.setZookeeperServers("127.0.0.1:" + zkPort3); diff --git a/pulsar-broker/src/test/java/com/yahoo/pulsar/common/naming/ServiceConfigurationTest.java b/pulsar-broker/src/test/java/com/yahoo/pulsar/common/naming/ServiceConfigurationTest.java index 3cdcc59fb103a..3582737967949 100644 --- a/pulsar-broker/src/test/java/com/yahoo/pulsar/common/naming/ServiceConfigurationTest.java +++ b/pulsar-broker/src/test/java/com/yahoo/pulsar/common/naming/ServiceConfigurationTest.java @@ -31,7 +31,7 @@ import org.testng.annotations.Test; import com.yahoo.pulsar.broker.ServiceConfiguration; -import com.yahoo.pulsar.broker.ServiceConfigurationLoader; +import com.yahoo.pulsar.common.configuration.PulsarConfigurationLoader; /** * @@ -51,7 +51,7 @@ public void testInit() throws Exception { final String zookeeperServer = "localhost:2184"; final int brokerServicePort = 1000; InputStream newStream = updateProp(zookeeperServer, String.valueOf(brokerServicePort), "ns1,ns2"); - final ServiceConfiguration config = ServiceConfigurationLoader.create(newStream); + final ServiceConfiguration config = PulsarConfigurationLoader.create(newStream, ServiceConfiguration.class); assertTrue(isNotBlank(config.getZookeeperServers())); assertTrue(config.getBrokerServicePort() == brokerServicePort); assertEquals(config.getBootstrapNamespaces().get(1), "ns2"); @@ -66,7 +66,7 @@ public void testInit() throws Exception { public void testInitFailure() throws Exception { final String zookeeperServer = "localhost:2184"; InputStream newStream = updateProp(zookeeperServer, String.valueOf("invalid-string"), null); - ServiceConfigurationLoader.create(newStream); + PulsarConfigurationLoader.create(newStream, ServiceConfiguration.class); } private InputStream updateProp(String zookeeperServer, String brokerServicePort, String namespace) diff --git a/pulsar-discovery-service/src/main/java/com/yahoo/pulsar/discovery/service/server/DiscoveryServiceStarter.java b/pulsar-discovery-service/src/main/java/com/yahoo/pulsar/discovery/service/server/DiscoveryServiceStarter.java index 6ef5edbe902fe..9305b95c597f9 100644 --- a/pulsar-discovery-service/src/main/java/com/yahoo/pulsar/discovery/service/server/DiscoveryServiceStarter.java +++ b/pulsar-discovery-service/src/main/java/com/yahoo/pulsar/discovery/service/server/DiscoveryServiceStarter.java @@ -16,23 +16,18 @@ package com.yahoo.pulsar.discovery.service.server; import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; -import static com.yahoo.pulsar.common.util.FieldParser.update; import static java.lang.Thread.setDefaultUncaughtExceptionHandler; import static org.apache.commons.lang3.StringUtils.isEmpty; import static org.slf4j.bridge.SLF4JBridgeHandler.install; import static org.slf4j.bridge.SLF4JBridgeHandler.removeHandlersForRootLogger; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; import java.util.Map; -import java.util.Properties; import java.util.TreeMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.yahoo.pulsar.common.configuration.PulsarConfigurationLoader; import com.yahoo.pulsar.discovery.service.DiscoveryService; import com.yahoo.pulsar.discovery.service.web.DiscoveryServiceServlet; @@ -52,7 +47,9 @@ public static void init(String configFile) throws Exception { }); // load config file - final ServiceConfig config = load(configFile); + final ServiceConfig config = PulsarConfigurationLoader.create(configFile, ServiceConfig.class); + checkArgument(!isEmpty(config.getZookeeperServers()), "zookeeperServers must be provided"); + checkArgument(!isEmpty(config.getGlobalZookeeperServers()), "global-zookeeperServers must be provided"); // create broker service DiscoveryService discoveryService = new DiscoveryService(config); @@ -98,26 +95,6 @@ public static void main(String[] args) { } } - @SuppressWarnings({ "unchecked", "rawtypes" }) - public static ServiceConfig load(String configFile) throws IOException, IllegalArgumentException { - final InputStream inStream = new FileInputStream(configFile); - try { - checkNotNull(inStream, "Unbable to read config file " + configFile); - ServiceConfig config = new ServiceConfig(); - Properties properties = new Properties(); - properties.load(inStream); - config.setProperties(properties); - update((Map) properties, config); - checkArgument(!isEmpty(config.getZookeeperServers()), "zookeeperServers must be provided"); - checkArgument(!isEmpty(config.getGlobalZookeeperServers()), "global-zookeeperServers must be provided"); - return config; - } finally { - if (inStream != null) { - inStream.close(); - } - } - } - private static final Logger log = LoggerFactory.getLogger(DiscoveryServiceStarter.class); } diff --git a/pulsar-discovery-service/src/main/java/com/yahoo/pulsar/discovery/service/server/ServiceConfig.java b/pulsar-discovery-service/src/main/java/com/yahoo/pulsar/discovery/service/server/ServiceConfig.java index d5c5f28180634..43971786d38e1 100644 --- a/pulsar-discovery-service/src/main/java/com/yahoo/pulsar/discovery/service/server/ServiceConfig.java +++ b/pulsar-discovery-service/src/main/java/com/yahoo/pulsar/discovery/service/server/ServiceConfig.java @@ -19,13 +19,14 @@ import java.util.Set; import com.google.common.collect.Sets; +import com.yahoo.pulsar.common.configuration.PulsarConfiguration; import com.yahoo.pulsar.discovery.service.web.DiscoveryServiceServlet; /** * Service Configuration to start :{@link DiscoveryServiceServlet} * */ -public class ServiceConfig { +public class ServiceConfig implements PulsarConfiguration{ // Local-Zookeeper quorum connection string private String zookeeperServers; diff --git a/pulsar-discovery-service/src/test/java/com/yahoo/pulsar/discovery/service/server/DiscoveryServiceWebTest.java b/pulsar-discovery-service/src/test/java/com/yahoo/pulsar/discovery/service/server/DiscoveryServiceWebTest.java index 617339e2ad1f0..425d2b8907f18 100644 --- a/pulsar-discovery-service/src/test/java/com/yahoo/pulsar/discovery/service/server/DiscoveryServiceWebTest.java +++ b/pulsar-discovery-service/src/test/java/com/yahoo/pulsar/discovery/service/server/DiscoveryServiceWebTest.java @@ -25,6 +25,8 @@ import org.testng.annotations.Test; +import com.yahoo.pulsar.common.configuration.PulsarConfigurationLoader; + /** * 1. starts discovery service a. loads broker list from zk 2. http-client calls multiple http request: GET, PUT and * POST. 3. discovery service redirects to appropriate brokers in round-robin 4. client receives unknown host exception @@ -48,7 +50,7 @@ public void testWebDiscoveryServiceStarter() throws Exception { printWriter.println("webServicePort=" + port); printWriter.close(); testConfigFile.deleteOnExit(); - final ServiceConfig config = DiscoveryServiceStarter.load(testConfigFile.getAbsolutePath()); + final ServiceConfig config = PulsarConfigurationLoader.create(testConfigFile.getAbsolutePath(), ServiceConfig.class); final ServerManager server = new ServerManager(config); DiscoveryServiceStarter.startWebService(server, config); assertTrue(server.isStarted()); diff --git a/pulsar-websocket/src/main/java/com/yahoo/pulsar/websocket/service/WebSocketProxyConfiguration.java b/pulsar-websocket/src/main/java/com/yahoo/pulsar/websocket/service/WebSocketProxyConfiguration.java index 9d7ed01e052d5..a21ff45884e01 100644 --- a/pulsar-websocket/src/main/java/com/yahoo/pulsar/websocket/service/WebSocketProxyConfiguration.java +++ b/pulsar-websocket/src/main/java/com/yahoo/pulsar/websocket/service/WebSocketProxyConfiguration.java @@ -15,12 +15,14 @@ */ package com.yahoo.pulsar.websocket.service; +import java.util.Properties; import java.util.Set; import com.google.common.collect.Sets; -import com.yahoo.pulsar.broker.FieldContext; +import com.yahoo.pulsar.common.configuration.FieldContext; +import com.yahoo.pulsar.common.configuration.PulsarConfiguration; -public class WebSocketProxyConfiguration { +public class WebSocketProxyConfiguration implements PulsarConfiguration { // Name of the cluster to which this broker belongs to @FieldContext(required = true) @@ -63,6 +65,8 @@ public class WebSocketProxyConfiguration { private String tlsCertificateFilePath; // Path for the TLS private key file private String tlsKeyFilePath; + + private Properties properties = new Properties(); public String getClusterName() { return clusterName; @@ -199,5 +203,13 @@ public String getTlsKeyFilePath() { public void setTlsKeyFilePath(String tlsKeyFilePath) { this.tlsKeyFilePath = tlsKeyFilePath; } + + public Properties getProperties() { + return properties; + } + + public void setProperties(Properties properties) { + this.properties = properties; + } } diff --git a/pulsar-websocket/src/main/java/com/yahoo/pulsar/websocket/service/WebSocketServiceStarter.java b/pulsar-websocket/src/main/java/com/yahoo/pulsar/websocket/service/WebSocketServiceStarter.java index 83fd1353b761a..46954bbc434b5 100644 --- a/pulsar-websocket/src/main/java/com/yahoo/pulsar/websocket/service/WebSocketServiceStarter.java +++ b/pulsar-websocket/src/main/java/com/yahoo/pulsar/websocket/service/WebSocketServiceStarter.java @@ -16,18 +16,11 @@ package com.yahoo.pulsar.websocket.service; import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; -import static com.yahoo.pulsar.common.util.FieldParser.update; - -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.Map; -import java.util.Properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.yahoo.pulsar.common.configuration.PulsarConfigurationLoader; import com.yahoo.pulsar.websocket.WebSocketConsumerServlet; import com.yahoo.pulsar.websocket.WebSocketProducerServlet; import com.yahoo.pulsar.websocket.WebSocketService; @@ -40,7 +33,8 @@ public static void main(String args[]) throws Exception { // load config file and start proxy service String configFile = args[0]; log.info("Loading configuration from {}", configFile); - WebSocketProxyConfiguration config = load(configFile); + WebSocketProxyConfiguration config = PulsarConfigurationLoader.create(configFile, + WebSocketProxyConfiguration.class); ProxyServer proxyServer = new ProxyServer(config); WebSocketService service = new WebSocketService(config); start(proxyServer, service); @@ -57,23 +51,6 @@ public static void start(ProxyServer proxyServer, WebSocketService service) thro service.start(); } - @SuppressWarnings({ "unchecked", "rawtypes" }) - public static WebSocketProxyConfiguration load(String configFile) throws IOException, IllegalArgumentException { - final InputStream inStream = new FileInputStream(configFile); - try { - checkNotNull(inStream, "Unbable to read config file " + configFile); - WebSocketProxyConfiguration config = new WebSocketProxyConfiguration(); - Properties properties = new Properties(); - properties.load(inStream); - update((Map) properties, config); - return config; - } finally { - if (inStream != null) { - inStream.close(); - } - } - } - private static final Logger log = LoggerFactory.getLogger(WebSocketServiceStarter.class); }