commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r437179 - in /jakarta/commons/proper/configuration/trunk: ./ conf/ src/java/org/apache/commons/configuration/ src/test/org/apache/commons/configuration/ xdocs/
Date Sat, 26 Aug 2006 16:38:44 GMT
Author: oheger
Date: Sat Aug 26 09:38:38 2006
New Revision: 437179

URL: http://svn.apache.org/viewvc?rev=437179&view=rev
Log:
Added support for system properties to ConfigurationFactory (CONFIGURATION-221); thanks to
Rainer Jung for the patch

Added:
    jakarta/commons/proper/configuration/trunk/conf/testDigesterConfigurationSysProps.xml
  (with props)
Modified:
    jakarta/commons/proper/configuration/trunk/RELEASE-NOTES.txt
    jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationFactory.java
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationFactory.java
    jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
    jakarta/commons/proper/configuration/trunk/xdocs/howto_configurationfactory.xml
    jakarta/commons/proper/configuration/trunk/xdocs/user_guide.xml

Modified: jakarta/commons/proper/configuration/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/RELEASE-NOTES.txt?rev=437179&r1=437178&r2=437179&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/RELEASE-NOTES.txt (original)
+++ jakarta/commons/proper/configuration/trunk/RELEASE-NOTES.txt Sat Aug 26 09:38:38 2006
@@ -25,6 +25,10 @@
 CHANGES
 =======
 
+- ConfigurationFactory now supports variables in its configuration
+  definition files. These variables are resolved using system properties
+  and have the typical ${} syntax. Fixes CONFIGURATION-221.
+  Thanks to Rainer Jung.
 - DataConfiguration.getDateArray() used to ignore the format argument.
   This was fixed. Fixes CONFIGURATION-220.
 - The dependency to servletapi was updated from version 2.3 to version 2.4, but
@@ -42,7 +46,7 @@
   Fixes CONFIGURATION-145.
 - If a configuration file was to be loaded from classpath, the constructor of
   AbstractFileConfiguration dropped the file's path. The path is now taken into
-  account. Fixes CONFIGURATION-216.
+  account. Fixes CONFIGURATION-216. Thanks to Gabriele Garuglieri.
 - The getter methods for numeric data types in AbstractConfiguration now support
   conversions between different Number types, e.g. you can now call getLong(key)
   when key points to an Integer value. Fixes CONFIGURATION-214.

Added: jakarta/commons/proper/configuration/trunk/conf/testDigesterConfigurationSysProps.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/conf/testDigesterConfigurationSysProps.xml?rev=437179&view=auto
==============================================================================
--- jakarta/commons/proper/configuration/trunk/conf/testDigesterConfigurationSysProps.xml
(added)
+++ jakarta/commons/proper/configuration/trunk/conf/testDigesterConfigurationSysProps.xml
Sat Aug 26 09:38:38 2006
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<!-- A test configuration file for loading a file specified by a
+     system property.
+-->
+<configuration>
+  <properties fileName="${config.file}"/>
+</configuration>

