ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From maart...@apache.org
Subject svn commit: r1374867 - in /ant/ivy/core/branches/2.3.x: ./ src/java/org/apache/ivy/core/module/id/ModuleId.java
Date Sun, 19 Aug 2012 22:00:33 GMT
Author: maartenc
Date: Sun Aug 19 22:00:33 2012
New Revision: 1374867

URL: http://svn.apache.org/viewvc?rev=1374867&view=rev
Log:
FIX: Memory leak and infinite loop in ModuleId.java (IVY-1362) (2nd attempt) (merged from
trunk)

Modified:
    ant/ivy/core/branches/2.3.x/   (props changed)
    ant/ivy/core/branches/2.3.x/src/java/org/apache/ivy/core/module/id/ModuleId.java

Propchange: ant/ivy/core/branches/2.3.x/
------------------------------------------------------------------------------
  Merged /ant/ivy/core/trunk:r1374865

Modified: ant/ivy/core/branches/2.3.x/src/java/org/apache/ivy/core/module/id/ModuleId.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/branches/2.3.x/src/java/org/apache/ivy/core/module/id/ModuleId.java?rev=1374867&r1=1374866&r2=1374867&view=diff
==============================================================================
--- ant/ivy/core/branches/2.3.x/src/java/org/apache/ivy/core/module/id/ModuleId.java (original)
+++ ant/ivy/core/branches/2.3.x/src/java/org/apache/ivy/core/module/id/ModuleId.java Sun Aug
19 22:00:33 2012
@@ -17,6 +17,7 @@
  */
 package org.apache.ivy.core.module.id;
 
+import java.lang.ref.WeakReference;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.WeakHashMap;
@@ -33,7 +34,7 @@ import org.apache.ivy.core.IvyPatternHel
 public class ModuleId implements Comparable {
     static final String ENCODE_SEPARATOR = ":#@#:";
     
-    private static final Map/*<ModuleId, ModuleId>*/ CACHE = new WeakHashMap();
+    private static final Map/*<ModuleId, WeakReference<ModuleId>>*/ CACHE = new
WeakHashMap();
 
     /**
      * Returns a ModuleId for the given organization and module name.
@@ -59,12 +60,20 @@ public class ModuleId implements Compara
      *            the module id to return
      * @return a unit instance of the given module id.
      */
-    public static synchronized ModuleId intern(ModuleId moduleId) {
-        ModuleId r = (ModuleId) CACHE.get(moduleId);
-        if (r == null) {
-            r = moduleId;
-            CACHE.put(r, r);
+    public static ModuleId intern(ModuleId moduleId) {
+        ModuleId r = null;
+        
+        synchronized (CACHE) {
+            WeakReference ref = (WeakReference) CACHE.get(moduleId);
+            if (ref != null) {
+                r = (ModuleId) ref.get();
+            }
+            if (r == null) {
+                r = moduleId;
+                CACHE.put(r, new WeakReference(r));
+            }
         }
+
         return r;
     }
 



Mime
View raw message