Start removing support for Tomcat 7
See gh-6416 Closes gh-7495
This commit is contained in:
committed by
Andy Wilkinson
parent
5c006d05f8
commit
df3e1f0898
@@ -16,19 +16,14 @@
|
||||
|
||||
package org.springframework.boot.context.embedded.tomcat;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import org.apache.tomcat.JarScanner;
|
||||
import org.apache.tomcat.JarScannerCallback;
|
||||
import org.apache.tomcat.util.scan.StandardJarScanFilter;
|
||||
import org.apache.tomcat.util.scan.StandardJarScanner;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -62,22 +57,6 @@ class SkipPatternJarScanner extends StandardJarScanner {
|
||||
}
|
||||
}
|
||||
|
||||
// For Tomcat 7 compatibility
|
||||
public void scan(ServletContext context, ClassLoader classloader,
|
||||
JarScannerCallback callback, Set<String> jarsToSkip) {
|
||||
Method scanMethod = ReflectionUtils.findMethod(this.jarScanner.getClass(), "scan",
|
||||
ServletContext.class, ClassLoader.class, JarScannerCallback.class,
|
||||
Set.class);
|
||||
Assert.notNull(scanMethod, "Unable to find scan method");
|
||||
try {
|
||||
scanMethod.invoke(this.jarScanner, context, classloader, callback,
|
||||
(jarsToSkip == null ? this.patterns : jarsToSkip));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException("Tomcat 7 reflection failed", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply this decorator the specified context.
|
||||
* @param context the context to apply to
|
||||
|
||||
@@ -18,8 +18,6 @@ package org.springframework.boot.context.embedded.tomcat;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -34,7 +32,6 @@ import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.servlet.ServletContainerInitializer;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import org.apache.catalina.Context;
|
||||
import org.apache.catalina.Engine;
|
||||
@@ -74,7 +71,6 @@ import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
import org.springframework.util.StreamUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -217,7 +213,6 @@ public class TomcatEmbeddedServletContainerFactory
|
||||
if (shouldRegisterJspServlet()) {
|
||||
addJspServlet(context);
|
||||
addJasperInitializer(context);
|
||||
context.addLifecycleListener(new StoreMergedWebXmlListener());
|
||||
}
|
||||
ServletContextInitializer[] initializersToUse = mergeInitializers(initializers);
|
||||
configureContext(context, initializersToUse);
|
||||
@@ -802,49 +797,6 @@ public class TomcatEmbeddedServletContainerFactory
|
||||
this.backgroundProcessorDelay = delay;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link LifecycleListener} that stores an empty merged web.xml. This is critical for
|
||||
* Jasper on Tomcat 7 to prevent warnings about missing web.xml files and to enable
|
||||
* EL.
|
||||
*/
|
||||
private static class StoreMergedWebXmlListener implements LifecycleListener {
|
||||
|
||||
private static final String MERGED_WEB_XML = "org.apache.tomcat.util.scan.MergedWebXml";
|
||||
|
||||
@Override
|
||||
public void lifecycleEvent(LifecycleEvent event) {
|
||||
if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) {
|
||||
onStart((Context) event.getLifecycle());
|
||||
}
|
||||
}
|
||||
|
||||
private void onStart(Context context) {
|
||||
ServletContext servletContext = context.getServletContext();
|
||||
if (servletContext.getAttribute(MERGED_WEB_XML) == null) {
|
||||
servletContext.setAttribute(MERGED_WEB_XML, getEmptyWebXml());
|
||||
}
|
||||
TomcatResources.get(context).addClasspathResources();
|
||||
}
|
||||
|
||||
private String getEmptyWebXml() {
|
||||
InputStream stream = TomcatEmbeddedServletContainerFactory.class
|
||||
.getResourceAsStream("empty-web.xml");
|
||||
Assert.state(stream != null, "Unable to read empty web.xml");
|
||||
try {
|
||||
try {
|
||||
return StreamUtils.copyToString(stream, Charset.forName("UTF-8"));
|
||||
}
|
||||
finally {
|
||||
stream.close();
|
||||
}
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link LifecycleListener} to disable persistence in the {@link StandardManager}. A
|
||||
* {@link LifecycleListener} is used so not to interfere with Tomcat's default manager
|
||||
|
||||
@@ -36,8 +36,6 @@ class TomcatErrorPage {
|
||||
|
||||
private static final String ERROR_PAGE_CLASS = "org.apache.tomcat.util.descriptor.web.ErrorPage";
|
||||
|
||||
private static final String LEGACY_ERROR_PAGE_CLASS = "org.apache.catalina.deploy.ErrorPage";
|
||||
|
||||
private final String location;
|
||||
|
||||
private final String exceptionType;
|
||||
@@ -59,10 +57,6 @@ class TomcatErrorPage {
|
||||
return BeanUtils
|
||||
.instantiateClass(ClassUtils.forName(ERROR_PAGE_CLASS, null));
|
||||
}
|
||||
if (ClassUtils.isPresent(LEGACY_ERROR_PAGE_CLASS, null)) {
|
||||
return BeanUtils.instantiateClass(
|
||||
ClassUtils.forName(LEGACY_ERROR_PAGE_CLASS, null));
|
||||
}
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
// Swallow and continue
|
||||
@@ -75,7 +69,7 @@ class TomcatErrorPage {
|
||||
|
||||
public void addToContext(Context context) {
|
||||
Assert.state(this.nativePage != null,
|
||||
"Neither Tomcat 7 nor 8 detected so no native error page exists");
|
||||
"No Tomcat 8 detected so no native error page exists");
|
||||
if (ClassUtils.isPresent(ERROR_PAGE_CLASS, null)) {
|
||||
org.apache.tomcat.util.descriptor.web.ErrorPage errorPage = (org.apache.tomcat.util.descriptor.web.ErrorPage) this.nativePage;
|
||||
errorPage.setLocation(this.location);
|
||||
|
||||
@@ -17,23 +17,14 @@
|
||||
package org.springframework.boot.context.embedded.tomcat;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
|
||||
import javax.naming.directory.DirContext;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import org.apache.catalina.Context;
|
||||
import org.apache.catalina.WebResourceRoot.ResourceSetType;
|
||||
import org.apache.catalina.core.StandardContext;
|
||||
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Abstraction to add resources that works with both Tomcat 8 and 7.
|
||||
* Abstraction to add resources that works with Tomcat 8.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Phillip Webb
|
||||
@@ -95,71 +86,9 @@ abstract class TomcatResources {
|
||||
* @return a {@link TomcatResources} instance.
|
||||
*/
|
||||
public static TomcatResources get(Context context) {
|
||||
if (ClassUtils.isPresent("org.apache.catalina.deploy.ErrorPage", null)) {
|
||||
return new Tomcat7Resources(context);
|
||||
}
|
||||
return new Tomcat8Resources(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link TomcatResources} for Tomcat 7.
|
||||
*/
|
||||
private static class Tomcat7Resources extends TomcatResources {
|
||||
|
||||
private final Method addResourceJarUrlMethod;
|
||||
|
||||
Tomcat7Resources(Context context) {
|
||||
super(context);
|
||||
this.addResourceJarUrlMethod = ReflectionUtils.findMethod(context.getClass(),
|
||||
"addResourceJarUrl", URL.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addJar(String jar) {
|
||||
URL url = getJarUrl(jar);
|
||||
if (url != null) {
|
||||
try {
|
||||
this.addResourceJarUrlMethod.invoke(getContext(), url);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private URL getJarUrl(String jar) {
|
||||
try {
|
||||
return new URL(jar);
|
||||
}
|
||||
catch (MalformedURLException ex) {
|
||||
// Ignore
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addDir(String dir, URL url) {
|
||||
if (getContext() instanceof ServletContext) {
|
||||
try {
|
||||
Class<?> fileDirContextClass = Class
|
||||
.forName("org.apache.naming.resources.FileDirContext");
|
||||
Method setDocBaseMethod = ReflectionUtils
|
||||
.findMethod(fileDirContextClass, "setDocBase", String.class);
|
||||
Object fileDirContext = fileDirContextClass.newInstance();
|
||||
setDocBaseMethod.invoke(fileDirContext, dir);
|
||||
Method addResourcesDirContextMethod = ReflectionUtils.findMethod(
|
||||
StandardContext.class, "addResourcesDirContext",
|
||||
DirContext.class);
|
||||
addResourcesDirContextMethod.invoke(getContext(), fileDirContext);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException("Tomcat 7 reflection failed", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link TomcatResources} for Tomcat 8.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user