Skip to content

Commit

Permalink
Fixes piranhacloud#3818 - Refactor to add SingleMain as base class fo…
Browse files Browse the repository at this point in the history
…r Piranha Core Profile (piranhacloud#3819)
  • Loading branch information
mnriem authored Jul 19, 2024
1 parent cf87bf5 commit 67a4d25
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,20 @@
package cloud.piranha.dist.coreprofile;

import cloud.piranha.extension.coreprofile.CoreProfileExtension;
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 Core Profile.
*
* <p>
* This version of Main sets the extension class to the CoreProfileExtension to
* deliver Piranha Core Profile (unless it was overridden).
* </p>
*
* @author Manfred Riem ([email protected])
*/
public class CoreProfilePiranhaMain {

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

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

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

SinglePiranhaBuilder builder = new SinglePiranhaBuilder()
.extensionClass(CoreProfileExtension.class)
.exitOnStop(true);
int httpPort = 0;
int httpsPort = 0;
if (arguments != null) {
for (int i = 0; i < arguments.length; i++) {
if (arguments[i].equals("--enable-crac")) {
builder = builder.crac(true);
}
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,
"""
--enable-crac - Enable Project CRaC support
--extension-class <className> - Set the extension to use
--context-path <string> - Set the Servlet context path
--help - Show this help
--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 java.util.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/coreprofile/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.coreprofile;
requires cloud.piranha.extension.coreprofile;
requires cloud.piranha.single;
requires java.logging;
}
38 changes: 0 additions & 38 deletions single/src/main/java/cloud/piranha/single/SingleExtension.java

This file was deleted.

186 changes: 186 additions & 0 deletions single/src/main/java/cloud/piranha/single/SingleMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/*
* Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package cloud.piranha.single;

import static java.lang.System.Logger.Level.WARNING;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;

/**
* The Single version of Main.
*
* @author Manfred Riem ([email protected])
*/
public class SingleMain {

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

/**
* Main method.
*
* @param arguments the arguments.
*/
public static void main(String[] arguments) {
SinglePiranhaBuilder builder = new SingleMain().processArguments(arguments);
if (builder != null) {
builder.build().start();
} else {
showHelp();
}
}

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

SinglePiranhaBuilder builder = new SinglePiranhaBuilder()
.exitOnStop(true);
int httpPort = 0;
int httpsPort = 0;
if (arguments != null) {
for (int i = 0; i < arguments.length; i++) {
if (arguments[i].equals("--enable-crac")) {
builder = builder.crac(true);
}
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;
}

/**
* Check the HTTP and HTTPS port.
*
* @param httpPort the HTTP port.
* @param httpsPort the HTTPS port.
*/
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.
*/
protected static void showHelp() {
LOGGER.log(Level.INFO, "");
LOGGER.log(Level.INFO,
"""
--enable-crac - Enable Project CRaC support
--extension-class <className> - Set the extension to use
--context-path <string> - Set the Servlet context path
--help - Show this help
--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 java.util.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
""");
}
}
Loading

0 comments on commit 67a4d25

Please sign in to comment.