Author: oheger
Date: Sat Oct 30 19:04:02 2010
New Revision: 1029163

URL: http://svn.apache.org/viewvc?rev=1029163&view=rev
Log:
[CONFIGURATION-410] Added a refresh() method to AbstractFileConfiguration.

Modified:
    
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
    
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java

Modified: 
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java?rev=1029163&r1=1029162&r2=1029163&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
 (original)
+++ 
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
 Sat Oct 30 19:04:02 2010
@@ -74,7 +74,7 @@ import org.apache.commons.logging.LogFac
  * in <code>java.util.Properties</code>.</p>
  *
  * @author Emmanuel Bourg
- * @version $Revision$, $Date$
+ * @version $Id$
  * @since 1.0-rc2
  */
 public abstract class AbstractFileConfiguration
@@ -828,21 +828,7 @@ implements FileConfiguration, FileSystem
                         {
                             getLogger().info("Reloading configuration. URL is 
" + getURL());
                         }
-                        fireEvent(EVENT_RELOAD, null, getURL(), true);
-                        setDetailEvents(false);
-                        boolean autoSaveBak = this.isAutoSave(); // save the 
current state
-                        this.setAutoSave(false); // deactivate autoSave to 
prevent information loss
-                        try
-                        {
-                            clear();
-                            load();
-                        }
-                        finally
-                        {
-                            this.setAutoSave(autoSaveBak); // set autoSave to 
previous value
-                            setDetailEvents(true);
-                        }
-                        fireEvent(EVENT_RELOAD, null, getURL(), false);
+                        refresh();
 
                         // notify the strategy
                         strategy.reloadingPerformed();
@@ -867,6 +853,35 @@ implements FileConfiguration, FileSystem
     }
 
     /**
+     * Reloads the associated configuration file. This method first clears the
+     * content of this configuration, then the associated configuration file is
+     * loaded again. Updates on this configuration which have not yet been 
saved
+     * are lost. Calling this method is like invoking <code>reload()</code>
+     * without checking the reloading strategy.
+     *
+     * @throws ConfigurationException if an error occurs
+     * @since 1.7
+     */
+    public void refresh() throws ConfigurationException
+    {
+        fireEvent(EVENT_RELOAD, null, getURL(), true);
+        setDetailEvents(false);
+        boolean autoSaveBak = this.isAutoSave(); // save the current state
+        this.setAutoSave(false); // deactivate autoSave to prevent information 
loss
+        try
+        {
+            clear();
+            load();
+        }
+        finally
+        {
+            this.setAutoSave(autoSaveBak); // set autoSave to previous value
+            setDetailEvents(true);
+        }
+        fireEvent(EVENT_RELOAD, null, getURL(), false);
+    }
+
+    /**
      * Send notification that the configuration has changed.
      */
     public void configurationChanged()

Modified: 
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java?rev=1029163&r1=1029162&r2=1029163&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
 (original)
+++ 
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
 Sat Oct 30 19:04:02 2010
@@ -653,6 +653,38 @@ public class TestFileConfiguration exten
     }
 
     /**
+     * Tests whether a configuration can be refreshed.
+     */
+    public void testRefresh() throws ConfigurationException
+    {
+        PropertiesConfiguration config = new 
PropertiesConfiguration(TEST_FILE);
+        assertEquals("Wrong value", 10, config.getInt("test.integer"));
+        config.setProperty("test.integer", new Integer(42));
+        assertEquals("Wrong value after update", 42,
+                config.getInt("test.integer"));
+        config.refresh();
+        assertEquals("Wrong value after refresh", 10,
+                config.getInt("test.integer"));
+    }
+
+    /**
+     * Tests refresh if the configuration is not associated with a file.
+     */
+    public void testRefreshNoFile() throws ConfigurationException
+    {
+        PropertiesConfiguration config = new PropertiesConfiguration();
+        try
+        {
+            config.refresh();
+            fail("Could refresh configuration without a file!");
+        }
+        catch (ConfigurationException cex)
+        {
+            // ok
+        }
+    }
+
+    /**
      * Helper method for testing an iteration over the keys of a file-based
      * configuration while a reload happens.
      *


Reply via email to