Author: oheger
Date: Thu Nov 2 13:22:49 2006
New Revision: 470528
URL: http://svn.apache.org/viewvc?view=rev&rev=470528
Log:
Changed the creation of file-based configurations in ConfigurationFactory so
that the load() method is called only after all specified properties have been
initialized; fix for CONFIGURATION-229
Added:
jakarta/commons/proper/configuration/trunk/conf/testDigesterConfigurationWithProps.xml
(with props)
Modified:
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
Added:
jakarta/commons/proper/configuration/trunk/conf/testDigesterConfigurationWithProps.xml
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/conf/testDigesterConfigurationWithProps.xml?view=auto&rev=470528
==============================================================================
---
jakarta/commons/proper/configuration/trunk/conf/testDigesterConfigurationWithProps.xml
(added)
+++
jakarta/commons/proper/configuration/trunk/conf/testDigesterConfigurationWithProps.xml
Thu Nov 2 13:22:49 2006
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+
+<configuration>
+ <properties fileName="test.properties" listDelimiter=";"/>
+</configuration>
Propchange:
jakarta/commons/proper/configuration/trunk/conf/testDigesterConfigurationWithProps.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
jakarta/commons/proper/configuration/trunk/conf/testDigesterConfigurationWithProps.xml
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange:
jakarta/commons/proper/configuration/trunk/conf/testDigesterConfigurationWithProps.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?view=diff&rev=470528&r1=470527&r2=470528
==============================================================================
---
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
Thu Nov 2 13:22:49 2006
@@ -29,6 +29,7 @@
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.CallMethodRule;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.ObjectCreationFactory;
import org.apache.commons.digester.Substitutor;
@@ -70,6 +71,9 @@
/** Constant for the fileName attribute.*/
private static final String ATTR_FILENAME = "fileName";
+ /** Constant for the load method.*/
+ private static final String METH_LOAD = "load";
+
/** Constant for the default base path (points to actual directory).*/
private static final String DEF_BASE_PATH = ".";
@@ -295,28 +299,28 @@
digester,
matchString + "properties",
new PropertiesConfigurationFactory(),
- null,
+ METH_LOAD,
additional);
setupDigesterInstance(
digester,
matchString + "plist",
new PropertyListConfigurationFactory(),
- null,
+ METH_LOAD,
additional);
setupDigesterInstance(
digester,
matchString + "xml",
new FileConfigurationFactory(XMLConfiguration.class),
- null,
+ METH_LOAD,
additional);
setupDigesterInstance(
digester,
matchString + "hierarchicalXml",
new FileConfigurationFactory(XMLConfiguration.class),
- null,
+ METH_LOAD,
additional);
setupDigesterInstance(
@@ -362,7 +366,7 @@
if (method != null)
{
- digester.addCallMethod(matchString, method);
+ digester.addRule(matchString, new CallOptionalMethodRule(method));
}
digester.addSetNext(matchString, "addConfiguration",
Configuration.class.getName());
@@ -516,24 +520,6 @@
{
FileConfiguration conf = createConfiguration(attributes);
conf.setBasePath(getBasePath());
- conf.setFileName(attributes.getValue(ATTR_FILENAME));
- try
- {
- log.info("Trying to load configuration " + conf.getFileName());
- conf.load();
- }
- catch (ConfigurationException cex)
- {
- if (attributes.getValue(ATTR_OPTIONAL) != null
- &&
PropertyConverter.toBoolean(attributes.getValue(ATTR_OPTIONAL)).booleanValue())
- {
- log.warn("Could not load optional configuration " +
conf.getFileName());
- }
- else
- {
- throw cex;
- }
- }
return conf;
}
@@ -825,6 +811,72 @@
HierarchicalConfiguration hc = new HierarchicalConfiguration();
ConfigurationUtils.copy(cdata.getConfiguration(), hc);
return hc.getRoot();
+ }
+ }
+ }
+
+ /**
+ * A special implementation of Digester's <code>CallMethodRule</code> that
+ * is internally used for calling a file configuration's
<code>load()</code>
+ * method. This class difers from its ancestor that it catches all occuring
+ * exceptions when the specified method is called. It then checks whether
+ * for the corresponding configuration the optional attribute is set. If
+ * this is the case, the exception will simply be ignored.
+ *
+ * @since 1.4
+ */
+ private static class CallOptionalMethodRule extends CallMethodRule
+ {
+ /** A flag whether the optional attribute is set for this node. */
+ private boolean optional;
+
+ /**
+ * Creates a new instance of <code>CallOptionalMethodRule</code> and
+ * sets the name of the method to invoke.
+ *
+ * @param methodName the name of the method
+ */
+ public CallOptionalMethodRule(String methodName)
+ {
+ super(methodName);
+ }
+
+ /**
+ * Checks if the optional attribute is set.
+ *
+ * @param attrs the attributes
+ * @throws Exception if an error occurs
+ */
+ public void begin(Attributes attrs) throws Exception
+ {
+ optional = attrs.getValue(ATTR_OPTIONAL) != null
+ && PropertyConverter.toBoolean(
+ attrs.getValue(ATTR_OPTIONAL)).booleanValue();
+ super.begin(attrs);
+ }
+
+ /**
+ * Calls the method. If the optional attribute was set, occurring
+ * exceptions will be ignored.
+ *
+ * @throws Exception if an error occurs
+ */
+ public void end() throws Exception
+ {
+ try
+ {
+ super.end();
+ }
+ catch (Exception ex)
+ {
+ if (optional)
+ {
+ log.warn("Could not create optional configuration!", ex);
+ }
+ else
+ {
+ throw ex;
+ }
}
}
}
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?view=diff&rev=470528&r1=470527&r2=470528
==============================================================================
---
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
Thu Nov 2 13:22:49 2006
@@ -20,6 +20,7 @@
import java.io.File;
import java.io.FileWriter;
import java.util.Collection;
+import java.util.List;
import junit.framework.TestCase;
@@ -52,6 +53,8 @@
new File("conf/testDigesterOptionalConfigurationEx.xml");
private File testDigesterFileSysProps =
new File("conf/testDigesterConfigurationSysProps.xml");
+ private File testDigesterFileInitProps =
+ new File("conf/testDigesterConfigurationWithProps.xml");
private File testDigesterBadXML = new File("conf/testDigesterBadXML.xml");
@@ -322,6 +325,21 @@
Configuration config = factory.getConfiguration();
assertTrue("Configuration not loaded", config
.getBoolean("configuration.loaded"));
+ }
+
+ // Tests if the properties of a configuration object are correctly set
+ // before it is loaded.
+ public void testLoadInitProperties() throws ConfigurationException
+ {
+ factory.setConfigurationFileName(testDigesterFileInitProps
+ .getAbsolutePath());
+ Configuration config = factory.getConfiguration();
+ PropertiesConfiguration c = (PropertiesConfiguration)
((CompositeConfiguration) config)
+ .getConfiguration(0);
+ assertEquals("List delimiter was not set", ';', c.getListDelimiter());
+ List l = c.getList("test.mixed.array");
+ assertEquals("Wrong number of list elements", 2, l.size());
+ assertEquals("List delimiter was not applied", "b, c, d", l.get(1));
}
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?view=diff&rev=470528&r1=470527&r2=470528
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Thu Nov 2
13:22:49 2006
@@ -23,6 +23,12 @@
<body>
<release version="1.4-dev" date="in SVN">
+ <action dev="oheger" type="update" issue="CONFIGURATION-229">
+ For file-based configurations loaded by ConfigurationFactory the load()
+ method was called before all of the properties specified by attributes
+ of the XML element have been initialized. Now load() is called after
+ property initialization.
+ </action>
<action dev="oheger" type="update" issue="CONFIGURATION-235">
Interpolation of non string values did not work when
SubsetConfiguration
was involved. This has now been fixed.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]