diff options
author | Robert Gemmell <robbie@apache.org> | 2013-04-15 10:38:03 +0000 |
---|---|---|
committer | Robert Gemmell <robbie@apache.org> | 2013-04-15 10:38:03 +0000 |
commit | 5eef6907dde64786c1d68d4903a7a5904fff6068 (patch) | |
tree | 1460a054a80e06c1c3bc83bc82b6d347482d313c | |
parent | 8123a2f1895047072b78438ced886558d470000d (diff) | |
download | qpid-python-5eef6907dde64786c1d68d4903a7a5904fff6068.tar.gz |
QPID-4741: add command line option to request overwritig existing broker config store with current initial config file
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1467930 13f79535-47bb-0310-9956-ffa450edef68
11 files changed, 126 insertions, 26 deletions
diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/Broker.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/Broker.java index d1be4213ec..5f0c7c7d3c 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/Broker.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/Broker.java @@ -124,7 +124,7 @@ public class Broker } BrokerConfigurationStoreCreator storeCreator = new BrokerConfigurationStoreCreator(); - ConfigurationEntryStore store = storeCreator.createStore(storeLocation, storeType, options.getInitialConfigurationLocation()); + ConfigurationEntryStore store = storeCreator.createStore(storeLocation, storeType, options.getInitialConfigurationLocation(), options.isOverwriteConfigurationStore()); if (options.isManagementMode()) { 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 8384af915f..4d03715f25 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 @@ -25,6 +25,7 @@ import java.io.File; import org.apache.qpid.server.configuration.BrokerProperties; import org.apache.qpid.server.configuration.ConfigurationEntryStore; import org.apache.qpid.server.configuration.store.JsonConfigurationEntryStore; +import org.apache.qpid.server.configuration.store.MemoryConfigurationEntryStore; public class BrokerOptions { @@ -49,6 +50,7 @@ public class BrokerOptions private int _managementModeHttpPort; private String _workingDir; private boolean _skipLoggingConfiguration; + private boolean _overwriteConfigurationStore; public String getLogConfigFile() { @@ -180,6 +182,24 @@ public class BrokerOptions } /** + * Returns whether the existing broker configuration store should be overwritten with the current + * initial configuration file (see {@link BrokerOptions#getInitialConfigurationLocation()}). + */ + public boolean isOverwriteConfigurationStore() + { + return _overwriteConfigurationStore; + } + + /** + * Sets whether the existing broker configuration store should be overwritten with the current + * initial configuration file (see {@link BrokerOptions#getInitialConfigurationLocation()}). + */ + public void setOverwriteConfigurationStore(boolean overwrite) + { + _overwriteConfigurationStore = overwrite; + } + + /** * Get the broker work directory location. * * Defaults to the location set in the "QPID_WORK" system property if it is set, or the 'work' sub-directory @@ -216,7 +236,7 @@ public class BrokerOptions /** * Get the broker initial JSON configuration location. * - * Defaults to an internal configuration file within the broker jar, which is loaded with the {@link JsonConfigurationEntryStore}. + * Defaults to an internal configuration file within the broker jar. * * @return the previously set configuration location, or the default location if none was set. */ @@ -232,7 +252,7 @@ public class BrokerOptions /** * Set the absolute path or URL to use for the initial JSON configuration, which is loaded with the - * {@link JsonConfigurationEntryStore} in order to initialise any new {@link ConfigurationEntryStore} for the broker. + * {@link MemoryConfigurationEntryStore} in order to initialise any new {@link ConfigurationEntryStore} for the broker. * * Passing null clears any previously set value and returns to the default. */ 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 45a07ec01e..7c7bbb572b 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 @@ -37,7 +37,6 @@ import org.apache.qpid.framing.ProtocolVersion; */ public class Main { - private static final Option OPTION_HELP = new Option("h", "help", false, "print this message"); private static final Option OPTION_VERSION = new Option("v", "version", false, "print the version information and exit"); @@ -46,10 +45,13 @@ public class Main .withDescription("use given configuration store location").withLongOpt("store-path").create("sp"); private static final Option OPTION_CONFIGURATION_STORE_TYPE = OptionBuilder.withArgName("type").hasArg() - .withDescription("use given store type").withLongOpt("store-type").create("st"); + .withDescription("use given broker configuration store type").withLongOpt("store-type").create("st"); private static final Option OPTION_INITIAL_CONFIGURATION_PATH = OptionBuilder.withArgName("path").hasArg() - .withDescription("pass the location of initial JSON config to use when creating a new configuration store").withLongOpt("initial-config-path").create("icp"); + .withDescription("set the location of initial JSON config to use when creating/overwriting a broker configuration store").withLongOpt("initial-config-path").create("icp"); + + private static final Option OPTION_OVERWRITE_CONFIGURATION_STORE = OptionBuilder.withDescription("overwrite the broker configuration store with the current initial configuration") + .withLongOpt("overwrite-store").create("os"); private static final Option OPTION_LOG_CONFIG_FILE = OptionBuilder.withArgName("file").hasArg() @@ -81,6 +83,7 @@ public class Main OPTIONS.addOption(OPTION_VERSION); OPTIONS.addOption(OPTION_CONFIGURATION_STORE_PATH); OPTIONS.addOption(OPTION_CONFIGURATION_STORE_TYPE); + OPTIONS.addOption(OPTION_OVERWRITE_CONFIGURATION_STORE); OPTIONS.addOption(OPTION_LOG_CONFIG_FILE); OPTIONS.addOption(OPTION_LOG_WATCH); OPTIONS.addOption(OPTION_INITIAL_CONFIGURATION_PATH); @@ -199,8 +202,11 @@ public class Main options.setInitialConfigurationLocation(initialConfigLocation); } - boolean managmentMode = _commandLine.hasOption(OPTION_MANAGEMENT_MODE.getOpt()); - if (managmentMode) + boolean overwriteConfigurationStore = _commandLine.hasOption(OPTION_OVERWRITE_CONFIGURATION_STORE.getOpt()); + options.setOverwriteConfigurationStore(overwriteConfigurationStore); + + boolean managementMode = _commandLine.hasOption(OPTION_MANAGEMENT_MODE.getOpt()); + if (managementMode) { options.setManagementMode(true); String rmiPort = _commandLine.getOptionValue(OPTION_MM_RMI_PORT.getOpt()); diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/BrokerConfigurationStoreCreator.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/BrokerConfigurationStoreCreator.java index 2bb63a803b..a91c3695b3 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/BrokerConfigurationStoreCreator.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/BrokerConfigurationStoreCreator.java @@ -58,9 +58,10 @@ public class BrokerConfigurationStoreCreator * @param storeLocation store location * @param storeType store type * @param initialConfigLocation initial store location + * @param overwrite whether to overwrite an existing configuration store with the initial configuration * @throws IllegalConfigurationException if store type is unknown */ - public ConfigurationEntryStore createStore(String storeLocation, String storeType, String initialConfigLocation) + public ConfigurationEntryStore createStore(String storeLocation, String storeType, String initialConfigLocation, boolean overwrite) { ConfigurationEntryStore initialStore = new MemoryConfigurationEntryStore(initialConfigLocation, null); ConfigurationStoreFactory factory = _factories.get(storeType.toLowerCase()); @@ -68,7 +69,7 @@ public class BrokerConfigurationStoreCreator { throw new IllegalConfigurationException("Unknown store type: " + storeType); } - return factory.createStore(storeLocation, initialStore); + return factory.createStore(storeLocation, initialStore, overwrite); } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/JsonConfigurationEntryStore.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/JsonConfigurationEntryStore.java index 7a1db3d46d..ee31f3fda0 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/JsonConfigurationEntryStore.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/JsonConfigurationEntryStore.java @@ -21,8 +21,27 @@ public class JsonConfigurationEntryStore extends MemoryConfigurationEntryStore public JsonConfigurationEntryStore(String storeLocation, ConfigurationEntryStore initialStore) { + this(storeLocation, initialStore, false); + } + + public JsonConfigurationEntryStore(String storeLocation, ConfigurationEntryStore initialStore, boolean overwrite) + { super(); _storeFile = new File(storeLocation); + + if(_storeFile.isDirectory()) + { + throw new IllegalConfigurationException("A directory exists at the location for the broker configuration store file: " + storeLocation); + } + + if(overwrite && _storeFile.exists()) + { + if(!_storeFile.delete()) + { + throw new RuntimeException("Unable to overwrite existing configuration store file as requested: " + storeLocation); + } + } + if ((!_storeFile.exists() || _storeFile.length() == 0)) { initialiseStore(_storeFile, initialStore); diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/factory/JsonConfigurationStoreFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/factory/JsonConfigurationStoreFactory.java index 1a0b514b4c..7c7d3e2071 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/factory/JsonConfigurationStoreFactory.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/factory/JsonConfigurationStoreFactory.java @@ -27,9 +27,9 @@ import org.apache.qpid.server.plugin.ConfigurationStoreFactory; public class JsonConfigurationStoreFactory implements ConfigurationStoreFactory { @Override - public ConfigurationEntryStore createStore(String storeLocation, ConfigurationEntryStore initialStore) + public ConfigurationEntryStore createStore(String storeLocation, ConfigurationEntryStore initialStore, boolean overwrite) { - return new JsonConfigurationEntryStore(storeLocation, initialStore); + return new JsonConfigurationEntryStore(storeLocation, initialStore, overwrite); } @Override diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/factory/MemoryConfigurationStoreFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/factory/MemoryConfigurationStoreFactory.java index fcd6c73170..ed952ff475 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/factory/MemoryConfigurationStoreFactory.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/factory/MemoryConfigurationStoreFactory.java @@ -27,7 +27,7 @@ import org.apache.qpid.server.plugin.ConfigurationStoreFactory; public class MemoryConfigurationStoreFactory implements ConfigurationStoreFactory { @Override - public ConfigurationEntryStore createStore(String storeLocation, ConfigurationEntryStore initialStore) + public ConfigurationEntryStore createStore(String storeLocation, ConfigurationEntryStore initialStore, boolean overwrite) { return new MemoryConfigurationEntryStore(null, initialStore); } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/plugin/ConfigurationStoreFactory.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/plugin/ConfigurationStoreFactory.java index a625579ece..c451ae2adc 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/plugin/ConfigurationStoreFactory.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/plugin/ConfigurationStoreFactory.java @@ -34,11 +34,12 @@ public interface ConfigurationStoreFactory /** * Creates and opens the store from a given location using initial store if provided. * <p> - * If location does not exists than a new store is created either empty or from the initial store if it is provided + * If location does not exist, or the overwrite option is specified, then a new store is created from the initial store if it is provided * * @param storeLocation store location * @param initialStore initial store + * @param overwrite overwrite existing store with initial store * @throws IllegalConfigurationException if store cannot be opened in the given location */ - public ConfigurationEntryStore createStore(String storeLocation, ConfigurationEntryStore initialStore); + public ConfigurationEntryStore createStore(String storeLocation, ConfigurationEntryStore initialStore, boolean overwrite); } diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/BrokerOptionsTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/BrokerOptionsTest.java index b36d1bd21f..c1acf81a1a 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/BrokerOptionsTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/BrokerOptionsTest.java @@ -28,8 +28,9 @@ public class BrokerOptionsTest extends QpidTestCase { private BrokerOptions _options; - protected void setUp() + protected void setUp() throws Exception { + super.setUp(); _options = new BrokerOptions(); } @@ -208,4 +209,15 @@ public class BrokerOptionsTest extends QpidTestCase _options.setSkipLoggingConfiguration(true); assertTrue(_options.isSkipLoggingConfiguration()); } + + public void testDefaultOverwriteConfigurationStore() + { + assertFalse(_options.isOverwriteConfigurationStore()); + } + + public void testOverriddenOverwriteConfigurationStore() + { + _options.setOverwriteConfigurationStore(true); + assertTrue(_options.isOverwriteConfigurationStore()); + } } diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/MainTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/MainTest.java index b587ee9030..aab919a828 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/MainTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/MainTest.java @@ -47,7 +47,7 @@ public class MainTest extends QpidTestCase assertEquals(null, options.getLogConfigFile()); assertEquals(0, options.getLogWatchFrequency()); assertEquals(BrokerOptions.DEFAULT_INITIAL_CONFIG_LOCATION, options.getInitialConfigurationLocation()); - + assertFalse(options.isOverwriteConfigurationStore()); assertFalse(options.isManagementMode()); assertEquals(0, options.getManagementModeConnectorPort()); assertEquals(0, options.getManagementModeRmiPort()); @@ -72,6 +72,15 @@ public class MainTest extends QpidTestCase assertEquals("bdb", options.getConfigurationStoreType()); } + public void testOverwriteConfigurationStore() + { + BrokerOptions options = startDummyMain("-os"); + assertTrue(options.isOverwriteConfigurationStore()); + + options = startDummyMain("-overwrite-store"); + assertTrue(options.isOverwriteConfigurationStore()); + } + public void testLogConfig() { BrokerOptions options = startDummyMain("-l wxyz/log4j.xml"); diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/BrokerConfigurationStoreCreatorTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/BrokerConfigurationStoreCreatorTest.java index d61117868f..47e6849c01 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/BrokerConfigurationStoreCreatorTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/BrokerConfigurationStoreCreatorTest.java @@ -73,7 +73,7 @@ public class BrokerConfigurationStoreCreatorTest extends QpidTestCase public void testCreateJsonStore() { - ConfigurationEntryStore store = _storeCreator.createStore(_userStoreLocation.getAbsolutePath(), "json", BrokerOptions.DEFAULT_INITIAL_CONFIG_LOCATION); + ConfigurationEntryStore store = _storeCreator.createStore(_userStoreLocation.getAbsolutePath(), "json", BrokerOptions.DEFAULT_INITIAL_CONFIG_LOCATION, false); assertNotNull("Store was not created", store); assertTrue("File should exists", _userStoreLocation.exists()); assertTrue("File size should be greater than 0", _userStoreLocation.length() > 0); @@ -84,41 +84,73 @@ public class BrokerConfigurationStoreCreatorTest extends QpidTestCase public void testCreateJsonStoreFromInitialStore() throws Exception { + createJsonStoreFromInitialStoreTestImpl(false); + } + + public void testOverwriteExistingJsonStoreWithInitialConfig() throws Exception + { + createJsonStoreFromInitialStoreTestImpl(true); + } + + public void createJsonStoreFromInitialStoreTestImpl(boolean overwrite) throws Exception + { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); + String defaultBrokerName = "QpidBroker"; + String testBrokerName = getTestName(); + Map<String, Object> brokerObjectMap = new HashMap<String, Object>(); - UUID brokerId = UUID.randomUUID(); - brokerObjectMap.put(Broker.ID, brokerId); - brokerObjectMap.put("name", "Test"); + UUID testBrokerId = UUID.randomUUID(); + brokerObjectMap.put(Broker.ID, testBrokerId); + brokerObjectMap.put("name", testBrokerName); StringWriter sw = new StringWriter(); objectMapper.writeValue(sw, brokerObjectMap); String brokerJson = sw.toString(); - File _storeFile = TestFileUtils.createTempFile(this, ".json", brokerJson); + File _initialStoreFile = TestFileUtils.createTempFile(this, ".json", brokerJson); - ConfigurationEntryStore store = _storeCreator.createStore(_userStoreLocation.getAbsolutePath(), "json", _storeFile.getAbsolutePath()); + ConfigurationEntryStore store = _storeCreator.createStore(_userStoreLocation.getAbsolutePath(), "json", _initialStoreFile.getAbsolutePath(), false); assertNotNull("Store was not created", store); assertTrue("File should exists", _userStoreLocation.exists()); assertTrue("File size should be greater than 0", _userStoreLocation.length() > 0); JsonConfigurationEntryStore jsonStore = new JsonConfigurationEntryStore(_userStoreLocation.getAbsolutePath(), null); ConfigurationEntry entry = jsonStore.getRootEntry(); - assertEquals("Unexpected root id", brokerId, entry.getId()); + assertEquals("Unexpected root id", testBrokerId, entry.getId()); Map<String, Object> attributes = entry.getAttributes(); assertNotNull("Unexpected attributes: " + attributes, attributes); assertEquals("Unexpected attributes size: " + attributes.size(), 1, attributes.size()); - assertEquals("Unexpected attribute name: " + attributes.get("name"), "Test", attributes.get("name")); + assertEquals("Unexpected attribute name: " + attributes.get("name"), testBrokerName, attributes.get(Broker.NAME)); Set<UUID> childrenIds = entry.getChildrenIds(); assertTrue("Unexpected children: " + childrenIds, childrenIds.isEmpty()); + + if(overwrite) + { + ConfigurationEntryStore overwrittenStore = _storeCreator.createStore(_userStoreLocation.getAbsolutePath(), "json", BrokerOptions.DEFAULT_INITIAL_CONFIG_LOCATION, true); + assertNotNull("Store was not created", overwrittenStore); + assertTrue("File should exists", _userStoreLocation.exists()); + assertTrue("File size should be greater than 0", _userStoreLocation.length() > 0); + + //check the contents reflect the test store content having been overwritten with the default store + JsonConfigurationEntryStore reopenedOverwrittenStore = new JsonConfigurationEntryStore(_userStoreLocation.getAbsolutePath(), null, false); + entry = reopenedOverwrittenStore.getRootEntry(); + assertFalse("Root id did not change, store content was not overwritten", testBrokerId.equals(entry.getId())); + attributes = entry.getAttributes(); + assertNotNull("No attributes found", attributes); + assertFalse("Test name should not equal default broker name", testBrokerName.equals(defaultBrokerName)); + assertEquals("Unexpected broker name value" , defaultBrokerName, attributes.get(Broker.NAME)); + childrenIds = entry.getChildrenIds(); + assertFalse("Expected children were not found" + childrenIds, childrenIds.isEmpty()); + } } public void testCreateStoreWithUnknownType() { try { - _storeCreator.createStore(_userStoreLocation.getAbsolutePath(), "derby", null); + _storeCreator.createStore(_userStoreLocation.getAbsolutePath(), "derby", null, false); fail("Store is not yet supported"); } catch(IllegalConfigurationException e) |