Skip to content

Commit

Permalink
Fixes issue piranhacloud#3464 - Update all distributions where applic…
Browse files Browse the repository at this point in the history
…able to use the HttpFeature
  • Loading branch information
mnriem committed Jun 29, 2023
1 parent 53fa386 commit c5d2926
Show file tree
Hide file tree
Showing 16 changed files with 220 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,18 @@ public class CoreProfilePiranha implements Piranha, Runnable {
/**
* Stores the HTTP feature.
*/
private final HttpFeature httpFeature = new HttpFeature();

private HttpFeature httpFeature;

/**
* Stores the HTTP port.
*/
private int httpPort = 8080;

/**
* Stores the HTTP server class.
*/
private String httpServerClass;

/**
* Stores the HTTPS feature.
*/
Expand Down Expand Up @@ -200,14 +210,17 @@ public void run() {
webApplicationServer.start();

/*
* Initialize and start HTTP endpoint (if applicable).
* Construct, initialize and start HTTP endpoint (if applicable).
*/
if (httpFeature.getPort() > 0) {
if (httpPort > 0) {
httpFeature = new HttpFeature();
httpFeature.setHttpServerClass(httpServerClass);
httpFeature.setPort(httpPort);
httpFeature.init();
httpFeature.getHttpServer().setHttpServerProcessor(webApplicationServer);
httpFeature.start();
}

/**
* Initialize and start the HTTPS endpoint (if applicable).
*/
Expand All @@ -216,7 +229,7 @@ public void run() {
httpsFeature.getHttpsServer().setHttpServerProcessor(webApplicationServer);
httpsFeature.start();
}

long finishTime = System.currentTimeMillis();
LOGGER.log(INFO, "Started Piranha");
LOGGER.log(INFO, "It took {0} milliseconds", finishTime - startTime);
Expand All @@ -226,7 +239,7 @@ public void run() {
if (!pidFile.getParentFile().exists() && !pidFile.getParentFile().mkdirs()) {
LOGGER.log(WARNING, "Unable to create tmp directory for PID file");
}
try ( PrintWriter writer = new PrintWriter(new FileWriter(pidFile))) {
try (PrintWriter writer = new PrintWriter(new FileWriter(pidFile))) {
writer.println(pid);
writer.flush();
} catch (IOException ioe) {
Expand Down Expand Up @@ -305,7 +318,7 @@ public void setExitOnStop(boolean exitOnStop) {
* @param httpPort the HTTP server port.
*/
public void setHttpPort(int httpPort) {
httpFeature.setPort(httpPort);
this.httpPort = httpPort;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ public class IsolatedPiranha implements Piranha, Runnable {
*/
private static IsolatedPiranha theOneAndOnlyInstance;

/**
* Stores the HTTP feature.
*/
private HttpFeature httpFeature;

/**
* Stores the HTTP port.
*/
private int httpPort = 8080;

/**
* Stores the HTTP server class.
*/
private String httpServerClass;

/**
* Stores the SSL flag.
*/
Expand All @@ -79,10 +94,12 @@ public class IsolatedPiranha implements Piranha, Runnable {
/**
* Stores the HTTP web application server.
*/
private HttpWebApplicationServer webApplicationServer;
private HttpWebApplicationServer webApplicationServer;

/**
* {@return the instance}
* {
*
* @return the instance}
*/
public static IsolatedPiranha get() {
return theOneAndOnlyInstance;
Expand All @@ -106,8 +123,14 @@ public static void main(String[] arguments) {
*/
private void processArguments(String[] arguments) {
if (arguments != null) {
for (String argument : arguments) {
if (argument.equals("--ssl")) {
for (int i = 0; i < arguments.length; i++) {
if (arguments[i].equals("--http-port")) {
httpPort = Integer.parseInt(arguments[i + 1]);
}
if (arguments[i].equals("--http-server-class")) {
httpServerClass = arguments[i + 1];
}
if (arguments[i].equals("--ssl")) {
ssl = true;
}
}
Expand Down Expand Up @@ -143,7 +166,7 @@ public void run() {

try {
Files.delete(deployingFile.toPath());
} catch(IOException ioe) {
} catch (IOException ioe) {
LOGGER.log(WARNING, "Unable to delete deploying file", ioe);
}
}
Expand All @@ -155,22 +178,25 @@ public void run() {
File startedFile = createStartedFile();

File pidFile = new File("tmp/piranha.pid");
HttpServer httpServer;
HttpServer httpServer = null;

if (ssl) {
httpServer = new DefaultHttpServer();
httpServer.setServerPort(8080);
httpServer.setHttpServerProcessor(webApplicationServer);
httpServer.setSSL(ssl);
httpServer.start();
} else {
/*
* Use HTTP feature.
*/
HttpFeature httpFeature = new HttpFeature();
httpFeature.setPort(8080);
}

/*
* Construct, initialize and start HTTP endpoint (if applicable).
*/
if (!ssl && httpPort > 0) {
httpFeature = new HttpFeature();
httpFeature.setHttpServerClass(httpServerClass);
httpFeature.setPort(httpPort);
httpFeature.init();
httpFeature.getHttpServer().setHttpServerProcessor(webApplicationServer);;
httpFeature.getHttpServer().setHttpServerProcessor(webApplicationServer);
httpFeature.start();
httpServer = httpFeature.getHttpServer();
}
Expand All @@ -187,13 +213,13 @@ public void run() {
httpServer.stop();
try {
Files.delete(startedFile.toPath());
} catch(IOException ioe) {
} catch (IOException ioe) {
LOGGER.log(WARNING, "Unable to delete PID file", ioe);
}
System.exit(0);
}
}

finishTime = System.currentTimeMillis();
LOGGER.log(INFO, "Stopped Piranha");
LOGGER.log(INFO, "We ran for {0} milliseconds", finishTime - startTime);
Expand Down Expand Up @@ -257,10 +283,10 @@ private File createStartedFile() {

return startedFile;
}

@Override
public void service(WebApplicationRequest request, WebApplicationResponse response)
public void service(WebApplicationRequest request, WebApplicationResponse response)
throws IOException, ServletException {
webApplicationServer.service(request, response);
}
}
}
6 changes: 6 additions & 0 deletions dist/microprofile/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.feature</groupId>
<artifactId>piranha-feature-http</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.http</groupId>
<artifactId>piranha-http-impl</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import cloud.piranha.core.impl.DefaultWebApplicationExtensionContext;
import static cloud.piranha.core.impl.WarFileExtractor.extractWarFile;
import cloud.piranha.extension.microprofile.MicroProfileExtension;
import cloud.piranha.feature.http.HttpFeature;
import cloud.piranha.http.api.HttpServer;
import cloud.piranha.http.impl.DefaultHttpServer;
import cloud.piranha.http.webapp.HttpWebApplicationServer;
Expand Down Expand Up @@ -86,6 +87,11 @@ public class MicroProfilePiranha implements Piranha, Runnable {
*/
private boolean exitOnStop = true;

/**
* Stores the HTTP feature.
*/
private HttpFeature httpFeature;

/**
* Stores the HTTP port.
*/
Expand Down Expand Up @@ -151,27 +157,6 @@ public void run() {

webApplicationServer = new HttpWebApplicationServer();

if (httpPort > 0) {
HttpServer httpServer = null;
if (httpServerClass == null) {
httpServerClass = DefaultHttpServer.class.getName();
}
try {
httpServer = (HttpServer) Class.forName(httpServerClass)
.getDeclaredConstructor().newInstance();
} catch (ClassNotFoundException | IllegalAccessException
| IllegalArgumentException | InstantiationException
| NoSuchMethodException | SecurityException
| InvocationTargetException t) {
LOGGER.log(ERROR, "Unable to construct HTTP server", t);
}
if (httpServer != null) {
httpServer.setServerPort(httpPort);
httpServer.setHttpServerProcessor(webApplicationServer);
httpServer.start();
}
}

if (httpsPort > 0) {
HttpServer httpsServer = null;
if (httpsServerClass == null) {
Expand Down Expand Up @@ -253,6 +238,18 @@ public void run() {

webApplicationServer.start();

/*
* Construct, initialize and start HTTP endpoint (if applicable).
*/
if (httpPort > 0) {
httpFeature = new HttpFeature();
httpFeature.setHttpServerClass(httpServerClass);
httpFeature.setPort(httpPort);
httpFeature.init();
httpFeature.getHttpServer().setHttpServerProcessor(webApplicationServer);
httpFeature.start();
}

long finishTime = System.currentTimeMillis();
LOGGER.log(INFO, "Started Piranha");
LOGGER.log(INFO, "It took {0} milliseconds", finishTime - startTime);
Expand All @@ -262,7 +259,7 @@ public void run() {
if (!pidFile.getParentFile().exists() && !pidFile.getParentFile().mkdirs()) {
LOGGER.log(WARNING, "Unable to create tmp directory for PID file");
}
try ( PrintWriter writer = new PrintWriter(new FileWriter(pidFile))) {
try (PrintWriter writer = new PrintWriter(new FileWriter(pidFile))) {
writer.println(pid);
writer.flush();
} catch (IOException ioe) {
Expand Down
1 change: 1 addition & 0 deletions dist/microprofile/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
opens cloud.piranha.dist.microprofile;
requires transitive cloud.piranha.core.api;
requires cloud.piranha.core.impl;
requires cloud.piranha.feature.http;
requires cloud.piranha.extension.microprofile;
requires cloud.piranha.http.impl;
requires cloud.piranha.http.webapp;
Expand Down
6 changes: 6 additions & 0 deletions dist/platform/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.feature</groupId>
<artifactId>piranha-feature-http</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.http</groupId>
<artifactId>piranha-http-impl</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import cloud.piranha.core.impl.DefaultWebApplicationClassLoader;
import cloud.piranha.core.impl.DefaultWebApplicationExtensionContext;
import static cloud.piranha.core.impl.WarFileExtractor.extractWarFile;
import cloud.piranha.feature.http.HttpFeature;
import cloud.piranha.http.api.HttpServer;
import cloud.piranha.http.impl.DefaultHttpServer;
import cloud.piranha.http.webapp.HttpWebApplicationServer;
Expand Down Expand Up @@ -85,6 +86,11 @@ public class PlatformPiranha implements Piranha, Runnable {
*/
private boolean exitOnStop = true;

/**
* Stores the HTTP feature.
*/
private HttpFeature httpFeature;

/**
* Stores the HTTP port.
*/
Expand Down Expand Up @@ -174,7 +180,6 @@ public void run() {

webApplicationServer = new HttpWebApplicationServer();

startHttpServer();
startHttpsServer();

webApplicationServer.start();
Expand Down Expand Up @@ -233,6 +238,19 @@ public void run() {
}
}
}

/*
* Construct, initialize and start HTTP endpoint (if applicable).
*/
if (httpPort > 0) {
httpFeature = new HttpFeature();
httpFeature.setHttpServerClass(httpServerClass);
httpFeature.setPort(httpPort);
httpFeature.init();
httpFeature.getHttpServer().setHttpServerProcessor(webApplicationServer);
httpFeature.start();
httpServer = httpFeature.getHttpServer();
}

long finishTime = System.currentTimeMillis();
LOGGER.log(INFO, "Started Piranha");
Expand Down
1 change: 1 addition & 0 deletions dist/platform/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
requires cloud.piranha.core.api;
requires cloud.piranha.core.impl;
requires cloud.piranha.extension.platform;
requires cloud.piranha.feature.http;
requires cloud.piranha.http.api;
requires cloud.piranha.http.impl;
requires cloud.piranha.http.webapp;
Expand Down
6 changes: 6 additions & 0 deletions dist/server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.feature</groupId>
<artifactId>piranha-feature-http</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.http</groupId>
<artifactId>piranha-http-impl</artifactId>
Expand Down
Loading

0 comments on commit c5d2926

Please sign in to comment.