kafka-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From guozh...@apache.org
Subject kafka git commit: KAFKA-2295; Support dynamically loaded classes from context class loader
Date Thu, 15 Oct 2015 18:06:53 GMT
Repository: kafka
Updated Branches:
  refs/heads/trunk c50d39ea8 -> a4dbf9010


KAFKA-2295; Support dynamically loaded classes from context class loader

Rebased code..

Author: Manikumar reddy O <manikumar.reddy@gmail.com>

Reviewers: Guozhang Wang

Closes #314 from omkreddy/KAFKA-2295


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

Branch: refs/heads/trunk
Commit: a4dbf9010754183965772066a9cd2c5c4a49d44a
Parents: c50d39e
Author: Manikumar reddy O <manikumar.reddy@gmail.com>
Authored: Thu Oct 15 11:11:32 2015 -0700
Committer: Guozhang Wang <wangguoz@gmail.com>
Committed: Thu Oct 15 11:11:32 2015 -0700

----------------------------------------------------------------------
 .../kafka/common/config/AbstractConfig.java     | 11 ++++-----
 .../apache/kafka/common/config/ConfigDef.java   |  2 +-
 .../org/apache/kafka/common/utils/Utils.java    | 24 +++++++++++++++++++-
 .../kafka/common/config/AbstractConfigTest.java |  3 ++-
 core/src/main/scala/kafka/utils/CoreUtils.scala |  2 +-
 5 files changed, 31 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kafka/blob/a4dbf901/clients/src/main/java/org/apache/kafka/common/config/AbstractConfig.java
