Defer removal of Connectors until after ServletContext initialization
Previously, we removed the Connectors from Tomcat's Service before the Context was started. The removal of the Connectors is required as it prevents Tomcat from accepting requests before we're ready to handle them. Part of starting the Context is creating and initializing the ServletContext. ServerProperties uses a ServletContextInitializer to set the session tracking modes and Tomcat rejects the SSL tracking mode if there is no SSL-enabled connector available. With the previous arrangement this led to a failure as the Connectors had been removed so the SSL-enabled connector could not be found. This commit updates the embedded Tomcat container to defer the removal of the Connectors until after the context has been started but still at a point that is before the Connectors themselves would have been started. Closes gh-12058
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -25,7 +25,10 @@ import javax.naming.NamingException;
|
||||
import org.apache.catalina.Container;
|
||||
import org.apache.catalina.Context;
|
||||
import org.apache.catalina.Engine;
|
||||
import org.apache.catalina.Lifecycle;
|
||||
import org.apache.catalina.LifecycleEvent;
|
||||
import org.apache.catalina.LifecycleException;
|
||||
import org.apache.catalina.LifecycleListener;
|
||||
import org.apache.catalina.LifecycleState;
|
||||
import org.apache.catalina.Service;
|
||||
import org.apache.catalina.connector.Connector;
|
||||
@@ -91,9 +94,21 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
|
||||
try {
|
||||
addInstanceIdToEngineName();
|
||||
try {
|
||||
// Remove service connectors to that protocol binding doesn't happen
|
||||
// yet
|
||||
removeServiceConnectors();
|
||||
final Context context = findContext();
|
||||
context.addLifecycleListener(new LifecycleListener() {
|
||||
|
||||
@Override
|
||||
public void lifecycleEvent(LifecycleEvent event) {
|
||||
if (context.equals(event.getSource())
|
||||
&& Lifecycle.START_EVENT.equals(event.getType())) {
|
||||
// Remove service connectors so that protocol
|
||||
// binding doesn't happen when the service is
|
||||
// started.
|
||||
removeServiceConnectors();
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
// Start the server to trigger initialization listeners
|
||||
this.tomcat.start();
|
||||
@@ -101,7 +116,6 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
|
||||
// We can re-throw failure exception directly in the main thread
|
||||
rethrowDeferredStartupExceptions();
|
||||
|
||||
Context context = findContext();
|
||||
try {
|
||||
ContextBindings.bindClassLoader(context, getNamingToken(context),
|
||||
getClass().getClassLoader());
|
||||
|
||||
Reference in New Issue
Block a user