You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ace.apache.org by an...@apache.org on 2009/08/31 11:29:47 UTC
svn commit: r809518 - in /incubator/ace/trunk:
gateway/src/org/apache/ace/configurator/Activator.java
gateway/src/org/apache/ace/configurator/Configurator.java
test/src/org/apache/ace/configurator/ConfiguratorTest.java
Author: angelos
Date: Mon Aug 31 09:29:46 2009
New Revision: 809518
URL: http://svn.apache.org/viewvc?rev=809518&view=rev
Log:
ACE-39 The configurator bundle now has a setting that causes it to reconfigure (or not). The default behavior is to use reconfiguration.
Modified:
incubator/ace/trunk/gateway/src/org/apache/ace/configurator/Activator.java
incubator/ace/trunk/gateway/src/org/apache/ace/configurator/Configurator.java
incubator/ace/trunk/test/src/org/apache/ace/configurator/ConfiguratorTest.java
Modified: incubator/ace/trunk/gateway/src/org/apache/ace/configurator/Activator.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/gateway/src/org/apache/ace/configurator/Activator.java?rev=809518&r1=809517&r2=809518&view=diff
==============================================================================
--- incubator/ace/trunk/gateway/src/org/apache/ace/configurator/Activator.java (original)
+++ incubator/ace/trunk/gateway/src/org/apache/ace/configurator/Activator.java Mon Aug 31 09:29:46 2009
@@ -32,7 +32,8 @@
manager.add(createService()
.setImplementation(new Configurator(new File(
getProperty(context.getProperty(Activator.class.getPackage().getName() + ".CONFIG_DIR"), "conf")),
- getProperty(context.getProperty(Activator.class.getPackage().getName() + ".POLL_INTERVAL"), 2000)))
+ getProperty(context.getProperty(Activator.class.getPackage().getName() + ".POLL_INTERVAL"), 2000),
+ getProperty(context.getProperty(Activator.class.getPackage().getName() + ".RECONFIG"), true)))
.add(createServiceDependency()
.setService(ConfigurationAdmin.class)
.setRequired(true))
@@ -52,4 +53,8 @@
public long getProperty(String prop, long def) {
return (prop == null) ? def : Long.parseLong(prop);
}
+
+ public boolean getProperty(String prop, boolean def) {
+ return (prop == null) ? def : Boolean.getBoolean(prop);
+ }
}
Modified: incubator/ace/trunk/gateway/src/org/apache/ace/configurator/Configurator.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/gateway/src/org/apache/ace/configurator/Configurator.java?rev=809518&r1=809517&r2=809518&view=diff
==============================================================================
--- incubator/ace/trunk/gateway/src/org/apache/ace/configurator/Configurator.java (original)
+++ incubator/ace/trunk/gateway/src/org/apache/ace/configurator/Configurator.java Mon Aug 31 09:29:46 2009
@@ -69,13 +69,22 @@
private final Map m_checksums = new HashMap(); // absolutepath -> xor(length, date)
private final Map m_foundFactories = new HashMap(); // absolutedirpath -> (absolutepath -> xor(length, date))
private Thread m_configThread;
+ private final boolean m_reconfig;
- public Configurator(File dir, long pollInterval) {
+ /**
+ * Instantiates a new configurator.
+ * @param dir The directory to watch.
+ * @param pollInterval The poll iterval in ms.
+ * @param reconfig Whether or not to use reconfiguration: if <code>false</code>, existing configuration
+ * values will not be overwritten, only new values (for a given pid) will be added.
+ */
+ public Configurator(File dir, long pollInterval, boolean reconfig) {
if ((dir == null) || !dir.isDirectory() || (pollInterval < 0)) {
throw new IllegalArgumentException("Bad arguments; either not an existing directory or an invalid interval.");
}
m_configDir = dir;
m_pollInterval = pollInterval;
+ m_reconfig = reconfig;
}
/**
@@ -211,16 +220,15 @@
try {
Configuration config = getConfiguration(pid, factoryPid);
Dictionary oldProps = config.getProperties();
- if (oldProps != null) {
- Enumeration keys = oldProps.keys();
- while (keys.hasMoreElements()) {
- String key = (String) keys.nextElement();
- if (properties.containsKey(key)) {
- // FIXME: this is to prevent overwriting configurations, that were changed by other means than this class, every time this class is ran
- // sadly, this also breaks editing a configuration file on the fly, degrading this class to do only first-time configurations (on a per key basis)
- // ultimately we may want to use autoconf everywhere for managing configurations, at the moment this is not feasible yet
- properties.put(key, oldProps.get(key));
- m_log.log(LogService.LOG_DEBUG, "Using previously configured value for bundle=" + pid + " key=" + key);
+ if (!m_reconfig) {
+ if (oldProps != null) {
+ Enumeration keys = oldProps.keys();
+ while (keys.hasMoreElements()) {
+ String key = (String) keys.nextElement();
+ if (properties.containsKey(key)) {
+ properties.put(key, oldProps.get(key));
+ m_log.log(LogService.LOG_DEBUG, "Using previously configured value for bundle=" + pid + " key=" + key);
+ }
}
}
}
Modified: incubator/ace/trunk/test/src/org/apache/ace/configurator/ConfiguratorTest.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/test/src/org/apache/ace/configurator/ConfiguratorTest.java?rev=809518&r1=809517&r2=809518&view=diff
==============================================================================
--- incubator/ace/trunk/test/src/org/apache/ace/configurator/ConfiguratorTest.java (original)
+++ incubator/ace/trunk/test/src/org/apache/ace/configurator/ConfiguratorTest.java Mon Aug 31 09:29:46 2009
@@ -44,11 +44,19 @@
@BeforeMethod(alwaysRun = true)
protected void setUp() throws Exception {
+ setUp(false);
+ }
+
+ /**
+ * Sets up the environment for testing.
+ * @param reconfig Indicates whether or not the configurator should use reconfiguration.
+ */
+ protected void setUp(boolean reconfig) throws Exception {
m_configAdmin = new MockConfigAdmin();
m_configDir = FileUtils.createTempFile(null);
m_configDir.mkdir();
- m_configurator = new Configurator(m_configDir, 400);
+ m_configurator = new Configurator(m_configDir, 400, reconfig);
TestUtils.configureObject(m_configurator, ConfigurationAdmin.class, m_configAdmin);
TestUtils.configureObject(m_configurator, LogService.class);
@@ -61,7 +69,6 @@
m_configurator.start();
}
-
/**
* save the properties into a configuration file the configurator can read.
* The file is first created and then moved to make sure the configuration doesn't read an empty file
@@ -150,7 +157,6 @@
}
// remove a configuration
- @SuppressWarnings("unchecked")
@Test(groups = { UNIT })
public void testRemoveFactoryConfiguration() {
Properties props = createProperties();
@@ -203,10 +209,10 @@
assert configuration.get("subst") != null : "Substitution failed";
}
- // update a configuration
+ // update a configuration, only adding a key (this is allowed in all cases)
@SuppressWarnings("unchecked")
@Test(groups = { UNIT })
- public void testChangeConfiguration() {
+ public void testChangeConfigurationUsingNewKey() {
Properties initialConfiguration = createProperties();
saveConfiguration("test-change", initialConfiguration);
@@ -223,6 +229,49 @@
assert configuration.equals(initialConfiguration) : "Configuration content not expected. Was expecting " + initialConfiguration.size() + " but got " + configuration.size();
}
+ // update a configuration, changing an already existing key, not using reconfiguration
+ @SuppressWarnings("unchecked")
+ @Test(groups = { UNIT })
+ public void testChangeConfigurationUsingSameKeyNoReconfigure() {
+ Properties configurationValues = createProperties();
+ Properties initialConfigurationValues = new Properties();
+ initialConfigurationValues.putAll(configurationValues);
+ saveConfiguration("test-change", configurationValues);
+
+ Dictionary configuration = getAndWaitForConfiguration(configurationValues);
+ assert configuration != null : "No configuration received from configurator";
+ assert configuration.equals(configurationValues) : "Configuration content not expected. Was expecting " + configurationValues.size() + " but got " + configuration.size();
+
+ configurationValues.put("test","value42");
+ saveConfiguration("test-change", configurationValues);
+
+ // The update should have been ignored, and the old values should still be present.
+ configuration = getAndWaitForConfiguration(configurationValues);
+ assert configuration != null : "No configuration received from configurator";
+ assert configuration.equals(initialConfigurationValues) : "Configuration content not expected. Was expecting " + configurationValues.size() + " but got " + configuration.size();
+ }
+
+ // update a configuration, changing an already existing key, using reconfiguration
+ @SuppressWarnings("unchecked")
+ @Test(groups = { UNIT })
+ public void testChangeConfigurationUsingSameKeyWithReconfigure() throws Exception {
+ setUp(true); // Instruct the configurator to reconfigure
+ Properties configurationValues = createProperties();
+ saveConfiguration("test-change", configurationValues);
+
+ Dictionary configuration = getAndWaitForConfiguration(configurationValues);
+ assert configuration != null : "No configuration received from configurator";
+ assert configuration.equals(configurationValues) : "Configuration content not expected. Was expecting " + configurationValues.size() + " but got " + configuration.size();
+
+ configurationValues.put("test","value42");
+ saveConfiguration("test-change", configurationValues);
+
+ // now the configuration should be updated
+ configuration = getAndWaitForConfiguration(configurationValues);
+ assert configuration != null : "No configuration received from configurator";
+ assert configuration.equals(configurationValues) : "Configuration content not expected. Was expecting " + configurationValues.size() + " but got " + configuration.size();
+ }
+
// remove a configuration
@SuppressWarnings("unchecked")
@Test(groups = { UNIT })