You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ratis.apache.org by sz...@apache.org on 2021/03/30 15:45:14 UTC

[ratis] branch master updated: RATIS-1351. Fix classloader mismatch while using ratis by fat-jar (#454)

This is an automated email from the ASF dual-hosted git repository.

szetszwo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ratis.git


The following commit(s) were added to refs/heads/master by this push:
     new f60fb15  RATIS-1351. Fix classloader mismatch while using ratis by fat-jar (#454)
f60fb15 is described below

commit f60fb15d6af72007f9ee8d5067e0701d67434ea8
Author: maobaolong <30...@qq.com>
AuthorDate: Tue Mar 30 23:45:05 2021 +0800

    RATIS-1351. Fix classloader mismatch while using ratis by fat-jar (#454)
---
 .../org/apache/ratis/util/ReflectionUtils.java     | 33 ++++++++++++++++++++--
 .../ratis/metrics/MetricRegistriesLoader.java      |  4 ++-
 2 files changed, 33 insertions(+), 4 deletions(-)

diff --git a/ratis-common/src/main/java/org/apache/ratis/util/ReflectionUtils.java b/ratis-common/src/main/java/org/apache/ratis/util/ReflectionUtils.java
index afd3817..6f05c9c 100644
--- a/ratis-common/src/main/java/org/apache/ratis/util/ReflectionUtils.java
+++ b/ratis-common/src/main/java/org/apache/ratis/util/ReflectionUtils.java
@@ -104,6 +104,19 @@ public interface ReflectionUtils {
    * @return the class object, or null if it could not be found.
    */
   static Class<?> getClassByNameOrNull(String name) {
+    return getClassByNameOrNull(name, null);
+  }
+
+  /**
+   * Load a class by name through a specific classloader, returning null rather
+   * than throwing an exception if it couldn't be loaded. This is to avoid the
+   * overhead of creating an exception.
+   *
+   * @param name the class name
+   * @param classLoader the classloader
+   * @return the class object, or null if it could not be found.
+   */
+  static Class<?> getClassByNameOrNull(String name, ClassLoader classLoader) {
     final Map<String, WeakReference<Class<?>>> map = Classes.getClassMap();
 
     Class<?> clazz = null;
@@ -114,7 +127,8 @@ public interface ReflectionUtils {
 
     if (clazz == null) {
       try {
-        clazz = Class.forName(name, true, Classes.CLASS_LOADER);
+        clazz = Class.forName(name, true,
+            classLoader != null ? classLoader : Classes.CLASS_LOADER);
       } catch (ClassNotFoundException e) {
         // Leave a marker that the class isn't found
         map.put(name, new WeakReference<>(Classes.NEGATIVE_CACHE_SENTINEL));
@@ -139,7 +153,20 @@ public interface ReflectionUtils {
    * @throws ClassNotFoundException if the class is not found.
    */
   static Class<?> getClassByName(String name) throws ClassNotFoundException {
-    Class<?> ret = getClassByNameOrNull(name);
+    return getClassByName(name, null);
+  }
+
+  /**
+   * Load a class by name through a specific classloader.
+   *
+   * @param name the class name.
+   * @param classLoader the classloader.
+   * @return the class object.
+   * @throws ClassNotFoundException if the class is not found.
+   */
+  static Class<?> getClassByName(String name, ClassLoader classLoader)
+      throws ClassNotFoundException {
+    Class<?> ret = getClassByNameOrNull(name, classLoader);
     if (ret == null) {
       throw new ClassNotFoundException("Class " + name + " not found");
     }
@@ -210,7 +237,7 @@ public interface ReflectionUtils {
 
   static <BASE> Class<? extends BASE> getClass(String subClassName, Class<BASE> base) {
     try {
-      return getClassByName(subClassName).asSubclass(base);
+      return getClassByName(subClassName, base.getClassLoader()).asSubclass(base);
     } catch (ClassNotFoundException e) {
       throw new IllegalArgumentException("Failed to get class "
           + subClassName + " as a subclass of " + base, e);
diff --git a/ratis-metrics/src/main/java/org/apache/ratis/metrics/MetricRegistriesLoader.java b/ratis-metrics/src/main/java/org/apache/ratis/metrics/MetricRegistriesLoader.java
index a60e253..37305fa 100644
--- a/ratis-metrics/src/main/java/org/apache/ratis/metrics/MetricRegistriesLoader.java
+++ b/ratis-metrics/src/main/java/org/apache/ratis/metrics/MetricRegistriesLoader.java
@@ -85,7 +85,9 @@ public final class MetricRegistriesLoader {
   }
 
   private static List<MetricRegistries> getDefinedImplemantations() {
-    ServiceLoader<MetricRegistries> loader = ServiceLoader.load(MetricRegistries.class);
+    ServiceLoader<MetricRegistries> loader = ServiceLoader.load(
+        MetricRegistries.class,
+        MetricRegistries.class.getClassLoader());
     List<MetricRegistries> availableFactories = new ArrayList<>();
     for (MetricRegistries impl : loader) {
       availableFactories.add(impl);