Propchange: jakarta/commons/proper/configuration/trunk/conf/testDigesterConfigurationSysProps.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/configuration/trunk/conf/testDigesterConfigurationSysProps.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/commons/proper/configuration/trunk/conf/testDigesterConfigurationSysProps.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationFactory.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationFactory.java?rev=437179&r1=437178&r2=437179&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationFactory.java
(original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationFactory.java
Sat Aug 26 09:38:38 2006
@@ -23,12 +23,16 @@
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.LinkedList;
+import java.util.Map;
 
 import org.apache.commons.configuration.plist.PropertyListConfiguration;
 import org.apache.commons.configuration.plist.XMLPropertyListConfiguration;
 import org.apache.commons.digester.AbstractObjectCreationFactory;
 import org.apache.commons.digester.Digester;
 import org.apache.commons.digester.ObjectCreationFactory;
+import org.apache.commons.digester.Substitutor;
+import org.apache.commons.digester.substitution.MultiVariableExpander;
+import org.apache.commons.digester.substitution.VariableSubstitutor;
 import org.apache.commons.digester.xmlrules.DigesterLoader;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.logging.Log;
@@ -156,6 +160,8 @@
 
         // Configure digester to always enable the context class loader
         digester.setUseContextClassLoader(true);
+        // Add a substitutor to resolve system properties
+        enableDigesterSubstitutor(digester);
         // Put the composite builder object below all of the other objects.
         digester.push(builder);
         // Parse the input stream to configure our mappings
@@ -242,12 +248,27 @@
     }
 
     /**
+     * Adds a substitutor to interpolate system properties
+     *
+     * @param digester The digester to which we add the substitutor
+     */
+    protected void enableDigesterSubstitutor(Digester digester)
+    {
+        Map systemProperties = System.getProperties();
+        MultiVariableExpander expander = new MultiVariableExpander();
+        expander.addSource("$", systemProperties);
+
+        // allow expansion in both xml attributes and element text
+        Substitutor substitutor = new VariableSubstitutor(expander);
+        digester.setSubstitutor(substitutor);
+    }
+
+    /**
      * Initializes the parsing rules for the default digester
      *
-     * This allows the Configuration Factory to understand the
-     * default types: Properties, XML and JNDI. Two special sections are
-     * introduced: <code>&lt;override&gt;</code> and
-     * <code>&lt;additional&gt;</code>.
+     * This allows the Configuration Factory to understand the default types:
+     * Properties, XML and JNDI. Two special sections are introduced:
+     * <code>&lt;override&gt;</code> and <code>&lt;additional&gt;</code>.
      *
      * @param digester The digester to configure
      */

Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationFactory.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationFactory.java?rev=437179&r1=437178&r2=437179&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationFactory.java
(original)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationFactory.java
Sat Aug 26 09:38:38 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 2001-2005 The Apache Software Foundation.
+ * Copyright 2001-2006 The Apache Software Foundation.
  *
  * Licensed under the Apache License, Version 2.0 (the "License")
  * you may not use this file except in compliance with the License.
@@ -49,11 +49,13 @@
             new File("conf/testDigesterOptionalConfiguration.xml");
     private File testDigesterFileOptionalEx =
             new File("conf/testDigesterOptionalConfigurationEx.xml");
+    private File testDigesterFileSysProps =
+            new File("conf/testDigesterConfigurationSysProps.xml");
 
     private File testDigesterBadXML = new File("conf/testDigesterBadXML.xml");
 
     private String testBasePath = new File("conf").getAbsolutePath();
-    
+
     private File testProperties = new File("conf/test.properties");
     private File testAbsConfig = new File("target/testAbsConfig.xml");
 
@@ -165,7 +167,7 @@
     {
         factory.setConfigurationURL(testDigesterFileEnhanced.toURL());
         checkUnionConfig();
-        
+
         factory = new ConfigurationFactory();
         File nonExistingFile = new File("conf/nonexisting.xml");
         factory.setConfigurationURL(nonExistingFile.toURL());
@@ -224,7 +226,7 @@
         assertNotNull(config.getProperty("java.version"));
         assertEquals(System.getProperty("java.version"), config.getString("java.version"));
     }
-    
+
     // Checks if optional configurations work
     public void testOptionalConfigurations() throws Exception
     {
@@ -232,7 +234,7 @@
         Configuration config = factory.getConfiguration();
         assertTrue(config.getBoolean("test.boolean"));
         assertEquals("value", config.getProperty("element"));
-        
+
         factory.setConfigurationURL(testDigesterFileOptionalEx.toURL());
         try
         {
@@ -244,7 +246,7 @@
             // fine
         }
     }
-    
+
     // Checks if a file with an absolute path can be loaded
     public void testLoadAbsolutePath() throws Exception
     {
@@ -281,7 +283,7 @@
             }
         }
     }
