logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mattsic...@apache.org
Subject svn commit: r1601024 - in /logging/log4j/log4j2/trunk: log4j-core/src/main/java/org/apache/logging/log4j/core/util/Loader.java src/changes/changes.xml
Date Fri, 06 Jun 2014 22:20:45 GMT
Author: mattsicker
Date: Fri Jun  6 22:20:44 2014
New Revision: 1601024

URL: http://svn.apache.org/r1601024
Log:
Fix NPE in Loader.getTCL() as reported in LOG4J2-640.

Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Loader.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Loader.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Loader.java?rev=1601024&r1=1601023&r2=1601024&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Loader.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Loader.java
Fri Jun  6 22:20:44 2014
@@ -20,6 +20,8 @@ package org.apache.logging.log4j.core.ut
 import java.io.InputStream;
 import java.lang.reflect.InvocationTargetException;
 import java.net.URL;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.status.StatusLogger;
@@ -36,6 +38,9 @@ public final class Loader {
 
     private static final String TSTR = "Caught Exception while in Loader.getResource. This
may be innocuous.";
 
+    private static final PrivilegedAction<ClassLoader> THREAD_CONTEXT_CLASS_LOADER_GETTER
=
+        new ThreadContextClassLoaderGetter();
+
     static {
         final String ignoreTCLProp = PropertiesUtil.getProperties().getStringProperty("log4j.ignoreTCL",
null);
         if (ignoreTCLProp != null) {
@@ -202,21 +207,18 @@ public final class Loader {
     }
 
     private static ClassLoader getTcl() {
-        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();
-                    }
-                }
-            );
-        }
+        return System.getSecurityManager() == null
+            ? THREAD_CONTEXT_CLASS_LOADER_GETTER.run()
+            : AccessController.doPrivileged(THREAD_CONTEXT_CLASS_LOADER_GETTER);
+    }
 
-        return cl;
+    private static class ThreadContextClassLoaderGetter implements PrivilegedAction<ClassLoader>
{
+        @Override
+        public ClassLoader run() {
+            final ClassLoader cl = Thread.currentThread().getContextClassLoader();
+            // if the TCCL is null, that means we're using the system CL
+            return cl == null ? ClassLoader.getSystemClassLoader() : cl;
+        }
     }
 
     private static boolean isChild(final ClassLoader loader1, final ClassLoader loader2)
{

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1601024&r1=1601023&r2=1601024&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Fri Jun  6 22:20:44 2014
@@ -22,6 +22,9 @@
   </properties>
   <body>
     <release version="2.0-rc2" date="2014-MM-DD" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-640" dev="mattsicker" type="fix">
+        Fix NPE that can be caused by a null ThreadContextClassLoader.
+      </action>
       <action issue="LOG4J2-655" dev="mattsicker" type="add">
         Add Vagrantfile for testing in GNU+Linux.
       </action>



Mime
View raw message