Polish "Fail startup when Tomcat's context fails to start"
Closes gh-9095
This commit is contained in:
committed by
Andy Wilkinson
parent
217b237b37
commit
8c46644231
@@ -163,6 +163,9 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
|
||||
throw exception;
|
||||
}
|
||||
}
|
||||
if (!LifecycleState.STARTED.equals(container.getState())) {
|
||||
throw new IllegalStateException(container + " failed to start");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,7 +195,6 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
|
||||
if (connector != null && this.autoStart) {
|
||||
startConnector(connector);
|
||||
}
|
||||
checkThatContextHaveStarted();
|
||||
checkThatConnectorsHaveStarted();
|
||||
this.started = true;
|
||||
TomcatEmbeddedServletContainer.logger
|
||||
@@ -222,13 +224,6 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
|
||||
}
|
||||
}
|
||||
|
||||
private void checkThatContextHaveStarted() {
|
||||
LifecycleState state = this.findContext().getState();
|
||||
if (!LifecycleState.STARTED.equals(state)) {
|
||||
throw new EmbeddedServletContainerException("Context state expect STARTED, but " + state, null);
|
||||
}
|
||||
}
|
||||
|
||||
private void stopSilently() {
|
||||
try {
|
||||
stopTomcat();
|
||||
|
||||
@@ -23,7 +23,13 @@ import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
@@ -45,8 +51,10 @@ import org.mockito.InOrder;
|
||||
import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactory;
|
||||
import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactoryTests;
|
||||
import org.springframework.boot.context.embedded.Compression;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerException;
|
||||
import org.springframework.boot.context.embedded.PortInUseException;
|
||||
import org.springframework.boot.context.embedded.Ssl;
|
||||
import org.springframework.boot.web.servlet.ServletContextInitializer;
|
||||
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
|
||||
@@ -282,6 +290,38 @@ public class JettyEmbeddedServletContainerFactoryTests
|
||||
.getThreadPool()).isSameAs(threadPool);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void faultyFilterCausesStartFailure() throws Exception {
|
||||
AbstractEmbeddedServletContainerFactory factory = getFactory();
|
||||
factory.addInitializers(new ServletContextInitializer() {
|
||||
|
||||
@Override
|
||||
public void onStartup(ServletContext servletContext) throws ServletException {
|
||||
servletContext.addFilter("faulty", new Filter() {
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
throw new ServletException("Faulty filter");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response,
|
||||
FilterChain chain) throws IOException, ServletException {
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
this.thrown.expect(EmbeddedServletContainerException.class);
|
||||
factory.getEmbeddedServletContainer().start();
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("serial")
|
||||
// Workaround for Jetty issue - https://bugs.eclipse.org/bugs/show_bug.cgi?id=470646
|
||||
|
||||
@@ -29,6 +29,7 @@ import javax.naming.NamingException;
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
@@ -58,7 +59,7 @@ import org.springframework.boot.context.embedded.AbstractEmbeddedServletContaine
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerException;
|
||||
import org.springframework.boot.context.embedded.Ssl;
|
||||
import org.springframework.boot.testutil.InternalOutputCapture;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.boot.web.servlet.ServletContextInitializer;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.util.SocketUtils;
|
||||
|
||||
@@ -466,6 +467,38 @@ public class TomcatEmbeddedServletContainerFactoryTests
|
||||
assertThat(sessionIdGenerator.getJvmRoute()).isEqualTo("test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void faultyFilterCausesStartFailure() throws Exception {
|
||||
AbstractEmbeddedServletContainerFactory factory = getFactory();
|
||||
factory.addInitializers(new ServletContextInitializer() {
|
||||
|
||||
@Override
|
||||
public void onStartup(ServletContext servletContext) throws ServletException {
|
||||
servletContext.addFilter("faulty", new Filter() {
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
throw new ServletException("Faulty filter");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response,
|
||||
FilterChain chain) throws IOException, ServletException {
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
this.thrown.expect(EmbeddedServletContainerException.class);
|
||||
factory.getEmbeddedServletContainer().start();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JspServlet getJspServlet() throws ServletException {
|
||||
Container context = ((TomcatEmbeddedServletContainer) this.container).getTomcat()
|
||||
@@ -515,29 +548,4 @@ public class TomcatEmbeddedServletContainerFactoryTests
|
||||
assertThat(((ConnectorStartFailedException) ex).getPort()).isEqualTo(blockedPort);
|
||||
}
|
||||
|
||||
@Test(expected = EmbeddedServletContainerException.class)
|
||||
public void startServletExceptionFilter() throws Exception {
|
||||
AbstractEmbeddedServletContainerFactory factory = getFactory();
|
||||
|
||||
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
|
||||
filterRegistrationBean.setFilter(new Filter() {
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
throw new ServletException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response,
|
||||
FilterChain chain) throws IOException, ServletException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
}
|
||||
});
|
||||
filterRegistrationBean.setUrlPatterns(Arrays.asList("/test"));
|
||||
|
||||
this.container = factory.getEmbeddedServletContainer(filterRegistrationBean);
|
||||
this.container.start();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user