Author: oheger Date: Fri Dec 8 10:30:15 2006 New Revision: 484692 URL: http://svn.apache.org/viewvc?view=rev&rev=484692 Log: Added new forceReloadCheck property to CombinedConfiguration; fix for CONFIGURATION-240
Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CombinedConfiguration.java jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCombinedConfiguration.java jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CombinedConfiguration.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CombinedConfiguration.java?view=diff&rev=484692&r1=484691&r2=484692 ============================================================================== --- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CombinedConfiguration.java (original) +++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CombinedConfiguration.java Fri Dec 8 10:30:15 2006 @@ -118,6 +118,9 @@ /** Constant for the default node combiner. */ private static final NodeCombiner DEFAULT_COMBINER = new UnionCombiner(); + /** Constant for the name of the property used for the reload check.*/ + private static final String PROP_RELOAD_CHECK = "CombinedConfigurationReloadCheck"; + /** Stores the combiner. */ private NodeCombiner nodeCombiner; @@ -130,6 +133,9 @@ /** Stores a map with the named configurations. */ private Map namedConfigurations; + /** A flag whether an enhanced reload check is to be performed.*/ + private boolean forceReloadCheck; + /** * Creates a new instance of <code>CombinedConfiguration</code> and * initializes the combiner to be used. @@ -186,6 +192,34 @@ } /** + * Returns a flag whether an enhanced reload check must be performed. + * + * @return the force reload check flag + * @since 1.4 + */ + public boolean isForceReloadCheck() + { + return forceReloadCheck; + } + + /** + * Sets the force reload check flag. If this flag is set, each property + * access on this configuration will cause a reload check on the contained + * configurations. This is a workaround for a problem with some reload + * implementations that only check if a reload is required when they are + * triggered. Per default this mode is disabled. If the force reload check + * flag is set to <b>true</b>, accessing properties will be less + * performant, but reloads on contained configurations will be detected. + * + * @param forceReloadCheck the value of the flag + * @since 1.4 + */ + public void setForceReloadCheck(boolean forceReloadCheck) + { + this.forceReloadCheck = forceReloadCheck; + } + + /** * Adds a new configuration to this combined configuration. It is possible * (but not mandatory) to give the new configuration a name. This name must * be unique, otherwise a <code>ConfigurationRuntimeException</code> will @@ -441,6 +475,40 @@ copy.setRootNode(new DefaultConfigurationNode()); return copy; + } + + /** + * Returns the value of the specified property. This implementation + * evaluates the <em>force reload check</em> flag. If it is set, all + * contained configurations will be triggered before the value of the + * requested property is retrieved. + * + * @param key the key of the desired property + * @return the value of this property + * @since 1.4 + */ + public Object getProperty(String key) + { + if (isForceReloadCheck()) + { + for (Iterator it = configurations.iterator(); it.hasNext();) + { + try + { + // simply retrieve a property; this is enough for + // triggering a reload + ((ConfigData) it.next()).getConfiguration().getProperty( + PROP_RELOAD_CHECK); + } + catch (Exception ex) + { + // ignore all exceptions, e.g. missing property exceptions + ; + } + } + } + + return super.getProperty(key); } /** Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCombinedConfiguration.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCombinedConfiguration.java?view=diff&rev=484692&r1=484691&r2=484692 ============================================================================== --- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCombinedConfiguration.java (original) +++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCombinedConfiguration.java Fri Dec 8 10:30:15 2006 @@ -16,11 +16,16 @@ */ package org.apache.commons.configuration; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; import java.util.Collection; import java.util.Set; import org.apache.commons.configuration.event.ConfigurationEvent; import org.apache.commons.configuration.event.ConfigurationListener; +import org.apache.commons.configuration.reloading.FileAlwaysReloadingStrategy; import org.apache.commons.configuration.tree.NodeCombiner; import org.apache.commons.configuration.tree.UnionCombiner; @@ -66,6 +71,7 @@ assertTrue("Wrong node combiner", config.getNodeCombiner() instanceof UnionCombiner); assertNull("Test config was found", config.getConfiguration(TEST_NAME)); + assertFalse("Force reload check flag is set", config.isForceReloadCheck()); } /** @@ -402,6 +408,63 @@ assertTrue("Config is not empty", config.isEmpty()); listener.checkEvent(3, 2); + } + + /** + * Tests if file-based configurations can be reloaded. + */ + public void testReloading() throws Exception + { + config.setForceReloadCheck(true); + File testDir = new File("target"); + File testXmlFile = new File(testDir, "reload.xml"); + File testPropsFile = new File(testDir, "reload.properties"); + writeFile(testXmlFile, "<xml><xmlReload>0</xmlReload></xml>"); + writeFile(testPropsFile, "propsReload = 0"); + XMLConfiguration c1 = new XMLConfiguration(testXmlFile); + c1.setReloadingStrategy(new FileAlwaysReloadingStrategy()); + PropertiesConfiguration c2 = new PropertiesConfiguration(testPropsFile); + c2.setThrowExceptionOnMissing(true); + c2.setReloadingStrategy(new FileAlwaysReloadingStrategy()); + config.addConfiguration(c1); + config.addConfiguration(c2); + assertEquals("Wrong xml reload value", 0, config.getInt("xmlReload")); + assertEquals("Wrong props reload value", 0, config + .getInt("propsReload")); + + writeFile(testXmlFile, "<xml><xmlReload>1</xmlReload></xml>"); + assertEquals("XML reload not detected", 1, config.getInt("xmlReload")); + config.setForceReloadCheck(false); + writeFile(testPropsFile, "propsReload = 1"); + assertEquals("Props reload detected though check flag is false", 0, config + .getInt("propsReload")); + + assertTrue("XML file cannot be removed", testXmlFile.delete()); + assertTrue("Props file cannot be removed", testPropsFile.delete()); + } + + /** + * Helper method for writing a file. + * + * @param file the file to be written + * @param content the file's content + * @throws IOException if an error occurs + */ + private static void writeFile(File file, String content) throws IOException + { + PrintWriter out = null; + try + { + out = new PrintWriter(new FileWriter(file)); + out.print(content); + } + finally + { + if (out != null) + { + out.close(); + } + } } /** 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=484692&r1=484691&r2=484692 ============================================================================== --- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original) +++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Fri Dec 8 10:30:15 2006 @@ -23,6 +23,16 @@ <body> <release version="1.4-dev" date="in SVN"> + <action dev="oheger" type="update" issue="CONFIGURATION-240"> + File-based configurations with a reloading strategy did not work well + together with CombinedConfiguration because the reloading strategy is + only checked when its associated configuration is accessed (which does + not happen when only the combined configuration is queried). + As a workaround CombinedConfiguration now provides the boolean + forceReloadCheck property. If this is set to true, all contained + configurations will be triggered when a property is queried. This will + cause a reload if necessary. + </action> <action dev="oheger" type="add" issue="CONFIGURATION-243"> Configuration declarations in the configuration definition file for DefaultConfigurationBuilder that are marked as optional now support a --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]