Skip to content

Commit

Permalink
Fixes piranhacloud#3820 - Refactor other distribution using SinglePir…
Browse files Browse the repository at this point in the history
…anha to use … (piranhacloud#3821)
  • Loading branch information
mnriem authored Jul 22, 2024
1 parent 67a4d25 commit 9ea6288
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 388 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,20 @@
package cloud.piranha.dist.microprofile;

import cloud.piranha.extension.microprofile.MicroProfileExtension;
import cloud.piranha.single.SingleMain;
import cloud.piranha.single.SinglePiranhaBuilder;
import static java.lang.System.Logger.Level.WARNING;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;

/**
* The Main for Piranha Web Profile.
* The Main for Piranha Micro Profile.
*
* <p>
* This version of Main sets the extension class to the MicroProfileExtension to
* deliver Piranha Micro Profile (unless it was overridden).
* </p>
*
* @author Manfred Riem ([email protected])
*/
public class MicroProfilePiranhaMain {

/**
* Stores the logger.
*/
private static final Logger LOGGER = System.getLogger(MicroProfilePiranhaMain.class.getName());
public class MicroProfilePiranhaMain extends SingleMain {

/**
* Main method.
Expand All @@ -53,126 +51,12 @@ public class MicroProfilePiranhaMain {
public static void main(String[] arguments) {
SinglePiranhaBuilder builder = new MicroProfilePiranhaMain().processArguments(arguments);
if (builder != null) {
if (builder.getConfiguration().getClass("extensionClass") == null) {
builder.extensionClass(MicroProfileExtension.class);
}
builder.build().start();
} else {
showHelp();
}
}

/**
* Process the arguments.
*
* @param arguments the arguments.
*/
private SinglePiranhaBuilder processArguments(String[] arguments) {

SinglePiranhaBuilder builder = new SinglePiranhaBuilder()
.extensionClass(MicroProfileExtension.class)
.exitOnStop(true);
int httpPort = 0;
int httpsPort = 0;
if (arguments != null) {
for (int i = 0; i < arguments.length; i++) {
if (arguments[i].equals("--extension-class")) {
builder = builder.extensionClass(arguments[i + 1]);
}
if (arguments[i].equals("--context-path")) {
builder = builder.contextPath(arguments[i + 1]);
}
if (arguments[i].equals("--help")) {
return null;
}
if (arguments[i].equals("--http-port")) {
int arg = Integer.parseInt(arguments[i + 1]);
builder = builder.httpPort(arg);
httpPort = arg;
}
if (arguments[i].equals("--http-server-class")) {
builder = builder.httpServerClass(arguments[i + 1]);
}
if (arguments[i].equals("--https-keystore-file")) {
builder = builder.httpsKeystoreFile(arguments[i + 1]);
}
if (arguments[i].equals("--https-keystore-password")) {
builder = builder.httpsKeystorePassword(arguments[i + 1]);
}
if (arguments[i].equals("--https-port")) {
int arg = Integer.parseInt(arguments[i + 1]);
builder = builder.httpsPort(arg);
httpsPort = arg;
}
if (arguments[i].equals("--https-server-class")) {
builder = builder.httpsServerClass(arguments[i + 1]);
}
if (arguments[i].equals("--https-truststore-file")) {
builder = builder.httpsTruststoreFile(arguments[i + 1]);
}
if (arguments[i].equals("--https-truststore-password")) {
builder = builder.httpsTruststorePassword(arguments[i + 1]);
}
if (arguments[i].equals("--jpms")) {
builder = builder.jpms(true);
}
if (arguments[i].equals("--logging-level")) {
builder = builder.loggingLevel(arguments[i + 1]);
}
if (arguments[i].equals("--verbose")) {
builder = builder.verbose(true);
}
if (arguments[i].equals("--war-file")) {
builder = builder.warFile(arguments[i + 1]);
}
if (arguments[i].equals("--webapp-dir")) {
builder = builder.webAppDir(arguments[i + 1]);
}
if (arguments[i].equals("--write-pid")) {
builder = builder.pid(ProcessHandle.current().pid());
}
}
checkPorts(httpPort, httpsPort);
}
return builder;
}

private void checkPorts(int httpPort, int httpsPort) {
if(httpsPort != 0 && httpPort == httpsPort) {
LOGGER.log(WARNING, "The http and the https ports are the same. Please use different ports");
System.exit(-1);
}
}

/**
* Show help.
*/
private static void showHelp() {
LOGGER.log(Level.INFO, "");
LOGGER.log(Level.INFO,
"""
--extension-class <className> - Set the extension to use
--help - Show this help
--context-path <string> - Set the Servlet context path
--http-port <integer> - Set the HTTP port (use -1 to disable)
--http-server-class <className> - Set the HTTP server class to use
--https-keystore-file <file> - Set the HTTPS keystore file (applies to
the whole JVM)
--https-keystore-password <string> - Set the HTTPS keystore password
(applies to the whole JVM)
--https-port <integer> - Set the HTTPS port (disabled by
default)
--https-server-class <className> - Set the HTTPS server class to use
--https-truststore-file <file> - Set the HTTPS keystore file (applies to
the whole JVM)
--https-truststore-password <string> - Set the HTTPS keystore password
(applies to the whole JVM)
--jpms - Enable Java Platform Module System
--logging-level - Set the logging level
--verbose - Shows the runtime parameters
--war-file <file> - The WAR file to deploy
--webapp-dir <directory> - The directory to use for the web
application (auto creates when it does
not exist, if omitted runtime will use
the filename portion of --war-file)
--write-pid - Write out a PID file
""");
}
}
3 changes: 1 addition & 2 deletions dist/microprofile/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/

/**
* This module delivers the Piranha MicroProfile distribution.
* This module delivers the Piranha Micro Profile distribution.
*
* @author Manfred Riem ([email protected])
*/
Expand All @@ -37,5 +37,4 @@
opens cloud.piranha.dist.microprofile;
requires cloud.piranha.extension.microprofile;
requires cloud.piranha.single;
requires java.logging;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,20 @@
package cloud.piranha.dist.servlet;

import cloud.piranha.extension.servlet.ServletExtension;
import cloud.piranha.single.SingleMain;
import cloud.piranha.single.SinglePiranhaBuilder;
import static java.lang.System.Logger.Level.WARNING;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;

/**
* The Main for Piranha Servlet.
*
* <p>
* This version of Main sets the extension class to the ServletExtension to
* deliver Piranha Servlet (unless it was overridden).
* </p>
*
* @author Manfred Riem ([email protected])
*/
public class ServletPiranhaMain {

/**
* Stores the logger.
*/
private static final Logger LOGGER = System.getLogger(ServletPiranhaMain.class.getName());
public class ServletPiranhaMain extends SingleMain {

/**
* Main method.
Expand All @@ -53,130 +51,12 @@ public class ServletPiranhaMain {
public static void main(String[] arguments) {
SinglePiranhaBuilder builder = new ServletPiranhaMain().processArguments(arguments);
if (builder != null) {
if (builder.getConfiguration().getClass("extensionClass") == null) {
builder.extensionClass(ServletExtension.class);
}
builder.build().start();
} else {
showHelp();
}
}

/**
* Process the arguments.
*
* @param arguments the arguments.
*/
private SinglePiranhaBuilder processArguments(String[] arguments) {

SinglePiranhaBuilder builder = new SinglePiranhaBuilder()
.extensionClass(ServletExtension.class)
.exitOnStop(true);
int httpPort = 0;
int httpsPort = 0;
if (arguments != null) {
for (int i = 0; i < arguments.length; i++) {
if (arguments[i].equals("--extension-class")) {
builder = builder.extensionClass(arguments[i + 1]);
}
if (arguments[i].equals("--context-path")) {
builder = builder.contextPath(arguments[i + 1]);
}
if (arguments[i].equals("--enable-crac")) {
builder = builder.crac(true);
}
if (arguments[i].equals("--help")) {
return null;
}
if (arguments[i].equals("--http-port")) {
int arg = Integer.parseInt(arguments[i + 1]);
builder = builder.httpPort(arg);
httpPort = arg;
}
if (arguments[i].equals("--http-server-class")) {
builder = builder.httpServerClass(arguments[i + 1]);
}
if (arguments[i].equals("--https-keystore-file")) {
builder = builder.httpsKeystoreFile(arguments[i + 1]);
}
if (arguments[i].equals("--https-keystore-password")) {
builder = builder.httpsKeystorePassword(arguments[i + 1]);
}
if (arguments[i].equals("--https-port")) {
int arg = Integer.parseInt(arguments[i + 1]);
builder = builder.httpsPort(arg);
httpsPort = arg;
}
if (arguments[i].equals("--https-server-class")) {
builder = builder.httpsServerClass(arguments[i + 1]);
}
if (arguments[i].equals("--https-truststore-file")) {
builder = builder.httpsTruststoreFile(arguments[i + 1]);
}
if (arguments[i].equals("--https-truststore-password")) {
builder = builder.httpsTruststorePassword(arguments[i + 1]);
}
if (arguments[i].equals("--jpms")) {
builder = builder.jpms(true);
}
if (arguments[i].equals("--logging-level")) {
builder = builder.loggingLevel(arguments[i + 1]);
}
if (arguments[i].equals("--verbose")) {
builder = builder.verbose(true);
}
if (arguments[i].equals("--war-file")) {
builder = builder.warFile(arguments[i + 1]);
}
if (arguments[i].equals("--webapp-dir")) {
builder = builder.webAppDir(arguments[i + 1]);
}
if (arguments[i].equals("--write-pid")) {
builder = builder.pid(ProcessHandle.current().pid());
}
}
checkPorts(httpPort, httpsPort);
}
return builder;
}

private void checkPorts(int httpPort, int httpsPort) {
if(httpsPort != 0 && httpPort == httpsPort) {
LOGGER.log(WARNING, "The http and the https ports are the same. Please use different ports");
System.exit(-1);
}
}

/**
* Show help.
*/
private static void showHelp() {
LOGGER.log(Level.INFO, "");
LOGGER.log(Level.INFO,
"""
--extension-class <className> - Set the extension to use
--help - Show this help
--context-path <string> - Set the Servlet context path
--enable-crac - Enable Project CRaC support
--http-port <integer> - Set the HTTP port (use -1 to disable)
--http-server-class <className> - Set the HTTP server class to use
--https-keystore-file <file> - Set the HTTPS keystore file (applies to
the whole JVM)
--https-keystore-password <string> - Set the HTTPS keystore password
(applies to the whole JVM)
--https-port <integer> - Set the HTTPS port (disabled by
default)
--https-server-class <className> - Set the HTTPS server class to use
--https-truststore-file <file> - Set the HTTPS keystore file (applies to
the whole JVM)
--https-truststore-password <string> - Set the HTTPS keystore password
(applies to the whole JVM)
--jpms - Enable Java Platform Module System
--logging-level <string> - Set the logging level
--verbose - Shows the runtime parameters
--war-file <file> - The WAR file to deploy
--webapp-dir <directory> - The directory to use for the web
application (auto creates when it does
not exist, if omitted runtime will use
the filename portion of --war-file)
--write-pid - Write out a PID file
""");
}
}
1 change: 0 additions & 1 deletion dist/servlet/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@
opens cloud.piranha.dist.servlet;
requires cloud.piranha.extension.servlet;
requires cloud.piranha.single;
requires java.logging;
}
Loading

0 comments on commit 9ea6288

Please sign in to comment.