----------------------------------------------------------------------
diff --git a/clients/src/main/java/org/apache/kafka/common/config/AbstractConfig.java b/clients/src/main/java/org/apache/kafka/common/config/AbstractConfig.java
index 2961e09..1dae61c 100644
--- a/clients/src/main/java/org/apache/kafka/common/config/AbstractConfig.java
+++ b/clients/src/main/java/org/apache/kafka/common/config/AbstractConfig.java
@@ -185,17 +185,14 @@ public class AbstractConfig {
         List<String> klasses = getList(key);
         List<T> objects = new ArrayList<T>();
         for (String klass : klasses) {
-            Class<?> c;
+            Object o;
             try {
-                c = Class.forName(klass);
+                o = Utils.newInstance(klass, t);
             } catch (ClassNotFoundException e) {
-                throw new ConfigException(key, klass, "Class " + klass + " could not be found.");
+                throw new KafkaException(klass + " ClassNotFoundException exception occured",
e);
             }
-            if (c == null)
-                return null;
-            Object o = Utils.newInstance(c);
             if (!t.isInstance(o))
-                throw new KafkaException(c.getName() + " is not an instance of " + t.getName());
+                throw new KafkaException(klass + " is not an instance of " + t.getName());
             if (o instanceof Configurable)
                 ((Configurable) o).configure(this.originals);
             objects.add(t.cast(o));

http://git-wip-us.apache.org/repos/asf/kafka/blob/a4dbf901/clients/src/main/java/org/apache/kafka/common/config/ConfigDef.java
----------------------------------------------------------------------
diff --git a/clients/src/main/java/org/apache/kafka/common/config/ConfigDef.java b/clients/src/main/java/org/apache/kafka/common/config/ConfigDef.java
index 168990f..2e820dd 100644
--- a/clients/src/main/java/org/apache/kafka/common/config/ConfigDef.java
+++ b/clients/src/main/java/org/apache/kafka/common/config/ConfigDef.java
@@ -263,7 +263,7 @@ public class ConfigDef {
                     if (value instanceof Class)
                         return (Class<?>) value;
                     else if (value instanceof String)
-                        return Class.forName(trimmed);
+                        return Class.forName(trimmed, true, Utils.getContextOrKafkaClassLoader());
                     else
                         throw new ConfigException(name, value, "Expected a Class instance
or class name.");
                 default:

http://git-wip-us.apache.org/repos/asf/kafka/blob/a4dbf901/clients/src/main/java/org/apache/kafka/common/utils/Utils.java
----------------------------------------------------------------------
diff --git a/clients/src/main/java/org/apache/kafka/common/utils/Utils.java b/clients/src/main/java/org/apache/kafka/common/utils/Utils.java
index aee379a..a7a2968 100755
--- a/clients/src/main/java/org/apache/kafka/common/utils/Utils.java
+++ b/clients/src/main/java/org/apache/kafka/common/utils/Utils.java
@@ -296,7 +296,7 @@ public class Utils {
      * @return the new instance
      */
     public static <T> T newInstance(String klass, Class<T> base) throws ClassNotFoundException
{
-        return Utils.newInstance(Class.forName(klass).asSubclass(base));
+        return Utils.newInstance(Class.forName(klass, true, Utils.getContextOrKafkaClassLoader()).asSubclass(base));
     }
 
     /**
@@ -578,4 +578,26 @@ public class Utils {
     public static <T> List<T> safe(List<T> other) {
         return other == null ? Collections.<T>emptyList() : other;
     }
+
+   /**
+    * Get the ClassLoader which loaded Kafka.
+    */
+    public static ClassLoader getKafkaClassLoader() {
+        return Utils.class.getClassLoader();
+    }
+
+    /**
+     * Get the Context ClassLoader on this thread or, if not present, the ClassLoader that
+     * loaded Kafka.
+     *
+     * This should be used whenever passing a ClassLoader to Class.forName
+     */
+    public static ClassLoader getContextOrKafkaClassLoader() {
+        ClassLoader cl = Thread.currentThread().getContextClassLoader();
+        if (cl == null)
+            return getKafkaClassLoader();
+        else
+            return cl;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/kafka/blob/a4dbf901/clients/src/test/java/org/apache/kafka/common/config/AbstractConfigTest.java
----------------------------------------------------------------------
diff --git a/clients/src/test/java/org/apache/kafka/common/config/AbstractConfigTest.java
b/clients/src/test/java/org/apache/kafka/common/config/AbstractConfigTest.java
index 28064ec..9698879 100644
--- a/clients/src/test/java/org/apache/kafka/common/config/AbstractConfigTest.java
+++ b/clients/src/test/java/org/apache/kafka/common/config/AbstractConfigTest.java
@@ -12,6 +12,7 @@
  */
 package org.apache.kafka.common.config;
 
+import org.apache.kafka.common.KafkaException;
 import org.apache.kafka.common.config.ConfigDef.Importance;
 import org.apache.kafka.common.config.ConfigDef.Type;
 import org.apache.kafka.common.metrics.MetricsReporter;
@@ -66,7 +67,7 @@ public class AbstractConfigTest {
         try {
             config.getConfiguredInstances(TestConfig.METRIC_REPORTER_CLASSES_CONFIG, MetricsReporter.class);
             fail("Expected a config exception due to invalid props :" + props);
-        } catch (ConfigException e) {
+        } catch (KafkaException e) {
             // this is good
         }
     }

http://git-wip-us.apache.org/repos/asf/kafka/blob/a4dbf901/core/src/main/scala/kafka/utils/CoreUtils.scala
----------------------------------------------------------------------
diff --git a/core/src/main/scala/kafka/utils/CoreUtils.scala b/core/src/main/scala/kafka/utils/CoreUtils.scala
index 168a18d..e25468f 100755
--- a/core/src/main/scala/kafka/utils/CoreUtils.scala
+++ b/core/src/main/scala/kafka/utils/CoreUtils.scala
@@ -218,7 +218,7 @@ object CoreUtils extends Logging {
    * Create an instance of the class with the given class name
    */
   def createObject[T<:AnyRef](className: String, args: AnyRef*): T = {
-    val klass = Class.forName(className).asInstanceOf[Class[T]]
+    val klass = Class.forName(className, true, Utils.getContextOrKafkaClassLoader()).asInstanceOf[Class[T]]
     val constructor = klass.getConstructor(args.map(_.getClass): _*)
     constructor.newInstance(args: _*)
   }


Mime
View raw message