Make TomcatEmbeddedWebappClassLoader parallel capable

Closes gh-10477
pull/10567/merge
Andy Wilkinson 7 years ago
parent 027c5a0e35
commit 77cbab794e

@ -21,12 +21,12 @@ import java.net.URL;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import org.apache.catalina.loader.WebappClassLoader; import org.apache.catalina.loader.ParallelWebappClassLoader;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
/** /**
* Extension of Tomcat's {@link WebappClassLoader} that does not consider the * Extension of Tomcat's {@link ParallelWebappClassLoader} that does not consider the
* {@link ClassLoader#getSystemClassLoader() system classloader}. This is required to * {@link ClassLoader#getSystemClassLoader() system classloader}. This is required to
* ensure that any custom context class loader is always used (as is the case with some * ensure that any custom context class loader is always used (as is the case with some
* executable archives). * executable archives).
@ -34,11 +34,15 @@ import org.apache.commons.logging.LogFactory;
* @author Phillip Webb * @author Phillip Webb
* @since 2.0.0 * @since 2.0.0
*/ */
public class TomcatEmbeddedWebappClassLoader extends WebappClassLoader { public class TomcatEmbeddedWebappClassLoader extends ParallelWebappClassLoader {
private static final Log logger = LogFactory private static final Log logger = LogFactory
.getLog(TomcatEmbeddedWebappClassLoader.class); .getLog(TomcatEmbeddedWebappClassLoader.class);
static {
ClassLoader.registerAsParallelCapable();
}
public TomcatEmbeddedWebappClassLoader() { public TomcatEmbeddedWebappClassLoader() {
super(); super();
} }
@ -58,8 +62,9 @@ public class TomcatEmbeddedWebappClassLoader extends WebappClassLoader {
} }
@Override @Override
public synchronized Class<?> loadClass(String name, boolean resolve) public Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException { throws ClassNotFoundException {
synchronized (getClassLoadingLock(name)) {
Class<?> result = findExistingLoadedClass(name); Class<?> result = findExistingLoadedClass(name);
result = (result == null ? doLoadClass(name) : result); result = (result == null ? doLoadClass(name) : result);
if (result == null) { if (result == null) {
@ -67,6 +72,7 @@ public class TomcatEmbeddedWebappClassLoader extends WebappClassLoader {
} }
return resolveIfNecessary(result, resolve); return resolveIfNecessary(result, resolve);
} }
}
private Class<?> findExistingLoadedClass(String name) { private Class<?> findExistingLoadedClass(String name) {
Class<?> resultClass = findLoadedClass0(name); Class<?> resultClass = findLoadedClass0(name);

@ -28,7 +28,7 @@ import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import org.apache.catalina.core.StandardContext; import org.apache.catalina.core.StandardContext;
import org.apache.catalina.loader.WebappClassLoader; import org.apache.catalina.loader.ParallelWebappClassLoader;
import org.apache.catalina.webresources.StandardRoot; import org.apache.catalina.webresources.StandardRoot;
import org.apache.catalina.webresources.WarResourceSet; import org.apache.catalina.webresources.WarResourceSet;
import org.junit.Rule; import org.junit.Rule;
@ -73,7 +73,7 @@ public class TomcatEmbeddedWebappClassLoaderTests {
throws Exception { throws Exception {
URLClassLoader parent = new URLClassLoader( URLClassLoader parent = new URLClassLoader(
new URL[] { new URL(webInfClassesUrlString(war)) }, null); new URL[] { new URL(webInfClassesUrlString(war)) }, null);
try (WebappClassLoader classLoader = new TomcatEmbeddedWebappClassLoader( try (ParallelWebappClassLoader classLoader = new TomcatEmbeddedWebappClassLoader(
parent)) { parent)) {
StandardContext context = new StandardContext(); StandardContext context = new StandardContext();
context.setName("test"); context.setName("test");

Loading…
Cancel
Save