-    
+
     public void testBasePath() throws Exception
     {
         assertEquals(".", factory.getBasePath());
@@ -297,16 +299,28 @@
         factory.setConfigurationFileName(testDigesterFile.getAbsolutePath());
         // if a base path was set, the file name does not play a role
         assertEquals(homeDir, factory.getBasePath());
-        
+
         factory = new ConfigurationFactory(testDigesterFile.getAbsolutePath());
         assertEquals(testDigesterFile.getParentFile().getAbsolutePath(),
                 factory.getBasePath());
         factory.setBasePath(homeDir);
         assertEquals(homeDir, factory.getBasePath());
-        
+
         factory = new ConfigurationFactory();
         factory.setConfigurationURL(testDigesterFile.toURL());
         assertEquals(testDigesterFile.toURL().toString(), factory.getBasePath());
+    }
+
+    // Tests if system properties can be resolved in the configuration
+    // definition
+    public void testLoadingWithSystemProperties() throws ConfigurationException
+    {
+        System.setProperty("config.file", "test.properties");
+        factory.setConfigurationFileName(testDigesterFileSysProps
+                .getAbsolutePath());
+        Configuration config = factory.getConfiguration();
+        assertTrue("Configuration not loaded", config
+                .getBoolean("configuration.loaded"));
     }
 
     private void checkUnionConfig() throws Exception

Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?rev=437179&r1=437178&r2=437179&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Sat Aug 26 09:38:38 2006
@@ -23,6 +23,11 @@
   <body>
 
     <release version="1.3-rc2" date="in SVN">
+      <action dev="oheger" type="add" issue="CONFIGURATION-221" due-to="Rainer Jung">
+        ConfigurationFactory now supports variables in its configuration
+        definition files. These variables are resolved using system properties
+        and have the typical ${} syntax.
+      </action>
       <action dev="oheger" type="update" issue="CONFIGURATION-216" due-to="Gabriele Garuglieri">
         There were still some problems with resolving relative paths when
         configuration files are loaded from classpath. This fix addresses these

Modified: jakarta/commons/proper/configuration/trunk/xdocs/howto_configurationfactory.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/xdocs/howto_configurationfactory.xml?rev=437179&r1=437178&r2=437179&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/howto_configurationfactory.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/howto_configurationfactory.xml Sat Aug
26 09:38:38 2006
@@ -519,6 +519,41 @@
           address by attributes.
         </p>
     </subsection>
+    
+    <subsection name="Referencing system properties">
+        <p>
+          Often the configuration definition files are contained in a 
+          distribution unit like a war file that should not be manipulated after
+          packaging. The path names these files refer to need some way of making
+          them externally customizable so that the packaging unit can be adoped
+          to a special environment without repackaging. One way to achieve this
+          is to allow system properties in the file names.
+        </p>
+        <p>
+          <code>ConfigurationFactory</code> supports a simple way of adding
+          references to system properties to the configuration definition
+          files. Below is an example that demonstrates this feature:
+        </p>
+		<source><![CDATA[
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<!-- A test configuration file for loading a file specified by a
+     system property.
+-->
+<configuration>
+  <properties fileName="${config.file}"/>
+</configuration>]]></source>
+        <p>
+          In this configuration definition file the name of the properties
+          file to be loaded is specified by means of a variable (which is
+          marked using the typical ${} syntax). When this file is parsed the
+          name of the variable is looked up in the set of defined system
+          properties. So to set a concrete file name for the properties file
+          the <code>config.file</code> system property must be defined (e.g.
+          by calling <code>System.setProperty("config.file", "myfile.properties");</code>
+          or using the <code>-D</code> command line switch when starting your
+          Java application).
+        </p>
+    </subsection>
     </section>
 </body>
 

Modified: jakarta/commons/proper/configuration/trunk/xdocs/user_guide.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/xdocs/user_guide.xml?rev=437179&r1=437178&r2=437179&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/user_guide.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/user_guide.xml Sat Aug 26 09:38:38 2006
@@ -114,6 +114,7 @@
       <li><a href="howto_configurationfactory.html#Configuration definition file
reference">Configuration definition file reference</a></li>
       <ul>
         <li><a href="howto_configurationfactory.html#Setting further options">Setting
further options</a></li>
+        <li><a href="howto_configurationfactory.html#Referencing_system_properties">Referencing
system properties</a></li>
       </ul>
       <li><a href="howto_configurationbuilder.html#Using DefaultConfigurationBuilder">Using
DefaultConfigurationBuilder</a></li>
       <ul>



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message