logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mattsic...@apache.org
Subject svn commit: r1612178 - /logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/util/Activator.java
Date Mon, 21 Jul 2014 03:41:08 GMT
Author: mattsicker
Date: Mon Jul 21 03:41:07 2014
New Revision: 1612178

URL: http://svn.apache.org/r1612178
Log:
Remove BundleDelegatingClassLoader.

  - As answered in the link below, there is a way to get the actual ClassLoader used for a
Bundle. Thanks to how the ClassLoaderContextSelector works, we can't really use a delegating
ClassLoader.
  - http://stackoverflow.com/questions/1503304/how-to-get-classloader-for-a-bundle-in-equinox

Modified:
    logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/util/Activator.java

Modified: logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/util/Activator.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/util/Activator.java?rev=1612178&r1=1612177&r2=1612178&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/util/Activator.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/util/Activator.java
Mon Jul 21 03:41:07 2014
@@ -16,10 +16,8 @@
  */
 package org.apache.logging.log4j.util;
 
-import java.io.IOException;
 import java.net.URL;
 import java.security.Permission;
-import java.util.Enumeration;
 
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.status.StatusLogger;
@@ -29,6 +27,7 @@ import org.osgi.framework.BundleActivato
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
 import org.osgi.framework.SynchronousBundleListener;
+import org.osgi.framework.wiring.BundleWiring;
 
 /**
  * OSGi bundle activator. Used for locating an implementation of
@@ -42,10 +41,6 @@ public class Activator implements Bundle
 
     private static final Logger LOGGER = StatusLogger.getLogger();
 
-    static {
-        checkPermission(new RuntimePermission("createClassLoader"));
-    }
-
     private static void checkPermission(final Permission permission) {
         if (SECURITY_MANAGER != null) {
             SECURITY_MANAGER.checkPermission(permission);
@@ -53,18 +48,26 @@ public class Activator implements Bundle
     }
 
     private void loadProvider(final Bundle bundle) {
+        if (bundle.getState() == Bundle.UNINSTALLED) {
+            return;
+        }
         try {
             checkPermission(new AdminPermission(bundle, AdminPermission.RESOURCE));
             final URL url = bundle.getEntry(ProviderUtil.PROVIDER_RESOURCE);
             if (url != null) {
                 checkPermission(new AdminPermission(bundle, AdminPermission.CLASS));
-                ProviderUtil.loadProvider(url, new BundleDelegatingClassLoader(bundle));
+                ProviderUtil.loadProvider(url, getBundleClassLoader(bundle));
             }
         } catch (final Exception e) {
             LOGGER.warn("Problem checking bundle {} for Log4j 2 provider.", bundle.getSymbolicName(),
e);
         }
     }
 
+    private static ClassLoader getBundleClassLoader(Bundle bundle) {
+        final BundleWiring wiring = bundle.adapt(BundleWiring.class);
+        return wiring.getClassLoader();
+    }
+
     @Override
     public void start(final BundleContext context) throws Exception {
         context.addBundleListener(this);
@@ -76,7 +79,7 @@ public class Activator implements Bundle
 
     @Override
     public void stop(final BundleContext context) throws Exception {
-
+        context.removeBundleListener(this);
     }
 
     @Override
@@ -92,30 +95,4 @@ public class Activator implements Bundle
         }
     }
 
-    /**
-     * ClassLoader that delegates to an OSGi Bundle.
-     */
-    public static class BundleDelegatingClassLoader extends ClassLoader {
-
-        private final Bundle bundle;
-
-        public BundleDelegatingClassLoader(final Bundle bundle) {
-            this.bundle = bundle;
-        }
-
-        @Override
-        protected Class<?> findClass(String name) throws ClassNotFoundException {
-            return bundle.loadClass(name);
-        }
-
-        @Override
-        protected URL findResource(String name) {
-            return bundle.getResource(name);
-        }
-
-        @Override
-        protected Enumeration<URL> findResources(String name) throws IOException {
-            return bundle.getResources(name);
-        }
-    }
 }



Mime
View raw message