Fix Tomcat 8 warnings and Jasper initialization

Fix MalformedURLException warnings caused by Tomcat 8 adding additional
URLs to the TomcatEmbeddedWebappClassLoader.

Add JasperInitializerLifecycleListener to call Tomcat 8's
JasperInitializer which is now required for JSP rendering.

Fixes gh-919
pull/965/head
Phillip Webb 11 years ago
parent 7d6438ad9b
commit 6e82e8ede8

@ -0,0 +1,47 @@
package org.springframework.boot.context.embedded.tomcat;
import org.apache.catalina.LifecycleEvent;
import org.apache.catalina.LifecycleListener;
import org.springframework.boot.context.embedded.ServletContextInitializer;
import org.springframework.util.ClassUtils;
/**
* Tomcat {@link LifecycleListener} to initialize Jasper by calling the
* `JasperInitializer` used in Tomcat 8.
*
* @author Phillip Webb
*/
class JasperInitializerLifecycleListener implements LifecycleListener {
private static final String JASPER_INITIALIZER_CLASS = "org.apache.jasper.servlet.JasperInitializer";
private final ServletContextInitializerLifecycleListener delegate;
public JasperInitializerLifecycleListener() {
ServletContextInitializer initializer = getJasperInitializer();
if (initializer != null) {
this.delegate = new ServletContextInitializerLifecycleListener(initializer);
}
else {
this.delegate = null;
}
}
@Override
public void lifecycleEvent(LifecycleEvent event) {
if (this.delegate != null) {
this.delegate.lifecycleEvent(event);
}
}
private ServletContextInitializer getJasperInitializer() {
try {
Class<?> jasperClass = ClassUtils.forName(JASPER_INITIALIZER_CLASS, null);
return (ServletContextInitializer) jasperClass.newInstance();
}
catch (Exception ex) {
return null;
}
}
}

@ -230,6 +230,7 @@ public class TomcatEmbeddedServletContainerFactory extends
ServletContextInitializer[] initializers) {
context.addLifecycleListener(new ServletContextInitializerLifecycleListener(
initializers));
context.addLifecycleListener(new JasperInitializerLifecycleListener());
for (LifecycleListener lifecycleListener : this.contextLifecycleListeners) {
context.addLifecycleListener(lifecycleListener);
}

@ -16,7 +16,11 @@
package org.springframework.boot.context.embedded.tomcat;
import java.net.URL;
import org.apache.catalina.loader.WebappClassLoader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Extension of Tomcat's {@link WebappClassLoader} that does not consider the
@ -28,6 +32,9 @@ import org.apache.catalina.loader.WebappClassLoader;
*/
public class TomcatEmbeddedWebappClassLoader extends WebappClassLoader {
private static final Log logger = LogFactory
.getLog(TomcatEmbeddedWebappClassLoader.class);
public TomcatEmbeddedWebappClassLoader() {
super();
}
@ -78,6 +85,14 @@ public class TomcatEmbeddedWebappClassLoader extends WebappClassLoader {
return (resultClass);
}
@Override
protected void addURL(URL url) {
// Ignore URLs added by the Tomcat 8 implementation (see gh-919)
if (logger.isTraceEnabled()) {
logger.trace("Ignoring request to add " + url + " to the tomcat classloader");
}
}
private Class<?> loadFromParent(String name) {
if (this.parent == null) {
return null;

Loading…
Cancel
Save