You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by oh...@apache.org on 2007/01/18 22:02:56 UTC
svn commit: r497574 - in /jakarta/commons/proper/configuration/trunk/src:
java/org/apache/commons/configuration/AbstractFileConfiguration.java
test/org/apache/commons/configuration/TestFileConfiguration.java
Author: oheger
Date: Thu Jan 18 13:02:55 2007
New Revision: 497574
URL: http://svn.apache.org/viewvc?view=rev&rev=497574
Log:
CONFIGURATION-245: AbstractFileConfiguration now sends an error event when an exception occurs during a reload operation
Modified:
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java?view=diff&rev=497574&r1=497573&r2=497574
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java Thu Jan 18 13:02:55 2007
@@ -115,6 +115,7 @@
{
initReloadingStrategy();
setLogger(LogFactory.getLog(getClass()));
+ addErrorLogListener();
}
/**
@@ -775,6 +776,16 @@
strategy.init();
}
+ /**
+ * Performs a reload operation if necessary. This method is called on each
+ * access of this configuration. It asks the associated reloading strategy
+ * whether a reload should be performed. If this is the case, the
+ * configuration is cleared and loaded again from its source. If this
+ * operation causes an exception, the registered error listeners will be
+ * notified. The error event passed to the listeners is of type
+ * <code>EVENT_RELOAD</code> and contains the exception that caused the
+ * event.
+ */
public void reload()
{
synchronized (reloadLock)
@@ -810,7 +821,7 @@
}
catch (Exception e)
{
- getLogger().warn("Error when reloading configuration", e);
+ fireError(EVENT_RELOAD, null, null, e);
// todo rollback the changes if the file can't be reloaded
}
finally
Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java?view=diff&rev=497574&r1=497573&r2=497574
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java Thu Jan 18 13:02:55 2007
@@ -26,6 +26,9 @@
import java.io.IOException;
import java.io.PrintWriter;
+import org.apache.commons.configuration.event.ConfigurationErrorEvent;
+import org.apache.commons.configuration.event.ConfigurationErrorListener;
+import org.apache.commons.configuration.reloading.FileAlwaysReloadingStrategy;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import junit.framework.TestCase;
@@ -520,6 +523,51 @@
assertNull("File name was not reset", copy.getFileName());
assertNotSame("Reloading strategy was not reset", config
.getReloadingStrategy(), copy.getReloadingStrategy());
+ }
+
+ /**
+ * Tests whether an error log listener was registered at the configuration.
+ */
+ public void testLogErrorListener()
+ {
+ PropertiesConfiguration config = new PropertiesConfiguration();
+ assertEquals("No error log listener registered", 1, config
+ .getErrorListeners().size());
+ }
+
+ /**
+ * Tests handling of errors in the reload() method.
+ */
+ public void testReloadError() throws ConfigurationException
+ {
+ class TestConfigurationErrorListener implements
+ ConfigurationErrorListener
+ {
+ ConfigurationErrorEvent event;
+
+ int errorCount;
+
+ public void configurationError(ConfigurationErrorEvent event)
+ {
+ this.event = event;
+ errorCount++;
+ }
+ };
+ TestConfigurationErrorListener l = new TestConfigurationErrorListener();
+ PropertiesConfiguration config = new PropertiesConfiguration(
+ RESOURCE_NAME);
+ config.clearErrorListeners();
+ config.addErrorListener(l);
+ config.setReloadingStrategy(new FileAlwaysReloadingStrategy());
+ config.getString("test");
+ config.setFileName("Not existing file");
+ config.getString("test");
+ assertEquals("Wrong number of error events", 1, l.errorCount);
+ assertEquals("Wrong error type",
+ AbstractFileConfiguration.EVENT_RELOAD, l.event.getType());
+ assertNull("Wrong property name", l.event.getPropertyName());
+ assertNull("Wrong property value", l.event.getPropertyValue());
+ assertNotNull("Exception is not set", l.event.getCause());
}
/**
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org