logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mattsic...@apache.org
Subject svn commit: r1611998 - in /logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j: spi/Provider.java util/ProviderUtil.java
Date Sun, 20 Jul 2014 02:12:13 GMT
Author: mattsicker
Date: Sun Jul 20 02:12:13 2014
New Revision: 1611998

URL: http://svn.apache.org/r1611998
Log:
Add ClassLoader to Provider along with additional methods.

  - Now a Provider can be added outside ProviderUtil which specifies a particular ClassLoader
to use.
  - Provides methods for loading the LoggerContextFactory and ThreadContextMap classes specified
by a Provider.
  - Added javadocs for Provider methods.
  - Delegated ProviderUtil.findClassLoader() to LoaderUtil.
  - Part of updated OSGi support.

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

Modified: logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/spi/Provider.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/spi/Provider.java?rev=1611998&r1=1611997&r2=1611998&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/spi/Provider.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/spi/Provider.java
Sun Jul 20 02:12:13 2014
@@ -19,40 +19,122 @@ package org.apache.logging.log4j.spi;
 import java.net.URL;
 import java.util.Properties;
 
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.status.StatusLogger;
+
 /**
- *
+ * Model class for a Log4j 2 provider. The properties in this class correspond to the properties
used in a
+ * {@code META-INF/log4j-provider.properties} file. Note that this class is automatically
created by Log4j and should
+ * not be used by providers.
  */
 public class Provider {
     private static final Integer DEFAULT_PRIORITY = Integer.valueOf(-1);
-    private static final String FACTORY_PRIORITY = "FactoryPriority";
-    private static final String THREAD_CONTEXT_MAP = "ThreadContextMap";
-    private static final String LOGGER_CONTEXT_FACTORY = "LoggerContextFactory";
+    /**
+     * Property name to set for a Log4j 2 provider to specify the priority of this implementation.
+     */
+    public static final String FACTORY_PRIORITY = "FactoryPriority";
+    /**
+     * Property name to set to the implementation of {@link org.apache.logging.log4j.spi.ThreadContextMap}.
+     */
+    public static final String THREAD_CONTEXT_MAP = "ThreadContextMap";
+    /**
+     * Property name to set to the implementation of {@link org.apache.logging.log4j.spi.LoggerContextFactory}.
+     */
+    public static final String LOGGER_CONTEXT_FACTORY = "LoggerContextFactory";
+
+    private static final Logger LOGGER = StatusLogger.getLogger();
 
     private final Integer priority;
     private final String className;
     private final String threadContextMap;
     private final URL url;
+    private final ClassLoader classLoader;
 
-    public Provider(final Properties props, final URL url) {
+    public Provider(final Properties props, final URL url, final ClassLoader classLoader)
{
         this.url = url;
+        this.classLoader = classLoader;
         final String weight = props.getProperty(FACTORY_PRIORITY);
         priority = weight == null ? DEFAULT_PRIORITY : Integer.valueOf(weight);
         className = props.getProperty(LOGGER_CONTEXT_FACTORY);
         threadContextMap = props.getProperty(THREAD_CONTEXT_MAP);
     }
 
+    /**
+     * Gets the priority (natural ordering) of this Provider.
+     *
+     * @return the priority of this Provider
+     */
     public Integer getPriority() {
         return priority;
     }
 
+    /**
+     * Gets the class name of the {@link org.apache.logging.log4j.spi.LoggerContextFactory}
implementation of this
+     * Provider.
+     *
+     * @return the class name of a LoggerContextFactory implementation
+     */
     public String getClassName() {
         return className;
     }
 
+    /**
+     * Loads the {@link org.apache.logging.log4j.spi.LoggerContextFactory} class specified
by this Provider.
+     *
+     * @return the LoggerContextFactory implementation class or {@code null} if there was
an error loading it
+     */
+    @SuppressWarnings("unchecked")
+    public Class<? extends LoggerContextFactory> loadLoggerContextFactory() {
+        if (className == null) {
+            return null;
+        }
+        try {
+            final Class<?> clazz = classLoader.loadClass(className);
+            if (LoggerContextFactory.class.isAssignableFrom(clazz)) {
+                return (Class<? extends LoggerContextFactory>) clazz;
+            }
+        } catch (final Exception e) {
+            LOGGER.error("Unable to create class {} specified in {}", className, url.toString(),
e);
+        }
+        return null;
+    }
+
+    /**
+     * Gets the class name of the {@link org.apache.logging.log4j.spi.ThreadContextMap} implementation
of this
+     * Provider.
+     *
+     * @return the class name of a ThreadContextMap implementation
+     */
     public String getThreadContextMap() {
         return threadContextMap;
     }
 
+    /**
+     * Loads the {@link org.apache.logging.log4j.spi.ThreadContextMap} class specified by
this Provider.
+     *
+     * @return the ThreadContextMap implementation class or {@code null} if there was an
error loading it
+     */
+    @SuppressWarnings("unchecked")
+    public Class<? extends ThreadContextMap> loadThreadContextMap() {
+        if (threadContextMap == null) {
+            return null;
+        }
+        try {
+            final Class<?> clazz = classLoader.loadClass(threadContextMap);
+            if (ThreadContextMap.class.isAssignableFrom(clazz)) {
+                return (Class<? extends ThreadContextMap>) clazz;
+            }
+        } catch (final Exception e) {
+            LOGGER.error("Unable to create class {} specified in {}", threadContextMap, url.toString(),
e);
+        }
+        return null;
+    }
+
+    /**
+     * Gets the URL containing this Provider's Log4j details.
+     *
+     * @return the URL corresponding to the Provider {@code META-INF/log4j-provider.properties}
file
+     */
     public URL getUrl() {
         return url;
     }

Modified: logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/util/ProviderUtil.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/util/ProviderUtil.java?rev=1611998&r1=1611997&r2=1611998&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/util/ProviderUtil.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/util/ProviderUtil.java
Sun Jul 20 02:12:13 2014
@@ -18,10 +18,10 @@ package org.apache.logging.log4j.util;
 
 import java.io.IOException;
 import java.net.URL;
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Enumeration;
 import java.util.Properties;
+import java.util.concurrent.CopyOnWriteArraySet;
 
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.spi.Provider;
@@ -32,7 +32,10 @@ import org.apache.logging.log4j.status.S
  */
 public final class ProviderUtil {
 
-    private static final String PROVIDER_RESOURCE = "META-INF/log4j-provider.properties";
+    /**
+     * Resource name for a Log4j 2 provider properties file.
+     */
+    protected static final String PROVIDER_RESOURCE = "META-INF/log4j-provider.properties";
     private static final String API_VERSION = "Log4jAPIVersion";
 
     private static final String[] COMPATIBLE_API_VERSIONS = {
@@ -41,7 +44,7 @@ public final class ProviderUtil {
 
     private static final Logger LOGGER = StatusLogger.getLogger();
 
-    private static final Collection<Provider> PROVIDERS = new ArrayList<Provider>();
+    private static final Collection<Provider> PROVIDERS = new CopyOnWriteArraySet<Provider>();
 
     private ProviderUtil() {
     }
@@ -54,26 +57,36 @@ public final class ProviderUtil {
         } catch (final IOException e) {
             LOGGER.fatal("Unable to locate {}", PROVIDER_RESOURCE, e);
         }
-        loadProviders(enumResources);
+        loadProviders(enumResources, cl);
     }
 
-    protected static void loadProviders(final Enumeration<URL> enumResources) {
+    protected static void loadProviders(final Enumeration<URL> enumResources, ClassLoader
cl) {
         if (enumResources != null) {
             while (enumResources.hasMoreElements()) {
                 final URL url = enumResources.nextElement();
-                try {
-                    final Properties props = PropertiesUtil.loadClose(url.openStream(), url);
-                    if (!validVersion(props.getProperty(API_VERSION))) {
-                        continue;
-                    }
-                    PROVIDERS.add(new Provider(props, url));
-                } catch (final IOException ioe) {
-                    LOGGER.error("Unable to open {}", url, ioe);
-                }
+                loadProvider(url, cl);
             }
         }
     }
 
+    /**
+     * Loads an individual Provider implementation. This method is really only useful for
the OSGi bundle activator
+     * and this class itself.
+     *
+     * @param url the URL to the provider properties file
+     * @param cl the ClassLoader to load the provider classes with
+     */
+    protected static void loadProvider(final URL url, final ClassLoader cl) {
+        try {
+            final Properties props = PropertiesUtil.loadClose(url.openStream(), url);
+            if (validVersion(props.getProperty(API_VERSION))) {
+                PROVIDERS.add(new Provider(props, url, cl));
+            }
+        } catch (final IOException e) {
+            LOGGER.error("Unable to open {}", url, e);
+        }
+    }
+
     public static Iterable<Provider> getProviders() {
         return PROVIDERS;
     }
@@ -83,24 +96,7 @@ public final class ProviderUtil {
     }
 
     public static ClassLoader findClassLoader() {
-        ClassLoader cl;
-        if (System.getSecurityManager() == null) {
-            cl = Thread.currentThread().getContextClassLoader();
-        } else {
-            cl = java.security.AccessController.doPrivileged(
-                new java.security.PrivilegedAction<ClassLoader>() {
-                    @Override
-                    public ClassLoader run() {
-                        return Thread.currentThread().getContextClassLoader();
-                    }
-                }
-            );
-        }
-        if (cl == null) {
-            cl = ProviderUtil.class.getClassLoader();
-        }
-
-        return cl;
+        return LoaderUtil.getThreadContextClassLoader();
     }
 
     private static boolean validVersion(final String version) {



Mime
View raw message