30
30
import cloud .piranha .core .api .Piranha ;
31
31
import cloud .piranha .core .api .PiranhaConfiguration ;
32
32
import cloud .piranha .core .api .WebApplicationExtension ;
33
- import cloud .piranha .core .api .WebApplicationServerRequestMapper ;
34
- import cloud .piranha .core .impl .DefaultModuleFinder ;
35
- import cloud .piranha .core .impl .DefaultModuleLayerProcessor ;
36
33
import cloud .piranha .core .impl .DefaultPiranhaConfiguration ;
37
- import cloud .piranha .core .impl .DefaultWebApplication ;
38
- import cloud .piranha .core .impl .DefaultWebApplicationClassLoader ;
39
- import cloud .piranha .core .impl .DefaultWebApplicationExtensionContext ;
40
- import static cloud .piranha .core .impl .WarFileExtractor .extractWarFile ;
41
34
import cloud .piranha .extension .platform .PlatformExtension ;
42
35
import cloud .piranha .feature .api .FeatureManager ;
43
36
import cloud .piranha .feature .exitonstop .ExitOnStopFeature ;
44
37
import cloud .piranha .feature .http .HttpFeature ;
45
38
import cloud .piranha .feature .https .HttpsFeature ;
46
39
import cloud .piranha .feature .impl .DefaultFeatureManager ;
40
+ import cloud .piranha .feature .webapps .WebAppsFeature ;
47
41
import cloud .piranha .http .api .HttpServer ;
48
- import cloud .piranha .http .webapp .HttpWebApplicationServer ;
49
- import cloud .piranha .resource .impl .DirectoryResource ;
50
42
import java .io .File ;
51
43
import java .io .IOException ;
52
44
import java .lang .System .Logger ;
53
- import static java .lang .System .Logger .Level .ERROR ;
54
45
import static java .lang .System .Logger .Level .INFO ;
55
46
import static java .lang .System .Logger .Level .WARNING ;
56
- import java .lang .module .Configuration ;
57
- import java .lang .module .ModuleDescriptor ;
58
- import java .lang .module .ModuleFinder ;
59
- import java .lang .module .ModuleReference ;
60
47
import java .nio .file .Files ;
61
- import java .util .List ;
62
- import java .util .ServiceLoader ;
63
48
64
49
/**
65
50
* The Piranha Server runtime.
@@ -86,7 +71,7 @@ public class PlatformPiranha implements Piranha, Runnable {
86
71
/**
87
72
* Stores the feature manager.
88
73
*/
89
- private FeatureManager featureManager ;
74
+ private final FeatureManager featureManager ;
90
75
91
76
/**
92
77
* Stores the HTTP feature.
@@ -119,15 +104,10 @@ public class PlatformPiranha implements Piranha, Runnable {
119
104
private Thread thread ;
120
105
121
106
/**
122
- * Stores the HTTP web application server .
107
+ * Stores the WebAppsFeature .
123
108
*/
124
- private HttpWebApplicationServer webApplicationServer ;
125
-
126
- /**
127
- * Stores the web applications directory.
128
- */
129
- private File webAppsDir = new File ("webapps" );
130
-
109
+ private WebAppsFeature webAppsFeature ;
110
+
131
111
/**
132
112
* Constructor.
133
113
*/
@@ -137,6 +117,7 @@ public PlatformPiranha() {
137
117
configuration .setClass ("extensionCLass" , PlatformExtension .class );
138
118
configuration .setInteger ("httpPort" , 8080 );
139
119
configuration .setInteger ("httpsPort" , -1 );
120
+ configuration .setFile ("webAppsDir" , new File ("webapps" ));
140
121
featureManager = new DefaultFeatureManager ();
141
122
}
142
123
@@ -176,65 +157,15 @@ private boolean isStarted() {
176
157
public void run () {
177
158
long startTime = System .currentTimeMillis ();
178
159
LOGGER .log (INFO , () -> "Starting Piranha" );
179
-
180
- webApplicationServer = new HttpWebApplicationServer ();
181
- webApplicationServer .start ();
182
-
183
- WebApplicationServerRequestMapper requestMapper = webApplicationServer .getRequestMapper ();
184
-
185
- File [] webapps = webAppsDir .listFiles ();
186
- if (webapps != null ) {
187
- for (File webapp : webapps ) {
188
- if (webapp .getName ().toLowerCase ().endsWith (".war" )) {
189
- String contextPath = webapp .getName ().substring (0 , webapp .getName ().length () - 4 );
190
- File webAppDirectory = new File (webAppsDir , contextPath );
191
- extractWarFile (webapp , webAppDirectory );
192
-
193
- DefaultWebApplication webApplication = new PlatformWebApplication (requestMapper );
194
-
195
- webApplication .addResource (new DirectoryResource (webAppDirectory ));
196
-
197
- DefaultWebApplicationClassLoader classLoader = new DefaultWebApplicationClassLoader (webAppDirectory );
198
- webApplication .setClassLoader (classLoader );
199
-
200
- if (Boolean .getBoolean ("cloud.piranha.modular.enable" ) || configuration .getBoolean ("jpmsEnabled" , false )) {
201
- setupLayers (classLoader );
202
- }
203
-
204
- if (classLoader .getResource ("/META-INF/services/" + WebApplicationExtension .class .getName ()) == null ) {
205
- DefaultWebApplicationExtensionContext extensionContext = new DefaultWebApplicationExtensionContext ();
206
- extensionContext .add ((Class <? extends WebApplicationExtension >) configuration .getClass ("extensionClass)" ));
207
- extensionContext .configure (webApplication );
208
- } else {
209
- DefaultWebApplicationExtensionContext extensionContext = new DefaultWebApplicationExtensionContext ();
210
- ServiceLoader <WebApplicationExtension > serviceLoader = ServiceLoader .load (WebApplicationExtension .class , classLoader );
211
- extensionContext .add (serviceLoader .iterator ().next ());
212
- extensionContext .configure (webApplication );
213
- }
214
-
215
- if (contextPath .equalsIgnoreCase ("ROOT" )) {
216
- contextPath = "" ;
217
- } else if (!contextPath .startsWith ("/" )) {
218
- contextPath = "/" + contextPath ;
219
- }
220
-
221
- webApplication .setContextPath (contextPath );
222
-
223
- try {
224
- webApplication .initialize ();
225
- webApplication .start ();
226
-
227
- LOGGER .log (INFO , "Deployed " + webapp .getName () + " at " + webApplication .getContextPath ());
228
-
229
- } catch (Error e ) {
230
- LOGGER .log (ERROR , () -> "Failed to initialize app " + webapp .getName (), e );
231
- }
232
-
233
- webApplicationServer .addWebApplication (webApplication );
234
- }
235
- }
236
- }
237
160
161
+ webAppsFeature = new WebAppsFeature ();
162
+ featureManager .addFeature (webAppsFeature );
163
+ webAppsFeature .setExtensionClass ((Class <? extends WebApplicationExtension >) configuration .getClass ("extensionClass" ));
164
+ webAppsFeature .setJpmsEnabled (configuration .getBoolean ("jpmsEnabled" , false ));
165
+ webAppsFeature .setWebAppsDir (configuration .getFile ("webAppsDir" ));
166
+ webAppsFeature .init ();
167
+ webAppsFeature .start ();
168
+
238
169
/*
239
170
* Construct, initialize and start HTTP endpoint (if applicable).
240
171
*/
@@ -244,7 +175,7 @@ public void run() {
244
175
httpFeature .setHttpServerClass (configuration .getString ("httpServerClass" ));
245
176
httpFeature .setPort (configuration .getInteger ("httpPort" ));
246
177
httpFeature .init ();
247
- httpFeature .getHttpServer ().setHttpServerProcessor (webApplicationServer );
178
+ httpFeature .getHttpServer ().setHttpServerProcessor (webAppsFeature . getHttpServerProcessor () );
248
179
httpFeature .start ();
249
180
httpServer = httpFeature .getHttpServer ();
250
181
}
@@ -260,7 +191,7 @@ public void run() {
260
191
httpsFeature .setHttpsServerClass (configuration .getString ("httpsServerClass" ));
261
192
httpsFeature .setPort (configuration .getInteger ("httpsPort" ));
262
193
httpsFeature .init ();
263
- httpsFeature .getHttpsServer ().setHttpServerProcessor (webApplicationServer );
194
+ httpsFeature .getHttpsServer ().setHttpServerProcessor (webAppsFeature . getHttpServerProcessor () );
264
195
httpsFeature .start ();
265
196
if (httpServer == null ) {
266
197
httpServer = httpsFeature .getHttpsServer ();
@@ -306,8 +237,9 @@ public void run() {
306
237
}
307
238
308
239
if (!pidFile .exists ()) {
309
- webApplicationServer .stop ();
310
- stopHttpServer ();
240
+ if (httpServer != null ) {
241
+ httpServer .stop ();
242
+ }
311
243
if (httpsServer != null ) {
312
244
httpsServer .stop ();
313
245
}
@@ -332,26 +264,10 @@ public void run() {
332
264
LOGGER .log (WARNING , "Unable to create piranha.stopped file" , ioe );
333
265
}
334
266
}
335
-
267
+
336
268
featureManager .stop ();
337
269
}
338
270
339
- private void setupLayers (DefaultWebApplicationClassLoader classLoader ) {
340
- ModuleFinder defaultModuleFinder = new DefaultModuleFinder (classLoader .getResourceManager ().getResourceList ());
341
-
342
- List <String > roots = defaultModuleFinder .findAll ()
343
- .stream ()
344
- .map (ModuleReference ::descriptor )
345
- .map (ModuleDescriptor ::name )
346
- .toList ();
347
-
348
- if (!roots .isEmpty ()) {
349
- Configuration configuration = ModuleLayer .boot ().configuration ().resolveAndBind (defaultModuleFinder , ModuleFinder .of (), roots );
350
- ModuleLayer .Controller controller = ModuleLayer .defineModules (configuration , List .of (ModuleLayer .boot ()), x -> classLoader );
351
- DefaultModuleLayerProcessor .INSTANCE .processModuleLayerOptions (controller );
352
- }
353
- }
354
-
355
271
/**
356
272
* Set the HTTPS truststore file.
357
273
*
@@ -392,7 +308,7 @@ void setHttpsTruststorePassword(String httpsTruststorePassword) {
392
308
* @param webAppsDir the web applications directory.
393
309
*/
394
310
public void setWebAppsDir (File webAppsDir ) {
395
- this .webAppsDir = webAppsDir ;
311
+ this .configuration . setFile ( " webAppsDir" , webAppsDir ) ;
396
312
}
397
313
398
314
/**
@@ -444,13 +360,4 @@ public void stop() {
444
360
started = false ;
445
361
thread = null ;
446
362
}
447
-
448
- /**
449
- * Stores the HTTP server (if requested).
450
- */
451
- private void stopHttpServer () {
452
- if (httpServer != null ) {
453
- httpServer .stop ();
454
- }
455
- }
456
363
}
0 commit comments