You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2007/02/22 19:54:11 UTC

svn commit: r510624 - in /tapestry/tapestry5/tapestry-core/trunk/src: main/java/org/apache/tapestry/internal/util/ main/resources/org/apache/tapestry/internal/util/ test/java/org/apache/tapestry/internal/util/

Author: hlship
Date: Thu Feb 22 10:54:10 2007
New Revision: 510624

URL: http://svn.apache.org/viewvc?view=rev&rev=510624
Log:
Only track timestamps on file system files, excluding the contents of JAR files. Handle the deletion of a tracked file as a change (rather than an exception).
[TAPESTRY-1243]

Modified:
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/util/URLChangeTracker.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/util/UtilMessages.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/util/UtilStrings.properties
    tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/util/URLChangeTrackerTest.java

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/util/URLChangeTracker.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/util/URLChangeTracker.java?view=diff&rev=510624&r1=510623&r2=510624
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/util/URLChangeTracker.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/util/URLChangeTracker.java Thu Feb 22 10:54:10 2007
@@ -1,4 +1,4 @@
-// Copyright 2006 The Apache Software Foundation
+// Copyright 2006, 2007 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -16,9 +16,10 @@
 
 import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newThreadSafeMap;
 
-import java.io.IOException;
+import java.io.File;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
-import java.net.URLConnection;
 import java.util.Map;
 
 /**
@@ -27,26 +28,41 @@
  */
 public class URLChangeTracker
 {
-    private final Map<URL, Long> _urlToTimestamp = newThreadSafeMap();
+    private static final long FILE_DOES_NOT_EXIST_TIMESTAMP = -1l;
+
+    private final Map<File, Long> _fileToTimestamp = newThreadSafeMap();
 
     /**
      * Stores a new URL into the tracker, or returns the previous time stamp for a previously added
-     * URL.
+     * URL. Filters out all non-file URLs.
      * 
      * @param url
      *            of the resource to add
-     * @return the current timestamp for the URL
+     * @return the current timestamp for the URL, or 0 if not a file URL
      */
     public long add(URL url)
     {
-        if (_urlToTimestamp.containsKey(url))
-            return _urlToTimestamp.get(url);
+        if (!url.getProtocol().equals("file"))
+            return 0;
+
+        try
+        {
+            URI resourceURI = url.toURI();
+            File resourceFile = new File(resourceURI);
+
+            if (_fileToTimestamp.containsKey(resourceFile))
+                return _fileToTimestamp.get(resourceFile);
 
-        long timestamp = readTimestamp(url);
+            long timestamp = readTimestamp(resourceFile);
 
-        _urlToTimestamp.put(url, timestamp);
+            _fileToTimestamp.put(resourceFile, timestamp);
 
-        return timestamp;
+            return timestamp;
+        }
+        catch (URISyntaxException ex)
+        {
+            throw new RuntimeException(ex);
+        }
     }
 
     /**
@@ -54,7 +70,7 @@
      */
     public void clear()
     {
-        _urlToTimestamp.clear();
+        _fileToTimestamp.clear();
     }
 
     /**
@@ -68,8 +84,8 @@
         // This code would be highly suspect if this method was expected to be invoked
         // concurrently, but CheckForUpdatesFilter ensures that it will be invoked
         // synchronously.
-        
-        for (Map.Entry<URL, Long> entry : _urlToTimestamp.entrySet())
+
+        for (Map.Entry<File, Long> entry : _fileToTimestamp.entrySet())
         {
             long newTimestamp = readTimestamp(entry.getKey());
             long current = entry.getValue();
@@ -84,28 +100,12 @@
         return result;
     }
 
-    private long readTimestamp(URL url)
+    private long readTimestamp(File file)
     {
-        try
-        {
-            URLConnection connection = url.openConnection();
-
-            connection.connect();
-
-            long result = connection.getLastModified();
-
-            // System.out.println(url + " --> " + result);
-
-            // So ... do you ever close the connection? So far, no problems using it like this.
-            // It must have a finalize() to close the connection if that's even necessary.
-
-            return result;
-        }
-        catch (IOException ex)
-        {
-            throw new RuntimeException(UtilMessages.unableToReadLastModified(url, ex), ex);
-        }
+        if (!file.exists())
+            return FILE_DOES_NOT_EXIST_TIMESTAMP;
 
+        return file.lastModified();
     }
 
     /**
@@ -114,9 +114,15 @@
      */
     public void forceChange()
     {
-        for (Map.Entry<URL, Long> e : _urlToTimestamp.entrySet())
+        for (Map.Entry<File, Long> e : _fileToTimestamp.entrySet())
         {
             e.setValue(0l);
         }
+    }
+
+    /** Needed for testing. */
+    int trackedFileCount()
+    {
+        return _fileToTimestamp.size();
     }
 }

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/util/UtilMessages.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/util/UtilMessages.java?view=diff&rev=510624&r1=510623&r2=510624
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/util/UtilMessages.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/util/UtilMessages.java Thu Feb 22 10:54:10 2007
@@ -14,8 +14,6 @@
 
 package org.apache.tapestry.internal.util;
 
