Skip to content

Commit 0bfc9ca

Browse files
authored
Fixes issue #3492 - Add WebAppsFeature (#3496)
1 parent 174b9f8 commit 0bfc9ca

File tree

14 files changed

+328
-290
lines changed

14 files changed

+328
-290
lines changed

dist/platform/pom.xml

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717

1818
<dependencies>
1919
<!-- compile -->
20-
<dependency>
21-
<groupId>cloud.piranha.core</groupId>
22-
<artifactId>piranha-core-impl</artifactId>
23-
<version>${project.version}</version>
24-
<scope>compile</scope>
25-
</dependency>
2620
<dependency>
2721
<groupId>cloud.piranha.extension</groupId>
2822
<artifactId>piranha-extension-platform</artifactId>
@@ -49,25 +43,7 @@
4943
</dependency>
5044
<dependency>
5145
<groupId>cloud.piranha.feature</groupId>
52-
<artifactId>piranha-feature-impl</artifactId>
53-
<version>${project.version}</version>
54-
<scope>compile</scope>
55-
</dependency>
56-
<dependency>
57-
<groupId>cloud.piranha.http</groupId>
58-
<artifactId>piranha-http-impl</artifactId>
59-
<version>${project.version}</version>
60-
<scope>compile</scope>
61-
</dependency>
62-
<dependency>
63-
<groupId>cloud.piranha.http</groupId>
64-
<artifactId>piranha-http-webapp</artifactId>
65-
<version>${project.version}</version>
66-
<scope>compile</scope>
67-
</dependency>
68-
<dependency>
69-
<groupId>cloud.piranha.resource</groupId>
70-
<artifactId>piranha-resource-impl</artifactId>
46+
<artifactId>piranha-feature-webapps</artifactId>
7147
<version>${project.version}</version>
7248
<scope>compile</scope>
7349
</dependency>

dist/platform/src/main/java/cloud/piranha/dist/platform/PlatformPiranha.java

Lines changed: 21 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -30,36 +30,21 @@
3030
import cloud.piranha.core.api.Piranha;
3131
import cloud.piranha.core.api.PiranhaConfiguration;
3232
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;
3633
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;
4134
import cloud.piranha.extension.platform.PlatformExtension;
4235
import cloud.piranha.feature.api.FeatureManager;
4336
import cloud.piranha.feature.exitonstop.ExitOnStopFeature;
4437
import cloud.piranha.feature.http.HttpFeature;
4538
import cloud.piranha.feature.https.HttpsFeature;
4639
import cloud.piranha.feature.impl.DefaultFeatureManager;
40+
import cloud.piranha.feature.webapps.WebAppsFeature;
4741
import cloud.piranha.http.api.HttpServer;
48-
import cloud.piranha.http.webapp.HttpWebApplicationServer;
49-
import cloud.piranha.resource.impl.DirectoryResource;
5042
import java.io.File;
5143
import java.io.IOException;
5244
import java.lang.System.Logger;
53-
import static java.lang.System.Logger.Level.ERROR;
5445
import static java.lang.System.Logger.Level.INFO;
5546
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;
6047
import java.nio.file.Files;
61-
import java.util.List;
62-
import java.util.ServiceLoader;
6348

6449
/**
6550
* The Piranha Server runtime.
@@ -86,7 +71,7 @@ public class PlatformPiranha implements Piranha, Runnable {
8671
/**
8772
* Stores the feature manager.
8873
*/
89-
private FeatureManager featureManager;
74+
private final FeatureManager featureManager;
9075

9176
/**
9277
* Stores the HTTP feature.
@@ -119,15 +104,10 @@ public class PlatformPiranha implements Piranha, Runnable {
119104
private Thread thread;
120105

121106
/**
122-
* Stores the HTTP web application server.
107+
* Stores the WebAppsFeature.
123108
*/
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+
131111
/**
132112
* Constructor.
133113
*/
@@ -137,6 +117,7 @@ public PlatformPiranha() {
137117
configuration.setClass("extensionCLass", PlatformExtension.class);
138118
configuration.setInteger("httpPort", 8080);
139119
configuration.setInteger("httpsPort", -1);
120+
configuration.setFile("webAppsDir", new File("webapps"));
140121
featureManager = new DefaultFeatureManager();
141122
}
142123

