From 928fed665a842440179db42ef090be91217284ce Mon Sep 17 00:00:00 2001 From: Alex Rudyy Date: Fri, 13 Feb 2015 17:13:35 +0000 Subject: QPID-6390: [Java Broker] Move setting of initial properties from Main into Broker git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1659613 13f79535-47bb-0310-9956-ffa450edef68 --- .../main/java/org/apache/qpid/server/Broker.java | 34 +++++++ .../java/org/apache/qpid/server/BrokerOptions.java | 21 +++++ .../src/main/resources/system.properties | 20 ---- .../org/apache/qpid/server/BrokerOptionsTest.java | 10 ++ .../java/org/apache/qpid/server/BrokerTest.java | 101 +++++++++++++++++++++ .../src/main/java/org/apache/qpid/server/Main.java | 39 ++------ 6 files changed, 172 insertions(+), 53 deletions(-) delete mode 100644 qpid/java/broker-core/src/main/resources/system.properties create mode 100644 qpid/java/broker-core/src/test/java/org/apache/qpid/server/BrokerTest.java diff --git a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/Broker.java b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/Broker.java index c18923ffe0..0d291fb6ea 100644 --- a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/Broker.java +++ b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/Broker.java @@ -23,9 +23,12 @@ package org.apache.qpid.server; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.net.URL; import java.security.PrivilegedAction; import java.security.PrivilegedExceptionAction; +import java.util.HashSet; import java.util.Properties; +import java.util.Set; import javax.security.auth.Subject; @@ -33,6 +36,7 @@ import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; +import org.apache.qpid.common.QpidProperties; import org.apache.qpid.server.configuration.BrokerProperties; import org.apache.qpid.server.configuration.updater.TaskExecutor; import org.apache.qpid.server.configuration.updater.TaskExecutorImpl; @@ -147,6 +151,8 @@ public class Broker implements BrokerShutdownProvider private void startupImpl(final BrokerOptions options) throws Exception { + populateSystemPropertiesFromDefaults(options.getInitialSystemProperties()); + String storeLocation = options.getConfigurationStoreLocation(); String storeType = options.getConfigurationStoreType(); @@ -314,6 +320,34 @@ public class Broker implements BrokerShutdownProvider } } + private void populateSystemPropertiesFromDefaults(final String initialProperties) throws IOException + { + URL initialPropertiesLocation; + if(initialProperties == null) + { + initialPropertiesLocation = getClass().getClassLoader().getResource("system.properties"); + } + else + { + initialPropertiesLocation = (new File(initialProperties)).toURI().toURL(); + } + + Properties props = new Properties(QpidProperties.asProperties()); + if(initialPropertiesLocation != null) + { + props.load(initialPropertiesLocation.openStream()); + } + + Set propertyNames = new HashSet<>(props.stringPropertyNames()); + propertyNames.removeAll(System.getProperties().stringPropertyNames()); + for (String propName : propertyNames) + { + System.setProperty(propName, props.getProperty(propName)); + } + + } + + private class ShutdownService implements Runnable { public void run() diff --git a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/BrokerOptions.java b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/BrokerOptions.java index 59075dfb57..ff3d9063f0 100644 --- a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/BrokerOptions.java +++ b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/BrokerOptions.java @@ -78,6 +78,7 @@ public class BrokerOptions private boolean _overwriteConfigurationStore; private Map _configProperties = new HashMap(); private boolean _startupLoggedToSystemOut = true; + private String _initialSystemProperties; public Map convertToSystemConfigAttributes() { @@ -390,4 +391,24 @@ public class BrokerOptions { this._startupLoggedToSystemOut = startupLoggedToSystemOut; } + + /** + * Get the location of initial JVM system properties to set. This can be URL or a file path + * + * @return the location of initial JVM system properties to set. + */ + public String getInitialSystemProperties() + { + return _initialSystemProperties; + } + + /** + * Set the location of initial properties file to set as JVM system properties. This can be URL or a file path + * + * @param initialSystemProperties the location of initial JVM system properties. + */ + public void setInitialSystemProperties(String initialSystemProperties) + { + _initialSystemProperties = initialSystemProperties; + } } diff --git a/qpid/java/broker-core/src/main/resources/system.properties b/qpid/java/broker-core/src/main/resources/system.properties deleted file mode 100644 index 661b0cba77..0000000000 --- a/qpid/java/broker-core/src/main/resources/system.properties +++ /dev/null @@ -1,20 +0,0 @@ -# -# 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. -# - - diff --git a/qpid/java/broker-core/src/test/java/org/apache/qpid/server/BrokerOptionsTest.java b/qpid/java/broker-core/src/test/java/org/apache/qpid/server/BrokerOptionsTest.java index 8c115c6e62..2846950bc1 100644 --- a/qpid/java/broker-core/src/test/java/org/apache/qpid/server/BrokerOptionsTest.java +++ b/qpid/java/broker-core/src/test/java/org/apache/qpid/server/BrokerOptionsTest.java @@ -309,4 +309,14 @@ public class BrokerOptionsTest extends QpidTestCase assertEquals("unexpected number of entries", 2, props.keySet().size()); assertEquals("value", props.get("name")); } + + + public void testSetInitialSystemProperties() + { + assertNull("Unexpected default value for initial system properties", _options.getInitialSystemProperties()); + + _options.setInitialSystemProperties("test.properties"); + + assertEquals("test.properties", _options.getInitialSystemProperties()); + } } diff --git a/qpid/java/broker-core/src/test/java/org/apache/qpid/server/BrokerTest.java b/qpid/java/broker-core/src/test/java/org/apache/qpid/server/BrokerTest.java new file mode 100644 index 0000000000..195414c7d7 --- /dev/null +++ b/qpid/java/broker-core/src/test/java/org/apache/qpid/server/BrokerTest.java @@ -0,0 +1,101 @@ +/* + * + * 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; + + +import java.io.File; +import java.util.HashMap; +import java.util.Map; + + +import org.apache.qpid.server.model.BrokerModel; +import org.apache.qpid.server.model.ConfiguredObject; +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; + +public class BrokerTest extends QpidTestCase +{ + private static final String INITIAL_SYSTEM_PROPERTY = "test"; + private static final String INITIAL_SYSTEM_PROPERTY_VALUE = "testValue"; + + private File _initialSystemProperties; + private File _initialConfiguration; + private File _brokerWork; + private Broker _broker; + + @Override + public void setUp() throws Exception + { + super.setUp(); + + // create empty initial configuration + Map initialConfig = new HashMap<>(); + initialConfig.put(ConfiguredObject.NAME, "test"); + initialConfig.put(org.apache.qpid.server.model.Broker.MODEL_VERSION, BrokerModel.MODEL_VERSION); + + ObjectMapper mapper = new ObjectMapper(); + String config = mapper.writeValueAsString(initialConfig); + _initialConfiguration = TestFileUtils.createTempFile(this, ".initial-config.json", config); + _brokerWork = TestFileUtils.createTestDirectory("qpid-work", true); + _initialSystemProperties = TestFileUtils.createTempFile(this, ".initial-system.properties", + INITIAL_SYSTEM_PROPERTY + "=" + INITIAL_SYSTEM_PROPERTY_VALUE + + "\nQPID_WORK=" + _brokerWork.getAbsolutePath() + "_test"); + setTestSystemProperty("QPID_WORK", _brokerWork.getAbsolutePath()); + } + + public void tearDown() throws Exception + { + try + { + super.tearDown(); + } + finally + { + if (_broker != null) + { + _broker.shutdown(); + } + System.clearProperty(INITIAL_SYSTEM_PROPERTY); + FileUtils.delete(_brokerWork, true); + FileUtils.delete(_initialSystemProperties, false); + FileUtils.delete(_initialConfiguration, false); + } + } + + public void testInitialSystemPropertiesAreSetOnBrokerStartup() throws Exception + { + BrokerOptions options = new BrokerOptions(); + options.setInitialSystemProperties(_initialSystemProperties.getAbsolutePath()); + options.setSkipLoggingConfiguration(true); + options.setStartupLoggedToSystemOut(true); + options.setInitialConfigurationLocation(_initialConfiguration.getAbsolutePath()); + _broker = new Broker(); + _broker.startup(options); + + // test JVM system property should be set from initial system config file + assertEquals("Unexpected JVM system property", INITIAL_SYSTEM_PROPERTY_VALUE, System.getProperty(INITIAL_SYSTEM_PROPERTY)); + + // existing system property should not be overridden + assertEquals("Unexpected QPID_WORK system property", _brokerWork.getAbsolutePath(), System.getProperty("QPID_WORK")); + } +} 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 85fe7af0fb..be2252abbd 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 @@ -25,9 +25,6 @@ import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; -import java.util.HashSet; -import java.util.Properties; -import java.util.Set; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.HelpFormatter; @@ -179,11 +176,14 @@ public class Main protected void execute() throws Exception { - String initialProperties = _commandLine.getOptionValue(OPTION_INITIAL_SYSTEM_PROPERTIES.getOpt()); - populateSystemPropertiesFromDefaults(initialProperties); - BrokerOptions options = new BrokerOptions(); + String initialProperties = _commandLine.getOptionValue(OPTION_INITIAL_SYSTEM_PROPERTIES.getOpt()); + if (initialProperties != null) + { + options.setInitialSystemProperties(initialProperties); + } + String initialConfigLocation = _commandLine.getOptionValue(OPTION_INITIAL_CONFIGURATION_PATH.getOpt()); if (initialConfigLocation != null) { @@ -320,33 +320,6 @@ public class Main } } - private void populateSystemPropertiesFromDefaults(final String initialProperties) throws IOException - { - URL initialPropertiesLocation; - if(initialProperties == null) - { - initialPropertiesLocation = getClass().getClassLoader().getResource("system.properties"); - } - else - { - initialPropertiesLocation = (new File(initialProperties)).toURI().toURL(); - } - - Properties props = new Properties(QpidProperties.asProperties()); - if(initialPropertiesLocation != null) - { - props.load(initialPropertiesLocation.openStream()); - } - - Set propertyNames = new HashSet<>(props.stringPropertyNames()); - propertyNames.removeAll(System.getProperties().stringPropertyNames()); - for (String propName : propertyNames) - { - System.setProperty(propName, props.getProperty(propName)); - } - - } - private void copyInitialConfigFile(final BrokerOptions options, final File destinationFile) { String initialConfigLocation = options.getInitialConfigurationLocation(); -- cgit v1.2.1