-import java.net.URL;
-
 import org.apache.tapestry.ioc.Messages;
 import org.apache.tapestry.ioc.internal.util.MessagesImpl;
 
@@ -25,11 +23,6 @@
 
     private UtilMessages()
     {
-    }
-
-    static String unableToReadLastModified(URL url, Throwable cause)
-    {
-        return MESSAGES.format("unable-to-read-last-modified", url, cause);
     }
 
     static String noReturnValueAccepted(String eventType, String componentId, Object returnValue,

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/util/UtilStrings.properties
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/util/UtilStrings.properties?view=diff&rev=510624&r1=510623&r2=510624
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/util/UtilStrings.properties (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/util/UtilStrings.properties Thu Feb 22 10:54:10 2007
@@ -12,6 +12,5 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-unable-to-read-last-modified=Unable to read last modified time stamp of resource %s: %s
 no-return-value-accepted=Event '%s' from %s received an event handler method return value of %s from %s. \
   This type of event does not support return values from event handler methods.

Modified: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/util/URLChangeTrackerTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/util/URLChangeTrackerTest.java?view=diff&rev=510624&r1=510623&r2=510624
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/util/URLChangeTrackerTest.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/util/URLChangeTrackerTest.java Thu Feb 22 10:54:10 2007
@@ -1,4 +1,4 @@
-// Copyright 2006 The Apache Software Foundation
+// Copyright 2006, 2007 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -40,6 +40,8 @@
 
         t.add(url);
 
+        assertEquals(t.trackedFileCount(), 1);
+
         assertFalse(t.containsChanges());
 
         boolean changed = false;
@@ -65,23 +67,15 @@
     }
 
     @Test
-    public void unreadable() throws Exception
+    public void non_file_URLs_are_ignored() throws Exception
     {
         URLChangeTracker t = new URLChangeTracker();
 
         URL url = new URL("ftp://breeblebrox.com");
 
-        try
-        {
-            t.add(url);
-            unreachable();
-        }
-        catch (RuntimeException ex)
-        {
-            assertTrue(ex.getMessage().contains(
-                    "Unable to read last modified time stamp of resource ftp://breeblebrox.com:"));
-        }
+        t.add(url);
 
+        assertEquals(t.trackedFileCount(), 0);
     }
 
     @Test
@@ -107,6 +101,27 @@
         current = t.add(url);
 
         assertFalse(current == initial);
+    }
+
+    @Test
+    public void deleted_files_show_as_changes() throws Exception
+    {
+        File f = File.createTempFile("changetracker0", ".tmp");
+        URL url = f.toURL();
+
+        URLChangeTracker t = new URLChangeTracker();
+
+        long timeModified = t.add(url);
+
+        assertTrue(timeModified > 0);
+
+        assertEquals(t.trackedFileCount(), 1);
+
+        assertFalse(t.containsChanges());
+
+        assertTrue(f.delete());
+
+        assertTrue(t.containsChanges());
     }
 
 }