24
24
import org .apache .catalina .Engine ;
25
25
import org .apache .catalina .LifecycleException ;
26
26
import org .apache .catalina .LifecycleState ;
27
- import org .apache .catalina .Server ;
28
27
import org .apache .catalina .Service ;
29
28
import org .apache .catalina .connector .Connector ;
30
29
import org .apache .catalina .startup .Tomcat ;
@@ -77,46 +76,21 @@ public TomcatEmbeddedServletContainer(Tomcat tomcat, boolean autoStart) {
77
76
78
77
private synchronized void initialize () throws EmbeddedServletContainerException {
79
78
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 ();
86
80
87
81
// 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 ();
95
83
96
84
// Start the server to trigger initialization listeners
97
85
this .tomcat .start ();
98
86
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 ();
109
89
110
90
// Unlike Jetty, all Tomcat threads are daemon threads. We create a
111
91
// 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
+
120
94
if (LifecycleState .FAILED .equals (this .tomcat .getConnector ().getState ())) {
121
95
this .tomcat .stop ();
122
96
throw new IllegalStateException ("Tomcat connector in failed state" );
@@ -128,42 +102,74 @@ public void run() {
128
102
}
129
103
}
130
104
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
+
131
147
@ Override
132
148
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 () {
134
157
Service [] services = this .tomcat .getServer ().findServices ();
135
158
for (Service service : services ) {
136
159
Connector [] connectors = this .serviceConnectors .get (service );
137
160
if (connectors != null ) {
138
161
for (Connector connector : connectors ) {
139
162
service .addConnector (connector );
140
163
if (!this .autoStart ) {
141
- unbind (connector );
164
+ stopProtocolHandler (connector );
142
165
}
143
166
}
144
167
this .serviceConnectors .remove (service );
145
168
}
146
169
}
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
- }
164
170
}
165
171
166
- private void unbind (Connector connector ) {
172
+ private void stopProtocolHandler (Connector connector ) {
167
173
try {
168
174
connector .getProtocolHandler ().stop ();
169
175
}
@@ -172,6 +178,22 @@ private void unbind(Connector connector) {
172
178
}
173
179
}
174
180
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
+
175
197
Map <Service , Connector []> getServiceConnectors () {
176
198
return this .serviceConnectors ;
177
199
}
0 commit comments