sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From romb...@apache.org
Subject [sling-org-apache-sling-commons-classloader] 02/08: SLING-1435 : After a NoClassDefError classes are still not found even if they have been installed in the meantime with Java 6
Date Tue, 07 Nov 2017 09:20:19 GMT
This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.commons.classloader-1.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-classloader.git

commit 275c4fdbb62b71135ce3a82671a915d9d65211a9
Author: Carsten Ziegeler <cziegeler@apache.org>
AuthorDate: Thu Mar 11 08:39:58 2010 +0000

    SLING-1435 :  After a NoClassDefError classes are still not found even if they have been
installed in the meantime with Java 6
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/classloader@921724
13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/commons/classloader/impl/Activator.java  | 16 ++++-----
 .../impl/DynamicClassLoaderManagerFactory.java     | 39 +++++++++++++---------
 .../impl/DynamicClassLoaderManagerImpl.java        |  7 ----
 .../classloader/impl/PackageAdminClassLoader.java  | 13 +++-----
 4 files changed, 36 insertions(+), 39 deletions(-)

diff --git a/src/main/java/org/apache/sling/commons/classloader/impl/Activator.java b/src/main/java/org/apache/sling/commons/classloader/impl/Activator.java
index 92f68ee..51af181 100644
--- a/src/main/java/org/apache/sling/commons/classloader/impl/Activator.java
+++ b/src/main/java/org/apache/sling/commons/classloader/impl/Activator.java
@@ -108,17 +108,15 @@ public class Activator implements SynchronousBundleListener, BundleActivator
{
      * @see org.osgi.framework.BundleListener#bundleChanged(org.osgi.framework.BundleEvent)
      */
     public void bundleChanged(BundleEvent event) {
-        final long bundleId = event.getBundle().getBundleId();
-        boolean needsUpdate = this.service.isBundleUsed(bundleId);
-        if ( needsUpdate ) {
+        boolean reload = false;
+        if ( event.getType() == BundleEvent.RESOLVED ) {
+            reload = this.service.hasUnresolvedPackages(event.getBundle());
+        } else if ( this.service.isBundleUsed(event.getBundle().getBundleId()) ) {
+            reload = true;
+        }
+        if ( reload ) {
             this.unregisterManagerFactory();
             this.registerManagerFactory();
-        } else {
-            // if a new bundle has been added (=resolved), we should
-            // clear the negative cache (see SLING-1302)
-            if ( event.getType() == BundleEvent.RESOLVED ) {
-                this.service.clearNegativeCaches();
-            }
         }
     }
 }
diff --git a/src/main/java/org/apache/sling/commons/classloader/impl/DynamicClassLoaderManagerFactory.java
b/src/main/java/org/apache/sling/commons/classloader/impl/DynamicClassLoaderManagerFactory.java
index 273d707..e6cd33a 100644
--- a/src/main/java/org/apache/sling/commons/classloader/impl/DynamicClassLoaderManagerFactory.java
+++ b/src/main/java/org/apache/sling/commons/classloader/impl/DynamicClassLoaderManagerFactory.java
@@ -16,16 +16,15 @@
  */
 package org.apache.sling.commons.classloader.impl;
 
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashSet;
-import java.util.List;
 import java.util.Set;
 
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceFactory;
 import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.packageadmin.ExportedPackage;
 import org.osgi.service.packageadmin.PackageAdmin;
 
 /**
@@ -42,8 +41,7 @@ public class DynamicClassLoaderManagerFactory
 
     private final Set<Long> usedBundles = Collections.synchronizedSet(new HashSet<Long>());
 
-    /** All created managers. */
-    private final List<DynamicClassLoaderManagerImpl> managers = new ArrayList<DynamicClassLoaderManagerImpl>();
+    private final Set<String> unresolvedPackages = Collections.synchronizedSet(new
HashSet<String>());
 
     /**
      * Create a new service instance
@@ -63,9 +61,6 @@ public class DynamicClassLoaderManagerFactory
                              final ServiceRegistration registration) {
         final DynamicClassLoaderManagerImpl manager =  new DynamicClassLoaderManagerImpl(this.context,
                 this.pckAdmin, new BundleProxyClassLoader(bundle), this);
-        synchronized ( managers ) {
-            managers.add(manager);
-        }
         return manager;
     }
 
@@ -76,9 +71,6 @@ public class DynamicClassLoaderManagerFactory
                              final ServiceRegistration registration,
                              final Object service) {
         if ( service != null ) {
-            synchronized ( managers ) {
-                managers.remove(service);
-            }
             ((DynamicClassLoaderManagerImpl)service).deactivate();
         }
     }
@@ -102,13 +94,30 @@ public class DynamicClassLoaderManagerFactory
     }
 
     /**
-     * Clear the negative caches of all registered managers.
+     * Notify that a package is not found during class loading.
+     * @param pckName The package name.
+     */
+    public void addUnresolvedPackage(final String pckName) {
+        this.unresolvedPackages.add(pckName);
+    }
+
+    /**
+     * Check if an exported package from the bundle has not been
+     * found during previous class loading attempts.
+     * @param bundle The bundle to check
+     * @return <code>true</code> if a package has not be found before
      */
-    public void clearNegativeCaches() {
-        synchronized ( this.managers ) {
-            for(final DynamicClassLoaderManagerImpl manager : this.managers) {
-                manager.clearNegativeCache();
+    public boolean hasUnresolvedPackages(final Bundle bundle) {
+        if ( !this.unresolvedPackages.isEmpty() ) {
+            final ExportedPackage[] pcks = this.pckAdmin.getExportedPackages(bundle);
+            if ( pcks != null ) {
+                for(final ExportedPackage pck : pcks ) {
+                    if ( this.unresolvedPackages.contains(pck.getName()) ) {
+                        return true;
+                    }
+                }
             }
         }
+        return false;
     }
 }
diff --git a/src/main/java/org/apache/sling/commons/classloader/impl/DynamicClassLoaderManagerImpl.java
b/src/main/java/org/apache/sling/commons/classloader/impl/DynamicClassLoaderManagerImpl.java
index 64d720d..b03514c 100644
--- a/src/main/java/org/apache/sling/commons/classloader/impl/DynamicClassLoaderManagerImpl.java
+++ b/src/main/java/org/apache/sling/commons/classloader/impl/DynamicClassLoaderManagerImpl.java
@@ -70,13 +70,6 @@ public class DynamicClassLoaderManagerImpl
         this.facade = new ClassLoaderFacade(this);
     }
 
-    /**
-     * Clear the negative cache
-     */
-    public void clearNegativeCache() {
-        this.pckAdminCL.clearNegativeCache();
-    }
-
     private synchronized void updateCache() {
         if ( this.trackingCount < this.getTrackingCount() ) {
             final ServiceReference[] refs = this.getServiceReferences();
diff --git a/src/main/java/org/apache/sling/commons/classloader/impl/PackageAdminClassLoader.java
b/src/main/java/org/apache/sling/commons/classloader/impl/PackageAdminClassLoader.java
index 1d8986d..0c869b7 100644
--- a/src/main/java/org/apache/sling/commons/classloader/impl/PackageAdminClassLoader.java
+++ b/src/main/java/org/apache/sling/commons/classloader/impl/PackageAdminClassLoader.java
@@ -61,13 +61,6 @@ class PackageAdminClassLoader extends ClassLoader {
     }
 
     /**
-     * Clear the negative cache.
-     */
-    public void clearNegativeCache() {
-        negativeClassCache.clear();
-    }
-
-    /**
      * Find the bundle for a given package.
      * @param pckName The package name.
      * @return The bundle or <code>null</code>
@@ -180,12 +173,14 @@ class PackageAdminClassLoader extends ClassLoader {
         try {
             clazz = super.loadClass(name, resolve);
         } catch (ClassNotFoundException cnfe) {
-            final Bundle bundle = this.findBundleForPackage(getPackageFromClassName(name));
+            final String pckName = getPackageFromClassName(name);
+            final Bundle bundle = this.findBundleForPackage(pckName);
             if ( bundle != null ) {
                 try {
                     clazz = bundle.loadClass(name);
                 } catch (ClassNotFoundException inner) {
                     negativeClassCache.add(name);
+                    this.factory.addUnresolvedPackage(pckName);
                     throw inner;
                 }
                 this.factory.addUsedBundle(bundle);
@@ -193,6 +188,8 @@ class PackageAdminClassLoader extends ClassLoader {
         }
         if ( clazz == null ) {
             negativeClassCache.add(name);
+            final String pckName = getPackageFromClassName(name);
+            this.factory.addUnresolvedPackage(pckName);
             throw new ClassNotFoundException("Class not found " + name);
         }
         this.classCache.put(name, clazz);

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <commits@sling.apache.org>.

Mime
View raw message