Skip to content

Commit

Permalink
*fix spi classloader
Browse files Browse the repository at this point in the history
Signed-off-by: provenceee <[email protected]>
  • Loading branch information
provenceee committed Nov 18, 2024
1 parent 531fb32 commit 9c8d7e5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ private ClassUtils() {
*
* @param className Class fully qualified name
* @param classLoader The classloader of the enhanced class
* @param classResourceLoader The classResourceLoader of the enhanced class
* @return defined class
*/
public static Optional<Class<?>> defineClass(String className, ClassLoader classLoader) {
if (classLoader == null || className == null) {
public static Optional<Class<?>> defineClass(String className, ClassLoader classLoader,
ClassLoader classResourceLoader) {
if (classLoader == null || className == null || classResourceLoader == null) {
return Optional.empty();
}
try {
return Optional.of(ClassLoaderUtils.defineClass(className, classLoader, ClassLoaderUtils
.getClassResource(ClassLoader.getSystemClassLoader(), className)));
.getClassResource(classResourceLoader, className)));
} catch (InvocationTargetException ex) {
LOGGER.fine(String.format(Locale.ENGLISH, "Can not define class [%s], reason: [%s], may be it has been "
+ "defined, so try to load it!", className, ex.getMessage()));
Expand All @@ -64,6 +66,17 @@ public static Optional<Class<?>> defineClass(String className, ClassLoader class
return loadClass(className, classLoader);
}

/**
* Load the class by specifying the classloader
*
* @param className Class fully qualified name
* @param classLoader The classloader of the enhanced class
* @return defined class
*/
public static Optional<Class<?>> defineClass(String className, ClassLoader classLoader) {
return defineClass(className, classLoader, ClassLoader.getSystemClassLoader());
}

/**
* load class
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,18 @@ protected ExecuteContext doAfter(ExecuteContext context) {
final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
if (APACHE_DUBBO_CLUSTER_CLASS_NAME.equals(type.getName())) {
ClassUtils.defineClass(
"io.sermant.flowcontrol.retry.cluster.ApacheDubboClusterInvoker", contextClassLoader);
"io.sermant.flowcontrol.retry.cluster.ApacheDubboClusterInvoker", contextClassLoader,
contextClassLoader);
retryInvokerClass = ClassUtils.defineClass(
"io.sermant.flowcontrol.retry.cluster.ApacheDubboCluster", contextClassLoader);
"io.sermant.flowcontrol.retry.cluster.ApacheDubboCluster", contextClassLoader,
contextClassLoader);
} else if (ALIBABA_DUBBO_CLUSTER_CLASS_NAME.equals(type.getName())) {
ClassUtils.defineClass(
"io.sermant.flowcontrol.retry.cluster.AlibabaDubboClusterInvoker", contextClassLoader);
"io.sermant.flowcontrol.retry.cluster.AlibabaDubboClusterInvoker", contextClassLoader,
contextClassLoader);
retryInvokerClass = ClassUtils.defineClass(
"io.sermant.flowcontrol.retry.cluster.AlibabaDubboCluster", contextClassLoader);
"io.sermant.flowcontrol.retry.cluster.AlibabaDubboCluster", contextClassLoader,
contextClassLoader);
} else {
return context;
}
Expand Down

0 comments on commit 9c8d7e5

Please sign in to comment.