Author: oheger
Date: Sat Jun  4 07:21:39 2005
New Revision: 179993

URL: http://svn.apache.org/viewcvs?rev=179993&view=rev
Log:
Some fixes for FileChangedReloadingStrategy related to issue 34289

Modified:
    
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
    
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/reloading/FileChangedReloadingStrategy.java
    
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/reloading/TestFileChangedReloadingStrategy.java
    jakarta/commons/proper/configuration/trunk/xdocs/changes.xml

Modified: 
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java?rev=179993&r1=179992&r2=179993&view=diff
==============================================================================
--- 
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
 (original)
+++ 
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
 Sat Jun  4 07:21:39 2005
@@ -467,7 +467,7 @@
      * @param url the URL
      * @return the resulting file object
      */
-    static File fileFromURL(URL url)
+    public static File fileFromURL(URL url)
     {
         if (PROTOCOL_FILE.equals(url.getProtocol()))
         {

Modified: 
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/reloading/FileChangedReloadingStrategy.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/reloading/FileChangedReloadingStrategy.java?rev=179993&r1=179992&r2=179993&view=diff
==============================================================================
--- 
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/reloading/FileChangedReloadingStrategy.java
 (original)
+++ 
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/reloading/FileChangedReloadingStrategy.java
 Sat Jun  4 07:21:39 2005
@@ -16,6 +16,11 @@
 
 package org.apache.commons.configuration.reloading;
 
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import org.apache.commons.configuration.ConfigurationUtils;
 import org.apache.commons.configuration.FileConfiguration;
 
 /**
@@ -30,6 +35,10 @@
  */
 public class FileChangedReloadingStrategy implements ReloadingStrategy
 {
+    /** Constant for the jar URL protocol.*/
+    private static final String JAR_PROTOCOL = "jar";
+    
+    /** Stores a reference to the configuration to be monitored.*/
     protected FileConfiguration configuration;
 
     /** The last time the configuration file was modified. */
@@ -57,10 +66,13 @@
 
         long now = System.currentTimeMillis();
 
-        if ((now > lastChecked + refreshDelay) && hasChanged())
+        if ((now > lastChecked + refreshDelay))
         {
             lastChecked = now;
-            reloading = true;
+            if(hasChanged())
+            {
+                reloading = true;
+            }
         }
 
         return reloading;
@@ -94,7 +106,11 @@
      */
     protected void updateLastModified()
     {
-        lastModified = configuration.getFile().lastModified();
+        File file = getFile();
+        if (file != null)
+        {
+            lastModified = file.lastModified();
+        }
     }
 
     /**
@@ -102,12 +118,52 @@
      */
     protected boolean hasChanged()
     {
-        if (!configuration.getFile().exists())
+        File file = getFile();
+        if (file == null || !file.exists())
         {
             return false;
         }
 
-        return (configuration.getFile().lastModified() > lastModified);
+        return (file.lastModified() > lastModified);
+    }
+
+    /**
+     * Returns the file that is monitored by this strategy. Note that the 
return
+     * value can be <b>null </b> under some circumstances.
+     * 
+     * @return the monitored file
+     */
+    protected File getFile()
+    {
+        return (configuration.getURL() != null) ? fileFromURL(configuration
+                .getURL()) : configuration.getFile();
     }
 
+    /**
+     * Helper method for transforming a URL into a file object. This method
+     * handles file: and jar: URLs.
+     * 
+     * @param url the URL to be converted
+     * @return the resulting file or <b>null </b>
+     */
+    private File fileFromURL(URL url)
+    {
+        if (JAR_PROTOCOL.equals(url.getProtocol()))
+        {
+            String path = url.getPath();
+            try
+            {
+                return ConfigurationUtils.fileFromURL(new URL(path.substring(0,
+                        path.indexOf('!'))));
+            }
+            catch (MalformedURLException mex)
+            {
+                return null;
+            }
+        }
+        else
+        {
+            return ConfigurationUtils.fileFromURL(url);
+        }
+    }
 }

Modified: 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/reloading/TestFileChangedReloadingStrategy.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/reloading/TestFileChangedReloadingStrategy.java?rev=179993&r1=179992&r2=179993&view=diff
==============================================================================
--- 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/reloading/TestFileChangedReloadingStrategy.java
 (original)
+++ 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/reloading/TestFileChangedReloadingStrategy.java
 Sat Jun  4 07:21:39 2005
@@ -21,6 +21,7 @@
 
 import junit.framework.TestCase;
 import org.apache.commons.configuration.PropertiesConfiguration;
+import org.apache.commons.configuration.XMLConfiguration;
 
 /**
  * Test case for the ReloadableConfiguration class.
@@ -102,4 +103,34 @@
         assertEquals("refresh delay", 500, strategy.getRefreshDelay());
     }
 
+    /**
+     * Tests if a file from the classpath can be monitored.
+     */
+    public void testFromClassPath() throws Exception
+    {
+        PropertiesConfiguration config = new PropertiesConfiguration();
+        config.setFileName("test.properties");
+        config.load();
+        assertTrue(config.getBoolean("configuration.loaded"));
+        FileChangedReloadingStrategy strategy = new 
FileChangedReloadingStrategy();
+        config.setReloadingStrategy(strategy);
+        assertEquals(config.getURL(), strategy.getFile().toURL());
+    }
+    
+    /**
+     * Tests to watch a configuration file in a jar. In this case the jar file
+     * itself should be monitored.
+     */
+    public void testFromJar() throws Exception
+    {
+        XMLConfiguration config = new XMLConfiguration();
+        config.setFileName("test-jar.xml");
+        config.load();
+        FileChangedReloadingStrategy strategy = new 
FileChangedReloadingStrategy();
+        config.setReloadingStrategy(strategy);
+        File file = strategy.getFile();
+        assertNotNull(file);
+        assertTrue(file.exists());
+        assertEquals("resources.jar", file.getName());
+    }
 }

Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?rev=179993&r1=179992&r2=179993&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Sat Jun  4 
07:21:39 2005
@@ -23,6 +23,16 @@
   <body>
 
     <release version="1.2-dev" date="in SVN">
+      <action dev="oheger" type="update" issue="34289">
+        Updated FileChangedReloadingStrategy to use the file based 
configuration's
+        source URL to find the file to watch. Before that it was possible that
+        the strategy checked the wrong file. For configuration files loaded
+        from a jar FileChangedReloadingStrategy now checks the jar file itself
+        for changes. Finally a bug was fixed which caused the strategy to
+        check the watched file's last change date on every invocation of its
+        reloadingRequired() method ignoring its refresh delay. Thanks to Jorge
+        Ferrer.
+      </action>
       <action dev="oheger" type="update" issue="35118">
         Fixed a bug in the collaboration between XMLConfiguration and its
         reloading strategy: The configuration did not check its reloading



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to