incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r801254 - in /sling/trunk/bundles/commons/classloader/src/main/java/org/apache/sling/commons/classloader/impl: ClassLoaderFacade.java PackageAdminClassLoader.java
Date Wed, 05 Aug 2009 15:07:52 GMT
Author: cziegeler
Date: Wed Aug  5 15:07:52 2009
New Revision: 801254

URL: http://svn.apache.org/viewvc?rev=801254&view=rev
Log:
Don't cache classes in the facade, cache them in the package admin class loader.

Modified:
    sling/trunk/bundles/commons/classloader/src/main/java/org/apache/sling/commons/classloader/impl/ClassLoaderFacade.java
    sling/trunk/bundles/commons/classloader/src/main/java/org/apache/sling/commons/classloader/impl/PackageAdminClassLoader.java

Modified: sling/trunk/bundles/commons/classloader/src/main/java/org/apache/sling/commons/classloader/impl/ClassLoaderFacade.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/classloader/src/main/java/org/apache/sling/commons/classloader/impl/ClassLoaderFacade.java?rev=801254&r1=801253&r2=801254&view=diff
==============================================================================
--- sling/trunk/bundles/commons/classloader/src/main/java/org/apache/sling/commons/classloader/impl/ClassLoaderFacade.java
(original)
+++ sling/trunk/bundles/commons/classloader/src/main/java/org/apache/sling/commons/classloader/impl/ClassLoaderFacade.java
Wed Aug  5 15:07:52 2009
@@ -21,8 +21,6 @@
 import java.io.IOException;
 import java.net.URL;
 import java.util.Enumeration;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
 
 
 /**
@@ -37,12 +35,6 @@
 
     private final DynamicClassLoaderManagerImpl manager;
 
-    /** A cache for resolved classes. */
-    private Map<String, Class<?>> classCache = new ConcurrentHashMap<String,
Class<?>>();
-
-    /** A cache for resolved urls. */
-    private Map<String, URL> urlCache = new ConcurrentHashMap<String, URL>();
-
     public ClassLoaderFacade(final DynamicClassLoaderManagerImpl manager) {
         this.manager = manager;
     }
@@ -54,16 +46,11 @@
         if ( !this.manager.isActive() ) {
             throw new RuntimeException("Dynamic class loader has already been deactivated.");
         }
-        final URL cachedURL = urlCache.get(name);
-        if ( cachedURL != null ) {
-            return cachedURL;
-        }
         final ClassLoader[] loaders = manager.getDynamicClassLoaders();
         for(final ClassLoader cl : loaders) {
             if ( cl != null ) {
                 final URL u = cl.getResource(name);
                 if ( u != null ) {
-                    urlCache.put(name, u);
                     return u;
                 }
             }
@@ -97,19 +84,13 @@
         if ( !this.manager.isActive() ) {
             throw new RuntimeException("Dynamic class loader has already been deactivated.");
         }
-        final Class<?> cachedClass = this.classCache.get(name);
-        if ( cachedClass != null ) {
-            return cachedClass;
-        }
         final ClassLoader[] loaders = manager.getDynamicClassLoaders();
         for(final ClassLoader cl : loaders) {
             if ( cl != null ) {
                 try {
                     final Class<?> c = cl.loadClass(name);
-                    this.classCache.put(name, c);
                     return c;
                 } catch (Exception cnfe) {
-                    cnfe.printStackTrace();
                     // we just ignore this and try the next class loader
                 }
             }

Modified: sling/trunk/bundles/commons/classloader/src/main/java/org/apache/sling/commons/classloader/impl/PackageAdminClassLoader.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/classloader/src/main/java/org/apache/sling/commons/classloader/impl/PackageAdminClassLoader.java?rev=801254&r1=801253&r2=801254&view=diff
==============================================================================
--- sling/trunk/bundles/commons/classloader/src/main/java/org/apache/sling/commons/classloader/impl/PackageAdminClassLoader.java
(original)
+++ sling/trunk/bundles/commons/classloader/src/main/java/org/apache/sling/commons/classloader/impl/PackageAdminClassLoader.java
Wed Aug  5 15:07:52 2009
@@ -20,7 +20,12 @@
 
 import java.io.IOException;
 import java.net.URL;
+import java.util.Collections;
 import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 
 import org.osgi.framework.Bundle;
 import org.osgi.service.packageadmin.ExportedPackage;
@@ -38,6 +43,15 @@
     /** The manager factory. */
     private final DynamicClassLoaderManagerFactory factory;
 
+    /** A cache for resolved classes. */
+    private Map<String, Class<?>> classCache = new ConcurrentHashMap<String,
Class<?>>();
+
+    /** Negative class cache. */
+    private Set<String> negativeClassCache = Collections.synchronizedSet(new HashSet<String>());
+
+    /** A cache for resolved urls. */
+    private Map<String, URL> urlCache = new ConcurrentHashMap<String, URL>();
+
     public PackageAdminClassLoader(final PackageAdmin pckAdmin,
                                    final ClassLoader parent,
                                    final DynamicClassLoaderManagerFactory factory) {
@@ -101,6 +115,10 @@
      * @see java.lang.ClassLoader#findResource(java.lang.String)
      */
     public URL findResource(String name) {
+        final URL cachedURL = urlCache.get(name);
+        if ( cachedURL != null ) {
+            return cachedURL;
+        }
         URL url = super.findResource(name);
         if ( url == null ) {
             final Bundle bundle = this.findBundleForPackage(getPackageFromResource(name));
@@ -108,6 +126,7 @@
                 url = bundle.getResource(name);
                 if ( url != null ) {
                     this.factory.addUsedBundle(bundle);
+                    urlCache.put(name, url);
                 }
             }
         }
@@ -118,6 +137,10 @@
      * @see java.lang.ClassLoader#findClass(java.lang.String)
      */
     public Class<?> findClass(String name) throws ClassNotFoundException {
+        final Class<?> cachedClass = this.classCache.get(name);
+        if ( cachedClass != null ) {
+            return cachedClass;
+        }
         Class<?> clazz = null;
         try {
             clazz = super.findClass(name);
@@ -131,6 +154,7 @@
         if ( clazz == null ) {
             throw new ClassNotFoundException("Class not found " + name);
         }
+        this.classCache.put(name, clazz);
         return clazz;
     }
 
@@ -138,19 +162,33 @@
      * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)
      */
     protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException
{
+        final Class<?> cachedClass = this.classCache.get(name);
+        if ( cachedClass != null ) {
+            return cachedClass;
+        }
+        if ( negativeClassCache.contains(name) ) {
+            throw new ClassNotFoundException("Class not found " + name);
+        }
         Class<?> clazz = null;
         try {
             clazz = super.loadClass(name, resolve);
         } catch (ClassNotFoundException cnfe) {
             final Bundle bundle = this.findBundleForPackage(getPackageFromClassName(name));
             if ( bundle != null ) {
-                clazz = bundle.loadClass(name);
+                try {
+                    clazz = bundle.loadClass(name);
+                } catch (ClassNotFoundException inner) {
+                    negativeClassCache.add(name);
+                    throw inner;
+                }
                 this.factory.addUsedBundle(bundle);
             }
         }
         if ( clazz == null ) {
+            negativeClassCache.add(name);
             throw new ClassNotFoundException("Class not found " + name);
         }
+        this.classCache.put(name, clazz);
         return clazz;
     }
 }



Mime
View raw message