You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by am...@apache.org on 2017/06/12 14:26:17 UTC

cxf git commit: CXF-7349: Resolve NPE when TCCL is null

Repository: cxf
Updated Branches:
  refs/heads/3.1.x-fixes 01121feb0 -> 4fd67ac58


CXF-7349: Resolve NPE when TCCL is null

Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/4fd67ac5
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/4fd67ac5
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/4fd67ac5

Branch: refs/heads/3.1.x-fixes
Commit: 4fd67ac588d5452f5b01abd97962399fbe43faa1
Parents: 01121fe
Author: Andy McCright <an...@us.ibm.com>
Authored: Thu Apr 27 16:09:55 2017 -0500
Committer: Andy McCright <j....@gmail.com>
Committed: Mon Jun 12 09:25:48 2017 -0500

----------------------------------------------------------------------
 .../common/classloader/ClassLoaderUtils.java    | 21 +++---
 .../classloader/ClassLoaderUtilsTest.java       | 79 ++++++++++++++++++++
 2 files changed, 89 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/4fd67ac5/core/src/main/java/org/apache/cxf/common/classloader/ClassLoaderUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/common/classloader/ClassLoaderUtils.java b/core/src/main/java/org/apache/cxf/common/classloader/ClassLoaderUtils.java
index cc73cf8..90b0626 100644
--- a/core/src/main/java/org/apache/cxf/common/classloader/ClassLoaderUtils.java
+++ b/core/src/main/java/org/apache/cxf/common/classloader/ClassLoaderUtils.java
@@ -89,11 +89,10 @@ public final class ClassLoaderUtils {
      * @param callingClass The Class object of the calling object
      */
     public static URL getResource(String resourceName, Class<?> callingClass) {
-        URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName);
+        URL url = getContextClassLoader().getResource(resourceName);
         if (url == null && resourceName.startsWith("/")) {
             //certain classloaders need it without the leading /
-            url = Thread.currentThread().getContextClassLoader()
-                .getResource(resourceName.substring(1));
+            url = getContextClassLoader().getResource(resourceName.substring(1));
         }
 
         ClassLoader cluClassloader = ClassLoaderUtils.class.getClassLoader();
@@ -151,16 +150,14 @@ public final class ClassLoaderUtils {
             
         };
         try {
-            urls = Thread.currentThread().getContextClassLoader()
-                .getResources(resourceName);
+            urls = getContextClassLoader().getResources(resourceName);
         } catch (IOException e) {
             //ignore
         }
         if (!urls.hasMoreElements() && resourceName.startsWith("/")) {
             //certain classloaders need it without the leading /
             try {
-                urls = Thread.currentThread().getContextClassLoader()
-                    .getResources(resourceName.substring(1));
+                urls = getContextClassLoader().getResources(resourceName.substring(1));
             } catch (IOException e) {
                 // ignore
             }
@@ -295,16 +292,18 @@ public final class ClassLoaderUtils {
         }
     }
 
-    private static ClassLoader getContextClassLoader() {
+    static ClassLoader getContextClassLoader() {
         final SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
             return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
                 public ClassLoader run() {
-                    return Thread.currentThread().getContextClassLoader();
+                    ClassLoader loader = Thread.currentThread().getContextClassLoader();
+                    return loader != null ? loader : ClassLoader.getSystemClassLoader();
                 }
             });
-        }
-        return Thread.currentThread().getContextClassLoader();
+        } 
+        ClassLoader loader = Thread.currentThread().getContextClassLoader();
+        return loader != null ? loader : ClassLoader.getSystemClassLoader();
     }
 
     private static ClassLoader getClassLoader(final Class<?> clazz) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/4fd67ac5/core/src/test/java/org/apache/cxf/common/classloader/ClassLoaderUtilsTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/cxf/common/classloader/ClassLoaderUtilsTest.java b/core/src/test/java/org/apache/cxf/common/classloader/ClassLoaderUtilsTest.java
new file mode 100644
index 0000000..8e1fa18
--- /dev/null
+++ b/core/src/test/java/org/apache/cxf/common/classloader/ClassLoaderUtilsTest.java
@@ -0,0 +1,79 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.common.classloader;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class ClassLoaderUtilsTest extends Assert {
+
+    private static void setTCCL(ClassLoader loader) {
+        Thread.currentThread().setContextClassLoader(loader);
+    }
+
+    /**
+     * This test confirms that the expected thread context classloader
+     * is returned from the getContextClassLoader method.
+     */
+    @Test
+    public void getContextClassLoader() throws MalformedURLException {
+        final ClassLoader nullLoader = null;
+        final ClassLoader jvmAppLoader = ClassLoader.getSystemClassLoader();
+        final ClassLoader jvmExtLoader = jvmAppLoader.getParent();
+        final ClassLoader testClassLoader = ClassLoaderUtilsTest.class.getClassLoader();
+        final ClassLoader clildLoader = new URLClassLoader(new URL[]{new URL("file:/.")});
+        final ClassLoader previousTCCL = Thread.currentThread().getContextClassLoader();
+
+        try {
+            // TCCL = null
+            setTCCL(nullLoader);
+            assertEquals("TCCL == null; wrong loader returned; expected JVM App loader", 
+                         jvmAppLoader, ClassLoaderUtils.getContextClassLoader());
+
+            // TCCL = JVM App CL
+            setTCCL(jvmAppLoader);
+            assertEquals("TCCL == JVM App loader; wrong loader returned; expected JVM App loader",
+                         jvmAppLoader, ClassLoaderUtils.getContextClassLoader());
+
+            // TCCL = JVM Ext CL
+            setTCCL(jvmExtLoader);
+            assertEquals("TCCL == JVM Ext loader; wrong loader returned; expected JVM Ext loader",
+                         jvmExtLoader, ClassLoaderUtils.getContextClassLoader());
+
+            // TCCL = This test class loader (which is likely also the JVM App CL)
+            setTCCL(testClassLoader);
+            assertEquals("TCCL == this test laoder; wrong loader returned; expected JVM App loader",
+                         testClassLoader, ClassLoaderUtils.getContextClassLoader());
+
+            // TCCL = a random child classloader
+            setTCCL(clildLoader);
+            assertEquals("TCCL == random child loader, wrong loader returned; expected child of test class loader",
+                         clildLoader, ClassLoaderUtils.getContextClassLoader());
+
+        } finally {
+            // reset the TCCL for other tests
+            setTCCL(previousTCCL);
+        }
+    }
+}