Skip to content

Commit

Permalink
Fixes issue #3492 - Add WebAppsFeature (#3496)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem authored Jul 22, 2023
1 parent 174b9f8 commit 0bfc9ca
Show file tree
Hide file tree
Showing 14 changed files with 328 additions and 290 deletions.
26 changes: 1 addition & 25 deletions dist/platform/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@

<dependencies>
<!-- compile -->
<dependency>
<groupId>cloud.piranha.core</groupId>
<artifactId>piranha-core-impl</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.extension</groupId>
<artifactId>piranha-extension-platform</artifactId>
Expand All @@ -49,25 +43,7 @@
</dependency>
<dependency>
<groupId>cloud.piranha.feature</groupId>
<artifactId>piranha-feature-impl</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.http</groupId>
<artifactId>piranha-http-impl</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.http</groupId>
<artifactId>piranha-http-webapp</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.resource</groupId>
<artifactId>piranha-resource-impl</artifactId>
<artifactId>piranha-feature-webapps</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,21 @@
import cloud.piranha.core.api.Piranha;
import cloud.piranha.core.api.PiranhaConfiguration;
import cloud.piranha.core.api.WebApplicationExtension;
import cloud.piranha.core.api.WebApplicationServerRequestMapper;
import cloud.piranha.core.impl.DefaultModuleFinder;
import cloud.piranha.core.impl.DefaultModuleLayerProcessor;
import cloud.piranha.core.impl.DefaultPiranhaConfiguration;
import cloud.piranha.core.impl.DefaultWebApplication;
import cloud.piranha.core.impl.DefaultWebApplicationClassLoader;
import cloud.piranha.core.impl.DefaultWebApplicationExtensionContext;
import static cloud.piranha.core.impl.WarFileExtractor.extractWarFile;
import cloud.piranha.extension.platform.PlatformExtension;
import cloud.piranha.feature.api.FeatureManager;
import cloud.piranha.feature.exitonstop.ExitOnStopFeature;
import cloud.piranha.feature.http.HttpFeature;
import cloud.piranha.feature.https.HttpsFeature;
import cloud.piranha.feature.impl.DefaultFeatureManager;
import cloud.piranha.feature.webapps.WebAppsFeature;
import cloud.piranha.http.api.HttpServer;
import cloud.piranha.http.webapp.HttpWebApplicationServer;
import cloud.piranha.resource.impl.DirectoryResource;
import java.io.File;
import java.io.IOException;
import java.lang.System.Logger;
import static java.lang.System.Logger.Level.ERROR;
import static java.lang.System.Logger.Level.INFO;
import static java.lang.System.Logger.Level.WARNING;
import java.lang.module.Configuration;
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleReference;
import java.nio.file.Files;
import java.util.List;
import java.util.ServiceLoader;

/**
* The Piranha Server runtime.
Expand All @@ -86,7 +71,7 @@ public class PlatformPiranha implements Piranha, Runnable {
/**
* Stores the feature manager.
*/
private FeatureManager featureManager;
private final FeatureManager featureManager;

/**
* Stores the HTTP feature.
Expand Down Expand Up @@ -119,15 +104,10 @@ public class PlatformPiranha implements Piranha, Runnable {
private Thread thread;

/**
* Stores the HTTP web application server.
* Stores the WebAppsFeature.
*/
private HttpWebApplicationServer webApplicationServer;

/**
* Stores the web applications directory.
*/
private File webAppsDir = new File("webapps");

private WebAppsFeature webAppsFeature;

/**
* Constructor.
*/
Expand All @@ -137,6 +117,7 @@ public PlatformPiranha() {
configuration.setClass("extensionCLass", PlatformExtension.class);
configuration.setInteger("httpPort", 8080);
configuration.setInteger("httpsPort", -1);
configuration.setFile("webAppsDir", new File("webapps"));
featureManager = new DefaultFeatureManager();
}

