Skip to content

Commit

Permalink
Ensure that Tomcat is completely stopped when its initialization fails
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Apr 3, 2018
1 parent 30de75c commit edc00ee
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public void lifecycleEvent(LifecycleEvent event) {
}
}
catch (Exception ex) {
stopSilently();
throw new EmbeddedServletContainerException(
"Unable to start embedded Tomcat", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import javax.naming.InitialContext;
import javax.naming.NamingException;
Expand Down Expand Up @@ -491,7 +492,18 @@ public void sessionIdGeneratorIsConfiguredWithAttributesFromTheManager() {

@Test
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() {

@Override
Expand All @@ -518,7 +530,13 @@ public void destroy() {

});
this.thrown.expect(EmbeddedServletContainerException.class);
factory.getEmbeddedServletContainer().start();
try {
factory.getEmbeddedServletContainer();
}
finally {
assertThat(tomcatReference.get().getServer().getState())
.isEqualTo(LifecycleState.STOPPED);
}
}

@Override
Expand Down

0 comments on commit edc00ee

Please sign in to comment.