Return-Path:
*
*
- * <configuration>
+ * <configuration systemProperties="properties file name">
* <header>
* <!-- Optional meta information about the composite configuration -->
* </header>
@@ -90,7 +90,13 @@
*
* The name of the root element (here configuration
) is
- * arbitrary. There are two sections (both of them are optional) for declaring
+ * arbitrary. The optional systemProperties attribute identifies the path to
+ * a property file containing properties that should be added to the system
+ * properties. If specified on the root element, the system properties are
+ * set before the rest of the configuration is processed.
+ *
+ * There are two sections (both of them are optional) for declaring
* override and additional configurations. Configurations
* in the former section are evaluated in the order of their declaration, and
* properties of configurations declared earlier hide those of configurations
@@ -262,6 +268,12 @@
+ "forceCreate"
+ DefaultExpressionEngine.DEFAULT_ATTRIBUTE_END;
+ /**
+ * Constant for the tag attribute for providers.
+ */
+ static final String KEY_SYSTEM_PROPS = "[@systemProperties]";
+
+
/** Constant for the name of the header section. */
static final String SEC_HEADER = "header";
@@ -547,6 +559,7 @@
load();
}
+ initSystemProperties();
registerConfiguredProviders();
registerConfiguredLookups();
@@ -676,6 +689,27 @@
}
/**
+ * If a property file is configured add the properties to the System properties.
+ * @throws ConfigurationException if an error occurs.
+ */
+ protected void initSystemProperties() throws ConfigurationException
+ {
+ String fileName = getString(KEY_SYSTEM_PROPS);
+ if (fileName != null)
+ {
+ try
+ {
+ SystemConfiguration.setSystemProperties(fileName);
+ }
+ catch (Exception ex)
+ {
+ throw new ConfigurationException("Error setting system properties from " + fileName, ex);
+ }
+
+ }
+ }
+
+ /**
* Performs interpolation. This method will not only take this configuration
* instance into account (which is the one that loaded the configuration
* definition file), but also the so far constructed combined configuration.
Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/SystemConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/SystemConfiguration.java?rev=727843&r1=727842&r2=727843&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/SystemConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/SystemConfiguration.java Thu Dec 18 14:23:33 2008
@@ -17,6 +17,8 @@
package org.apache.commons.configuration2;
+import java.util.Iterator;
+
/**
* A configuration based on the system properties.
*
@@ -35,4 +37,32 @@
{
super(System.getProperties());
}
+
+
+ /**
+ * The method allows system properties to be set from a property file.
+ * @param fileName The name of the property file.
+ * @throws Exception if an error occurs.
+ */
+ public static void setSystemProperties(String fileName) throws Exception
+ {
+ PropertiesConfiguration config = fileName.endsWith(".xml")
+ ? new XMLPropertiesConfiguration(fileName) : new PropertiesConfiguration(fileName);
+ setSystemProperties(config);
+ }
+
+ /**
+ * Set System properties from a configuration file.
+ * @param systemConfig The configuration containing the properties to be set.
+ */
+ public static void setSystemProperties(PropertiesConfiguration systemConfig)
+ {
+ Iterator iter = systemConfig.getKeys();
+ while (iter.hasNext())
+ {
+ String key = (String) iter.next();
+ String value = (String) systemConfig.getProperty(key);
+ System.setProperty(key, value);
+ }
+ }
}
Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestDefaultConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestDefaultConfigurationBuilder.java?rev=727843&r1=727842&r2=727843&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestDefaultConfigurationBuilder.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestDefaultConfigurationBuilder.java Thu Dec 18 14:23:33 2008
@@ -65,8 +65,11 @@
private static final File PROVIDER_FILE = ConfigurationAssert
.getTestFile("testConfigurationProvider.xml");
- private static final File GLOBAL_LOOKUP_FILE = new File(
- "conf/testGlobalLookup.xml");
+ private static final File GLOBAL_LOOKUP_FILE = ConfigurationAssert
+ .getTestFile("testGlobalLookup.xml");
+
+ private static final File SYSTEM_PROPS_FILE = ConfigurationAssert
+ .getTestFile("testSystemProperties.xml");
/** Constant for the name of an optional configuration.*/
@@ -805,6 +808,15 @@
assertEquals("Incorrect value retrieved","test.value",value);
}
+ public void testSystemProperties() throws Exception
+ {
+ factory.setFile(SYSTEM_PROPS_FILE);
+ CombinedConfiguration cc = factory.getConfiguration(true);
+ String value = System.getProperty("key1");
+ assertNotNull("The test key was not located", value);
+ assertEquals("Incorrect value retrieved","value1",value);
+ }
+
/**
* A specialized combined configuration implementation used for testing
* custom result classes.
Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestSystemConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestSystemConfiguration.java?rev=727843&r1=727842&r2=727843&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestSystemConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestSystemConfiguration.java Thu Dec 18 14:23:33 2008
@@ -40,4 +40,13 @@
Configuration conf = new SystemConfiguration();
assertEquals("number", 123, conf.getInt("test.number"));
}
+
+ public void testSetSystemProperties()
+ {
+ PropertiesConfiguration props = new PropertiesConfiguration();
+ props.addProperty("test.name", "Apache");
+ SystemConfiguration conf = new SystemConfiguration();
+ conf.setSystemProperties(props);
+ assertEquals("System Properties", "Apache", System.getProperty("test.name"));
+ }
}
Added: commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testSystemProperties.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testSystemProperties.xml?rev=727843&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testSystemProperties.xml (added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testSystemProperties.xml Thu Dec 18 14:23:33 2008
@@ -0,0 +1,16 @@
+
+
+ConfigurationFactory
class that was introduced in the
last
section is a powerful tool for dealing with multiple different
- configuration sources, but it also has some shortcommings:
+ configuration sources, but it also has some shortcomings:
From its basic usage scenarios DefaultConfigurationBuilder
is
very similar to ConfigurationFactory
. It is able to process
the same configuration definition files as can be read by
- ConfigurationFactory
, but supports some more options. It
- follows a list with the main differences between these classes:
+ ConfigurationFactory
, but supports more options. The following
+ list identifies the main differences between these classes:
As was already pointed out, DefaultConfigurationBuilder
- maintains compatibility to ConfigurationFactory
in that it
+ maintains compatibility with ConfigurationFactory
in that it
understands the same configuration definition files. In addition to the
elements that are allowed in a configuration definition file for
ConfigurationFactory
the data files for
DefaultConfigurationBuilder
support some additional options
- providing a greater flexibility. This section explains these enhanced
+ providing greater flexibility. This section explains these enhanced
features.
@@ -101,7 +101,7 @@
In addition to the attributes that correspond to properties of the
- configuration object to be created a configuration declaration can have a
+ configuration object to be created, a configuration declaration can have a
set of special attributes that are evaluated by
DefaultConfigurationBuilder
when it creates the objects.
These attributes are listed in the following table:
@@ -199,9 +199,9 @@
config-name
config-at
config-optional
config-name
- attribute), you can later check the combined configuration whether it
- contains a configuration with this name. With the
+ occur when creating the configuration are silently ignored. The default
+ behavior when an error occurs is that no configuration is added to
+ the resulting combined configuration. This behavior can be used to find
+ out whether an optional configuration could be successfully created or
+ not. If you specify a name for the optional configuration (using the
+ config-name
attribute), you can later check whether the
+ combined configuration contains a configuration with this name. With the
config-forceCreate
attribute (see below) this default
behavior can be changed.${CONFIG_FILE}
variable will then be
undefined at the moment it is evaluated.
+
+ + This example differs from the previous one by allowing CONFIG_FILE, and other + properties, to be defined in a properties file and added to the system + properties before the configuration is constructed. +
The header section
@@ -323,7 +336,7 @@ node combiner.
- The combiner
section allows to define nodes as list nodes.
+ The combiner
section allows nodes to be defined as list nodes.
This can be necessary for certain node combiner implementations to work
correctly. More information can be found in the section about
Node combiners.