diff options
author | Alex Rudyy <orudyy@apache.org> | 2012-12-27 17:41:37 +0000 |
---|---|---|
committer | Alex Rudyy <orudyy@apache.org> | 2012-12-27 17:41:37 +0000 |
commit | 21ebe679928fab6f3cd17129b561af7df4eac373 (patch) | |
tree | a8cbc2d241a253693e7b55e9da373c9d2e19861a | |
parent | 001a1471c43804dbbbef46b332ec767edbe54c29 (diff) | |
download | qpid-python-21ebe679928fab6f3cd17129b561af7df4eac373.tar.gz |
QPID-4390: Add broker command line arguments to pass store location and store type, create store for given location and type
git-svn-id: https://svn.apache.org/repos/asf/qpid/branches/java-broker-config-qpid-4390@1426269 13f79535-47bb-0310-9956-ffa450edef68
5 files changed, 304 insertions, 10 deletions
diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/BrokerLauncher.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/BrokerLauncher.java index f1f1c05992..8551147df3 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/BrokerLauncher.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/BrokerLauncher.java @@ -31,7 +31,7 @@ import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import org.apache.qpid.framing.AMQShortString; import org.apache.qpid.server.configuration.ConfigurationEntryStore; -import org.apache.qpid.server.configuration.store.XMLConfigurationEntryStore; +import org.apache.qpid.server.configuration.ConfigurationEntryStoreFactory; import org.apache.qpid.server.logging.SystemOutMessageLogger; import org.apache.qpid.server.logging.actors.BrokerActor; import org.apache.qpid.server.logging.actors.CurrentActor; @@ -108,17 +108,26 @@ public class BrokerLauncher private void startupImpl(final BrokerOptions options) throws Exception { final String qpidHome = options.getQpidHome(); - final File configFile = getConfigFile(options.getConfigFile(), - BrokerOptions.DEFAULT_CONFIG_FILE, qpidHome, true); + String storeLocation = options.getConfigurationStore(); + String storeType = options.getConfigurationStoreType(); - CurrentActor.get().message(BrokerMessages.CONFIG(configFile.getAbsolutePath())); + // Temporarily support for old configuration file option + if (storeLocation == null && storeType == null && options.getConfigFile() != null) + { + storeLocation = options.getConfigFile(); + } + if (storeLocation == null) + { + storeLocation = new File(qpidHome, BrokerOptions.DEFAULT_CONFIG_FILE).getAbsolutePath(); + } - File logConfigFile = getConfigFile(options.getLogConfigFile(), - BrokerOptions.DEFAULT_LOG_CONFIG_FILE, qpidHome, false); + CurrentActor.get().message(BrokerMessages.CONFIG(storeLocation)); + File logConfigFile = getConfigFile(options.getLogConfigFile(), BrokerOptions.DEFAULT_LOG_CONFIG_FILE, qpidHome, false); configureLogging(logConfigFile, options.getLogWatchFrequency()); - ConfigurationEntryStore store = new XMLConfigurationEntryStore(configFile, options); + ConfigurationEntryStoreFactory storeFactory = new ConfigurationEntryStoreFactory(); + ConfigurationEntryStore store = storeFactory.createStore(storeLocation, storeType, options); _applicationRegistry = new ApplicationRegistry(store); try diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/BrokerOptions.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/BrokerOptions.java index 1a00467c3d..0947cdc097 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/BrokerOptions.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/BrokerOptions.java @@ -28,7 +28,7 @@ import java.util.Set; public class BrokerOptions { - public static final String DEFAULT_CONFIG_FILE = "etc/config.xml"; + public static final String DEFAULT_CONFIG_FILE = "etc/config.json"; public static final String DEFAULT_LOG_CONFIG_FILE = "etc/log4j.xml"; public static final String QPID_HOME = "QPID_HOME"; public static final String QPID_WORK = "QPID_WORK"; @@ -48,6 +48,10 @@ public class BrokerOptions private String _qpidWorkFolder; private String _qpidHomeFolder; + private String _configurationStore; + private String _configurationStoreType; + private boolean _noDefault; + public void addPort(final int port) { _ports.add(port); @@ -68,11 +72,13 @@ public class BrokerOptions return Collections.unmodifiableSet(_sslPorts); } + @Deprecated public String getConfigFile() { return _configFile; } + @Deprecated public void setConfigFile(final String configFile) { _configFile = configFile; @@ -184,4 +190,35 @@ public class BrokerOptions { _qpidHomeFolder = qpidHomeFolder; } + + public String getConfigurationStore() + { + return _configurationStore; + } + + public void setConfigurationStore(String cofigurationStore) + { + _configurationStore = cofigurationStore; + } + + public String getConfigurationStoreType() + { + return _configurationStoreType; + } + + public void setConfigurationStoreType(String cofigurationStoreType) + { + _configurationStoreType = cofigurationStoreType; + } + + public boolean isNoDefault() + { + return _noDefault; + } + + public void setNoDefault(boolean noDefault) + { + _noDefault = noDefault; + } + }
\ No newline at end of file diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/Main.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/Main.java index 01771ff03d..d2256dc60e 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/Main.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/Main.java @@ -31,8 +31,6 @@ import org.apache.log4j.Logger; import org.apache.qpid.common.QpidProperties; import org.apache.qpid.framing.ProtocolVersion; import org.apache.qpid.server.BrokerLauncher.InitException; -import org.apache.qpid.server.registry.ApplicationRegistry; - /** * Main entry point for AMQPD. @@ -45,6 +43,16 @@ public class Main private static final Option OPTION_VERSION = new Option("v", "version", false, "print the version information and exit"); + private static final Option OPTION_CONFIGURATION_STORE = OptionBuilder.withArgName("location").hasArg() + .withDescription("use given configuration store location").withLongOpt("config-store").create("cs"); + + private static final Option OPTION_CONFIGURATION_STORE_TYPE = OptionBuilder.withArgName("type").hasArg() + .withDescription("use given store type (json|derby), json by default").withLongOpt("config-store-type").create("cst"); + + private static final Option OPTION_CONFIGURATION_STORE_NO_DEFAULTS = OptionBuilder.withType(Boolean.class) + .withDescription("disables default configuration if set to true").withLongOpt("no-defaults").create("nd"); + + @Deprecated private static final Option OPTION_CONFIG_FILE = OptionBuilder.withArgName("file").hasArg().withDescription("use given configuration file").withLongOpt("config") .create("c"); @@ -143,6 +151,9 @@ private static final Option OPTION_INCLUDE_0_8 = { OPTIONS.addOption(OPTION_HELP); OPTIONS.addOption(OPTION_VERSION); + OPTIONS.addOption(OPTION_CONFIGURATION_STORE); + OPTIONS.addOption(OPTION_CONFIGURATION_STORE_TYPE); + OPTIONS.addOption(OPTION_CONFIGURATION_STORE_NO_DEFAULTS); OPTIONS.addOption(OPTION_CONFIG_FILE); OPTIONS.addOption(OPTION_LOG_CONFIG_FILE); OPTIONS.addOption(OPTION_LOG_WATCH); @@ -243,6 +254,17 @@ private static final Option OPTION_INCLUDE_0_8 = else { BrokerOptions options = new BrokerOptions(); + String configurationStore = _commandLine.getOptionValue(OPTION_CONFIGURATION_STORE.getOpt()); + if (configurationStore != null) + { + options.setConfigurationStore(configurationStore); + } + String configurationStoreType = _commandLine.getOptionValue(OPTION_CONFIGURATION_STORE_TYPE.getOpt()); + if (configurationStoreType != null) + { + options.setConfigurationStoreType(configurationStoreType); + } + String configFile = _commandLine.getOptionValue(OPTION_CONFIG_FILE.getOpt()); if(configFile != null) { diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ConfigurationEntryStoreFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ConfigurationEntryStoreFactory.java new file mode 100644 index 0000000000..a4aa081679 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ConfigurationEntryStoreFactory.java @@ -0,0 +1,110 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.qpid.server.configuration; + +import java.io.File; +import java.net.URL; + +import org.apache.commons.configuration.ConfigurationException; +import org.apache.qpid.server.BrokerOptions; +import org.apache.qpid.server.configuration.store.MergingStore; +import org.apache.qpid.server.configuration.store.JsonConfigurationEntryStore; +import org.apache.qpid.server.configuration.store.XMLConfigurationEntryStore; + +public class ConfigurationEntryStoreFactory +{ + /** + * Path to resource containing broker default configuration + */ + public static final String DEFAULT_STORE = "default.json"; + + /** + * Create broker configuration store for given store location, store type + * and command line options + */ + public ConfigurationEntryStore createStore(String storeLocation, String storeType, BrokerOptions options) + { + ConfigurationEntryStoreType type = ConfigurationEntryStoreType.getType(storeType, storeLocation); + ConfigurationEntryStore store = null; + switch (type) + { + case JSON: + store = new JsonConfigurationEntryStore(new File(storeLocation)); + break; + case XML: + try + { + return new XMLConfigurationEntryStore(new File(storeLocation), options); + } + catch (ConfigurationException e) + { + throw new IllegalConfigurationException("Unexpected error", e); + } + case DERBY: + case BDB: + default: + throw new IllegalConfigurationException("Unsupported store type " + type); + } + + if (!options.isNoDefault()) + { + URL defaultStoreLocation = ConfigurationEntryStoreFactory.class.getClassLoader().getResource(DEFAULT_STORE); + store = new MergingStore(store, new JsonConfigurationEntryStore(defaultStoreLocation)); + } + return store; + } + + public static enum ConfigurationEntryStoreType + { + JSON, XML, DERBY, BDB; + + public static ConfigurationEntryStoreType getType(String storeType, String storeLocation) + { + ConfigurationEntryStoreType type = null; + if (storeType == null) + { + if (storeLocation != null) + { + // define type from file extension + String lower = storeLocation.toLowerCase(); + if (lower.endsWith(".json")) + { + type = ConfigurationEntryStoreType.JSON; + } + else if (lower.endsWith(".xml")) + { + type = ConfigurationEntryStoreType.XML; + } + } + if (type == null) + { + // default is JSON + type = ConfigurationEntryStoreType.JSON; + } + } + else + { + type = ConfigurationEntryStoreType.valueOf(storeType.toUpperCase()); + } + return type; + } + } +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/ConfigurationEntryStoreFactoryTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/ConfigurationEntryStoreFactoryTest.java new file mode 100644 index 0000000000..93ef89021a --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/ConfigurationEntryStoreFactoryTest.java @@ -0,0 +1,116 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.qpid.server.configuration; + +import java.io.File; + +import org.apache.commons.configuration.XMLConfiguration; +import org.apache.qpid.server.BrokerOptions; +import org.apache.qpid.server.configuration.store.JsonConfigurationEntryStore; +import org.apache.qpid.server.configuration.store.MergingStore; +import org.apache.qpid.server.configuration.store.XMLConfigurationEntryStore; +import org.apache.qpid.test.utils.QpidTestCase; +import org.apache.qpid.util.FileUtils; + +public class ConfigurationEntryStoreFactoryTest extends QpidTestCase +{ + private File _userStoreFile; + private ConfigurationEntryStoreFactory _factory; + private BrokerOptions _options; + + public void setUp() throws Exception + { + super.setUp(); + setTestSystemProperty("QPID_HOME", TMP_FOLDER); + _factory = new ConfigurationEntryStoreFactory(); + _userStoreFile = new File(TMP_FOLDER, "_store_" + System.currentTimeMillis() + "_" + getTestName()); + _options = new BrokerOptions(); + } + + public void tearDown() throws Exception + { + try + { + super.tearDown(); + } + finally + { + if (_userStoreFile != null) + { + FileUtils.delete(_userStoreFile, true); + } + } + } + public void testCreateJsonStoreWithDefaults() + { + _options.setNoDefault(false); + ConfigurationEntryStore store = _factory.createStore(_userStoreFile.getAbsolutePath(), "json", _options); + assertNotNull("Store was not created", store); + assertTrue("Unexpected store type", store instanceof MergingStore); + } + + public void testCreateJsonStoreWithNoDefaults() + { + _options.setNoDefault(true); + ConfigurationEntryStore store = _factory.createStore(_userStoreFile.getAbsolutePath(), "json", _options); + assertNotNull("Store was not created", store); + assertTrue("Unexpected store type", store instanceof JsonConfigurationEntryStore); + } + + public void testCreateDerbyStoreWithNoDefaults() + { + _options.setNoDefault(true); + try + { + _factory.createStore(_userStoreFile.getAbsolutePath(), "derby", _options); + fail("Store is not yet supported"); + } + catch(IllegalConfigurationException e) + { + // pass + } + } + + public void testCreateBDBDerbyStoreWithNoDefaults() + { + _options.setNoDefault(true); + try + { + _factory.createStore(_userStoreFile.getAbsolutePath(), "bdb", _options); + fail("Store is not yet supported"); + } + catch(IllegalConfigurationException e) + { + // pass + } + } + + public void testCreateXmlStoreWithNoDefaults() throws Exception + { + _options.setNoDefault(true); + XMLConfiguration config = new XMLConfiguration(); + config.save(_userStoreFile); + ConfigurationEntryStore store = _factory.createStore(_userStoreFile.getAbsolutePath(), "xml", _options); + assertNotNull("Store was not created", store); + assertTrue("Unexpected store type", store instanceof XMLConfigurationEntryStore); + } + +} |