Skip to content

Commit

Permalink
Issue #315: ThreadContextClassLoader ignored by ResourceManager when …
Browse files Browse the repository at this point in the history
…extension classloader is set

- Consider TCCL when looking for class
- Improved exception where we hook up the suppressed exceptions from all the classloaders that failed and where the error message includes the classloaders as well
- Deprecated two methods that are no longer used or should no longer be used because they would only take a single classloader into account instead of trying multiple
  • Loading branch information
reckart committed Jun 29, 2023
1 parent e0482e6 commit 0ebf16d
Showing 1 changed file with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

package org.apache.uima.internal.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.uima.resource.Resource;
import org.apache.uima.resource.ResourceManager;
Expand Down Expand Up @@ -52,11 +56,49 @@ static public <T> Class<T> forName(String className, ResourceManager rm)

static public <T> Class<T> forName(String className, ResourceManager rm, boolean resolve)
throws ClassNotFoundException {
List<ClassLoader> clsTried = new ArrayList<>();
List<ClassNotFoundException> suppressedExceptions = new ArrayList<>();

// Try extension classloader
if (rm != null) {
ClassLoader excl = rm.getExtensionClassLoader();

if (excl != null) {
try {
return (Class<T>) Class.forName(className, resolve, excl);
}
catch (ClassNotFoundException e) {
clsTried.add(excl);
suppressedExceptions.add(e);
}
}
}

// Try TCCL
ClassLoader tccl = Thread.currentThread().getContextClassLoader();
if (tccl != null) {
try {
return (Class<T>) Class.forName(className, resolve, tccl);
}
catch (ClassNotFoundException e) {
clsTried.add(tccl);
suppressedExceptions.add(e);
}
}

try {
return (Class<T>) Class.forName(className, resolve, get_cl(rm));
} catch (ClassNotFoundException x) { //
return (Class<T>) Class.forName(className, resolve, Class_TCCL.class.getClassLoader());
}
catch (ClassNotFoundException e) {
clsTried.add(tccl);
suppressedExceptions.add(e);
}

ClassNotFoundException e = new ClassNotFoundException(
"Class [" + className + "] not found in any of the accessible classloaders "
+ clsTried.stream().map(Objects::toString).collect(Collectors.joining(", ")));
suppressedExceptions.forEach(e::addSuppressed);
throw e;
}

static public <T> Class<T> forName(String className, Map<String, Object> additionalParams)
Expand All @@ -67,6 +109,10 @@ static public <T> Class<T> forName(String className, Map<String, Object> additio
return forName(className, rm);
}

/**
* @deprecated Method should not be used and will be removed in a future version.
*/
@Deprecated
static public ClassLoader get_cl(ResourceManager rm) {

ClassLoader cl = (rm == null) ? null : rm.getExtensionClassLoader();
Expand All @@ -78,6 +124,10 @@ static public ClassLoader get_cl(ResourceManager rm) {
return cl;
}

/**
* @deprecated Method should not be used and will be removed in a future version.
*/
@Deprecated
static public ClassLoader get_parent_cl() {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
return (cl == null) ? Class_TCCL.class.getClassLoader() : cl;
Expand Down

0 comments on commit 0ebf16d

Please sign in to comment.