sqoop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jar...@apache.org
Subject sqoop git commit: SQOOP-2602: Sqoop2: Static initialization of ClassUtils defaultClassloader is fragile
Date Mon, 05 Oct 2015 23:15:28 GMT
Repository: sqoop
Updated Branches:
  refs/heads/sqoop2 a0ebf8f29 -> 89be1f723


SQOOP-2602: Sqoop2: Static initialization of ClassUtils defaultClassloader is fragile

(Abraham Fine via Jarek Jarcec Cecho)


Project: http://git-wip-us.apache.org/repos/asf/sqoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/sqoop/commit/89be1f72
Tree: http://git-wip-us.apache.org/repos/asf/sqoop/tree/89be1f72
Diff: http://git-wip-us.apache.org/repos/asf/sqoop/diff/89be1f72

Branch: refs/heads/sqoop2
Commit: 89be1f7237277d4038660913259a8965c33c4011
Parents: a0ebf8f
Author: Jarek Jarcec Cecho <jarcec@apache.org>
Authored: Mon Oct 5 16:15:04 2015 -0700
Committer: Jarek Jarcec Cecho <jarcec@apache.org>
Committed: Mon Oct 5 16:15:04 2015 -0700

----------------------------------------------------------------------
 .../java/org/apache/sqoop/utils/ClassUtils.java | 24 ++++++++------------
 .../org/apache/sqoop/utils/TestClassUtils.java  |  3 +--
 2 files changed, 11 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/sqoop/blob/89be1f72/common/src/main/java/org/apache/sqoop/utils/ClassUtils.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/utils/ClassUtils.java b/common/src/main/java/org/apache/sqoop/utils/ClassUtils.java
index ca43848..ed68988 100644
--- a/common/src/main/java/org/apache/sqoop/utils/ClassUtils.java
+++ b/common/src/main/java/org/apache/sqoop/utils/ClassUtils.java
@@ -50,14 +50,6 @@ public final class ClassUtils {
   private static final Class<?> NEGATIVE_CACHE_SENTINEL =
     NegativeCacheSentinel.class;
 
-  private static ClassLoader defaultClassLoader;
-  static {
-    defaultClassLoader = Thread.currentThread().getContextClassLoader();
-    if (defaultClassLoader == null) {
-      defaultClassLoader = ClassUtils.class.getClassLoader();
-    }
-  }
-
   /**
    * A unique class which is used as a sentinel value in the caching
    * for loadClass.
@@ -74,7 +66,7 @@ public final class ClassUtils {
    * @return Class instance or NULL
    */
   public static Class<?> loadClass(String className) {
-    return loadClassWithClassLoader(className, defaultClassLoader);
+    return loadClassWithClassLoader(className, getClassLoader());
   }
 
   /**
@@ -139,7 +131,7 @@ public final class ClassUtils {
    * @return Instance of new class or NULL in case of any error
    */
   public static Object instantiate(String className, Object ... args) {
-    return instantiateWithClassLoader(className, defaultClassLoader, args);
+    return instantiateWithClassLoader(className, getClassLoader(), args);
   }
 
   /**
@@ -196,7 +188,7 @@ public final class ClassUtils {
    * @return Path on local filesystem to jar where given jar is present
    */
   public static String jarForClass(String className) {
-    return jarForClassWithClassLoader(className, defaultClassLoader);
+    return jarForClassWithClassLoader(className, getClassLoader());
   }
 
   /**
@@ -223,7 +215,7 @@ public final class ClassUtils {
     String jarPath = null;
     String class_file = klass.getName().replaceAll("\\.", "/") + ".class";
     try {
-      URL url = defaultClassLoader.getResource(class_file);
+      URL url = getClassLoader().getResource(class_file);
       String path = url.getPath();
       path = URLDecoder.decode(path, "UTF-8");
       if ("jar".equals(url.getProtocol())) {
@@ -271,8 +263,12 @@ public final class ClassUtils {
     CACHE_CLASSES.clear();
   }
 
-  public static void setDefaultClassLoader(ClassLoader classLoader) {
-    defaultClassLoader = classLoader;
+  public static ClassLoader getClassLoader() {
+    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+    if (classLoader == null) {
+      classLoader = ClassUtils.class.getClassLoader();
+    }
+    return classLoader;
   }
 
   private ClassUtils() {

http://git-wip-us.apache.org/repos/asf/sqoop/blob/89be1f72/common/src/test/java/org/apache/sqoop/utils/TestClassUtils.java
----------------------------------------------------------------------
diff --git a/common/src/test/java/org/apache/sqoop/utils/TestClassUtils.java b/common/src/test/java/org/apache/sqoop/utils/TestClassUtils.java
index 8851440..674ae6a 100644
--- a/common/src/test/java/org/apache/sqoop/utils/TestClassUtils.java
+++ b/common/src/test/java/org/apache/sqoop/utils/TestClassUtils.java
@@ -60,10 +60,10 @@ public class TestClassUtils {
     File jarFile = compileAJar();
     URL[] urlArray = { jarFile.toURI().toURL() };
     URLClassLoader newClassLoader = new URLClassLoader(urlArray, classLoader);
-    ClassUtils.setDefaultClassLoader(newClassLoader);
     testAClass = newClassLoader.loadClass("A");
     testParentClass = newClassLoader.loadClass("Parent");
     testChildClass = newClassLoader.loadClass("Child");
+    Thread.currentThread().setContextClassLoader(newClassLoader);
   }
 
   private File compileAJar() throws Exception{
@@ -159,7 +159,6 @@ public class TestClassUtils {
   @AfterMethod
   public void restoreClassLoader() {
     Thread.currentThread().setContextClassLoader(classLoader);
-    ClassUtils.setDefaultClassLoader(classLoader);
   }
 
   @Test


Mime
View raw message