geronimo-xbean-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r1347593 - /geronimo/xbean/trunk/xbean-classloader/src/main/java/org/apache/xbean/classloader/MultiParentClassLoader.java
Date Thu, 07 Jun 2012 12:53:45 GMT
Author: gnodet
Date: Thu Jun  7 12:53:45 2012
New Revision: 1347593

URL: http://svn.apache.org/viewvc?rev=1347593&view=rev
Log:
[XBEAN-203] Performance improvement by adding local cache for MultiParentClassLoader#loadClass

Modified:
    geronimo/xbean/trunk/xbean-classloader/src/main/java/org/apache/xbean/classloader/MultiParentClassLoader.java

Modified: geronimo/xbean/trunk/xbean-classloader/src/main/java/org/apache/xbean/classloader/MultiParentClassLoader.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-classloader/src/main/java/org/apache/xbean/classloader/MultiParentClassLoader.java?rev=1347593&r1=1347592&r2=1347593&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-classloader/src/main/java/org/apache/xbean/classloader/MultiParentClassLoader.java
(original)
+++ geronimo/xbean/trunk/xbean-classloader/src/main/java/org/apache/xbean/classloader/MultiParentClassLoader.java
Thu Jun  7 12:53:45 2012
@@ -17,6 +17,7 @@
 package org.apache.xbean.classloader;
 
 import java.io.IOException;
+import java.lang.ref.SoftReference;
 import java.net.URL;
 import java.net.URLStreamHandlerFactory;
 import java.util.ArrayList;
@@ -25,6 +26,8 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * A MultiParentClassLoader is a simple extension of the URLClassLoader that simply changes
the single parent class
@@ -44,6 +47,7 @@ public class MultiParentClassLoader exte
     private final String[] nonOverridableClasses;
     private final String[] hiddenResources;
     private final String[] nonOverridableResources;
+    private final Map<String, SoftReference<Class>> cache = new ConcurrentHashMap<String,
SoftReference<Class>>();
 
     /**
      * Creates a named class loader with no parents.
@@ -154,7 +158,25 @@ public class MultiParentClassLoader exte
     /**
      * {@inheritDoc}
      */
-    protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException
{
+    protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException
{
+        Class result = null;
+
+        //
+        // check if the class is already in the local cache
+        //
+        SoftReference<Class> reference = cache.get(name);
+		if (reference != null) {
+            result = reference.get();
+		}
+        if (result == null) {
+            result = doLoadClass(name, resolve);
+            cache.put(name, new SoftReference<Class>(result));
+        }
+
+        return result;
+    }
+
+    private synchronized Class doLoadClass(String name, boolean resolve) throws ClassNotFoundException
{
         //
         // Check if class is in the loaded classes cache
         //



Mime
View raw message