You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by rg...@apache.org on 2008/12/18 23:16:33 UTC
svn commit: r727834 - in /commons/proper/configuration/trunk: ./ conf/
src/java/org/apache/commons/configuration/
src/test/org/apache/commons/configuration/ xdocs/ xdocs/userguide/
Author: rgoers
Date: Thu Dec 18 14:16:32 2008
New Revision: 727834
URL: http://svn.apache.org/viewvc?rev=727834&view=rev
Log:
Allow system properties to be set from a configuration. Fix grammar errors in defaultconfigurationbuilder documentation
Added:
commons/proper/configuration/trunk/conf/testSystemProperties.xml (contents, props changed)
- copied, changed from r724593, commons/proper/configuration/trunk/conf/testConfigurationProvider.xml
Modified:
commons/proper/configuration/trunk/pom.xml
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/SystemConfiguration.java
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestSystemConfiguration.java
commons/proper/configuration/trunk/xdocs/changes.xml
commons/proper/configuration/trunk/xdocs/userguide/howto_configurationbuilder.xml
Copied: commons/proper/configuration/trunk/conf/testSystemProperties.xml (from r724593, commons/proper/configuration/trunk/conf/testConfigurationProvider.xml)
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/conf/testSystemProperties.xml?p2=commons/proper/configuration/trunk/conf/testSystemProperties.xml&p1=commons/proper/configuration/trunk/conf/testConfigurationProvider.xml&r1=724593&r2=727834&rev=727834&view=diff
==============================================================================
--- commons/proper/configuration/trunk/conf/testConfigurationProvider.xml (original)
+++ commons/proper/configuration/trunk/conf/testSystemProperties.xml Thu Dec 18 14:16:32 2008
@@ -1,37 +1,16 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
-<!-- Test configuration definition file that demonstrates complex initialization -->
-<configuration>
+<!-- Test configuration definition file that sets system properties from an XML file -->
+<configuration systemProperties="test.properties.xml">
<header>
<result delimiterParsingDisabled="true">
<nodeCombiner config-class="org.apache.commons.configuration.tree.OverrideCombiner"/>
<expressionEngine config-class="org.apache.commons.configuration.tree.xpath.XPathExpressionEngine"/>
</result>
- <providers>
- <provider config-tag="test"
- config-class="org.apache.commons.configuration.DefaultConfigurationBuilder$FileConfigurationProvider"/>
- </providers>
- <combiner>
- <override>
- <list-nodes>
- <node>table</node>
- <node>list</node>
- </list-nodes>
- </override>
- </combiner>
</header>
<system/>
- <properties fileName="test.properties" throwExceptionOnMissing="true"
+ <properties fileName="test2.properties" throwExceptionOnMissing="true"
config-name="properties">
<reloadingStrategy config-class="org.apache.commons.configuration.reloading.FileChangedReloadingStrategy"
refreshDelay="10000"/>
</properties>
- <!-- Fetch the file name from a variable -->
- <xml fileName="${test_file_xml}" config-name="xml">
- <expressionEngine config-class="org.apache.commons.configuration.tree.DefaultExpressionEngine"
- propertyDelimiter="/" indexStart="[" indexEnd="]"/>
- </xml>
- <additional>
- <xml config-name="combiner1" fileName="${test_file_combine}"/> -->
- <xml config-name="combiner2" fileName="testcombine2.xml"/>
- </additional>
</configuration>
\ No newline at end of file
Propchange: commons/proper/configuration/trunk/conf/testSystemProperties.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/configuration/trunk/conf/testSystemProperties.xml
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/proper/configuration/trunk/conf/testSystemProperties.xml
------------------------------------------------------------------------------
svn:mergeinfo =
Propchange: commons/proper/configuration/trunk/conf/testSystemProperties.xml
------------------------------------------------------------------------------
svn:mime-type = text/xml
Modified: commons/proper/configuration/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/pom.xml?rev=727834&r1=727833&r2=727834&view=diff
==============================================================================
--- commons/proper/configuration/trunk/pom.xml (original)
+++ commons/proper/configuration/trunk/pom.xml Thu Dec 18 14:16:32 2008
@@ -160,6 +160,7 @@
<name>Ralph Goers</name>
<id>rgoers</id>
<email>rgoers@apache.org</email>
+ <organization>Intuit</organization>
<timezone>-8</timezone>
<roles>
<role>Java Developer</role>
Modified: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java?rev=727834&r1=727833&r2=727834&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java (original)
+++ commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java Thu Dec 18 14:16:32 2008
@@ -69,7 +69,7 @@
* <p>
*
* <pre>
- * <configuration>
+ * <configuration systemProperties="properties file name">
* <header>
* <!-- Optional meta information about the composite configuration -->
* </header>
@@ -85,7 +85,13 @@
* </p>
* <p>
* The name of the root element (here <code>configuration</code>) 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.
+ * </p>
+ * <p>
+ * There are two sections (both of them are optional) for declaring
* <em>override</em> and <em>additional</em> configurations. Configurations
* in the former section are evaluated in the order of their declaration, and
* properties of configurations declared earlier hide those of configurations
@@ -259,6 +265,11 @@
+ "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";
@@ -537,6 +548,7 @@
load();
}
+ initSystemProperties();
registerConfiguredProviders();
registerConfiguredLookups();
@@ -671,6 +683,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/trunk/src/java/org/apache/commons/configuration/SystemConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/SystemConfiguration.java?rev=727834&r1=727833&r2=727834&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/SystemConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/SystemConfiguration.java Thu Dec 18 14:16:32 2008
@@ -17,6 +17,8 @@
package org.apache.commons.configuration;
+import java.util.Iterator;
+
/**
* A configuration based on the system properties.
*
@@ -35,4 +37,31 @@
{
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/trunk/src/test/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java?rev=727834&r1=727833&r2=727834&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java (original)
+++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java Thu Dec 18 14:16:32 2008
@@ -70,6 +70,9 @@
private static final File GLOBAL_LOOKUP_FILE = new File(
"conf/testGlobalLookup.xml");
+ private static final File SYSTEM_PROPS_FILE = new File(
+ "conf/testSystemProperties.xml");
+
/** Constant for the name of an optional configuration.*/
private static final String OPTIONAL_NAME = "optionalConfig";
@@ -808,6 +811,15 @@
assertNotNull("The test key was not located", value);
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);
+ }
/**
Modified: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestSystemConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestSystemConfiguration.java?rev=727834&r1=727833&r2=727834&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestSystemConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestSystemConfiguration.java Thu Dec 18 14:16:32 2008
@@ -37,4 +37,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"));
+ }
}
Modified: commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/xdocs/changes.xml?rev=727834&r1=727833&r2=727834&view=diff
==============================================================================
--- commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ commons/proper/configuration/trunk/xdocs/changes.xml Thu Dec 18 14:16:32 2008
@@ -30,6 +30,9 @@
Commons Logging, Commons BeanUtils, and Commons JXPath. The older
versions should still work.
</action>
+ <action dev="rgoers" type="add" issue="CONFIGURATION-353">
+ Allow system properties to be set from a configuration file.
+ </action>
<action dev="rgoers" type="add" issue="CONFIGURATION-351">
Allow variable resolvers to be defined configured in
DefaultConfigurationBuilder.
Modified: commons/proper/configuration/trunk/xdocs/userguide/howto_configurationbuilder.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/xdocs/userguide/howto_configurationbuilder.xml?rev=727834&r1=727833&r2=727834&view=diff
==============================================================================
--- commons/proper/configuration/trunk/xdocs/userguide/howto_configurationbuilder.xml (original)
+++ commons/proper/configuration/trunk/xdocs/userguide/howto_configurationbuilder.xml Thu Dec 18 14:16:32 2008
@@ -29,7 +29,7 @@
The <code>ConfigurationFactory</code> class that was introduced in the
<a href="howto_configurationfactory.html#Using_a_Configuration_Factory">last
section</a> 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:
<ul>
<li>The format for configuration definition files is not extensible.</li>
<li>Complex initializations of the declared configuration sources (e.g.
@@ -48,13 +48,13 @@
DefaultConfigurationBuilder</a></code> was introduced.
</p>
- <subsection name="Differences to ConfigurationFactory">
+ <subsection name="ConfigurationFactory Differences">
<p>
From its basic usage scenarios <code>DefaultConfigurationBuilder</code> is
very similar to <code>ConfigurationFactory</code>. It is able to process
the same configuration definition files as can be read by
- <code>ConfigurationFactory</code>, but supports some more options. It
- follows a list with the main differences between these classes:
+ <code>ConfigurationFactory</code>, but supports more options. The following
+ list identifies the main differences between these classes:
</p>
<p>
<ul>
@@ -79,15 +79,15 @@
</p>
</subsection>
- <subsection name="Enhancements of the configuration definition file">
+ <subsection name="Enhancements to the configuration definition file">
<p>
As was already pointed out, <code>DefaultConfigurationBuilder</code>
- maintains compatibility to <code>ConfigurationFactory</code> in that it
+ maintains compatibility with <code>ConfigurationFactory</code> in that it
understands the same configuration definition files. In addition to the
elements that are allowed in a configuration definition file for
<code>ConfigurationFactory</code> the data files for
<code>DefaultConfigurationBuilder</code> support some additional options
- providing a greater flexibility. This section explains these enhanced
+ providing greater flexibility. This section explains these enhanced
features.
</p>
<p>
@@ -101,7 +101,7 @@
<source><![CDATA[
<?xml version="1.0" encoding="ISO-8859-1" ?>
-<configuration>
+<configuration systemProperties="path to property file">
<header>
<!-- Meta data about the resulting combined configuration -->
</header>
@@ -139,13 +139,13 @@
<p>
<dl>
<dt>configuration</dt>
- <dd>The <code>configuration</code> tag allows to include other
- configuration definition files. This makes it possible to nest these
+ <dd>The <code>configuration</code> tag allows other configuration
+ definition files to be included. This makes it possible to nest these
definition files up to an arbitrary depth. In fact, this tag will
create another <code>DefaultConfigurationBuilder</code> object,
initialize it, and obtain the <code>CombinedConfiguation</code> from it.
This combined configuration will then be added to the resulting
- combined configuration. Like for other file-based configurations the
+ combined configuration. Like all file-based configurations the
<code>fileName</code> attribute can be used to specify the configuration
definition file to be loaded. This file must be an XML document that
conforms to the format described here.</dd>
@@ -186,7 +186,7 @@
</p>
<p>
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
<code>DefaultConfigurationBuilder</code> when it creates the objects.
These attributes are listed in the following table:
@@ -199,9 +199,9 @@
</tr>
<tr>
<td valign="top"><code>config-name</code></td>
- <td>Allows to specify a name for this configuration. This name can be used
- to obtain a reference to the configuration from the resulting combined
- configuration (see below).</td>
+ <td>Allows a name to be specified for this configuration. This name can
+ be used to obtain a reference to the configuration from the resulting
+ combined configuration (see below).</td>
</tr>
<tr>
<td valign="top"><code>config-at</code></td>
@@ -211,13 +211,13 @@
<tr>
<td valign="top"><code>config-optional</code></td>
<td>Declares a configuration as optional. This means that errors that
- occur when creating the configuration are silently ignored. Note that in
- case of an error per default no configuration is added to the resulting combined
- configuration. This fact 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 <code>config-name</code>
- 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
+ <code>config-name</code> attribute), you can later check whether the
+ combined configuration contains a configuration with this name. With the
<code>config-forceCreate</code> attribute (see below) this default
behavior can be changed.</td>
</tr>
@@ -280,6 +280,19 @@
will occur because the <code>${CONFIG_FILE}</code> variable will then be
undefined at the moment it is evaluated.
</p>
+ <source><![CDATA[
+<configuration systemProperties="systemProperties.xml">
+ <!-- Load the system properties -->
+ <system/>
+ <!-- Now load the config file, using a system property as file name -->
+ <properties fileName="${CONFIG_FILE}"/>
+</configuration>
+]]></source>
+ <p>
+ 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.
+ </p>
<p>
<strong>The header section</strong>
</p>
@@ -323,7 +336,7 @@
node combiner.
</p>
<p>
- The <code>combiner</code> section allows to define nodes as list nodes.
+ The <code>combiner</code> 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
<a href="howto_combinedconfiguration.html#Node combiners">Node combiners</a>.