Return-Path: Delivered-To: apmail-commons-commits-archive@locus.apache.org Received: (qmail 41426 invoked from network); 18 Dec 2008 22:24:01 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 18 Dec 2008 22:24:01 -0000 Received: (qmail 83932 invoked by uid 500); 18 Dec 2008 22:24:13 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 83850 invoked by uid 500); 18 Dec 2008 22:24:12 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 83841 invoked by uid 99); 18 Dec 2008 22:24:12 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 18 Dec 2008 14:24:12 -0800 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 18 Dec 2008 22:23:54 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 30C9223889A0; Thu, 18 Dec 2008 14:23:34 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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/ Date: Thu, 18 Dec 2008 22:23:33 -0000 To: commits@commons.apache.org From: rgoers@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20081218222334.30C9223889A0@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 @@ Ralph Goers rgoers rgoers@apache.org + Intuit -8 Java Developer 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 @@ *

* *

- * <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 @@ + + + +

+ + + + +
+ + + + + \ 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 @@ + + Allow system properties to be set from a configuration file. + 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 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:
  • The format for configuration definition files is not extensible.
  • Complex initializations of the declared configuration sources (e.g. @@ -48,13 +48,13 @@ DefaultConfigurationBuilder was introduced.

    - +

    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:

      @@ -79,15 +79,15 @@

      - +

      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 @@ - +

      @@ -139,13 +139,13 @@

      configuration
      -
      The configuration tag allows to include other - configuration definition files. This makes it possible to nest these +
      The configuration 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 DefaultConfigurationBuilder object, initialize it, and obtain the CombinedConfiguation 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 fileName 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.
      @@ -186,7 +186,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 - 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). + 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). config-at @@ -211,13 +211,13 @@ config-optional 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 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. @@ -280,6 +280,19 @@ will occur because the ${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.