Author: oheger Date: Tue Mar 26 15:11:47 2013 New Revision: 1461176 URL: http://svn.apache.org/r1461176 Log: ReloadingFileBasedConfigurationBuilder now evaluates the reloadingDetectorFactory property in its parameters.
This makes it possible to specify a custom ReloadingDetectoryFactory, so that different reloading strategies can be used. Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ReloadingFileBasedConfigurationBuilder.java commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestReloadingFileBasedConfigurationBuilder.java Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ReloadingFileBasedConfigurationBuilder.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ReloadingFileBasedConfigurationBuilder.java?rev=1461176&r1=1461175&r2=1461176&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ReloadingFileBasedConfigurationBuilder.java (original) +++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ReloadingFileBasedConfigurationBuilder.java Tue Mar 26 15:11:47 2013 @@ -65,6 +65,10 @@ import org.apache.commons.configuration. public class ReloadingFileBasedConfigurationBuilder<T extends FileBasedConfiguration> extends FileBasedConfigurationBuilder<T> implements ReloadingControllerSupport { + /** The default factory for creating reloading detector objects. */ + private static final ReloadingDetectorFactory DEFAULT_DETECTOR_FACTORY = + new DefaultReloadingDetectorFactory(); + /** The reloading controller associated with this object. */ private final ReloadingController reloadingController; @@ -139,21 +143,23 @@ public class ReloadingFileBasedConfigura /** * Creates a {@code ReloadingDetector} which monitors the passed in * {@code FileHandler}. This method is called each time a new result object - * is created with the current {@code FileHandler}. The - * {@code ReloadingDetector} associated with this builder's - * {@link ReloadingController} delegates to this object. This implementation - * returns a new {@code FileHandlerReloadingDetector} object. Note: This - * method is called from a synchronized block. + * is created with the current {@code FileHandler}. This implementation + * checks whether a {@code ReloadingDetectorFactory} is specified in the + * current parameters. If this is the case, it is invoked. Otherwise, a + * default factory is used to create a {@code FileHandlerReloadingDetector} + * object. Note: This method is called from a synchronized block. * * @param handler the current {@code FileHandler} * @param fbparams the object with parameters related to file-based builders * @return a {@code ReloadingDetector} for this {@code FileHandler} + * @throws ConfigurationException if an error occurs */ protected ReloadingDetector createReloadingDetector(FileHandler handler, FileBasedBuilderParametersImpl fbparams) + throws ConfigurationException { - return new FileHandlerReloadingDetector(handler, - fbparams.getReloadingRefreshDelay()); + return fetchDetectorFactory(fbparams).createReloadingDetector(handler, + fbparams); } /** @@ -243,4 +249,18 @@ public class ReloadingFileBasedConfigura } }; } + + /** + * Returns a {@code ReloadingDetectorFactory} either from the passed in + * parameters or a default factory. + * + * @param params the current parameters object + * @return the {@code ReloadingDetectorFactory} to be used + */ + private static ReloadingDetectorFactory fetchDetectorFactory( + FileBasedBuilderParametersImpl params) + { + ReloadingDetectorFactory factory = params.getReloadingDetectorFactory(); + return (factory != null) ? factory : DEFAULT_DETECTOR_FACTORY; + } } Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestReloadingFileBasedConfigurationBuilder.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestReloadingFileBasedConfigurationBuilder.java?rev=1461176&r1=1461175&r2=1461176&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestReloadingFileBasedConfigurationBuilder.java (original) +++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestReloadingFileBasedConfigurationBuilder.java Tue Mar 26 15:11:47 2013 @@ -58,10 +58,11 @@ public class TestReloadingFileBasedConfi } /** - * Tests whether a correct reloading detector is created. + * Tests whether a correct reloading detector is created if no custom factory + * was set. */ @Test - public void testCreateReloadingDetector() + public void testCreateReloadingDetectorDefaultFactory() throws ConfigurationException { ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration> builder = new ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration>( @@ -79,6 +80,32 @@ public class TestReloadingFileBasedConfi } /** + * Tests whether a custom reloading detector factory can be installed. + */ + @Test + public void testCreateReloadingDetectoryCustomFactory() + throws ConfigurationException + { + ReloadingDetector detector = + EasyMock.createMock(ReloadingDetector.class); + ReloadingDetectorFactory factory = + EasyMock.createMock(ReloadingDetectorFactory.class); + FileHandler handler = new FileHandler(); + FileBasedBuilderParametersImpl params = + new FileBasedBuilderParametersImpl(); + EasyMock.expect(factory.createReloadingDetector(handler, params)) + .andReturn(detector); + EasyMock.replay(detector, factory); + params.setReloadingDetectorFactory(factory); + ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration> builder = + new ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration>( + PropertiesConfiguration.class); + assertSame("Wrong detector", detector, + builder.createReloadingDetector(handler, params)); + EasyMock.verify(factory); + } + + /** * Tests the isReloadingRequired() implementation of the detector associated * with the reloading controller. */