You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2010/10/30 21:24:23 UTC

svn commit: r1029169 - in /commons/proper/configuration/branches/configuration2_experimental/src: changes/ main/java/org/apache/commons/configuration2/ test/java/org/apache/commons/configuration2/

Author: oheger
Date: Sat Oct 30 19:24:22 2010
New Revision: 1029169

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

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/changes/changes.xml
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalFileConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestFileConfiguration.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/changes/changes.xml?rev=1029169&r1=1029168&r2=1029169&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/changes/changes.xml (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/changes/changes.xml Sat Oct 30 19:24:22 2010
@@ -102,6 +102,10 @@
         update of the managed database table. This makes it usable in
         environments where the connections do not use auto-commit mode.
       </action>
+      <action dev="oheger" type="add" issue="CONFIGURATION-410">
+        Added a refresh() method to AbstractFileConfiguration and
+        AbstractHierarchicalFileConfiguration.
+      </action>
       <action dev="oheger" type="fix" issue="CONFIGURATION-409">
         HierarchicalINIConfiguration now correctly saves sections whose name
         contains delimiter characters.

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalFileConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalFileConfiguration.java?rev=1029169&r1=1029168&r2=1029169&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalFileConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalFileConfiguration.java Sat Oct 30 19:24:22 2010
@@ -30,10 +30,10 @@ import java.net.URL;
 import java.util.Collection;
 import java.util.Iterator;
 
-import org.apache.commons.configuration2.event.ConfigurationEvent;
-import org.apache.commons.configuration2.event.ConfigurationListener;
 import org.apache.commons.configuration2.event.ConfigurationErrorEvent;
 import org.apache.commons.configuration2.event.ConfigurationErrorListener;
+import org.apache.commons.configuration2.event.ConfigurationEvent;
+import org.apache.commons.configuration2.event.ConfigurationListener;
 import org.apache.commons.configuration2.expr.NodeList;
 import org.apache.commons.configuration2.fs.DefaultFileSystem;
 import org.apache.commons.configuration2.fs.FileSystem;
@@ -82,7 +82,7 @@ import org.apache.commons.lang.StringUti
  * in <code>java.util.Properties</code>.</p>
  *
  * @author Emmanuel Bourg
- * @version $Revision$, $Date$
+ * @version $Id$
  * @since 1.0-rc2
  */
 public abstract class AbstractHierarchicalFileConfiguration
@@ -833,21 +833,7 @@ implements FileConfiguration, Configurat
                         {
                             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();
@@ -872,6 +858,35 @@ implements FileConfiguration, Configurat
     }
 
     /**
+     * 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);
+    }
+
+    /**
      * Enters the &quot;No reloading mode&quot;. As long as this mode is active
      * no reloading will be performed. This is necessary for some
      * implementations of <code>save()</code> in derived classes, which may

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestFileConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestFileConfiguration.java?rev=1029169&r1=1029168&r2=1029169&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestFileConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestFileConfiguration.java Sat Oct 30 19:24:22 2010
@@ -632,6 +632,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 comparing the content of two configuration objects.
      *
      * @param config1 the first configuration