@@ -176,65 +157,15 @@ private boolean isStarted() {
176157
public void run() {
177158
long startTime = System.currentTimeMillis();
178159
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-
}
237160

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+
238169
/*
239170
* Construct, initialize and start HTTP endpoint (if applicable).
240171
*/
@@ -244,7 +175,7 @@ public void run() {
244175
httpFeature.setHttpServerClass(configuration.getString("httpServerClass"));
245176
httpFeature.setPort(configuration.getInteger("httpPort"));
246177
httpFeature.init();
247-
httpFeature.getHttpServer().setHttpServerProcessor(webApplicationServer);
178+
httpFeature.getHttpServer().setHttpServerProcessor(webAppsFeature.getHttpServerProcessor());
248179
httpFeature.start();
249180
httpServer = httpFeature.getHttpServer();
250181
}
@@ -260,7 +191,7 @@ public void run() {
260191
httpsFeature.setHttpsServerClass(configuration.getString("httpsServerClass"));
261192
httpsFeature.setPort(configuration.getInteger("httpsPort"));
262193
httpsFeature.init();
263-
httpsFeature.getHttpsServer().setHttpServerProcessor(webApplicationServer);
194+
httpsFeature.getHttpsServer().setHttpServerProcessor(webAppsFeature.getHttpServerProcessor());
264195
httpsFeature.start();
265196
if (httpServer == null) {
266197
httpServer = httpsFeature.getHttpsServer();
@@ -306,8 +237,9 @@ public void run() {
306237
}
307238

308239
if (!pidFile.exists()) {
309-
webApplicationServer.stop();
310-
stopHttpServer();
240+
if (httpServer != null) {
241+
httpServer.stop();
242+
}
311243
if (httpsServer != null) {
312244
httpsServer.stop();
313245
}
@@ -332,26 +264,10 @@ public void run() {
332264
LOGGER.log(WARNING, "Unable to create piranha.stopped file", ioe);
333265
}
334266
}
335-
267+
336268
featureManager.stop();
337269
}
338270

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-
355271
/**
356272
* Set the HTTPS truststore file.
357273
*
@@ -392,7 +308,7 @@ void setHttpsTruststorePassword(String httpsTruststorePassword) {
392308
* @param webAppsDir the web applications directory.
393309
*/
394310
public void setWebAppsDir(File webAppsDir) {
395-
this.webAppsDir = webAppsDir;
311+
this.configuration.setFile("webAppsDir", webAppsDir);
396312
}
397313

398314
/**
@@ -444,13 +360,4 @@ public void stop() {
444360
started = false;
445361
thread = null;
446362
}
447-
448-
/**
449-
* Stores the HTTP server (if requested).
450-
*/
451-
private void stopHttpServer() {
452-
if (httpServer != null) {
453-
httpServer.stop();
454-
}
455-
}
456363
}

dist/platform/src/main/java/module-info.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@
3939
requires cloud.piranha.feature.exitonstop;
4040
requires cloud.piranha.feature.http;
4141
requires cloud.piranha.feature.https;
42-
requires cloud.piranha.feature.impl;
43-
requires cloud.piranha.http.webapp;
42+
requires cloud.piranha.feature.webapps;
4443
requires java.logging;
4544
requires java.naming;
4645
}

dist/server/pom.xml

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717

1818
<dependencies>
1919
<!-- compile -->
20-
<dependency>
21-
<groupId>cloud.piranha.core</groupId>
22-
<artifactId>piranha-core-impl</artifactId>
23-
<version>${project.version}</version>
24-
<scope>compile</scope>
25-
</dependency>
2620
<dependency>
2721
<groupId>cloud.piranha.extension</groupId>
2822
<artifactId>piranha-extension-servlet</artifactId>
@@ -49,19 +43,7 @@
4943
</dependency>
5044
<dependency>
5145
<groupId>cloud.piranha.feature</groupId>
52-
<artifactId>piranha-feature-impl</artifactId>
53-
<version>${project.version}</version>
54-
<scope>compile</scope>
55-
</dependency>
56-
<dependency>
57-
<groupId>cloud.piranha.http</groupId>
58-
<artifactId>piranha-http-webapp</artifactId>
59-
<version>${project.version}</version>
60-
<scope>compile</scope>
61-
</dependency>
62-
<dependency>
63-
<groupId>cloud.piranha.resource</groupId>
64-
<artifactId>piranha-resource-impl</artifactId>
46+
<artifactId>piranha-feature-webapps</artifactId>
6547
<version>${project.version}</version>
6648
<scope>compile</scope>
6749
</dependency>
@@ -231,5 +213,4 @@
231213
</dependencies>
232214
</profile>
233215
</profiles>
234-
235216
</project>

0 commit comments

Comments
 (0)