Ensure that Tomcat is completely stopped when its initialization fails

Closes gh-12736
pull/12859/head
Andy Wilkinson 7 years ago
parent 30de75c75c
commit edc00eef24

@ -134,6 +134,7 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
} }
} }
catch (Exception ex) { catch (Exception ex) {
stopSilently();
throw new EmbeddedServletContainerException( throw new EmbeddedServletContainerException(
"Unable to start embedded Tomcat", ex); "Unable to start embedded Tomcat", ex);
} }

@ -23,6 +23,7 @@ import java.util.Arrays;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import javax.naming.InitialContext; import javax.naming.InitialContext;
import javax.naming.NamingException; import javax.naming.NamingException;
@ -491,7 +492,18 @@ public class TomcatEmbeddedServletContainerFactoryTests
@Test @Test
public void faultyFilterCausesStartFailure() throws Exception { public void faultyFilterCausesStartFailure() throws Exception {
AbstractEmbeddedServletContainerFactory factory = getFactory(); final AtomicReference<Tomcat> tomcatReference = new AtomicReference<Tomcat>();
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(
0) {
@Override
protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
Tomcat tomcat) {
tomcatReference.set(tomcat);
return super.getTomcatEmbeddedServletContainer(tomcat);
}
};
factory.addInitializers(new ServletContextInitializer() { factory.addInitializers(new ServletContextInitializer() {
@Override @Override
@ -518,7 +530,13 @@ public class TomcatEmbeddedServletContainerFactoryTests
}); });
this.thrown.expect(EmbeddedServletContainerException.class); this.thrown.expect(EmbeddedServletContainerException.class);
factory.getEmbeddedServletContainer().start(); try {
factory.getEmbeddedServletContainer();
}
finally {
assertThat(tomcatReference.get().getServer().getState())
.isEqualTo(LifecycleState.STOPPED);
}
} }
@Override @Override

Loading…
Cancel
Save