You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2014/09/08 00:38:54 UTC

[15/32] git commit: Fix circular class linkage in LoaderUtil.

Fix circular class linkage in LoaderUtil.

  - As mentioned in the comments, LoaderUtil cannot reference PropertiesUtil in its static initialization block as this will cause mass errors everywhere.


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/cb653024
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/cb653024
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/cb653024

Branch: refs/heads/master
Commit: cb653024181839703e370f9a58430ddb63709acb
Parents: 2a96237
Author: Matt Sicker <ma...@apache.org>
Authored: Mon Sep 1 23:21:27 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Mon Sep 1 23:21:27 2014 -0500

----------------------------------------------------------------------
 .../org/apache/logging/log4j/util/LoaderUtil.java  | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/cb653024/log4j-api/src/main/java/org/apache/logging/log4j/util/LoaderUtil.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/LoaderUtil.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/LoaderUtil.java
index 0771054..ed77589 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util/LoaderUtil.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/LoaderUtil.java
@@ -31,7 +31,9 @@ public final class LoaderUtil {
 
     public static final String IGNORE_TCCL_PROPERTY = "log4j.ignoreTCL";
 
-    private static final boolean IGNORE_TCCL;
+    // this variable must be lazily loaded; otherwise, we get a nice circular class loading problem where LoaderUtil
+    // wants to use PropertiesUtil, but then PropertiesUtil wants to use LoaderUtil.
+    private static Boolean ignoreTCCL;
 
     private static final PrivilegedAction<ClassLoader> TCCL_GETTER = new ThreadContextClassLoaderGetter();
 
@@ -40,8 +42,6 @@ public final class LoaderUtil {
         if (sm != null) {
             sm.checkPermission(new RuntimePermission("getClassLoader"));
         }
-        final String ignoreTccl = PropertiesUtil.getProperties().getStringProperty(IGNORE_TCCL_PROPERTY, null);
-        IGNORE_TCCL = ignoreTccl != null && !"false".equalsIgnoreCase(ignoreTccl.trim());
     }
 
     /**
@@ -73,7 +73,7 @@ public final class LoaderUtil {
      * @throws ClassNotFoundException if the specified class name could not be found
      */
     public static Class<?> loadClass(final String className) throws ClassNotFoundException {
-        if (IGNORE_TCCL) {
+        if (isIgnoreTccl()) {
             return Class.forName(className);
         }
         try {
@@ -124,4 +124,13 @@ public final class LoaderUtil {
         IllegalAccessException {
         return clazz.cast(newInstanceOf(className));
     }
+
+    private static boolean isIgnoreTccl() {
+        // we need to lazily initialize this, but concurrent access is not an issue
+        if (ignoreTCCL == null) {
+            final String ignoreTccl = PropertiesUtil.getProperties().getStringProperty(IGNORE_TCCL_PROPERTY, null);
+            ignoreTCCL = ignoreTccl != null && !"false".equalsIgnoreCase(ignoreTccl.trim());
+        }
+        return ignoreTCCL;
+    }
 }