commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r701654 - in /commons/proper/configuration/trunk: conf/ src/java/org/apache/commons/configuration/ src/test/org/apache/commons/configuration/ xdocs/
Date Sat, 04 Oct 2008 15:39:26 GMT
Author: oheger
Date: Sat Oct  4 08:39:26 2008
New Revision: 701654

URL: http://svn.apache.org/viewvc?rev=701654&view=rev
Log:
DefaultConfigurationBuilder now supports defining new configuration providers in the configuration
definition file. Thanks to Ralph Goers (ralph dot goers at dslextreme dot com) for the patch.

Added:
    commons/proper/configuration/trunk/conf/testConfigurationProvider.xml   (with props)
    commons/proper/configuration/trunk/conf/testExtendedClass.xml   (with props)
Modified:
    commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java
    commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java
    commons/proper/configuration/trunk/xdocs/changes.xml

Added: commons/proper/configuration/trunk/conf/testConfigurationProvider.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/conf/testConfigurationProvider.xml?rev=701654&view=auto
==============================================================================
--- commons/proper/configuration/trunk/conf/testConfigurationProvider.xml (added)
+++ commons/proper/configuration/trunk/conf/testConfigurationProvider.xml Sat Oct  4 08:39:26
2008
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<!-- Test configuration definition file that demonstrates complex initialization -->
+<configuration>
+  <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"
+    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/testConfigurationProvider.xml
------------------------------------------------------------------------------
    svn:eol-style = native

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

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

Added: commons/proper/configuration/trunk/conf/testExtendedClass.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/conf/testExtendedClass.xml?rev=701654&view=auto
==============================================================================
--- commons/proper/configuration/trunk/conf/testExtendedClass.xml (added)
+++ commons/proper/configuration/trunk/conf/testExtendedClass.xml Sat Oct  4 08:39:26 2008
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<!-- Test configuration definition file that demonstrates complex initialization -->
+<configuration>
+  <header>
+    <result delimiterParsingDisabled="true"
+            config-class="org.apache.commons.configuration.TestDefaultConfigurationBuilder$ExtendedCombinedConfiguration">
+      <nodeCombiner config-class="org.apache.commons.configuration.tree.OverrideCombiner"/>
+      <expressionEngine config-class="org.apache.commons.configuration.tree.xpath.XPathExpressionEngine"/>
+    </result>
+    <combiner>
+      <override>
+        <list-nodes>
+          <node>table</node>
+          <node>list</node>
+        </list-nodes>
+      </override>
+    </combiner>
+  </header>
+  <system/>
+  <properties fileName="test.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/testExtendedClass.xml
------------------------------------------------------------------------------
    svn:eol-style = native

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

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

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=701654&r1=701653&r2=701654&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
Sat Oct  4 08:39:26 2008
@@ -51,7 +51,7 @@
  * <code>beanutils</code> package (namely
  * <code>{@link org.apache.commons.configuration.beanutils.XMLBeanDeclaration XMLBeanDeclaration}</code>
  * will be used to extract the configuration's initialization parameters, which
- * allows for complex initialization szenarios).
+ * allows for complex initialization scenarios).
  * </p>
  * <p>
  * It is also possible to add custom tags to the configuration definition file.
@@ -137,7 +137,7 @@
  * the resulting configuration.
  * </p>
  * <p>
- * The configuration object returned by this builder is an instance of the
+ * The default configuration object returned by this builder is an instance of the
  * <code>{@link CombinedConfiguration}</code> class. The return value of the
  * <code>getConfiguration()</code> method can be casted to this type, and the
  * <code>getConfiguration(boolean)</code> method directly declares
@@ -146,7 +146,18 @@
  * configuration (e.g. for updates of single configuration objects). It has also
  * the advantage that the properties stored in all declared configuration
  * objects are collected and transformed into a single hierarchical structure,
- * which can be accessed using different expression engines.
+ * which can be accessed using different expression engines. The actual CombinedConfiguration
+ * implementation can be overridden by specifying the class in the <em>config-class</em>
+ * attribute of the result element.
+ * </p>
+ * <p>
+ * Additional ConfigurationProviders can be added by configuring them in the <em>header</em>
+ * section.
+ * <pre>
+ * &lt;providers&gt;
+ *   &lt;provider config-tag="tag name" config-class="provider fully qualified class
name"/&gt;
+ * &lt;/providers&gt;
+ * </pre>
  * </p>
  * <p>
  * All declared override configurations are directly added to the resulting
@@ -154,7 +165,7 @@
  * <code>config-name</code> attribute), they can directly be accessed using
  * the <code>getConfiguration(String)</code> method of
  * <code>CombinedConfiguration</code>. The additional configurations are
- * alltogether added to another combined configuration, which uses a union
+ * altogether added to another combined configuration, which uses a union
  * combiner. Then this union configuration is added to the resulting combined
  * configuration under the name defined by the <code>ADDITIONAL_NAME</code>
  * constant.
