diff options
| author | Alex Rudyy <orudyy@apache.org> | 2013-02-20 17:17:00 +0000 |
|---|---|---|
| committer | Alex Rudyy <orudyy@apache.org> | 2013-02-20 17:17:00 +0000 |
| commit | 5ac93e55ad1ec43d4fd985f364cb222d80e915ec (patch) | |
| tree | 4da575d3621f568ed6f660ccac11de9e6e64f74e /qpid/java | |
| parent | 883120a80c24ca270e13e38c25c18ac3a2dd725a (diff) | |
| download | qpid-python-5ac93e55ad1ec43d4fd985f364cb222d80e915ec.tar.gz | |
QPID-4593: add command line argument to pass path to initial store
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1448306 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/java')
13 files changed, 668 insertions, 151 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 f1a054d2d6..32080e8e67 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 @@ -128,7 +128,8 @@ public class Broker configureLogging(logConfigFile, options.getLogWatchFrequency()); BrokerConfigurationStoreCreator storeCreator = new BrokerConfigurationStoreCreator(); - ConfigurationEntryStore store = storeCreator.createStore(storeLocation, storeType, options); + ConfigurationEntryStore store = storeCreator.createStore(storeLocation, storeType, + options.getInitialConfigurationStoreLocation(), options.getInitialConfigurationStoreLocation()); _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 f85cf202a1..11eebf9865 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 @@ -32,6 +32,9 @@ public class BrokerOptions private String _configurationStoreLocation; private String _configurationStoreType = DEFAULT_STORE_TYPE; + private String _initialConfigurationStoreLocation; + private String _initialConfigurationStoreType = DEFAULT_STORE_TYPE; + public String getLogConfigFile() { return _logConfigFile; @@ -76,4 +79,24 @@ public class BrokerOptions _configurationStoreType = cofigurationStoreType; } + public void setInitialConfigurationStoreLocation(String initialConfigurationStore) + { + _initialConfigurationStoreLocation = initialConfigurationStore; + } + + public void setInitialConfigurationStoreType(String initialConfigurationStoreType) + { + _initialConfigurationStoreType = initialConfigurationStoreType; + } + + public String getInitialConfigurationStoreLocation() + { + return _initialConfigurationStoreLocation; + } + + public String getInitialConfigurationStoreType() + { + return _initialConfigurationStoreType; + } + }
\ 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 d834dd0b1e..245b0cf1fb 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 @@ -48,6 +48,12 @@ public class Main private static final Option OPTION_CONFIGURATION_STORE_TYPE = OptionBuilder.withArgName("type").hasArg() .withDescription("use given store type").withLongOpt("store-type").create("st"); + private static final Option OPTION_INITIAL_CONFIGURATION_STORE_PATH = OptionBuilder.withArgName("path").hasArg() + .withDescription("pass the location of initial store to use to create a user store").withLongOpt("initial-store-path").create("isp"); + + private static final Option OPTION_INITIAL_CONFIGURATION_STORE_TYPE = OptionBuilder.withArgName("type").hasArg() + .withDescription("the type of initial store").withLongOpt("initial-store-type").create("ist"); + private static final Option OPTION_LOG_CONFIG_FILE = OptionBuilder.withArgName("file").hasArg() .withDescription("use the specified log4j xml configuration file. By " @@ -69,6 +75,8 @@ public class Main OPTIONS.addOption(OPTION_CONFIGURATION_STORE_TYPE); OPTIONS.addOption(OPTION_LOG_CONFIG_FILE); OPTIONS.addOption(OPTION_LOG_WATCH); + OPTIONS.addOption(OPTION_INITIAL_CONFIGURATION_STORE_PATH); + OPTIONS.addOption(OPTION_INITIAL_CONFIGURATION_STORE_TYPE); } protected CommandLine _commandLine; @@ -173,6 +181,17 @@ public class Main options.setLogConfigFile(logConfig); } + String initialConfigurationStore = _commandLine.getOptionValue(OPTION_INITIAL_CONFIGURATION_STORE_PATH.getOpt()); + if (initialConfigurationStore != null) + { + options.setInitialConfigurationStoreLocation(initialConfigurationStore); + } + String initailConfigurationStoreType = _commandLine.getOptionValue(OPTION_INITIAL_CONFIGURATION_STORE_TYPE.getOpt()); + if (initailConfigurationStoreType != null) + { + options.setInitialConfigurationStoreType(initailConfigurationStoreType); + } + setExceptionHandler(); startBroker(options); 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 efae100c9d..31e08ab88a 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 @@ -20,39 +20,83 @@ */ package org.apache.qpid.server.configuration; -import org.apache.qpid.server.BrokerOptions; +import java.util.HashMap; +import java.util.Map; + +import org.apache.qpid.server.configuration.store.JsonConfigurationEntryStore; import org.apache.qpid.server.plugin.QpidServiceLoader; +/** + * A helper class responsible for creation and opening of broker store. + */ public class BrokerConfigurationStoreCreator { /** - * Path to resource containing broker default configuration + * URL to resource containing broker default configuration */ - public static final String INITIAL_STORE_LOCATION = "initial-store.json"; + public static final String DEFAULT_INITIAL_STORE_LOCATION = BrokerConfigurationStoreCreator.class.getClassLoader() + .getResource("initial-store.json").toExternalForm(); - /** - * Create broker configuration store for given store location, store type - * and command line options - */ - public ConfigurationEntryStore createStore(String storeLocation, String storeType, BrokerOptions options) + private Map<String, ConfigurationStoreFactory> _factories = new HashMap<String, ConfigurationStoreFactory>(); + + public BrokerConfigurationStoreCreator() { - ConfigurationEntryStore store = null; QpidServiceLoader<ConfigurationStoreFactory> serviceLoader = new QpidServiceLoader<ConfigurationStoreFactory>(); - Iterable<ConfigurationStoreFactory> configurationStoreFactories = serviceLoader.instancesOf(ConfigurationStoreFactory.class); + Iterable<ConfigurationStoreFactory> configurationStoreFactories = serviceLoader + .instancesOf(ConfigurationStoreFactory.class); for (ConfigurationStoreFactory storeFactory : configurationStoreFactories) { - if (storeFactory.getStoreType().equals(storeType)) + String type = storeFactory.getStoreType(); + ConfigurationStoreFactory factory = _factories.put(type.toLowerCase(), storeFactory); + if (factory != null) { - store = storeFactory.createStore(); - break; + throw new IllegalStateException("ConfigurationStoreFactory with type name '" + type + + "' is already registered using class '" + factory.getClass().getName() + "', can not register class '" + + storeFactory.getClass().getName() + "'"); } } - if (store == null) + } + + /** + * Create broker configuration store for a given store location, store type, initial store location and initial store type + * + * @param storeLocation store location + * @param storeType store type + * @param initialStoreLocation initial store location + * @param initialStoreType initial store type + * @return store instance opened at given store location + * @throws IllegalConfigurationException if store type is unknown + */ + public ConfigurationEntryStore createStore(String storeLocation, String storeType, String initialStoreLocation, + String initialStoreType) + { + ConfigurationEntryStore store = createStore(storeType); + if (initialStoreLocation == null) { - throw new IllegalConfigurationException("Cannot create store for the type " + storeType); + initialStoreLocation = DEFAULT_INITIAL_STORE_LOCATION; + initialStoreType = JsonConfigurationEntryStore.STORE_TYPE; + } + if (storeType.equals(initialStoreType)) + { + store.open(storeLocation, initialStoreLocation); + } + else + { + ConfigurationEntryStore initialStore = createStore(initialStoreType); + initialStore.open(initialStoreLocation); + store.open(storeLocation, initialStore); } - store.open(storeLocation); return store; } + private ConfigurationEntryStore createStore(String storeType) + { + ConfigurationStoreFactory factory = _factories.get(storeType.toLowerCase()); + if (factory == null) + { + throw new IllegalConfigurationException("Unknown store type: " + storeType); + } + return factory.createStore(); + } + } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ConfigurationEntryStore.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ConfigurationEntryStore.java index 08736c36f1..8238d147bd 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ConfigurationEntryStore.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ConfigurationEntryStore.java @@ -24,14 +24,75 @@ import java.util.UUID; public interface ConfigurationEntryStore { + /** + * Opens the store from a given location. + * <p> + * If location does not exists than a new empty store is created with a single root entry + * + * @param storeLocation store location + * @throws IllegalConfigurationException if store cannot be opened in the given location + */ void open(String storeLocation); + /** + * Opens the store from a given location. + * <p> + * If location does not exists than a new store is created either empty or from the initial store location if it is provided + * + * @param storeLocation store location + * @param initialStoreLocation initial store location + * @throws IllegalConfigurationException if store cannot be opened in the given location or initial store location does not + * exists or corrupted. + */ + void open(String storeLocation, String initialStoreLocation); + + /** + * Opens the store from a given location. + * <p> + * If location does not exists than a new store is created either empty or from the initial store if it is provided + * + * @param storeLocation store location + * @param initialStore initial store + * @throws IllegalConfigurationException if store cannot be opened in the given location + */ + void open(String storeLocation, ConfigurationEntryStore initialStore); + + /** + * Returns stored root configuration entry + * + * @return root entry + */ ConfigurationEntry getRootEntry(); + /** + * Returns the configuration entry with a given id. + * + * @return entry with a given id or null if entry does not exists + */ ConfigurationEntry getEntry(UUID id); + /** + * Saves given entries in the store. + * + * @param entries entries to store + * @throws IllegalConfigurationException if save operation fails + */ void save(ConfigurationEntry... entries); + /** + * Removes the entries with given IDs and all their children + * + * @param entryIds IDs of entries to remove + * @return IDs of removed entries + * @throws IllegalConfigurationException if remove operation fails + */ UUID[] remove(UUID... entryIds); + /** + * Copies the store into the given location + * + * @param target location to copy store into + * @throws IllegalConfigurationException if store cannot be copied into given location + */ + public void copyTo(String copyLocation); } 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 b8481de2cc..e11b63001a 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 @@ -9,6 +9,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -21,7 +22,6 @@ import java.util.UUID; import org.apache.qpid.server.configuration.ConfigurationEntry; import org.apache.qpid.server.configuration.ConfigurationEntryStore; -import org.apache.qpid.server.configuration.BrokerConfigurationStoreCreator; import org.apache.qpid.server.configuration.IllegalConfigurationException; import org.apache.qpid.server.model.Broker; import org.apache.qpid.server.model.ConfiguredObject; @@ -40,6 +40,9 @@ import org.codehaus.jackson.node.ArrayNode; public class JsonConfigurationEntryStore implements ConfigurationEntryStore { + public static final String STORE_TYPE = "json"; + public static final String IN_MEMORY = ":memory:"; + private static final String DEFAULT_BROKER_NAME = "Broker"; private static final String ID = "id"; private static final String TYPE = "@type"; @@ -48,17 +51,10 @@ public class JsonConfigurationEntryStore implements ConfigurationEntryStore private Map<UUID, ConfigurationEntry> _entries; private File _storeFile; private UUID _rootId; - private String _initialStoreLocation; private Map<String, Class<? extends ConfiguredObject>> _relationshipClasses; public JsonConfigurationEntryStore() { - this(BrokerConfigurationStoreCreator.INITIAL_STORE_LOCATION); - } - - public JsonConfigurationEntryStore(String initialStore) - { - _initialStoreLocation = initialStore; _objectMapper = new ObjectMapper(); _objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); _objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true); @@ -66,69 +62,81 @@ public class JsonConfigurationEntryStore implements ConfigurationEntryStore _relationshipClasses = buildRelationshipClassMap(); } - private Map<String, Class<? extends ConfiguredObject>> buildRelationshipClassMap() + @Override + public void open(String storeLocation) { - Map<String, Class<? extends ConfiguredObject>> relationships = new HashMap<String, Class<? extends ConfiguredObject>>(); - - Collection<Class<? extends ConfiguredObject>> children = Model.getInstance().getChildTypes(Broker.class); - for (Class<? extends ConfiguredObject> childClass : children) + if (_rootId != null) { - String name = childClass.getSimpleName().toLowerCase(); - String relationshipName = name + (name.endsWith("s") ? "es" : "s"); - relationships.put(relationshipName, childClass); + throw new IllegalConfigurationException("The store has been opened alread"); } - return relationships; - } - - public void load(URL storeURL) - { - if (_rootId != null) + if (!IN_MEMORY.equals(storeLocation)) { - throw new IllegalStateException("Cannot load the store from"); + _storeFile = new File(storeLocation); } - JsonNode node = load(storeURL, _objectMapper); - ConfigurationEntry brokerEntry = toEntry(node, Broker.class, _entries); - _rootId = brokerEntry.getId(); + createOrLoadStore(); } @Override - public void open(String storeLocation) + public void open(String storeLocation, String initialStoreLocation) { - _storeFile = new File(storeLocation); - if (!_storeFile.exists() || _storeFile.length() == 0) + if (_rootId != null) { - copyInitialStore(); + throw new IllegalConfigurationException("The store has been opened already"); + } + if (!IN_MEMORY.equals(storeLocation)) + { + _storeFile = new File(storeLocation); + if ((!_storeFile.exists() || _storeFile.length() == 0) && initialStoreLocation != null) + { + copyInitialStoreFile(initialStoreLocation); + } + createOrLoadStore(); + } + else + { + if (initialStoreLocation == null) + { + createRootEntryIfNotExists(); + } + else + { + load(toURL(initialStoreLocation)); + } } - - load(fileToURL(_storeFile)); } - private void copyInitialStore() + @Override + public void open(String storeLocation, ConfigurationEntryStore initialStore) { - InputStream in = null; - try + if (_rootId != null) { - in = JsonConfigurationEntryStore.class.getClassLoader().getResourceAsStream(_initialStoreLocation); - FileUtils.copy(in, _storeFile); + throw new IllegalConfigurationException("The store has been opened already"); } - catch (IOException e) + boolean copyStore = false; + if (IN_MEMORY.equals(storeLocation)) { - throw new IllegalConfigurationException("Cannot create store file by copying initial store", e); + copyStore = initialStore != null; } - finally + else { - if (in != null) + _storeFile = new File(storeLocation); + if ((!_storeFile.exists() || _storeFile.length() == 0) && initialStore != null) { - try - { - in.close(); - } - catch (IOException e) - { - throw new IllegalConfigurationException("Cannot close initial store input stream", e); - } + createStoreFileIfNotExist(_storeFile); + copyStore = true; } } + if (copyStore) + { + ConfigurationEntry rootEntry = initialStore.getRootEntry(); + _rootId = rootEntry.getId(); + copyEntry(rootEntry.getId(), initialStore); + saveAsTree(); + } + else + { + createOrLoadStore(); + } } @Override @@ -201,11 +209,133 @@ public class JsonConfigurationEntryStore implements ConfigurationEntryStore return _entries.get(id); } - public void saveTo(File file) + @Override + public void copyTo(String copyLocation) { + if (_rootId == null) + { + throw new IllegalConfigurationException("The store has not been opened"); + } + File file = new File(copyLocation); + if (!file.exists()) + { + createStoreFileIfNotExist(file); + } saveAsTree(_rootId, _entries, _objectMapper, file); } + @Override + public String toString() + { + return "JsonConfigurationEntryStore [_storeFile=" + _storeFile + ", _rootId=" + _rootId + "]"; + } + + private Map<String, Class<? extends ConfiguredObject>> buildRelationshipClassMap() + { + Map<String, Class<? extends ConfiguredObject>> relationships = new HashMap<String, Class<? extends ConfiguredObject>>(); + + Collection<Class<? extends ConfiguredObject>> children = Model.getInstance().getChildTypes(Broker.class); + for (Class<? extends ConfiguredObject> childClass : children) + { + String name = childClass.getSimpleName().toLowerCase(); + String relationshipName = name + (name.endsWith("s") ? "es" : "s"); + relationships.put(relationshipName, childClass); + } + return relationships; + } + + private void createOrLoadStore() + { + if (_storeFile != null) + { + if (!_storeFile.exists() || _storeFile.length() == 0) + { + createStoreFileIfNotExist(_storeFile); + } + else + { + load(fileToURL(_storeFile)); + } + } + + createRootEntryIfNotExists(); + } + + private void createRootEntryIfNotExists() + { + if (_rootId == null) + { + // create a root entry for an empty store + ConfigurationEntry brokerEntry = new ConfigurationEntry(UUIDGenerator.generateRandomUUID(), + Broker.class.getSimpleName(), Collections.<String, Object> emptyMap(), Collections.<UUID> emptySet(), this); + _rootId = brokerEntry.getId(); + _entries.put(_rootId, brokerEntry); + } + } + + private void load(URL url) + { + InputStream is = null; + try + { + is = url.openStream(); + JsonNode node = loadJsonNodes(is, _objectMapper); + ConfigurationEntry brokerEntry = toEntry(node, Broker.class, _entries); + _rootId = brokerEntry.getId(); + } + catch (IOException e) + { + throw new IllegalConfigurationException("Cannot load store from: " + url, e); + } + finally + { + if (is != null) + { + if (is != null) + { + try + { + is.close(); + } + catch (IOException e) + { + throw new IllegalConfigurationException("Cannot close input stream for: " + url, e); + } + } + } + } + } + + private void copyInitialStoreFile(String initialStoreLocation) + { + createStoreFileIfNotExist(_storeFile); + URL initialStoreURL = toURL(initialStoreLocation); + InputStream in = null; + try + { + in = initialStoreURL.openStream(); + FileUtils.copy(in, _storeFile); + } + catch (IOException e) + { + throw new IllegalConfigurationException("Cannot create store file " + _storeFile + " by copying initial store from " + initialStoreLocation , e); + } + finally + { + if (in != null) + { + try + { + in.close(); + } + catch (IOException e) + { + throw new IllegalConfigurationException("Cannot close initial store input stream: " + initialStoreLocation , e); + } + } + } + } + private URL fileToURL(File storeFile) { URL storeURL = null; @@ -278,7 +408,7 @@ public class JsonConfigurationEntryStore implements ConfigurationEntryStore Map<String, Object> attributes = entry.getAttributes(); if (attributes != null) { - tree.putAll( attributes); + tree.putAll(attributes); } tree.put(ID, entry.getId()); tree.put(TYPE, entry.getType()); @@ -307,20 +437,20 @@ public class JsonConfigurationEntryStore implements ConfigurationEntryStore return tree; } - private JsonNode load(URL url, ObjectMapper mapper) + private JsonNode loadJsonNodes(InputStream is, ObjectMapper mapper) { JsonNode root = null; try { - root = mapper.readTree(url); + root = mapper.readTree(is); } catch (JsonProcessingException e) { - throw new IllegalConfigurationException("Cannot parse json from '" + url + "'", e); + throw new IllegalConfigurationException("Cannot parse json", e); } catch (IOException e) { - throw new IllegalConfigurationException("Cannot read from '" + url + "'", e); + throw new IllegalConfigurationException("Cannot read json", e); } return root; } @@ -519,10 +649,63 @@ public class JsonConfigurationEntryStore implements ConfigurationEntryStore return array; } - @Override - public String toString() + /* + * Initial store location can be URL or absolute path + */ + private URL toURL(String location) + { + URL url = null; + try + { + url = new URL(location); + } + catch (MalformedURLException e) + { + File locationFile = new File(location); + url = fileToURL(locationFile); + } + return url; + } + + private void createStoreFileIfNotExist(File file) { - return "JsonConfigurationEntryStore [_storeFile=" + _storeFile + ", _rootId=" + _rootId + ", _initialStoreLocation=" - + _initialStoreLocation + "]"; + File parent = file.getParentFile(); + if (!parent.exists()) + { + if (!parent.mkdirs()) + { + throw new IllegalConfigurationException("Cannot create folders " + parent); + } + } + try + { + file.createNewFile(); + } + catch (IOException e) + { + throw new IllegalConfigurationException("Cannot create file " + file, e); + } + } + + private void copyEntry(UUID entryId, ConfigurationEntryStore initialStore) + { + ConfigurationEntry entry = initialStore.getEntry(entryId); + if (entry != null) + { + if (_entries.containsKey(entryId)) + { + throw new IllegalConfigurationException("Duplicate id is found: " + entryId + + "! The following configuration entries have the same id: " + _entries.get(entryId) + ", " + entry); + } + _entries.put(entryId, entry); + Set<UUID> children = entry.getChildrenIds(); + if (children != null) + { + for (UUID uuid : children) + { + copyEntry(uuid, 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 a2664219bc..e37e58b840 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 @@ -26,8 +26,6 @@ import org.apache.qpid.server.configuration.store.JsonConfigurationEntryStore; public class JsonConfigurationStoreFactory implements ConfigurationStoreFactory { - private static final String STORE_TYPE = "json"; - @Override public ConfigurationEntryStore createStore() { @@ -37,7 +35,7 @@ public class JsonConfigurationStoreFactory implements ConfigurationStoreFactory @Override public String getStoreType() { - return STORE_TYPE; + return JsonConfigurationEntryStore.STORE_TYPE; } } 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 9b7e3794d0..1352ea5164 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 @@ -78,4 +78,28 @@ public class BrokerOptionsTest extends QpidTestCase _options.setLogWatchFrequency(myFreq); assertEquals(myFreq, _options.getLogWatchFrequency()); } + + + public void testDefaultInitialConfigurationStoreType() + { + assertEquals("json", _options.getInitialConfigurationStoreType()); + } + + public void testOverriddenInitialConfigurationStoreType() + { + _options.setInitialConfigurationStoreType("dby"); + assertEquals("dby", _options.getInitialConfigurationStoreType()); + } + + public void testDefaultInitialConfigurationStoreLocation() + { + assertNull(_options.getInitialConfigurationStoreLocation()); + } + + public void testOverriddenInitialConfigurationStoreLocation() + { + final String testConfigFile = "etc/mytestconfig.xml"; + _options.setInitialConfigurationStoreLocation(testConfigFile); + assertEquals(testConfigFile, _options.getInitialConfigurationStoreLocation()); + } } 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 b0533080a5..bef9b8a78b 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 @@ -38,20 +38,26 @@ public class MainTest extends QpidTestCase assertEquals(null, options.getConfigurationStoreLocation()); assertEquals(null, options.getLogConfigFile()); assertEquals(0, options.getLogWatchFrequency()); + assertEquals("json", options.getInitialConfigurationStoreType()); + assertEquals(null, options.getInitialConfigurationStoreLocation()); } public void testConfigurationStoreLocation() { BrokerOptions options = startDummyMain("-sp abcd/config.xml"); - assertEquals("abcd/config.xml", options.getConfigurationStoreLocation()); + + options = startDummyMain("-store-path abcd/config2.xml"); + assertEquals("abcd/config2.xml", options.getConfigurationStoreLocation()); } public void testConfigurationStoreType() { BrokerOptions options = startDummyMain("-st dby"); - assertEquals("dby", options.getConfigurationStoreType()); + + options = startDummyMain("-store-type bdb"); + assertEquals("bdb", options.getConfigurationStoreType()); } public void testLogConfig() @@ -84,6 +90,25 @@ public class MainTest extends QpidTestCase assertTrue("Parsed command line didnt pick up help option", main.getCommandLine().hasOption("h")); } + public void testInitailConfigurationStoreLocation() + { + BrokerOptions options = startDummyMain("-isp abcd/config.xml"); + assertEquals("abcd/config.xml", options.getInitialConfigurationStoreLocation()); + + options = startDummyMain("-initial-store-path abcd/config.xml"); + assertEquals("abcd/config.xml", options.getInitialConfigurationStoreLocation()); + } + + public void testInitialConfigurationStoreType() + { + BrokerOptions options = startDummyMain("-ist dby"); + assertEquals("dby", options.getInitialConfigurationStoreType()); + + options = startDummyMain("-initial-store-type bdb"); + assertEquals("bdb", options.getInitialConfigurationStoreType()); + + } + private BrokerOptions startDummyMain(String commandLine) { return (new TestMain(commandLine.split("\\s"))).getOptions(); 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 d9bdd444aa..fa1bd966a7 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 @@ -21,19 +21,24 @@ package org.apache.qpid.server.configuration; import java.io.File; +import java.io.StringWriter; +import java.util.HashMap; +import java.util.Map; import java.util.Set; import java.util.UUID; -import org.apache.qpid.server.BrokerOptions; import org.apache.qpid.server.configuration.store.JsonConfigurationEntryStore; +import org.apache.qpid.server.model.Broker; import org.apache.qpid.test.utils.QpidTestCase; +import org.apache.qpid.test.utils.TestFileUtils; import org.apache.qpid.util.FileUtils; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.map.SerializationConfig; public class BrokerConfigurationStoreCreatorTest extends QpidTestCase { private File _userStoreLocation; private BrokerConfigurationStoreCreator _storeCreator; - private BrokerOptions _options; public void setUp() throws Exception { @@ -47,7 +52,6 @@ public class BrokerConfigurationStoreCreatorTest extends QpidTestCase } _storeCreator = new BrokerConfigurationStoreCreator(); _userStoreLocation = new File(TMP_FOLDER, "_store_" + System.currentTimeMillis() + "_" + getTestName()); - _options = new BrokerOptions(); } public void tearDown() throws Exception @@ -67,7 +71,7 @@ public class BrokerConfigurationStoreCreatorTest extends QpidTestCase public void testCreateJsonStore() { - ConfigurationEntryStore store = _storeCreator.createStore(_userStoreLocation.getAbsolutePath(), "json", _options); + ConfigurationEntryStore store = _storeCreator.createStore(_userStoreLocation.getAbsolutePath(), "json", null, null); assertNotNull("Store was not created", store); assertTrue("File should exists", _userStoreLocation.exists()); assertTrue("File size should be greater than 0", _userStoreLocation.length() > 0); @@ -77,12 +81,45 @@ public class BrokerConfigurationStoreCreatorTest extends QpidTestCase assertFalse("Unexpected children: " + childrenIds, childrenIds.isEmpty()); } + public void testCreateJsonStoreFromInitialStore() throws Exception + { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); + + Map<String, Object> brokerObjectMap = new HashMap<String, Object>(); + UUID brokerId = UUID.randomUUID(); + brokerObjectMap.put(Broker.ID, brokerId); + brokerObjectMap.put("name", "Test"); + + StringWriter sw = new StringWriter(); + objectMapper.writeValue(sw, brokerObjectMap); + + String brokerJson = sw.toString(); + + File _storeFile = TestFileUtils.createTempFile(this, ".json", brokerJson); + + ConfigurationEntryStore store = _storeCreator.createStore(_userStoreLocation.getAbsolutePath(), "json", _storeFile.getAbsolutePath(), "json"); + 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(); + jsonStore.open(_userStoreLocation.getAbsolutePath()); + ConfigurationEntry entry = jsonStore.getRootEntry(); + assertEquals("Unexpected root id", brokerId, 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")); + Set<UUID> childrenIds = entry.getChildrenIds(); + assertTrue("Unexpected children: " + childrenIds, childrenIds.isEmpty()); + } + public void testCreateDerbyStore() { //TODO: Implement DERBY store try { - _storeCreator.createStore(_userStoreLocation.getAbsolutePath(), "derby", _options); + _storeCreator.createStore(_userStoreLocation.getAbsolutePath(), "derby", null, null); fail("Store is not yet supported"); } catch(IllegalConfigurationException e) @@ -95,7 +132,7 @@ public class BrokerConfigurationStoreCreatorTest extends QpidTestCase { try { - _storeCreator.createStore(_userStoreLocation.getAbsolutePath(), "xml", _options); + _storeCreator.createStore(_userStoreLocation.getAbsolutePath(), "xml", null, null); fail("Store is not yet supported"); } catch(IllegalConfigurationException e) diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/store/JsonConfigurationEntryStoreTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/store/JsonConfigurationEntryStoreTest.java index a18d5501f0..7c9f4889f8 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/store/JsonConfigurationEntryStoreTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/configuration/store/JsonConfigurationEntryStoreTest.java @@ -1,6 +1,7 @@ package org.apache.qpid.server.configuration.store; import java.io.File; +import java.io.IOException; import java.io.StringWriter; import java.util.Collections; import java.util.HashMap; @@ -11,6 +12,8 @@ import org.apache.qpid.server.configuration.ConfigurationEntry; import org.apache.qpid.server.configuration.ConfigurationEntryStore; import org.apache.qpid.server.model.Broker; import org.apache.qpid.test.utils.TestFileUtils; +import org.codehaus.jackson.JsonGenerationException; +import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.SerializationConfig; @@ -19,6 +22,15 @@ public class JsonConfigurationEntryStoreTest extends ConfigurationEntryStoreTest private File _storeFile; private ObjectMapper _objectMapper; + @Override + public void setUp() throws Exception + { + _objectMapper = new ObjectMapper(); + _objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); + super.setUp(); + } + + @Override public void tearDown() throws Exception { _storeFile.delete(); @@ -28,9 +40,15 @@ public class JsonConfigurationEntryStoreTest extends ConfigurationEntryStoreTest @Override protected ConfigurationEntryStore createStore(UUID brokerId, Map<String, Object> brokerAttributes) throws Exception { - _objectMapper = new ObjectMapper(); - _objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); + _storeFile = createStoreFile(brokerId, brokerAttributes); + JsonConfigurationEntryStore store = new JsonConfigurationEntryStore(); + store.open(_storeFile.getAbsolutePath()); + return store; + } + private File createStoreFile(UUID brokerId, Map<String, Object> brokerAttributes) throws IOException, + JsonGenerationException, JsonMappingException + { Map<String, Object> brokerObjectMap = new HashMap<String, Object>(); brokerObjectMap.put(Broker.ID, brokerId); brokerObjectMap.put("@type", Broker.class.getSimpleName()); @@ -41,11 +59,7 @@ public class JsonConfigurationEntryStoreTest extends ConfigurationEntryStoreTest String brokerJson = sw.toString(); - _storeFile = TestFileUtils.createTempFile(this, ".json", brokerJson); - - JsonConfigurationEntryStore store = new JsonConfigurationEntryStore(); - store.open(_storeFile.getAbsolutePath()); - return store; + return TestFileUtils.createTempFile(this, ".json", brokerJson); } @Override @@ -74,4 +88,129 @@ public class JsonConfigurationEntryStoreTest extends ConfigurationEntryStoreTest assertEquals("Unresolved ACL value", aclLocation, store2.getRootEntry().getAttributes().get(Broker.ACL_FILE)); } + public void testOpenEmpty() + { + File file = TestFileUtils.createTempFile(this, ".json"); + JsonConfigurationEntryStore store = new JsonConfigurationEntryStore(); + store.open(file.getAbsolutePath()); + ConfigurationEntry root = store.getRootEntry(); + assertNotNull("Root entry is not found", root); + store.copyTo(file.getAbsolutePath()); + + JsonConfigurationEntryStore store2 = new JsonConfigurationEntryStore(); + store2.open(file.getAbsolutePath()); + ConfigurationEntry root2 = store.getRootEntry(); + assertEquals("Unexpected root entry", root.getId(), root2.getId()); + } + + public void testOpenNotEmpty() throws Exception + { + UUID brokerId = UUID.randomUUID(); + Map<String, Object> brokerAttributes = new HashMap<String, Object>(); + brokerAttributes.put(Broker.NAME, getTestName()); + File file = createStoreFile(brokerId, brokerAttributes); + + JsonConfigurationEntryStore store = new JsonConfigurationEntryStore(); + store.open(file.getAbsolutePath()); + ConfigurationEntry root = store.getRootEntry(); + assertNotNull("Root entry is not found", root); + assertEquals("Unexpected root entry", brokerId, root.getId()); + Map<String, Object> attributes = root.getAttributes(); + assertNotNull("Attributes not found", attributes); + assertEquals("Unexpected number of attriburtes", 1, attributes.size()); + assertEquals("Unexpected name attribute", getTestName(), attributes.get(Broker.NAME)); + } + + public void testOpenInMemoryEmpty() + { + JsonConfigurationEntryStore store = new JsonConfigurationEntryStore(); + store.open(JsonConfigurationEntryStore.IN_MEMORY); + + ConfigurationEntry root = store.getRootEntry(); + assertNotNull("Root entry is not found", root); + } + + public void testOpenWithInitialStoreLocation() throws Exception + { + UUID brokerId = UUID.randomUUID(); + Map<String, Object> brokerAttributes = new HashMap<String, Object>(); + brokerAttributes.put(Broker.NAME, getTestName()); + File initialStoreFile = createStoreFile(brokerId, brokerAttributes); + + File storeFile = TestFileUtils.createTempFile(this, ".json"); + JsonConfigurationEntryStore store = new JsonConfigurationEntryStore(); + store.open(storeFile.getAbsolutePath(), initialStoreFile.getAbsolutePath()); + + ConfigurationEntry root = store.getRootEntry(); + assertNotNull("Root entry is not found", root); + assertEquals("Unexpected root entry", brokerId, root.getId()); + Map<String, Object> attributes = root.getAttributes(); + assertNotNull("Attributes not found", attributes); + assertEquals("Unexpected number of attriburtes", 1, attributes.size()); + assertEquals("Unexpected name attribute", getTestName(), attributes.get(Broker.NAME)); + } + + public void testOpenInMemoryWithInitialStoreLocation() throws Exception + { + UUID brokerId = UUID.randomUUID(); + Map<String, Object> brokerAttributes = new HashMap<String, Object>(); + brokerAttributes.put(Broker.NAME, getTestName()); + File initialStoreFile = createStoreFile(brokerId, brokerAttributes); + + JsonConfigurationEntryStore store = new JsonConfigurationEntryStore(); + store.open(JsonConfigurationEntryStore.IN_MEMORY, initialStoreFile.getAbsolutePath()); + + ConfigurationEntry root = store.getRootEntry(); + assertNotNull("Root entry is not found", root); + assertEquals("Unexpected root entry", brokerId, root.getId()); + Map<String, Object> attributes = root.getAttributes(); + assertNotNull("Attributes not found", attributes); + assertEquals("Unexpected number of attriburtes", 1, attributes.size()); + assertEquals("Unexpected name attribute", getTestName(), attributes.get(Broker.NAME)); + } + + public void testOpenWithInitialStore() throws Exception + { + UUID brokerId = UUID.randomUUID(); + Map<String, Object> brokerAttributes = new HashMap<String, Object>(); + brokerAttributes.put(Broker.NAME, getTestName()); + File initialStoreFile = createStoreFile(brokerId, brokerAttributes); + + JsonConfigurationEntryStore initialStore = new JsonConfigurationEntryStore(); + initialStore.open(initialStoreFile.getAbsolutePath()); + + File storeFile = TestFileUtils.createTempFile(this, ".json"); + JsonConfigurationEntryStore store = new JsonConfigurationEntryStore(); + store.open(storeFile.getAbsolutePath(), initialStore); + + ConfigurationEntry root = store.getRootEntry(); + assertNotNull("Root entry is not found", root); + assertEquals("Unexpected root entry", brokerId, root.getId()); + Map<String, Object> attributes = root.getAttributes(); + assertNotNull("Attributes not found", attributes); + assertEquals("Unexpected number of attriburtes", 1, attributes.size()); + assertEquals("Unexpected name attribute", getTestName(), attributes.get(Broker.NAME)); + } + + public void testOpenInMemoryWithInitialStore() throws Exception + { + UUID brokerId = UUID.randomUUID(); + Map<String, Object> brokerAttributes = new HashMap<String, Object>(); + brokerAttributes.put(Broker.NAME, getTestName()); + File initialStoreFile = createStoreFile(brokerId, brokerAttributes); + + JsonConfigurationEntryStore initialStore = new JsonConfigurationEntryStore(); + initialStore.open(initialStoreFile.getAbsolutePath()); + + JsonConfigurationEntryStore store = new JsonConfigurationEntryStore(); + store.open(JsonConfigurationEntryStore.IN_MEMORY, initialStore); + + ConfigurationEntry root = store.getRootEntry(); + assertNotNull("Root entry is not found", root); + assertEquals("Unexpected root entry", brokerId, root.getId()); + Map<String, Object> attributes = root.getAttributes(); + assertNotNull("Attributes not found", attributes); + assertEquals("Unexpected number of attriburtes", 1, attributes.size()); + assertEquals("Unexpected name attribute", getTestName(), attributes.get(Broker.NAME)); + } } diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/model/BrokerShutdownTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/model/BrokerShutdownTest.java index 6ed9740c6d..7c1db6348b 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/model/BrokerShutdownTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/model/BrokerShutdownTest.java @@ -21,6 +21,7 @@ package org.apache.qpid.server.model; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import org.apache.qpid.server.configuration.ConfigurationEntry; import org.apache.qpid.server.configuration.ConfigurationEntryStore; @@ -95,51 +96,23 @@ public class BrokerShutdownTest extends QpidTestCase private Broker startBroker() throws Exception { - // test store with only broker and authentication provider entries - ConfigurationEntryStore store = new ConfigurationEntryStore() - { - private UUID _brokerId = UUID.randomUUID(); - private UUID _authenticationProviderId = UUID.randomUUID(); - - @Override - public ConfigurationEntry getRootEntry() - { - return new ConfigurationEntry(_brokerId, Broker.class.getSimpleName(), Collections.<String, Object> emptyMap(), - Collections.singleton(_authenticationProviderId), this); - } - - @Override - public ConfigurationEntry getEntry(UUID id) - { - if (_authenticationProviderId.equals(id)) - { - File file = TestFileUtils.createTempFile(BrokerShutdownTest.this, ".db.users"); - Map<String, Object> attributes = new HashMap<String, Object>(); - attributes.put(AuthenticationManagerFactory.ATTRIBUTE_TYPE, PlainPasswordFileAuthenticationManagerFactory.PROVIDER_TYPE); - attributes.put(PlainPasswordFileAuthenticationManagerFactory.ATTRIBUTE_PATH, file.getAbsolutePath()); - return new ConfigurationEntry(_authenticationProviderId, AuthenticationProvider.class.getSimpleName(), attributes, - Collections.<UUID> emptySet(), this); - } - return null; - } - - @Override - public void save(ConfigurationEntry... entries) - { - } - - @Override - public UUID[] remove(UUID... entryIds) - { - return null; - } - - @Override - public void open(String storeLocation) - { - } - - }; + ConfigurationEntryStore store = mock(ConfigurationEntryStore.class); + UUID brokerId = UUID.randomUUID(); + UUID authenticationProviderId = UUID.randomUUID(); + + ConfigurationEntry root = new ConfigurationEntry(brokerId, Broker.class.getSimpleName(), Collections.<String, Object> emptyMap(), + Collections.singleton(authenticationProviderId), store); + + File file = TestFileUtils.createTempFile(BrokerShutdownTest.this, ".db.users"); + Map<String, Object> attributes = new HashMap<String, Object>(); + attributes.put(AuthenticationManagerFactory.ATTRIBUTE_TYPE, PlainPasswordFileAuthenticationManagerFactory.PROVIDER_TYPE); + attributes.put(PlainPasswordFileAuthenticationManagerFactory.ATTRIBUTE_PATH, file.getAbsolutePath()); + ConfigurationEntry authenticationProviderEntry = new ConfigurationEntry(authenticationProviderId, AuthenticationProvider.class.getSimpleName(), attributes, + Collections.<UUID> emptySet(), store); + + when(store.getRootEntry()).thenReturn(root); + when(store.getEntry(brokerId)).thenReturn(root); + when(store.getEntry(authenticationProviderId)).thenReturn(authenticationProviderEntry); // mocking the required object StatisticsGatherer statisticsGatherer = mock(StatisticsGatherer.class); diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/test/utils/TestBrokerConfiguration.java b/qpid/java/systests/src/main/java/org/apache/qpid/test/utils/TestBrokerConfiguration.java index 5f712f1127..80f8010678 100644 --- a/qpid/java/systests/src/main/java/org/apache/qpid/test/utils/TestBrokerConfiguration.java +++ b/qpid/java/systests/src/main/java/org/apache/qpid/test/utils/TestBrokerConfiguration.java @@ -21,7 +21,6 @@ package org.apache.qpid.test.utils; import java.io.File; -import java.net.MalformedURLException; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -61,16 +60,7 @@ public class TestBrokerConfiguration { // TODO: add support for DERBY store _store = new JsonConfigurationEntryStore(); - - try - { - // load the initial store data into our store - _store.load(new File(intialStoreLocation).toURI().toURL()); - } - catch (MalformedURLException e) - { - // ignore - } + _store.open(JsonConfigurationEntryStore.IN_MEMORY, intialStoreLocation); } public boolean setBrokerAttribute(String name, Object value) @@ -100,7 +90,7 @@ public class TestBrokerConfiguration public boolean save(File configFile) { - _store.saveTo(configFile); + _store.copyTo(configFile.getAbsolutePath()); return true; } |
