Fail startup when Tomcat's context fails to start
See gh-9095
This commit is contained in:
committed by
Andy Wilkinson
parent
b61b1f0e08
commit
217b237b37
@@ -192,6 +192,7 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
|
||||
if (connector != null && this.autoStart) {
|
||||
startConnector(connector);
|
||||
}
|
||||
checkThatContextHaveStarted();
|
||||
checkThatConnectorsHaveStarted();
|
||||
this.started = true;
|
||||
TomcatEmbeddedServletContainer.logger
|
||||
@@ -221,6 +222,13 @@ 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();
|
||||
|
||||
@@ -26,7 +26,12 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.naming.InitialContext;
|
||||
import javax.naming.NamingException;
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
|
||||
import org.apache.catalina.Container;
|
||||
import org.apache.catalina.Context;
|
||||
@@ -53,6 +58,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.test.util.ReflectionTestUtils;
|
||||
import org.springframework.util.SocketUtils;
|
||||
|
||||
@@ -509,4 +515,29 @@ 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