From 67a4d25173b53902563302be635812933ab59f1e Mon Sep 17 00:00:00 2001 From: Manfred Riem Date: Fri, 19 Jul 2024 07:53:08 -0500 Subject: [PATCH] Fixes #3818 - Refactor to add SingleMain as base class for Piranha Core Profile (#3819) --- .../coreprofile/CoreProfilePiranhaMain.java | 140 +------------ .../src/main/java/module-info.java | 1 - .../cloud/piranha/single/SingleExtension.java | 38 ---- .../java/cloud/piranha/single/SingleMain.java | 186 ++++++++++++++++++ .../cloud/piranha/single/SinglePiranha.java | 1 - .../piranha/single/SinglePiranhaBuilder.java | 11 +- 6 files changed, 206 insertions(+), 171 deletions(-) delete mode 100644 single/src/main/java/cloud/piranha/single/SingleExtension.java create mode 100644 single/src/main/java/cloud/piranha/single/SingleMain.java diff --git a/dist/coreprofile/src/main/java/cloud/piranha/dist/coreprofile/CoreProfilePiranhaMain.java b/dist/coreprofile/src/main/java/cloud/piranha/dist/coreprofile/CoreProfilePiranhaMain.java index e29b12a677..b7f72c06db 100644 --- a/dist/coreprofile/src/main/java/cloud/piranha/dist/coreprofile/CoreProfilePiranhaMain.java +++ b/dist/coreprofile/src/main/java/cloud/piranha/dist/coreprofile/CoreProfilePiranhaMain.java @@ -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. * + *

+ * This version of Main sets the extension class to the CoreProfileExtension to + * deliver Piranha Core Profile (unless it was overridden). + *

+ * * @author Manfred Riem (mriem@manorrock.com) */ -public class CoreProfilePiranhaMain { - - /** - * Stores the logger. - */ - private static final Logger LOGGER = System.getLogger(CoreProfilePiranhaMain.class.getName()); +public class CoreProfilePiranhaMain extends SingleMain { /** * Main method. @@ -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 - Set the extension to use - --context-path - Set the Servlet context path - --help - Show this help - --http-port - Set the HTTP port (use -1 to disable) - --http-server-class - Set the HTTP server class to use - --https-keystore-file - Set the HTTPS keystore file (applies to - the whole JVM) - --https-keystore-password - Set the HTTPS keystore password - (applies to the whole JVM) - --https-port - Set the HTTPS port (disabled by - default) - --https-server-class - Set the HTTPS server class to use - --https-truststore-file - Set the HTTPS keystore file (applies to - the whole JVM) - --https-truststore-password - Set the HTTPS keystore password - (applies to the whole JVM) - --jpms - Enable Java Platform Module System - --logging-level - Set the java.util.logging.Level - --verbose - Shows the runtime parameters - --war-file - The WAR file to deploy - --webapp-dir - 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 - """); - } } diff --git a/dist/coreprofile/src/main/java/module-info.java b/dist/coreprofile/src/main/java/module-info.java index 45f87de9c6..8b39412662 100644 --- a/dist/coreprofile/src/main/java/module-info.java +++ b/dist/coreprofile/src/main/java/module-info.java @@ -37,5 +37,4 @@ opens cloud.piranha.dist.coreprofile; requires cloud.piranha.extension.coreprofile; requires cloud.piranha.single; - requires java.logging; } diff --git a/single/src/main/java/cloud/piranha/single/SingleExtension.java b/single/src/main/java/cloud/piranha/single/SingleExtension.java deleted file mode 100644 index 08037085f0..0000000000 --- a/single/src/main/java/cloud/piranha/single/SingleExtension.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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 cloud.piranha.core.api.WebApplicationExtension; - -/** - * The "Single" WebApplicationExtension. - * - * @author Manfred Riem (mriem@manorrock.com) - */ -public class SingleExtension implements WebApplicationExtension { -} diff --git a/single/src/main/java/cloud/piranha/single/SingleMain.java b/single/src/main/java/cloud/piranha/single/SingleMain.java new file mode 100644 index 0000000000..66f10509d9 --- /dev/null +++ b/single/src/main/java/cloud/piranha/single/SingleMain.java @@ -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 (mriem@manorrock.com) + */ +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 - Set the extension to use + --context-path - Set the Servlet context path + --help - Show this help + --http-port - Set the HTTP port (use -1 to disable) + --http-server-class - Set the HTTP server class to use + --https-keystore-file - Set the HTTPS keystore file (applies to + the whole JVM) + --https-keystore-password - Set the HTTPS keystore password + (applies to the whole JVM) + --https-port - Set the HTTPS port (disabled by + default) + --https-server-class - Set the HTTPS server class to use + --https-truststore-file - Set the HTTPS keystore file (applies to + the whole JVM) + --https-truststore-password - Set the HTTPS keystore password + (applies to the whole JVM) + --jpms - Enable Java Platform Module System + --logging-level - Set the java.util.logging.Level + --verbose - Shows the runtime parameters + --war-file - The WAR file to deploy + --webapp-dir - 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 + """); + } +} diff --git a/single/src/main/java/cloud/piranha/single/SinglePiranha.java b/single/src/main/java/cloud/piranha/single/SinglePiranha.java index 1701a3cc76..ce679c7483 100644 --- a/single/src/main/java/cloud/piranha/single/SinglePiranha.java +++ b/single/src/main/java/cloud/piranha/single/SinglePiranha.java @@ -98,7 +98,6 @@ public class SinglePiranha implements Piranha, Runnable { */ public SinglePiranha() { configuration = new DefaultPiranhaConfiguration(); - configuration.setClass("extensionClass", SingleExtension.class); configuration.setBoolean("exitOnStop", false); configuration.setBoolean("cracEnabled", false); configuration.setInteger("httpPort", 8080); diff --git a/single/src/main/java/cloud/piranha/single/SinglePiranhaBuilder.java b/single/src/main/java/cloud/piranha/single/SinglePiranhaBuilder.java index 2de3fd1fce..cea1f1aae5 100644 --- a/single/src/main/java/cloud/piranha/single/SinglePiranhaBuilder.java +++ b/single/src/main/java/cloud/piranha/single/SinglePiranhaBuilder.java @@ -130,7 +130,16 @@ public SinglePiranhaBuilder extensionClass(String extensionClassName) { } return this; } - + + /** + * Get the configuration. + * + * @return the configuration. + */ + public PiranhaConfiguration getConfiguration() { + return piranha.getConfiguration(); + } + /** * Set the HTTP server port. *