@@ -269,6 +280,17 @@
             + ".combiner.additional.list-nodes.node";
 
     /**
+     * Constant for the key for defining providers in the configuration file.
+     */
+    static final String KEY_CONFIGURATION_PROVIDERS = SEC_HEADER
+            + ".providers.provider";
+
+    /**
+     * Constant for the tag attribute for providers.
+     */
+    static final String KEY_PROVIDER_KEY = XMLBeanDeclaration.ATTR_PREFIX + "tag]";
+
+    /**
      * Constant for the key of the result declaration. This key can point to a
      * bean declaration, which defines properties of the resulting combined
      * configuration.
@@ -493,6 +515,8 @@
             load();
         }
 
+        registerConfiguredProviders();
+
         CombinedConfiguration result = createResultConfiguration();
         constructedConfiguration = result;
 
@@ -589,6 +613,24 @@
     }
 
     /**
+     * Registers providers defined in the configuration.
+     *
+     * @throws ConfigurationException if an error occurs
+     */
+    protected void registerConfiguredProviders() throws ConfigurationException
+    {
+        List nodes = configurationsAt(KEY_CONFIGURATION_PROVIDERS);
+        for (Iterator it = nodes.iterator(); it.hasNext();)
+        {
+            HierarchicalConfiguration config = (HierarchicalConfiguration) it.next();
+            XMLBeanDeclaration decl = new XMLBeanDeclaration(config);
+            String key = config.getString(KEY_PROVIDER_KEY);
+            addConfigurationProvider(key, (ConfigurationProvider) BeanHelper
+                    .createBean(decl));
+        }
+    }
+
+    /**
      * 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/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=701654&r1=701653&r2=701654&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
Sat Oct  4 08:39:26 2008
@@ -21,13 +21,13 @@
 import java.util.Collection;
 import java.util.Set;
 
+import junit.framework.TestCase;
+
 import org.apache.commons.configuration.beanutils.BeanHelper;
 import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
 import org.apache.commons.configuration.tree.DefaultConfigurationNode;
 import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
 
-import junit.framework.TestCase;
-
 /**
  * Test class for DefaultConfigurationBuilder.
  *
@@ -55,6 +55,12 @@
     private static final File INIT_FILE = new File(
             "conf/testComplexInitialization.xml");
 
+    private static final File CLASS_FILE = new File(
+            "conf/testExtendedClass.xml");
+
+    private static final File PROVIDER_FILE = new File(
+            "conf/testConfigurationProvider.xml");
+
     /** Constant for the name of an optional configuration.*/
     private static final String OPTIONAL_NAME = "optionalConfig";
 
@@ -743,4 +749,53 @@
         testSavedXML.delete();
         testSavedFactory.delete();
     }
+
+    /**
+     * Tests loading a configuration definition file that defines a custom
+     * result class.
+     */
+    public void testExtendedClass() throws ConfigurationException
+    {
+        DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
+        builder.setFile(CLASS_FILE);
+        CombinedConfiguration cc = builder.getConfiguration(true);
+        assertEquals("Extended", cc.getProperty("test"));
+        assertTrue("Wrong result class: " + cc.getClass(),
+                cc instanceof ExtendedCombinedConfiguration);
+    }
+
+    /**
+     * Tests loading a configuration definition file that defines new providers.
+     */
+    public void testConfigurationProvider() throws ConfigurationException
+    {
+        factory.setFile(PROVIDER_FILE);
+        factory.getConfiguration(true);
+        DefaultConfigurationBuilder.ConfigurationProvider provider = factory
+                .providerForTag("test");
+        assertNotNull("Provider 'test' not registered", provider);
+    }
+
+    /**
+     * A specialized combined configuration implementation used for testing
+     * custom result classes.
+     */
+    public static class ExtendedCombinedConfiguration extends
+            CombinedConfiguration
+    {
+        /**
+         * The serial version UID.
+         */
+        private static final long serialVersionUID = 4678031745085083392L;
+
+        public Object getProperty(String key)
+        {
+            if (key.equals("test"))
+            {
+                return "Extended";
+            }
+            return super.getProperty(key);
+        }
+    }
 }
+

Modified: commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/xdocs/changes.xml?rev=701654&r1=701653&r2=701654&view=diff
==============================================================================
--- commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ commons/proper/configuration/trunk/xdocs/changes.xml Sat Oct  4 08:39:26 2008
@@ -23,6 +23,10 @@
 
   <body>
     <release version="1.6" date="in SVN" description="">
+      <action dev="oheger" type="add" issue="CONFIGURATION-337" due-to="Ralph Goers">
+        DefaultConfigurationBuilder now supports defining new configuration
+        providers in the configuration definition file.
+      </action>
       <action dev="oheger" type="fix" issue="CONFIGURATION-334">
         Made handling of parent nodes more consistent when setRoot() or
         setRootNode() of HierarchicalConfiguration are involved.



Mime
View raw message