Skip to content

Commit

Permalink
Avoid a clash with TomcatURLStreamHandlerFactory
Browse files Browse the repository at this point in the history
In the unlikely (outside the tests of a number of Spring Boot's
modules) event that both Tomcat and Jetty are on the classpath,
there's a risk that Tomcat will try to register its
URLStreamHandlerFactory after JasperInitializer has registered its
WarUrlStreamHandlerFactory. Should this happen, Tomcat will fail to
start.

TomcatURLStreamHandlerFactory registers a handler for the war: URLs
produced by org.apache.tomcat.util.scan.JarFactory that's used by
both Tomcat and Jetty's Jasper integration. This makes it a suitable
replacement for WarUrlStreamHandlerFactory so this commit updates
JasperIntializer to register TomcatURLStreamHandlerFactory when it's
available, falling back to WarUrlStreamHandlerFactory when it's not.

Closes gh-8622
  • Loading branch information
wilkinsona committed May 11, 2017
1 parent 71b53e8 commit ef12eec
Showing 1 changed file with 12 additions and 4 deletions.
Expand Up @@ -25,6 +25,7 @@

import javax.servlet.ServletContainerInitializer;

import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.webapp.WebAppContext;

Expand Down Expand Up @@ -69,11 +70,18 @@ protected void doStart() throws Exception {
if (this.initializer == null) {
return;
}
try {
URL.setURLStreamHandlerFactory(new WarUrlStreamHandlerFactory());
if (ClassUtils.isPresent(
"org.apache.catalina.webresources.TomcatURLStreamHandlerFactory",
getClass().getClassLoader())) {
TomcatURLStreamHandlerFactory.register();
}
catch (Error ex) {
// Ignore
else {
try {
URL.setURLStreamHandlerFactory(new WarUrlStreamHandlerFactory());
}
catch (Error ex) {
// Ignore
}
}
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try {
Expand Down

0 comments on commit ef12eec

Please sign in to comment.