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:23:33 UTC

svn commit: r727843 - in /commons/proper/configuration/branches/configuration2_experimental: ./ src/main/java/org/apache/commons/configuration2/ src/test/java/org/apache/commons/configuration2/ src/test/resources/ xdocs/ xdocs/userguide/

Author: rgoers
Date: Thu Dec 18 14:23:33 2008
New Revision: 727843

URL: http://svn.apache.org/viewvc?rev=727843&view=rev
Log:
Allow system properties to be set from a configuration. Fix grammar errors in defaultconfigurationbuilder documentation

Added:
    commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testSystemProperties.xml
Modified:
    commons/proper/configuration/branches/configuration2_experimental/pom.xml
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/DefaultConfigurationBuilder.java
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/SystemConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestDefaultConfigurationBuilder.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestSystemConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml
    commons/proper/configuration/branches/configuration2_experimental/xdocs/userguide/howto_configurationbuilder.xml

Modified: commons/proper/configuration/branches/configuration2_experimental/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/pom.xml?rev=727843&r1=727842&r2=727843&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/pom.xml (original)
+++ commons/proper/configuration/branches/configuration2_experimental/pom.xml Thu Dec 18 14:23:33 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/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/DefaultConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/DefaultConfigurationBuilder.java?rev=727843&r1=727842&r2=727843&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/DefaultConfigurationBuilder.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/DefaultConfigurationBuilder.java Thu Dec 18 14:23:33 2008
@@ -74,7 +74,7 @@
  * <p>
  *
  * <pre>
- * &lt;configuration&gt;
+ * &lt;configuration systemProperties="properties file name"&gt;
  *   &lt;header&gt;
  *     &lt;!-- Optional meta information about the composite configuration --&gt;
  *   &lt;/header&gt;
@@ -90,7 +90,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
@@ -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 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<!-- 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.configuration2.tree.OverrideCombiner"/>
+      <expressionEngine config-class="org.apache.commons.configuration2.tree.xpath.XPathExpressionEngine"/>
+    </result>
+  </header>
+  <system/>
+  <properties fileName="test2.properties" throwExceptionOnMissing="true"
+    config-name="properties">
+    <reloadingStrategy config-class="org.apache.commons.configuration2.reloading.FileChangedReloadingStrategy"
+      refreshDelay="10000"/>
+  </properties>
+</configuration>
\ No newline at end of file

Modified: commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml?rev=727843&r1=727842&r2=727843&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml (original)
+++ commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml Thu Dec 18 14:23:33 2008
@@ -85,6 +85,9 @@
     </release>
 
     <release version="1.6" date="in SVN" description="">
+      <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/branches/configuration2_experimental/xdocs/userguide/howto_configurationbuilder.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/xdocs/userguide/howto_configurationbuilder.xml?rev=727843&r1=727842&r2=727843&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/xdocs/userguide/howto_configurationbuilder.xml (original)
+++ commons/proper/configuration/branches/configuration2_experimental/xdocs/userguide/howto_configurationbuilder.xml Thu Dec 18 14:23:33 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>.