Author: oheger
Date: Sat Oct 4 12:23:17 2008
New Revision: 701696
URL: http://svn.apache.org/viewvc?rev=701696&view=rev
Log:
[CONFIGURATION-377] 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. Ported patch to
configuration2 branch.
Added:
commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testConfigurationProvider.xml
(with props)
commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testExtendedClass.xml
(with props)
Modified:
commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/DefaultConfigurationBuilder.java
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestDefaultConfigurationBuilder.java
commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml
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=701696&r1=701695&r2=701696&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
Sat Oct 4 12:23:17 2008
@@ -142,7 +142,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>[EMAIL PROTECTED] 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
@@ -154,6 +154,15 @@
* which can be accessed using different expression engines.
* </p>
* <p>
+ * Additional ConfigurationProviders can be added by configuring them in the
<em>header</em>
+ * section.
+ * <pre>
+ * <providers>
+ * <provider config-tag="tag name" config-class="provider fully qualified
class name"/>
+ * </providers>
+ * </pre>
+ * </p>
+ * <p>
* All declared override configurations are directly added to the resulting
* combined configuration. If they are given names (using the
* <code>config-name</code> attribute), they can directly be accessed using
@@ -274,6 +283,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.
@@ -412,9 +432,9 @@
/**
* Sets the base path for the configuration sources to load. Normally a
base
* path need not to be set because it is determined by the location of the
- * configuration definition file to load. All relative pathes in this file
+ * configuration definition file to load. All relative paths in this file
* are resolved relative to this file. Setting a base path makes sense if
- * such relative pathes should be otherwise resolved, e.g. if the
+ * such relative paths should be otherwise resolved, e.g. if the
* configuration file is loaded from the class path and all sub
* configurations it refers to are stored in a special config directory.
*
@@ -505,6 +525,8 @@
load();
}
+ registerConfiguredProviders();
+
CombinedConfiguration result = createResultConfiguration();
constructedConfiguration = result;
@@ -599,6 +621,23 @@
}
/**
+ * Registers providers defined in the configuration.
+ *
+ * @throws ConfigurationException if an error occurs
+ */
+ protected void registerConfiguredProviders() throws ConfigurationException
+ {
+ List<SubConfiguration<ConfigurationNode>> nodes =
configurationsAt(KEY_CONFIGURATION_PROVIDERS);
+ for (SubConfiguration<ConfigurationNode> config : nodes)
+ {
+ XMLBeanDeclaration<ConfigurationNode> decl = new
XMLBeanDeclaration<ConfigurationNode>(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/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=701696&r1=701695&r2=701696&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
Sat Oct 4 12:23:17 2008
@@ -56,6 +56,12 @@
private static final File INIT_FILE = ConfigurationAssert
.getTestFile("testComplexInitialization.xml");
+ private static final File CLASS_FILE = ConfigurationAssert
+ .getTestFile("testExtendedClass.xml");
+
+ private static final File PROVIDER_FILE = ConfigurationAssert
+ .getTestFile("testConfigurationProvider.xml");
+
/** Constant for the name of an optional configuration.*/
private static final String OPTIONAL_NAME = "optionalConfig";
@@ -757,4 +763,52 @@
testSavedXML.delete();
testSavedFactory.delete();
}
+
+ /**
+ * Tests loading a configuration definition file that defines a custom
+ * result class.
+ */
+ public void testExtendedClass() throws ConfigurationException
+ {
+ factory.setFile(CLASS_FILE);
+ CombinedConfiguration cc = factory.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;
+
+ @Override
+ public Object getProperty(String key)
+ {
+ if (key.equals("test"))
+ {
+ return "Extended";
+ }
+ return super.getProperty(key);
+ }
+ }
}
Added:
commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testConfigurationProvider.xml
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testConfigurationProvider.xml?rev=701696&view=auto
==============================================================================
---
commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testConfigurationProvider.xml
(added)
+++
commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testConfigurationProvider.xml
Sat Oct 4 12:23:17 2008
@@ -0,0 +1,36 @@
+<?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.configuration2.tree.OverrideCombiner"/>
+ </result>
+ <providers>
+ <provider config-tag="test"
+
config-class="org.apache.commons.configuration2.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.configuration2.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.configuration2.expr.def.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/branches/configuration2_experimental/src/test/resources/testConfigurationProvider.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testConfigurationProvider.xml
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange:
commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testConfigurationProvider.xml
------------------------------------------------------------------------------
svn:mime-type = text/xml
Added:
commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testExtendedClass.xml
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testExtendedClass.xml?rev=701696&view=auto
==============================================================================
---
commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testExtendedClass.xml
(added)
+++
commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testExtendedClass.xml
Sat Oct 4 12:23:17 2008
@@ -0,0 +1,33 @@
+<?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.configuration2.TestDefaultConfigurationBuilder$ExtendedCombinedConfiguration">
+ <nodeCombiner
config-class="org.apache.commons.configuration2.tree.OverrideCombiner"/>
+ </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.configuration2.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.configuration2.expr.def.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/branches/configuration2_experimental/src/test/resources/testExtendedClass.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testExtendedClass.xml
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange:
commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testExtendedClass.xml
------------------------------------------------------------------------------
svn:mime-type = text/xml
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=701696&r1=701695&r2=701696&view=diff
==============================================================================
---
commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml
(original)
+++
commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml
Sat Oct 4 12:23:17 2008
@@ -85,6 +85,14 @@
</release>
<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.
+ </action>
<action dev="oheger" type="fix" issue="CONFIGURATION-332">
Properties written through a DataConfiguration to a wrapped
PropertiesConfiguration got lost when the PropertiesConfiguration was