diff options
| author | Robert Gemmell <robbie@apache.org> | 2013-04-15 10:41:44 +0000 |
|---|---|---|
| committer | Robert Gemmell <robbie@apache.org> | 2013-04-15 10:41:44 +0000 |
| commit | a6f4af6c1085bcb25124ba276c6b1e74c3931ef3 (patch) | |
| tree | 4baa106e7d2ae950ca4080a99de6acd5eafa7bf2 /qpid/java | |
| parent | 5eef6907dde64786c1d68d4903a7a5904fff6068 (diff) | |
| download | qpid-python-a6f4af6c1085bcb25124ba276c6b1e74c3931ef3.tar.gz | |
QPID-4742: add option to create a copy of the initial config file, either at a specified location or as initial-config.json in the current directory
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1467932 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/java')
5 files changed, 138 insertions, 76 deletions
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 4d03715f25..48e6351942 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 @@ -24,11 +24,11 @@ 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 { + public static final String DEFAULT_INITIAL_CONFIG_NAME = "initial-config.json"; public static final String DEFAULT_STORE_TYPE = "json"; public static final String DEFAULT_CONFIG_NAME_PREFIX = "config"; public static final String DEFAULT_LOG_CONFIG_FILE = "etc/log4j.xml"; 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 7c7bbb572b..28827a8bb5 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 @@ -20,6 +20,8 @@ */ package org.apache.qpid.server; +import java.io.File; + import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Option; @@ -30,6 +32,7 @@ import org.apache.commons.cli.PosixParser; import org.apache.log4j.Logger; import org.apache.qpid.common.QpidProperties; import org.apache.qpid.framing.ProtocolVersion; +import org.apache.qpid.server.configuration.store.ConfigurationEntryStoreUtil; /** * Main entry point for AMQPD. @@ -53,6 +56,10 @@ public class Main 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_CREATE_INITIAL_CONFIG = OptionBuilder.withArgName("path").hasOptionalArg().withDescription("create a copy of the initial config file, either to an" + + " optionally specified file path, or as " + BrokerOptions.DEFAULT_INITIAL_CONFIG_NAME + " in the current directory") + .withLongOpt("create-initial-config").create("cic"); + private static final Option OPTION_LOG_CONFIG_FILE = OptionBuilder.withArgName("file").hasArg() .withDescription("use the specified log4j xml configuration file. By " @@ -64,7 +71,7 @@ public class Main .withDescription("monitor the log file configuration file for changes. Units are seconds. " + "Zero means do not check for changes.").withLongOpt("logwatch").create("w"); - private static final Option OPTION_MANAGEMENT_MODE = OptionBuilder.withDescription("start broker in a management mode") + private static final Option OPTION_MANAGEMENT_MODE = OptionBuilder.withDescription("start broker in management mode, disabling the AMQP ports") .withLongOpt("management-mode").create("mm"); private static final Option OPTION_MM_QUIESCE_VHOST = OptionBuilder.withDescription("make virtualhosts stay in the quiesced state during management mode.") .withLongOpt("management-mode-quiesce-virtualhosts").create("mmqv"); @@ -84,6 +91,7 @@ public class Main OPTIONS.addOption(OPTION_CONFIGURATION_STORE_PATH); OPTIONS.addOption(OPTION_CONFIGURATION_STORE_TYPE); OPTIONS.addOption(OPTION_OVERWRITE_CONFIGURATION_STORE); + OPTIONS.addOption(OPTION_CREATE_INITIAL_CONFIG); OPTIONS.addOption(OPTION_LOG_CONFIG_FILE); OPTIONS.addOption(OPTION_LOG_WATCH); OPTIONS.addOption(OPTION_INITIAL_CONFIGURATION_PATH); @@ -146,11 +154,38 @@ public class Main protected void execute() throws Exception { + BrokerOptions options = new BrokerOptions(); + String initialConfigLocation = _commandLine.getOptionValue(OPTION_INITIAL_CONFIGURATION_PATH.getOpt()); + if (initialConfigLocation != null) + { + options.setInitialConfigurationLocation(initialConfigLocation); + } + + //process the remaining options if (_commandLine.hasOption(OPTION_HELP.getOpt())) { final HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("Qpid", OPTIONS, true); } + else if (_commandLine.hasOption(OPTION_CREATE_INITIAL_CONFIG.getOpt())) + { + File destinationFile = null; + + String destinationOption = _commandLine.getOptionValue(OPTION_CREATE_INITIAL_CONFIG.getOpt()); + if (destinationOption != null) + { + destinationFile = new File(destinationOption); + } + else + { + destinationFile = new File(System.getProperty("user.dir"), BrokerOptions.DEFAULT_INITIAL_CONFIG_NAME); + } + + ConfigurationEntryStoreUtil util = new ConfigurationEntryStoreUtil(); + util.copyInitialConfigFile(options.getInitialConfigurationLocation(), destinationFile); + + System.out.println("Initial config written to: " + destinationFile.getAbsolutePath()); + } else if (_commandLine.hasOption(OPTION_VERSION.getOpt())) { final StringBuilder protocol = new StringBuilder("AMQP version(s) [major.minor]: "); @@ -172,12 +207,12 @@ public class Main } else { - BrokerOptions options = new BrokerOptions(); String configurationStore = _commandLine.getOptionValue(OPTION_CONFIGURATION_STORE_PATH.getOpt()); if (configurationStore != null) { options.setConfigurationStoreLocation(configurationStore); } + String configurationStoreType = _commandLine.getOptionValue(OPTION_CONFIGURATION_STORE_TYPE.getOpt()); if (configurationStoreType != null) { @@ -196,12 +231,6 @@ public class Main options.setLogConfigFile(logConfig); } - String initialConfigLocation = _commandLine.getOptionValue(OPTION_INITIAL_CONFIGURATION_PATH.getOpt()); - if (initialConfigLocation != null) - { - options.setInitialConfigurationLocation(initialConfigLocation); - } - boolean overwriteConfigurationStore = _commandLine.hasOption(OPTION_OVERWRITE_CONFIGURATION_STORE.getOpt()); options.setOverwriteConfigurationStore(overwriteConfigurationStore); diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/ConfigurationEntryStoreUtil.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/ConfigurationEntryStoreUtil.java new file mode 100644 index 0000000000..6d895892b3 --- /dev/null +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/ConfigurationEntryStoreUtil.java @@ -0,0 +1,91 @@ +/* + * + * 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.store; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; + +import org.apache.qpid.server.configuration.IllegalConfigurationException; +import org.apache.qpid.util.FileUtils; + +public class ConfigurationEntryStoreUtil +{ + public void copyInitialConfigFile(String initialConfigLocation, File destinationFile) + { + URL initialStoreURL = toURL(initialConfigLocation); + InputStream in = null; + try + { + in = initialStoreURL.openStream(); + FileUtils.copy(in, destinationFile); + } + catch (IOException e) + { + throw new IllegalConfigurationException("Cannot create file " + destinationFile + " by copying initial config from " + initialConfigLocation , e); + } + finally + { + if (in != null) + { + try + { + in.close(); + } + catch (IOException e) + { + throw new IllegalConfigurationException("Cannot close initial config input stream: " + initialConfigLocation , e); + } + } + } + } + + public 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; + } + + protected URL fileToURL(File storeFile) + { + URL storeURL = null; + try + { + storeURL = storeFile.toURI().toURL(); + } + catch (MalformedURLException e) + { + throw new IllegalConfigurationException("Cannot create URL for file " + storeFile, e); + } + return storeURL; + } +} 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 ee31f3fda0..5f7e68b62a 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 @@ -1,9 +1,6 @@ package org.apache.qpid.server.configuration.store; import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.UUID; @@ -11,7 +8,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.IllegalConfigurationException; -import org.apache.qpid.util.FileUtils; public class JsonConfigurationEntryStore extends MemoryConfigurationEntryStore { @@ -46,7 +42,7 @@ public class JsonConfigurationEntryStore extends MemoryConfigurationEntryStore { initialiseStore(_storeFile, initialStore); } - load(fileToURL(_storeFile)); + load(getConfigurationEntryStoreUtil().fileToURL(_storeFile)); if(isGeneratedObjectIdDuringLoad()) { saveAsTree(_storeFile); @@ -91,7 +87,6 @@ public class JsonConfigurationEntryStore extends MemoryConfigurationEntryStore return "JsonConfigurationEntryStore [_storeFile=" + _storeFile + ", _rootId=" + getRootEntry().getId() + "]"; } - private void initialiseStore(File storeFile, ConfigurationEntryStore initialStore) { createFileIfNotExist(storeFile); @@ -103,7 +98,7 @@ public class JsonConfigurationEntryStore extends MemoryConfigurationEntryStore { if (initialStore instanceof MemoryConfigurationEntryStore && initialStore.getStoreLocation() != null) { - copyInitialStoreFile(initialStore.getStoreLocation(), storeFile); + getConfigurationEntryStoreUtil().copyInitialConfigFile(initialStore.getStoreLocation(), storeFile); } else { @@ -115,33 +110,4 @@ public class JsonConfigurationEntryStore extends MemoryConfigurationEntryStore } } - private void copyInitialStoreFile(String initialStoreLocation, File 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); - } - } - } - } - } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/MemoryConfigurationEntryStore.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/MemoryConfigurationEntryStore.java index 6931e22d63..ebb009c09c 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/MemoryConfigurationEntryStore.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/store/MemoryConfigurationEntryStore.java @@ -26,7 +26,6 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Collection; @@ -71,6 +70,7 @@ public class MemoryConfigurationEntryStore implements ConfigurationEntryStore private final ObjectMapper _objectMapper; private final Map<UUID, ConfigurationEntry> _entries; private final Map<String, Class<? extends ConfiguredObject>> _relationshipClasses; + private final ConfigurationEntryStoreUtil _util = new ConfigurationEntryStoreUtil(); private String _storeLocation; private UUID _rootId; @@ -119,7 +119,7 @@ public class MemoryConfigurationEntryStore implements ConfigurationEntryStore else { _storeLocation = initialStoreLocation; - load(toURL(_storeLocation)); + load(_util.toURL(_storeLocation)); } } @@ -289,21 +289,6 @@ public class MemoryConfigurationEntryStore implements ConfigurationEntryStore } } - protected 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; - } - protected void createFileIfNotExist(File file) { File parent = file.getParentFile(); @@ -349,20 +334,6 @@ public class MemoryConfigurationEntryStore implements ConfigurationEntryStore } } - protected URL fileToURL(File storeFile) - { - URL storeURL = null; - try - { - storeURL = storeFile.toURI().toURL(); - } - catch (MalformedURLException e) - { - throw new IllegalConfigurationException("Cannot create URL for file " + storeFile, e); - } - return storeURL; - } - private void loadFromJson(String json) { ByteArrayInputStream bais = null; @@ -691,4 +662,9 @@ public class MemoryConfigurationEntryStore implements ConfigurationEntryStore { return _generatedObjectIdDuringLoad; } + + protected ConfigurationEntryStoreUtil getConfigurationEntryStoreUtil() + { + return _util; + } } |
