use of org.mule.runtime.module.artifact.api.classloader.exception.CompositeClassNotFoundException in project mule by mulesoft.
the class FineGrainedControlClassLoader method loadClass.
@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
Class<?> result = findLoadedClass(name);
if (result != null) {
return result;
}
final LookupStrategy lookupStrategy = lookupPolicy.getClassLookupStrategy(name);
if (lookupStrategy == null) {
throw new NullPointerException(format("Unable to find a lookup strategy for '%s' from %s", name, this));
}
if (verboseLogging) {
logLoadingClass(name, lookupStrategy, "Loading class '%s' with '%s' on '%s'", this);
}
// Gather information about the exceptions in each of the searched class loaders to provide
// troubleshooting information in case of throwing a ClassNotFoundException.
List<ClassNotFoundException> exceptions = new ArrayList<>();
for (ClassLoader classLoader : lookupStrategy.getClassLoaders(this)) {
try {
if (classLoader == this) {
result = findLocalClass(name);
break;
} else {
result = findParentClass(name, classLoader);
break;
}
} catch (ClassNotFoundException e) {
exceptions.add(e);
}
}
if (result == null) {
throw new CompositeClassNotFoundException(name, lookupStrategy, exceptions);
}
if (verboseLogging) {
logLoadedClass(name, result);
}
if (resolve) {
resolveClass(result);
}
return result;
}
use of org.mule.runtime.module.artifact.api.classloader.exception.CompositeClassNotFoundException in project mule by mulesoft.
the class FineGrainedControlClassLoaderTestCase method usesParentFirstAndChildLookupAndFails.
@Test
public void usesParentFirstAndChildLookupAndFails() throws Exception {
ClassLoader parent = Thread.currentThread().getContextClassLoader();
final ClassLoaderLookupPolicy lookupPolicy = mock(ClassLoaderLookupPolicy.class);
when(lookupPolicy.getClassLookupStrategy(TEST_CLASS_NAME)).thenReturn(PARENT_FIRST);
expected.expect(CompositeClassNotFoundException.class);
expected.expectMessage(startsWith("Cannot load class '" + TEST_CLASS_NAME + "': ["));
FineGrainedControlClassLoader ext = buildFineGrainedControlClassLoader(parent, lookupPolicy);
expected.expect(expressionMatches((e) -> ((CompositeClassNotFoundException) e).getExceptions(), contains(hasMessage(is(TEST_CLASS_NAME)), expressionMatches((e) -> ((TestClassNotFoundException) e).getClassLoader(), is((ClassLoader) ext)))));
invokeTestClassMethod(ext);
}
use of org.mule.runtime.module.artifact.api.classloader.exception.CompositeClassNotFoundException in project mule by mulesoft.
the class FineGrainedControlClassLoaderTestCase method usesParentOnlyLookupAndFails.
@Test
public void usesParentOnlyLookupAndFails() throws Exception {
ClassLoader parent = mock(ClassLoader.class);
final ClassNotFoundException thrownException = new ClassNotFoundException("ERROR");
when(parent.loadClass(TEST_CLASS_NAME)).thenThrow(thrownException);
final ClassLoaderLookupPolicy lookupPolicy = mock(ClassLoaderLookupPolicy.class);
when(lookupPolicy.getClassLookupStrategy(TEST_CLASS_NAME)).thenReturn(PARENT_ONLY);
expected.expect(CompositeClassNotFoundException.class);
expected.expectMessage(startsWith("Cannot load class '" + TEST_CLASS_NAME + "': [" + lineSeparator() + "\t" + "ERROR]"));
expected.expect(expressionMatches((e) -> ((CompositeClassNotFoundException) e).getExceptions(), contains(sameInstance(thrownException))));
FineGrainedControlClassLoader ext = new FineGrainedControlClassLoader(new URL[] { getChildFileResource() }, parent, lookupPolicy);
ext.loadClass(TEST_CLASS_NAME);
}
use of org.mule.runtime.module.artifact.api.classloader.exception.CompositeClassNotFoundException in project mule by mulesoft.
the class FineGrainedControlClassLoaderTestCase method usesChildFirstThenParentLookupAndFails.
@Test
public void usesChildFirstThenParentLookupAndFails() throws Exception {
ClassLoader parent = Thread.currentThread().getContextClassLoader();
final ClassLoaderLookupPolicy lookupPolicy = mock(ClassLoaderLookupPolicy.class);
when(lookupPolicy.getClassLookupStrategy(TEST_CLASS_NAME)).thenReturn(CHILD_FIRST);
expected.expect(CompositeClassNotFoundException.class);
expected.expectMessage(startsWith("Cannot load class '" + TEST_CLASS_NAME + "': ["));
FineGrainedControlClassLoader ext = buildFineGrainedControlClassLoader(parent, lookupPolicy);
expected.expect(expressionMatches((e) -> ((CompositeClassNotFoundException) e).getExceptions(), contains(expressionMatches((e) -> ((TestClassNotFoundException) e).getClassLoader(), is((ClassLoader) ext)), hasMessage(is(TEST_CLASS_NAME)))));
invokeTestClassMethod(ext);
}
Aggregations