Commit f7b7b1c8 authored by Phillip Webb's avatar Phillip Webb

Polish embedded tomcat classes

Extract some methods to aid readability.
parent aed243fb
...@@ -37,6 +37,7 @@ public class ServletContextInitializerLifecycleListener implements LifecycleList ...@@ -37,6 +37,7 @@ public class ServletContextInitializerLifecycleListener implements LifecycleList
.getLog(ServletContextInitializerLifecycleListener.class); .getLog(ServletContextInitializerLifecycleListener.class);
private final ServletContextInitializer[] initializers; private final ServletContextInitializer[] initializers;
private Exception startUpException; private Exception startUpException;
/** /**
......
...@@ -24,7 +24,6 @@ import org.apache.catalina.Container; ...@@ -24,7 +24,6 @@ import org.apache.catalina.Container;
import org.apache.catalina.Engine; import org.apache.catalina.Engine;
import org.apache.catalina.LifecycleException; import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleState; import org.apache.catalina.LifecycleState;
import org.apache.catalina.Server;
import org.apache.catalina.Service; import org.apache.catalina.Service;
import org.apache.catalina.connector.Connector; import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.Tomcat;
...@@ -77,46 +76,21 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer ...@@ -77,46 +76,21 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
private synchronized void initialize() throws EmbeddedServletContainerException { private synchronized void initialize() throws EmbeddedServletContainerException {
try { try {
Server server = this.tomcat.getServer(); addInstanceIdToEngineName();
int instanceId = containerCounter.incrementAndGet();
if (instanceId > 0) {
Engine engine = this.tomcat.getEngine();
engine.setName(engine.getName() + "-" + instanceId);
}
// Remove service connectors to that protocol binding doesn't happen yet // Remove service connectors to that protocol binding doesn't happen yet
for (Service service : server.findServices()) { removeServiceConnectors();
Connector[] connectors = service.findConnectors().clone();
this.serviceConnectors.put(service, connectors);
for (Connector connector : connectors) {
service.removeConnector(connector);
}
}
// Start the server to trigger initialization listeners // Start the server to trigger initialization listeners
this.tomcat.start(); this.tomcat.start();
Container[] children = this.tomcat.getHost().findChildren(); // We can re-throw failure exception directly in the main thread
for (Container container : children) { rethrowDeferredStartupExceptions();
if (container instanceof TomcatEmbeddedContext) {
Exception exception = ((TomcatEmbeddedContext) container)
.getStarter().getStartUpException();
if (exception != null) {
throw exception;
}
}
}
// Unlike Jetty, all Tomcat threads are daemon threads. We create a // Unlike Jetty, all Tomcat threads are daemon threads. We create a
// blocking non-daemon to stop immediate shutdown // blocking non-daemon to stop immediate shutdown
Thread awaitThread = new Thread("container-" + (containerCounter.get())) { startDaemonAwaitThread();
@Override
public void run() {
TomcatEmbeddedServletContainer.this.tomcat.getServer().await();
};
};
awaitThread.setDaemon(false);
awaitThread.start();
if (LifecycleState.FAILED.equals(this.tomcat.getConnector().getState())) { if (LifecycleState.FAILED.equals(this.tomcat.getConnector().getState())) {
this.tomcat.stop(); this.tomcat.stop();
throw new IllegalStateException("Tomcat connector in failed state"); throw new IllegalStateException("Tomcat connector in failed state");
...@@ -128,9 +102,58 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer ...@@ -128,9 +102,58 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
} }
} }
private void addInstanceIdToEngineName() {
int instanceId = containerCounter.incrementAndGet();
if (instanceId > 0) {
Engine engine = this.tomcat.getEngine();
engine.setName(engine.getName() + "-" + instanceId);
}
}
private void removeServiceConnectors() {
for (Service service : this.tomcat.getServer().findServices()) {
Connector[] connectors = service.findConnectors().clone();
this.serviceConnectors.put(service, connectors);
for (Connector connector : connectors) {
service.removeConnector(connector);
}
}
}
private void rethrowDeferredStartupExceptions() throws Exception {
Container[] children = this.tomcat.getHost().findChildren();
for (Container container : children) {
if (container instanceof TomcatEmbeddedContext) {
Exception exception = ((TomcatEmbeddedContext) container).getStarter()
.getStartUpException();
if (exception != null) {
throw exception;
}
}
}
}
private void startDaemonAwaitThread() {
Thread awaitThread = new Thread("container-" + (containerCounter.get())) {
@Override
public void run() {
TomcatEmbeddedServletContainer.this.tomcat.getServer().await();
};
};
awaitThread.setDaemon(false);
awaitThread.start();
}
@Override @Override
public void start() throws EmbeddedServletContainerException { public void start() throws EmbeddedServletContainerException {
// Add the previously removed connectors (also starting them) addPreviouslyRemovedConnectors();
Connector connector = this.tomcat.getConnector();
if (connector != null && this.autoStart) {
startConnector(connector);
}
}
private void addPreviouslyRemovedConnectors() {
Service[] services = this.tomcat.getServer().findServices(); Service[] services = this.tomcat.getServer().findServices();
for (Service service : services) { for (Service service : services) {
Connector[] connectors = this.serviceConnectors.get(service); Connector[] connectors = this.serviceConnectors.get(service);
...@@ -138,32 +161,15 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer ...@@ -138,32 +161,15 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
for (Connector connector : connectors) { for (Connector connector : connectors) {
service.addConnector(connector); service.addConnector(connector);
if (!this.autoStart) { if (!this.autoStart) {
unbind(connector); stopProtocolHandler(connector);
} }
} }
this.serviceConnectors.remove(service); this.serviceConnectors.remove(service);
} }
} }
Connector connector = this.tomcat.getConnector();
if (connector != null && this.autoStart) {
try {
for (Container child : this.tomcat.getHost().findChildren()) {
if (child instanceof TomcatEmbeddedContext) {
((TomcatEmbeddedContext) child).deferredLoadOnStartup();
}
}
connector.getProtocolHandler().start();
logPorts();
}
catch (Exception ex) {
this.logger.error("Cannot start connector: ", ex);
throw new EmbeddedServletContainerException(
"Unable to start embedded Tomcat connectors", ex);
}
}
} }
private void unbind(Connector connector) { private void stopProtocolHandler(Connector connector) {
try { try {
connector.getProtocolHandler().stop(); connector.getProtocolHandler().stop();
} }
...@@ -172,6 +178,22 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer ...@@ -172,6 +178,22 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
} }
} }
private void startConnector(Connector connector) {
try {
for (Container child : this.tomcat.getHost().findChildren()) {
if (child instanceof TomcatEmbeddedContext) {
((TomcatEmbeddedContext) child).deferredLoadOnStartup();
}
}
logPorts();
}
catch (Exception ex) {
this.logger.error("Cannot start connector: ", ex);
throw new EmbeddedServletContainerException(
"Unable to start embedded Tomcat connectors", ex);
}
}
Map<Service, Connector[]> getServiceConnectors() { Map<Service, Connector[]> getServiceConnectors() {
return this.serviceConnectors; return this.serviceConnectors;
} }
......
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