2424import org .apache .catalina .Engine ;
2525import org .apache .catalina .LifecycleException ;
2626import org .apache .catalina .LifecycleState ;
27- import org .apache .catalina .Server ;
2827import org .apache .catalina .Service ;
2928import org .apache .catalina .connector .Connector ;
3029import org .apache .catalina .startup .Tomcat ;
@@ -77,46 +76,21 @@ public TomcatEmbeddedServletContainer(Tomcat tomcat, boolean autoStart) {
7776
7877 private synchronized void initialize () throws EmbeddedServletContainerException {
7978 try {
80- Server server = this .tomcat .getServer ();
81- int instanceId = containerCounter .incrementAndGet ();
82- if (instanceId > 0 ) {
83- Engine engine = this .tomcat .getEngine ();
84- engine .setName (engine .getName () + "-" + instanceId );
85- }
79+ addInstanceIdToEngineName ();
8680
8781 // Remove service connectors to that protocol binding doesn't happen yet
88- for (Service service : server .findServices ()) {
89- Connector [] connectors = service .findConnectors ().clone ();
90- this .serviceConnectors .put (service , connectors );
91- for (Connector connector : connectors ) {
92- service .removeConnector (connector );
93- }
94- }
82+ removeServiceConnectors ();
9583
9684 // Start the server to trigger initialization listeners
9785 this .tomcat .start ();
9886
99- Container [] children = this .tomcat .getHost ().findChildren ();
100- for (Container container : children ) {
101- if (container instanceof TomcatEmbeddedContext ) {
102- Exception exception = ((TomcatEmbeddedContext ) container )
103- .getStarter ().getStartUpException ();
104- if (exception != null ) {
105- throw exception ;
106- }
107- }
108- }
87+ // We can re-throw failure exception directly in the main thread
88+ rethrowDeferredStartupExceptions ();
10989
11090 // Unlike Jetty, all Tomcat threads are daemon threads. We create a
11191 // blocking non-daemon to stop immediate shutdown
112- Thread awaitThread = new Thread ("container-" + (containerCounter .get ())) {
113- @ Override
114- public void run () {
115- TomcatEmbeddedServletContainer .this .tomcat .getServer ().await ();
116- };
117- };
118- awaitThread .setDaemon (false );
119- awaitThread .start ();
92+ startDaemonAwaitThread ();
93+
12094 if (LifecycleState .FAILED .equals (this .tomcat .getConnector ().getState ())) {
12195 this .tomcat .stop ();
12296 throw new IllegalStateException ("Tomcat connector in failed state" );
@@ -128,42 +102,74 @@ public void run() {
128102 }
129103 }
130104
105+ private void addInstanceIdToEngineName () {
106+ int instanceId = containerCounter .incrementAndGet ();
107+ if (instanceId > 0 ) {
108+ Engine engine = this .tomcat .getEngine ();
109+ engine .setName (engine .getName () + "-" + instanceId );
110+ }
111+ }
112+
113+ private void removeServiceConnectors () {
114+ for (Service service : this .tomcat .getServer ().findServices ()) {
115+ Connector [] connectors = service .findConnectors ().clone ();
116+ this .serviceConnectors .put (service , connectors );
117+ for (Connector connector : connectors ) {
118+ service .removeConnector (connector );
119+ }
120+ }
121+ }
122+
123+ private void rethrowDeferredStartupExceptions () throws Exception {
124+ Container [] children = this .tomcat .getHost ().findChildren ();
125+ for (Container container : children ) {
126+ if (container instanceof TomcatEmbeddedContext ) {
127+ Exception exception = ((TomcatEmbeddedContext ) container ).getStarter ()
128+ .getStartUpException ();
129+ if (exception != null ) {
130+ throw exception ;
131+ }
132+ }
133+ }
134+ }
135+
136+ private void startDaemonAwaitThread () {
137+ Thread awaitThread = new Thread ("container-" + (containerCounter .get ())) {
138+ @ Override
139+ public void run () {
140+ TomcatEmbeddedServletContainer .this .tomcat .getServer ().await ();
141+ };
142+ };
143+ awaitThread .setDaemon (false );
144+ awaitThread .start ();
145+ }
146+
131147 @ Override
132148 public void start () throws EmbeddedServletContainerException {
133- // Add the previously removed connectors (also starting them)
149+ addPreviouslyRemovedConnectors ();
150+ Connector connector = this .tomcat .getConnector ();
151+ if (connector != null && this .autoStart ) {
152+ startConnector (connector );
153+ }
154+ }
155+
156+ private void addPreviouslyRemovedConnectors () {
134157 Service [] services = this .tomcat .getServer ().findServices ();
135158 for (Service service : services ) {
136159 Connector [] connectors = this .serviceConnectors .get (service );
137160 if (connectors != null ) {
138161 for (Connector connector : connectors ) {
139162 service .addConnector (connector );
140163 if (!this .autoStart ) {
141- unbind (connector );
164+ stopProtocolHandler (connector );
142165 }
143166 }
144167 this .serviceConnectors .remove (service );
145168 }
146169 }
147- Connector connector = this .tomcat .getConnector ();
148- if (connector != null && this .autoStart ) {
149- try {
150- for (Container child : this .tomcat .getHost ().findChildren ()) {
151- if (child instanceof TomcatEmbeddedContext ) {
152- ((TomcatEmbeddedContext ) child ).deferredLoadOnStartup ();
153- }
154- }
155- connector .getProtocolHandler ().start ();
156- logPorts ();
157- }
158- catch (Exception ex ) {
159- this .logger .error ("Cannot start connector: " , ex );
160- throw new EmbeddedServletContainerException (
161- "Unable to start embedded Tomcat connectors" , ex );
162- }
163- }
164170 }
165171
166- private void unbind (Connector connector ) {
172+ private void stopProtocolHandler (Connector connector ) {
167173 try {
168174 connector .getProtocolHandler ().stop ();
169175 }
@@ -172,6 +178,22 @@ private void unbind(Connector connector) {
172178 }
173179 }
174180
181+ private void startConnector (Connector connector ) {
182+ try {
183+ for (Container child : this .tomcat .getHost ().findChildren ()) {
184+ if (child instanceof TomcatEmbeddedContext ) {
185+ ((TomcatEmbeddedContext ) child ).deferredLoadOnStartup ();
186+ }
187+ }
188+ logPorts ();
189+ }
190+ catch (Exception ex ) {
191+ this .logger .error ("Cannot start connector: " , ex );
192+ throw new EmbeddedServletContainerException (
193+ "Unable to start embedded Tomcat connectors" , ex );
194+ }
195+ }
196+
175197 Map <Service , Connector []> getServiceConnectors () {
176198 return this .serviceConnectors ;
177199 }
0 commit comments