Expand Down Expand Up @@ -176,65 +157,15 @@ private boolean isStarted() {
public void run() {
long startTime = System.currentTimeMillis();
LOGGER.log(INFO, () -> "Starting Piranha");

webApplicationServer = new HttpWebApplicationServer();
webApplicationServer.start();

WebApplicationServerRequestMapper requestMapper = webApplicationServer.getRequestMapper();

File[] webapps = webAppsDir.listFiles();
if (webapps != null) {
for (File webapp : webapps) {
if (webapp.getName().toLowerCase().endsWith(".war")) {
String contextPath = webapp.getName().substring(0, webapp.getName().length() - 4);
File webAppDirectory = new File(webAppsDir, contextPath);
extractWarFile(webapp, webAppDirectory);

DefaultWebApplication webApplication = new PlatformWebApplication(requestMapper);

webApplication.addResource(new DirectoryResource(webAppDirectory));

DefaultWebApplicationClassLoader classLoader = new DefaultWebApplicationClassLoader(webAppDirectory);
webApplication.setClassLoader(classLoader);

if (Boolean.getBoolean("cloud.piranha.modular.enable") || configuration.getBoolean("jpmsEnabled", false)) {
setupLayers(classLoader);
}

if (classLoader.getResource("/META-INF/services/" + WebApplicationExtension.class.getName()) == null) {
DefaultWebApplicationExtensionContext extensionContext = new DefaultWebApplicationExtensionContext();
extensionContext.add((Class<? extends WebApplicationExtension>) configuration.getClass("extensionClass)"));
extensionContext.configure(webApplication);
} else {
DefaultWebApplicationExtensionContext extensionContext = new DefaultWebApplicationExtensionContext();
ServiceLoader<WebApplicationExtension> serviceLoader = ServiceLoader.load(WebApplicationExtension.class, classLoader);
extensionContext.add(serviceLoader.iterator().next());
extensionContext.configure(webApplication);
}

if (contextPath.equalsIgnoreCase("ROOT")) {
contextPath = "";
} else if (!contextPath.startsWith("/")) {
contextPath = "/" + contextPath;
}

webApplication.setContextPath(contextPath);

try {
webApplication.initialize();
webApplication.start();

LOGGER.log(INFO, "Deployed " + webapp.getName() + " at " + webApplication.getContextPath());

} catch (Error e) {
LOGGER.log(ERROR, () -> "Failed to initialize app " + webapp.getName(), e);
}

webApplicationServer.addWebApplication(webApplication);
}
}
}

webAppsFeature = new WebAppsFeature();
featureManager.addFeature(webAppsFeature);
webAppsFeature.setExtensionClass((Class<? extends WebApplicationExtension>) configuration.getClass("extensionClass"));
webAppsFeature.setJpmsEnabled(configuration.getBoolean("jpmsEnabled", false));
webAppsFeature.setWebAppsDir(configuration.getFile("webAppsDir"));
webAppsFeature.init();
webAppsFeature.start();

/*
* Construct, initialize and start HTTP endpoint (if applicable).
*/
Expand All @@ -244,7 +175,7 @@ public void run() {
httpFeature.setHttpServerClass(configuration.getString("httpServerClass"));
httpFeature.setPort(configuration.getInteger("httpPort"));
httpFeature.init();
httpFeature.getHttpServer().setHttpServerProcessor(webApplicationServer);
httpFeature.getHttpServer().setHttpServerProcessor(webAppsFeature.getHttpServerProcessor());
httpFeature.start();
httpServer = httpFeature.getHttpServer();
}
Expand All @@ -260,7 +191,7 @@ public void run() {
httpsFeature.setHttpsServerClass(configuration.getString("httpsServerClass"));
httpsFeature.setPort(configuration.getInteger("httpsPort"));
httpsFeature.init();
httpsFeature.getHttpsServer().setHttpServerProcessor(webApplicationServer);
httpsFeature.getHttpsServer().setHttpServerProcessor(webAppsFeature.getHttpServerProcessor());
httpsFeature.start();
if (httpServer == null) {
httpServer = httpsFeature.getHttpsServer();
Expand Down Expand Up @@ -306,8 +237,9 @@ public void run() {
}

if (!pidFile.exists()) {
webApplicationServer.stop();
stopHttpServer();
if (httpServer != null) {
httpServer.stop();
}
if (httpsServer != null) {
httpsServer.stop();
}
Expand All @@ -332,26 +264,10 @@ public void run() {
LOGGER.log(WARNING, "Unable to create piranha.stopped file", ioe);
}
}

featureManager.stop();
}

private void setupLayers(DefaultWebApplicationClassLoader classLoader) {
ModuleFinder defaultModuleFinder = new DefaultModuleFinder(classLoader.getResourceManager().getResourceList());

List<String> roots = defaultModuleFinder.findAll()
.stream()
.map(ModuleReference::descriptor)
.map(ModuleDescriptor::name)
.toList();

if (!roots.isEmpty()) {
Configuration configuration = ModuleLayer.boot().configuration().resolveAndBind(defaultModuleFinder, ModuleFinder.of(), roots);
ModuleLayer.Controller controller = ModuleLayer.defineModules(configuration, List.of(ModuleLayer.boot()), x -> classLoader);
DefaultModuleLayerProcessor.INSTANCE.processModuleLayerOptions(controller);
}
}

/**
* Set the HTTPS truststore file.
*
Expand Down Expand Up @@ -392,7 +308,7 @@ void setHttpsTruststorePassword(String httpsTruststorePassword) {
* @param webAppsDir the web applications directory.
*/
public void setWebAppsDir(File webAppsDir) {
this.webAppsDir = webAppsDir;
this.configuration.setFile("webAppsDir", webAppsDir);
}

/**
Expand Down Expand Up @@ -444,13 +360,4 @@ public void stop() {
started = false;
thread = null;
}

/**
* Stores the HTTP server (if requested).
*/
private void stopHttpServer() {
if (httpServer != null) {
httpServer.stop();
}
}
}
3 changes: 1 addition & 2 deletions dist/platform/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
requires cloud.piranha.feature.exitonstop;
requires cloud.piranha.feature.http;
requires cloud.piranha.feature.https;
requires cloud.piranha.feature.impl;
requires cloud.piranha.http.webapp;
requires cloud.piranha.feature.webapps;
requires java.logging;
requires java.naming;
}
21 changes: 1 addition & 20 deletions dist/server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@

<dependencies>
<!-- compile -->
<dependency>
<groupId>cloud.piranha.core</groupId>
<artifactId>piranha-core-impl</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.extension</groupId>
<artifactId>piranha-extension-servlet</artifactId>
Expand All @@ -49,19 +43,7 @@
</dependency>
<dependency>
<groupId>cloud.piranha.feature</groupId>
<artifactId>piranha-feature-impl</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.http</groupId>
<artifactId>piranha-http-webapp</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.resource</groupId>
<artifactId>piranha-resource-impl</artifactId>
<artifactId>piranha-feature-webapps</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
Expand Down Expand Up @@ -231,5 +213,4 @@
</dependencies>
</profile>
</profiles>

</project>
Loading

0 comments on commit 0bfc9ca

Please sign in to comment.