Commit df3e1f08 authored by Eddú Meléndez's avatar Eddú Meléndez Committed by Andy Wilkinson

Start removing support for Tomcat 7

See gh-6416
Closes gh-7495
parent 5c006d05
...@@ -16,19 +16,14 @@ ...@@ -16,19 +16,14 @@
package org.springframework.boot.context.embedded.tomcat; package org.springframework.boot.context.embedded.tomcat;
import java.lang.reflect.Method;
import java.util.Set; import java.util.Set;
import javax.servlet.ServletContext;
import org.apache.tomcat.JarScanner; import org.apache.tomcat.JarScanner;
import org.apache.tomcat.JarScannerCallback;
import org.apache.tomcat.util.scan.StandardJarScanFilter; import org.apache.tomcat.util.scan.StandardJarScanFilter;
import org.apache.tomcat.util.scan.StandardJarScanner; import org.apache.tomcat.util.scan.StandardJarScanner;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
...@@ -62,22 +57,6 @@ class SkipPatternJarScanner extends StandardJarScanner { ...@@ -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. * Apply this decorator the specified context.
* @param context the context to apply to * @param context the context to apply to
......
...@@ -18,8 +18,6 @@ package org.springframework.boot.context.embedded.tomcat; ...@@ -18,8 +18,6 @@ package org.springframework.boot.context.embedded.tomcat;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
...@@ -34,7 +32,6 @@ import java.util.Set; ...@@ -34,7 +32,6 @@ import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.servlet.ServletContainerInitializer; import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import org.apache.catalina.Context; import org.apache.catalina.Context;
import org.apache.catalina.Engine; import org.apache.catalina.Engine;
...@@ -74,7 +71,6 @@ import org.springframework.core.io.ResourceLoader; ...@@ -74,7 +71,6 @@ import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils; import org.springframework.util.ResourceUtils;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
...@@ -217,7 +213,6 @@ public class TomcatEmbeddedServletContainerFactory ...@@ -217,7 +213,6 @@ public class TomcatEmbeddedServletContainerFactory
if (shouldRegisterJspServlet()) { if (shouldRegisterJspServlet()) {
addJspServlet(context); addJspServlet(context);
addJasperInitializer(context); addJasperInitializer(context);
context.addLifecycleListener(new StoreMergedWebXmlListener());
} }
ServletContextInitializer[] initializersToUse = mergeInitializers(initializers); ServletContextInitializer[] initializersToUse = mergeInitializers(initializers);
configureContext(context, initializersToUse); configureContext(context, initializersToUse);
...@@ -802,49 +797,6 @@ public class TomcatEmbeddedServletContainerFactory ...@@ -802,49 +797,6 @@ public class TomcatEmbeddedServletContainerFactory
this.backgroundProcessorDelay = delay; 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} to disable persistence in the {@link StandardManager}. A
* {@link LifecycleListener} is used so not to interfere with Tomcat's default manager * {@link LifecycleListener} is used so not to interfere with Tomcat's default manager
......
...@@ -36,8 +36,6 @@ class TomcatErrorPage { ...@@ -36,8 +36,6 @@ class TomcatErrorPage {
private static final String ERROR_PAGE_CLASS = "org.apache.tomcat.util.descriptor.web.ErrorPage"; 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 location;
private final String exceptionType; private final String exceptionType;
...@@ -59,10 +57,6 @@ class TomcatErrorPage { ...@@ -59,10 +57,6 @@ class TomcatErrorPage {
return BeanUtils return BeanUtils
.instantiateClass(ClassUtils.forName(ERROR_PAGE_CLASS, null)); .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) { catch (ClassNotFoundException ex) {
// Swallow and continue // Swallow and continue
...@@ -75,7 +69,7 @@ class TomcatErrorPage { ...@@ -75,7 +69,7 @@ class TomcatErrorPage {
public void addToContext(Context context) { public void addToContext(Context context) {
Assert.state(this.nativePage != null, 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)) { if (ClassUtils.isPresent(ERROR_PAGE_CLASS, null)) {
org.apache.tomcat.util.descriptor.web.ErrorPage errorPage = (org.apache.tomcat.util.descriptor.web.ErrorPage) this.nativePage; org.apache.tomcat.util.descriptor.web.ErrorPage errorPage = (org.apache.tomcat.util.descriptor.web.ErrorPage) this.nativePage;
errorPage.setLocation(this.location); errorPage.setLocation(this.location);
......
...@@ -17,23 +17,14 @@ ...@@ -17,23 +17,14 @@
package org.springframework.boot.context.embedded.tomcat; package org.springframework.boot.context.embedded.tomcat;
import java.io.File; import java.io.File;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import javax.naming.directory.DirContext;
import javax.servlet.ServletContext;
import org.apache.catalina.Context; import org.apache.catalina.Context;
import org.apache.catalina.WebResourceRoot.ResourceSetType; 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 Dave Syer
* @author Phillip Webb * @author Phillip Webb
...@@ -95,71 +86,9 @@ abstract class TomcatResources { ...@@ -95,71 +86,9 @@ abstract class TomcatResources {
* @return a {@link TomcatResources} instance. * @return a {@link TomcatResources} instance.
*/ */
public static TomcatResources get(Context context) { public static TomcatResources get(Context context) {
if (ClassUtils.isPresent("org.apache.catalina.deploy.ErrorPage", null)) {
return new Tomcat7Resources(context);
}
return new Tomcat8Resources(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. * {@link TomcatResources} for Tomcat 8.
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment