Author: oheger Date: Sat Jul 21 12:00:51 2007 New Revision: 558368 URL: http://svn.apache.org/viewvc?view=rev&rev=558368 Log: CONFIGURATION-285: DefaultConfigurationBuilder now fires an error event when an optional configuration source could not be loaded.
Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java?view=diff&rev=558368&r1=558367&r2=558368 ============================================================================== --- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java (original) +++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java Sat Jul 21 12:00:51 2007 @@ -30,10 +30,12 @@ import org.apache.commons.configuration.beanutils.BeanHelper; import org.apache.commons.configuration.beanutils.DefaultBeanFactory; import org.apache.commons.configuration.beanutils.XMLBeanDeclaration; +import org.apache.commons.configuration.event.ConfigurationErrorListener; import org.apache.commons.configuration.tree.ConfigurationNode; import org.apache.commons.configuration.tree.DefaultExpressionEngine; import org.apache.commons.configuration.tree.OverrideCombiner; import org.apache.commons.configuration.tree.UnionCombiner; +import org.apache.commons.logging.LogFactory; /** * <p> @@ -120,7 +122,11 @@ * <tr> * <td valign="top"><code>config-optional</code></td> * <td>Declares a configuration as optional. This means that errors that occur - * when creating the configuration are silently ignored.</td> + * when creating the configuration are ignored. (However + * <code>[EMAIL PROTECTED] ConfigurationErrorListener}</code>s registered at the builder + * instance will get notified about this error: they receive an event of type + * <code>EVENT_ERR_LOAD_OPTIONAL</code>. The key property of this event contains + * the name of the optional configuration source that caused this problem.)</td> * </tr> * </table> * </p> @@ -178,6 +184,12 @@ .getName() + "/ADDITIONAL_CONFIG"; + /** + * Constant for the type of error events caused by optional configurations + * that cannot be loaded. + */ + public static final int EVENT_ERR_LOAD_OPTIONAL = 51; + /** Constant for the name of the configuration bean factory. */ static final String CONFIG_BEAN_FACTORY_NAME = DefaultConfigurationBuilder.class .getName() @@ -329,6 +341,8 @@ super(); providers = new HashMap(); registerDefaultProviders(); + setLogger(LogFactory.getLog(getClass())); + addErrorLogListener(); // log errors per default } /** @@ -1061,6 +1075,12 @@ } else { + // Notify registered error listeners + decl.getConfigurationBuilder().fireError( + EVENT_ERR_LOAD_OPTIONAL, + decl.getConfiguration().getString(ATTR_NAME), null, + ex); + if (decl.isForceCreate()) { try Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java?view=diff&rev=558368&r1=558367&r2=558368 ============================================================================== --- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java (original) +++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java Sat Jul 21 12:00:51 2007 @@ -31,8 +31,7 @@ * Test class for DefaultConfigurationBuilder. * * @author Oliver Heger - * @version $Id: TestDefaultConfigurationBuilder.java 384601 2006-03-09 - * 20:22:58Z oheger $ + * @version $Id$ */ public class TestDefaultConfigurationBuilder extends TestCase { @@ -70,6 +69,7 @@ System.setProperty("test_file_xml", "test.xml"); System.setProperty("test_file_combine", "testcombine1.xml"); factory = new DefaultConfigurationBuilder(); + factory.clearErrorListeners(); // avoid exception messages } /** @@ -401,6 +401,16 @@ } /** + * Tests whether a default log error listener is registered at the builder + * instance. + */ + public void testLogErrorListener() + { + assertEquals("No default error listener registered", 1, + new DefaultConfigurationBuilder().getErrorListeners().size()); + } + + /** * Tests loading a definition file that contains optional configurations. */ public void testLoadOptional() throws Exception @@ -409,6 +419,20 @@ Configuration config = factory.getConfiguration(); assertTrue(config.getBoolean("test.boolean")); assertEquals("value", config.getProperty("element")); + } + + /** + * Tests whether loading a failing optional configuration causes an error + * event. + */ + public void testLoadOptionalErrorEvent() throws Exception + { + factory.clearErrorListeners(); + ConfigurationErrorListenerImpl listener = new ConfigurationErrorListenerImpl(); + factory.addErrorListener(listener); + prepareOptionalTest("configuration", false); + listener.verify(DefaultConfigurationBuilder.EVENT_ERR_LOAD_OPTIONAL, + OPTIONAL_NAME, null); } /** 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=558368&r1=558367&r2=558368 ============================================================================== --- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original) +++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Sat Jul 21 12:00:51 2007 @@ -23,6 +23,11 @@ <body> <release version="1.5-SNAPSHOT" date="in SVN" description=""> + <action dev="oheger" type="add" issue="CONFIGURATION-285"> + DefaultConfigurationBuilder will now notify registered error listeners + about optional configuration sources that could not be created. Before + exceptions thrown by optional configurations were swallowed + </action> <action dev="oheger" type="fix" issue="CONFIGURATION-283"> ConfigurationUtils.convertToHierarchical() now correctly deals with property values containing escaped list delimiters. This also affects --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]