xmlgraphics-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From maxber...@apache.org
Subject svn commit: r816640 - in /xmlgraphics/commons/trunk: src/java/org/apache/xmlgraphics/image/loader/cache/ImageCache.java test/java/org/apache/xmlgraphics/image/loader/cache/ImageCacheTestCase.java
Date Fri, 18 Sep 2009 14:14:55 GMT
Author: maxberger
Date: Fri Sep 18 14:14:55 2009
New Revision: 816640

URL: http://svn.apache.org/viewvc?rev=816640&view=rev
Log:
removed ConcurrentModificationException from ImageCache (bugzilla #47868). TestCase thanks
to mercuron@gmx.ch

Modified:
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/cache/ImageCache.java
    xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/cache/ImageCacheTestCase.java

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/cache/ImageCache.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/cache/ImageCache.java?rev=816640&r1=816639&r2=816640&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/cache/ImageCache.java
(original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/cache/ImageCache.java
Fri Sep 18 14:14:55 2009
@@ -22,8 +22,10 @@
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.Set;
 
 import javax.xml.transform.Source;
 
@@ -144,22 +146,22 @@
      * @return true if the URI is invalid
      */
     public boolean isInvalidURI(String uri) {
-        Long timestamp = (Long)invalidURIs.get(uri);
-        if (timestamp != null) {
-            boolean expired = removeInvalidURIIfExpired(uri, timestamp.longValue());
-            if (!expired) {
-                if (cacheListener != null) {
-                    cacheListener.invalidHit(uri);
-                }
-                return true;
+        boolean expired = removeInvalidURIIfExpired(uri);
+        if (expired) {
+            return false;
+        } else {
+            if (cacheListener != null) {
+                cacheListener.invalidHit(uri);
             }
+            return true;
         }
-        return false;
     }
 
-    private boolean removeInvalidURIIfExpired(String uri, long timestamp) {
-        boolean expired = this.invalidURIExpirationPolicy.isExpired(
-                this.timeStampProvider, timestamp);
+    private boolean removeInvalidURIIfExpired(String uri) {
+        Long timestamp = (Long) invalidURIs.get(uri);
+        boolean expired = (timestamp == null)
+                || this.invalidURIExpirationPolicy.isExpired(
+                        this.timeStampProvider, timestamp.longValue());
         if (expired) {
             this.invalidURIs.remove(uri);
         }
@@ -288,12 +290,11 @@
     }
 
     private void doInvalidURIHouseKeeping() {
-        synchronized(this.invalidURIs) {
-            Iterator iter = this.invalidURIs.entrySet().iterator();
-            while (iter.hasNext()) {
-                Map.Entry entry = (Map.Entry)iter.next();
-                removeInvalidURIIfExpired((String)entry.getKey(), ((Long)entry.getValue()).longValue());
-            }
+        final Set currentEntries = new HashSet(this.invalidURIs.keySet());
+        final Iterator iter = currentEntries.iterator();
+        while (iter.hasNext()) {
+            final String key = (String) iter.next();
+            removeInvalidURIIfExpired(key);
         }
     }
 

Modified: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/cache/ImageCacheTestCase.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/cache/ImageCacheTestCase.java?rev=816640&r1=816639&r2=816640&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/cache/ImageCacheTestCase.java
(original)
+++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/cache/ImageCacheTestCase.java
Fri Sep 18 14:14:55 2009
@@ -177,4 +177,21 @@
         assertEquals(1, statistics.getImageCacheMisses());
     }
 
+    
+    /**
+     * Test to check if doInvalidURIHouseKeeping() throws a
+     * ConcurrentModificationException.
+     */
+    public void testImageCacheHouseKeeping() {
+        ImageCache imageCache = new ImageCache(new TimeStampProvider(),
+                new DefaultExpirationPolicy(1));
+        imageCache.registerInvalidURI("invalid");
+        imageCache.registerInvalidURI("invalid2");
+        try {
+            Thread.sleep(1200);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        imageCache.doHouseKeeping();
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org


Mime
View raw message