Commit 84305825 authored by Phillip Webb's avatar Phillip Webb

Throw EmbeddedContainerExceptions from Tomcat

Wrap Tomcat start errors in EmbeddedContainerException.

Fixes gh-4204
parent 5a84c02b
...@@ -146,18 +146,24 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer ...@@ -146,18 +146,24 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
@Override @Override
public void start() throws EmbeddedServletContainerException { public void start() throws EmbeddedServletContainerException {
addPreviouslyRemovedConnectors(); try {
Connector connector = this.tomcat.getConnector(); addPreviouslyRemovedConnectors();
if (connector != null && this.autoStart) { Connector connector = this.tomcat.getConnector();
startConnector(connector); if (connector != null && this.autoStart) {
startConnector(connector);
}
// Ensure process isn't left running if it actually failed to start
if (connectorsHaveFailedToStart()) {
stopSilently();
throw new IllegalStateException("Tomcat connector in failed state");
}
TomcatEmbeddedServletContainer.logger
.info("Tomcat started on port(s): " + getPortsDescription(true));
} }
// Ensure process isn't left running if it actually failed to start catch (Exception ex) {
if (connectorsHaveFailedToStart()) { throw new EmbeddedServletContainerException(
stopSilently(); "Unable to start embedded Tomcat servlet container", ex);
throw new IllegalStateException("Tomcat connector in failed state");
} }
TomcatEmbeddedServletContainer.logger
.info("Tomcat started on port(s): " + getPortsDescription(true));
} }
private boolean connectorsHaveFailedToStart() { private boolean connectorsHaveFailedToStart() {
......
...@@ -41,6 +41,7 @@ import org.apache.coyote.http11.AbstractHttp11JsseProtocol; ...@@ -41,6 +41,7 @@ import org.apache.coyote.http11.AbstractHttp11JsseProtocol;
import org.junit.Test; import org.junit.Test;
import org.mockito.InOrder; import org.mockito.InOrder;
import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactoryTests; import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactoryTests;
import org.springframework.boot.context.embedded.EmbeddedServletContainerException;
import org.springframework.boot.context.embedded.Ssl; import org.springframework.boot.context.embedded.Ssl;
import org.springframework.util.SocketUtils; import org.springframework.util.SocketUtils;
...@@ -278,7 +279,7 @@ public class TomcatEmbeddedServletContainerFactoryTests ...@@ -278,7 +279,7 @@ public class TomcatEmbeddedServletContainerFactoryTests
TomcatEmbeddedServletContainerFactoryTests.this.container.start(); TomcatEmbeddedServletContainerFactoryTests.this.container.start();
fail(); fail();
} }
catch (IllegalStateException ex) { catch (EmbeddedServletContainerException ex) {
// Ignore // Ignore
} }
} }
...@@ -308,7 +309,7 @@ public class TomcatEmbeddedServletContainerFactoryTests ...@@ -308,7 +309,7 @@ public class TomcatEmbeddedServletContainerFactoryTests
TomcatEmbeddedServletContainerFactoryTests.this.container.start(); TomcatEmbeddedServletContainerFactoryTests.this.container.start();
fail(); fail();
} }
catch (IllegalStateException ex) { catch (EmbeddedServletContainerException ex) {
// Ignore // Ignore
} }
} }
...@@ -382,7 +383,6 @@ public class TomcatEmbeddedServletContainerFactoryTests ...@@ -382,7 +383,6 @@ public class TomcatEmbeddedServletContainerFactoryTests
private void doWithBlockedPort(final int port, Runnable action) throws IOException { private void doWithBlockedPort(final int port, Runnable action) throws IOException {
ServerSocket serverSocket = new ServerSocket(); ServerSocket serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress(port)); serverSocket.bind(new InetSocketAddress(port));
try { try {
action.run(); action.run();
} }
......
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