diff --git a/dist/coreprofile/src/main/java/cloud/piranha/dist/coreprofile/CoreProfilePiranha.java b/dist/coreprofile/src/main/java/cloud/piranha/dist/coreprofile/CoreProfilePiranha.java index 2e9562315..d8c198c85 100644 --- a/dist/coreprofile/src/main/java/cloud/piranha/dist/coreprofile/CoreProfilePiranha.java +++ b/dist/coreprofile/src/main/java/cloud/piranha/dist/coreprofile/CoreProfilePiranha.java @@ -150,6 +150,8 @@ public void run() { httpsFeature.setHttpsKeystoreFile(configuration.getString("httpsKeystoreFile")); httpsFeature.setHttpsKeystorePassword(configuration.getString("httpsKeystorePassword")); httpsFeature.setHttpsServerClass(configuration.getString("httpsServerClass")); + httpsFeature.setHttpsTruststoreFile(configuration.getString("httpsTruststoreFile")); + httpsFeature.setHttpsTruststorePassword(configuration.getString("httpsTruststorePassword")); httpsFeature.setPort(configuration.getInteger("httpsPort")); httpsFeature.init(); httpsFeature.getHttpsServer().setHttpServerProcessor(webAppFeature.getHttpServerProcessor()); diff --git a/dist/coreprofile/src/main/java/cloud/piranha/dist/coreprofile/CoreProfilePiranhaBuilder.java b/dist/coreprofile/src/main/java/cloud/piranha/dist/coreprofile/CoreProfilePiranhaBuilder.java index 36956b918..bfee86056 100644 --- a/dist/coreprofile/src/main/java/cloud/piranha/dist/coreprofile/CoreProfilePiranhaBuilder.java +++ b/dist/coreprofile/src/main/java/cloud/piranha/dist/coreprofile/CoreProfilePiranhaBuilder.java @@ -184,6 +184,28 @@ public CoreProfilePiranhaBuilder httpsServerClass(String httpsServerClass) { return this; } + /** + * Set the HTTPS truststore file. + * + * @param httpsTruststoreFile the HTTPS truststore file. + * @return the builder. + */ + public CoreProfilePiranhaBuilder httpsTruststoreFile(String httpsTruststoreFile) { + piranha.getConfiguration().setString("httpsTruststoreFile", httpsTruststoreFile); + return this; + } + + /** + * Set the HTTPS truststore password. + * + * @param httpsTruststorePassword the HTTPS truststore password. + * @return the builder. + */ + public CoreProfilePiranhaBuilder httpsTruststorePassword(String httpsTruststorePassword) { + piranha.getConfiguration().setString("httpsTruststorePassword", httpsTruststorePassword); + return this; + } + /** * Enable/disable JPMS. * @@ -220,19 +242,21 @@ private void showArguments() { Arguments ========= - Context path : %s - Extension class : %s - Exit on stop : %s - HTTP port : %s - HTTP server class : %s - HTTPS keystore file : %s - HTTPS keystore password : **** - HTTPS port : %s - HTTPS server class : %s - JPMS enabled : %s - PID : %s - WAR filename : %s - Web application dir : %s + Context path : %s + Extension class : %s + Exit on stop : %s + HTTP port : %s + HTTP server class : %s + HTTPS keystore file : %s + HTTPS keystore password : **** + HTTPS port : %s + HTTPS server class : %s + HTTPS truststore file : %s + HTTPS truststore password : **** + JPMS enabled : %s + PID : %s + WAR filename : %s + Web application dir : %s """.formatted( configuration.getString("contextPath"), @@ -243,6 +267,7 @@ private void showArguments() { configuration.getString("httpsKeystoreFile"), configuration.getInteger("httpsPort"), configuration.getString("httpsServerClass"), + configuration.getString("httpsTruststoreFile"), configuration.getBoolean("jpms", false), configuration.getLong("pid"), configuration.getFile("warFile"), @@ -250,7 +275,7 @@ private void showArguments() { ) ); } - + /** * Set the verbose flag. * 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 be2727092..1b3eb43c8 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 @@ -101,6 +101,12 @@ private CoreProfilePiranhaBuilder processArguments(String[] arguments) { 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); } @@ -136,25 +142,30 @@ private static void showHelp() { LOGGER.log(Level.INFO, ""); LOGGER.log(Level.INFO, """ - --extension-class - Set the extension to use - --help - Show this help - --context-path - Set the Servlet context path - --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 - --jpms - Enable Java Platform Module System - --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 + --extension-class - Set the extension to use + --help - Show this help + --context-path - Set the Servlet context path + --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 + --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/microprofile/src/main/java/cloud/piranha/dist/microprofile/MicroProfilePiranha.java b/dist/microprofile/src/main/java/cloud/piranha/dist/microprofile/MicroProfilePiranha.java index ced0c0717..92ccc60e3 100644 --- a/dist/microprofile/src/main/java/cloud/piranha/dist/microprofile/MicroProfilePiranha.java +++ b/dist/microprofile/src/main/java/cloud/piranha/dist/microprofile/MicroProfilePiranha.java @@ -146,6 +146,8 @@ public void run() { httpsFeature.setHttpsKeystoreFile(configuration.getString("httpsKeystoreFile")); httpsFeature.setHttpsKeystorePassword(configuration.getString("httpsKeystorePassword")); httpsFeature.setHttpsServerClass(configuration.getString("httpsServerClass")); + httpsFeature.setHttpsTruststoreFile(configuration.getString("httpsTruststoreFile")); + httpsFeature.setHttpsTruststorePassword(configuration.getString("httpsTruststorePassword")); httpsFeature.setPort(configuration.getInteger("httpsPort")); httpsFeature.init(); httpsFeature.getHttpsServer().setHttpServerProcessor(webAppFeature.getHttpServerProcessor()); diff --git a/dist/microprofile/src/main/java/cloud/piranha/dist/microprofile/MicroProfilePiranhaBuilder.java b/dist/microprofile/src/main/java/cloud/piranha/dist/microprofile/MicroProfilePiranhaBuilder.java index 8b3ce9f7b..4ab13d9ca 100644 --- a/dist/microprofile/src/main/java/cloud/piranha/dist/microprofile/MicroProfilePiranhaBuilder.java +++ b/dist/microprofile/src/main/java/cloud/piranha/dist/microprofile/MicroProfilePiranhaBuilder.java @@ -27,6 +27,7 @@ */ package cloud.piranha.dist.microprofile; +import cloud.piranha.core.api.PiranhaConfiguration; import cloud.piranha.core.api.WebApplicationExtension; import java.io.File; import java.lang.System.Logger; @@ -183,6 +184,28 @@ public MicroProfilePiranhaBuilder httpsServerClass(String httpsServerClass) { return this; } + /** + * Set the HTTPS truststore file. + * + * @param httpsTruststoreFile the HTTPS truststore file. + * @return the builder. + */ + public MicroProfilePiranhaBuilder httpsTruststoreFile(String httpsTruststoreFile) { + piranha.getConfiguration().setString("httpsTruststoreFile", httpsTruststoreFile); + return this; + } + + /** + * Set the HTTPS truststore password. + * + * @param httpsTruststorePassword the HTTPS truststore password. + * @return the builder. + */ + public MicroProfilePiranhaBuilder httpsTruststorePassword(String httpsTruststorePassword) { + piranha.getConfiguration().setString("httpsTruststorePassword", httpsTruststorePassword); + return this; + } + /** * Enable/disable JPMS. * @@ -198,6 +221,8 @@ public MicroProfilePiranhaBuilder jpms(boolean jpms) { * Show the arguments used. */ private void showArguments() { + PiranhaConfiguration configuration = piranha.getConfiguration(); + LOGGER.log(Level.INFO, """ @@ -206,33 +231,38 @@ private void showArguments() { Arguments ========= - Context path : %s - Extension class : %s - Exit on stop : %s - HTTP port : %s - HTTP server class : %s - HTTPS keystore file : %s - HTTPS keystore password : **** - HTTPS port : %s - HTTPS server class : %s - JPMS enabled : %s - PID : %s - WAR filename : %s - Web application dir : %s + Context path : %s + Extension class : %s + Exit on stop : %s + HTTP port : %s + HTTP server class : %s + HTTPS keystore file : %s + HTTPS keystore password : **** + HTTPS port : %s + HTTPS server class : %s + HTTPS truststore file : %s + HTTPS truststore password : **** + JPMS enabled : %s + PID : %s + WAR filename : %s + Web application dir : %s """.formatted( - piranha.getConfiguration().getString("contextPath"), - piranha.getConfiguration().getClass("extensionClass"), - piranha.getConfiguration().getBoolean("exitOnStop", false), - piranha.getConfiguration().getInteger("httpPort"), - piranha.getConfiguration().getString("httpServerClass"), - piranha.getConfiguration().getString("httpsKeystoreFile"), - piranha.getConfiguration().getInteger("httpsPort"), - piranha.getConfiguration().getString("httpsServerClass"), - piranha.getConfiguration().getBoolean("jpmsEnabled", false), - piranha.getConfiguration().getLong("pid"), - piranha.getConfiguration().getFile("warFile"), - piranha.getConfiguration().getFile("webappDir"))); + configuration.getString("contextPath"), + configuration.getClass("extensionClass"), + configuration.getBoolean("exitOnStop", false), + configuration.getInteger("httpPort"), + configuration.getString("httpServerClass"), + configuration.getString("httpsKeystoreFile"), + configuration.getInteger("httpsPort"), + configuration.getString("httpsServerClass"), + configuration.getString("httpsTruststoreFile"), + configuration.getBoolean("jpms", false), + configuration.getLong("pid"), + configuration.getFile("warFile"), + configuration.getFile("webAppDir") + ) + ); } /** diff --git a/dist/microprofile/src/main/java/cloud/piranha/dist/microprofile/MicroProfilePiranhaMain.java b/dist/microprofile/src/main/java/cloud/piranha/dist/microprofile/MicroProfilePiranhaMain.java index a9624f6d1..db0bbca91 100644 --- a/dist/microprofile/src/main/java/cloud/piranha/dist/microprofile/MicroProfilePiranhaMain.java +++ b/dist/microprofile/src/main/java/cloud/piranha/dist/microprofile/MicroProfilePiranhaMain.java @@ -103,6 +103,12 @@ private MicroProfilePiranhaBuilder processArguments(String[] arguments) { 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); } @@ -138,25 +144,30 @@ private static void showHelp() { LOGGER.log(Level.INFO, ""); LOGGER.log(Level.INFO, """ - --extension-class - Set the extension to use - --help - Show this help - --context-path - Set the Servlet context path - --http-port - Set the HTTP port (use -1 to disable) - --http-server-class - Set the HTTP server class to use - --https-port - Set the HTTPS port (disabled by default) - --https-server-class - Set the HTTPS server class to use - --jpms - Enable Java Platform Module System - --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 - --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 + --extension-class - Set the extension to use + --help - Show this help + --context-path - Set the Servlet context path + --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 + --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/platform/src/main/java/cloud/piranha/dist/platform/PlatformPiranha.java b/dist/platform/src/main/java/cloud/piranha/dist/platform/PlatformPiranha.java index 74d970bd2..48b6138ee 100644 --- a/dist/platform/src/main/java/cloud/piranha/dist/platform/PlatformPiranha.java +++ b/dist/platform/src/main/java/cloud/piranha/dist/platform/PlatformPiranha.java @@ -189,6 +189,8 @@ public void run() { httpsFeature.setHttpsKeystoreFile(configuration.getString("httpsKeystoreFile")); httpsFeature.setHttpsKeystorePassword(configuration.getString("httpsKeystorePassword")); httpsFeature.setHttpsServerClass(configuration.getString("httpsServerClass")); + httpsFeature.setHttpsTruststoreFile(configuration.getString("httpsTruststoreFile")); + httpsFeature.setHttpsTruststorePassword(configuration.getString("httpsTruststorePassword")); httpsFeature.setPort(configuration.getInteger("httpsPort")); httpsFeature.init(); httpsFeature.getHttpsServer().setHttpServerProcessor(webAppsFeature.getHttpServerProcessor()); @@ -268,49 +270,6 @@ public void run() { featureManager.stop(); } - /** - * Set the HTTPS truststore file. - * - *

- * Convenience wrapper around the javax.net.ssl.trustStore - * system property. Note using this method sets the property for the entire - * JVM. - *

- * - * @param httpsTruststoreFile the HTTPS truststore file. - */ - public void setHttpsTruststoreFile(String httpsTruststoreFile) { - if (httpsTruststoreFile != null) { - System.setProperty("javax.net.ssl.trustStore", httpsTruststoreFile); - } - } - - /** - * Set the HTTPS truststore password. - * - *

- * Convenience wrapper around the - * javax.net.ssl.trustStorePassword system property. Note using - * this method sets the property for the entire JVM. - *

- * - * @param httpsTruststorePassword the HTTPS truststore password. - */ - void setHttpsTruststorePassword(String httpsTruststorePassword) { - if (httpsTruststorePassword != null) { - System.setProperty("javax.net.ssl.trustStorePassword", httpsTruststorePassword); - } - } - - /** - * Set the web applications directory. - * - * @param webAppsDir the web applications directory. - */ - public void setWebAppsDir(File webAppsDir) { - this.configuration.setFile("webAppsDir", webAppsDir); - } - /** * Start the server. */ diff --git a/dist/platform/src/main/java/cloud/piranha/dist/platform/PlatformPiranhaBuilder.java b/dist/platform/src/main/java/cloud/piranha/dist/platform/PlatformPiranhaBuilder.java index 1dac053a8..9a7306a35 100644 --- a/dist/platform/src/main/java/cloud/piranha/dist/platform/PlatformPiranhaBuilder.java +++ b/dist/platform/src/main/java/cloud/piranha/dist/platform/PlatformPiranhaBuilder.java @@ -53,16 +53,6 @@ public class PlatformPiranhaBuilder { */ private final PlatformPiranha piranha = new PlatformPiranha(); - /** - * Stores the HTTPS truststore file. - */ - private String httpsTruststoreFile; - - /** - * Stores the HTTPS truststore password. - */ - private String httpsTruststorePassword; - /** * Stores the InitialContext factory. */ @@ -73,11 +63,6 @@ public class PlatformPiranhaBuilder { */ private boolean verbose = false; - /** - * Stores the web applications directory. - */ - private String webAppsDir = "webapps"; - /** * Build the server. * @@ -88,13 +73,6 @@ public PlatformPiranha build() { showArguments(); } System.setProperty(INITIAL_CONTEXT_FACTORY, initialContextFactory); - if (httpsTruststoreFile != null) { - piranha.setHttpsTruststoreFile(httpsTruststoreFile); - } - if (httpsTruststorePassword != null) { - piranha.setHttpsTruststorePassword(httpsTruststorePassword); - } - piranha.setWebAppsDir(new File(webAppsDir)); return piranha; } @@ -209,7 +187,7 @@ public PlatformPiranhaBuilder httpsServerClass(String httpsServerClass) { * @return the builder. */ public PlatformPiranhaBuilder httpsTruststoreFile(String httpsTruststoreFile) { - this.httpsTruststoreFile = httpsTruststoreFile; + piranha.getConfiguration().setString("httpsTruststoreFile", httpsTruststoreFile); return this; } @@ -220,7 +198,7 @@ public PlatformPiranhaBuilder httpsTruststoreFile(String httpsTruststoreFile) { * @return the builder. */ public PlatformPiranhaBuilder httpsTruststorePassword(String httpsTruststorePassword) { - this.httpsTruststorePassword = httpsTruststorePassword; + piranha.getConfiguration().setString("httpsTruststorePassword", httpsTruststorePassword); return this; } @@ -267,9 +245,9 @@ private void showArguments() { piranha.getConfiguration().getString("httpsKeystoreFile"), piranha.getConfiguration().getInteger("httpsPort"), piranha.getConfiguration().getString("httpsServerClass"), - httpsTruststoreFile, + piranha.getConfiguration().getString("httpsTruststoreFile"), piranha.getConfiguration().getBoolean("jpmsEnabled", false), - webAppsDir + piranha.getConfiguration().getFile("webAppsDir") )); } @@ -291,7 +269,9 @@ public PlatformPiranhaBuilder verbose(boolean verbose) { * @return the builder. */ public PlatformPiranhaBuilder webAppsDir(String webAppsDir) { - this.webAppsDir = webAppsDir; + if (webAppsDir != null) { + piranha.getConfiguration().setFile("webAppsDir", new File(webAppsDir)); + } return this; } } diff --git a/dist/platform/src/main/java/cloud/piranha/dist/platform/PlatformPiranhaMain.java b/dist/platform/src/main/java/cloud/piranha/dist/platform/PlatformPiranhaMain.java index 590c7b5bb..55da69790 100644 --- a/dist/platform/src/main/java/cloud/piranha/dist/platform/PlatformPiranhaMain.java +++ b/dist/platform/src/main/java/cloud/piranha/dist/platform/PlatformPiranhaMain.java @@ -131,18 +131,24 @@ protected static void showHelp() { LOGGER.log(Level.INFO, ""); LOGGER.log(Level.INFO, """ - --extension-class - Set the extension - --help - Show this help - --http-port - Set the HTTP port (use -1 to disable) - --http-server-class - Set the HTTP server class - --https-keystore-file - Set the HTTPS keystore file - --https-keystore-password - Set the HTTPS keystore password - --https-port - Set the HTTPS port (disabled by default) - --https-server-class - Set the HTTPS server class - --https-truststore-file - Set the HTTPS truststore file - --https-truststore-password - Set the HTTPS truststore password - --jpms - Enable Java Platform Module System - --webapps-dir - Set the web applications directory + --extension-class - Set the extension to use + --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 + --verbose - Shows the runtime parameters + --webapps-dir - Set the web applications directory """); } } diff --git a/dist/server/src/main/java/cloud/piranha/dist/server/ServerPiranha.java b/dist/server/src/main/java/cloud/piranha/dist/server/ServerPiranha.java index ec82a779a..186e9fc38 100644 --- a/dist/server/src/main/java/cloud/piranha/dist/server/ServerPiranha.java +++ b/dist/server/src/main/java/cloud/piranha/dist/server/ServerPiranha.java @@ -186,6 +186,8 @@ public void run() { httpsFeature.setHttpsKeystoreFile(configuration.getString("httpsKeystoreFile")); httpsFeature.setHttpsKeystorePassword(configuration.getString("httpsKeystorePassword")); httpsFeature.setHttpsServerClass(configuration.getString("httpsServerClass")); + httpsFeature.setHttpsTruststoreFile(configuration.getString("httpsTruststoreFile")); + httpsFeature.setHttpsTruststorePassword(configuration.getString("httpsTruststorePassword")); httpsFeature.setPort(configuration.getInteger("httpsPort")); httpsFeature.init(); httpsFeature.getHttpsServer().setHttpServerProcessor(webAppsFeature.getHttpServerProcessor()); @@ -263,40 +265,6 @@ public void run() { featureManager.stop(); } - /** - * Set the HTTPS truststore file. - * - *

- * Convenience wrapper around the javax.net.ssl.trustStore - * system property. Note using this method sets the property for the entire - * JVM. - *

- * - * @param httpsTruststoreFile the HTTPS truststore file. - */ - public void setHttpsTruststoreFile(String httpsTruststoreFile) { - if (httpsTruststoreFile != null) { - System.setProperty("javax.net.ssl.trustStore", httpsTruststoreFile); - } - } - - /** - * Set the SSL truststore password. - * - *

- * Convenience wrapper around the - * javax.net.ssl.trustStorePassword system property. Note using - * this method sets the property for the entire JVM. - *

- * - * @param httpsTruststorePassword the HTTPS truststore password. - */ - void setHttpsTruststorePassword(String httpsTruststorePassword) { - if (httpsTruststorePassword != null) { - System.setProperty("javax.net.ssl.trustStorePassword", httpsTruststorePassword); - } - } - /** * Set the web applications directory. * diff --git a/dist/server/src/main/java/cloud/piranha/dist/server/ServerPiranhaBuilder.java b/dist/server/src/main/java/cloud/piranha/dist/server/ServerPiranhaBuilder.java index 1900e93bd..03270ebe6 100644 --- a/dist/server/src/main/java/cloud/piranha/dist/server/ServerPiranhaBuilder.java +++ b/dist/server/src/main/java/cloud/piranha/dist/server/ServerPiranhaBuilder.java @@ -27,11 +27,11 @@ */ package cloud.piranha.dist.server; -import java.io.File; import java.lang.System.Logger.Level; import static javax.naming.Context.INITIAL_CONTEXT_FACTORY; import cloud.piranha.core.api.WebApplicationExtension; +import java.io.File; import static java.lang.System.Logger.Level.WARNING; /** @@ -58,26 +58,11 @@ public class ServerPiranhaBuilder { */ private String initialContextFactory = "cloud.piranha.naming.thread.ThreadInitialContextFactory"; - /** - * Stores the SSL truststore file. - */ - private String httpsTruststoreFile; - - /** - * Stores the SSL truststore password. - */ - private String httpsTruststorePassword; - /** * Stores the verbose flag. */ private boolean verbose = false; - /** - * Stores the web applications directory. - */ - private String webAppsDir = "webapps"; - /** * Build the server. * @@ -88,13 +73,6 @@ public ServerPiranha build() { showArguments(); } System.setProperty(INITIAL_CONTEXT_FACTORY, initialContextFactory); - if (httpsTruststoreFile != null) { - piranha.setHttpsTruststoreFile(httpsTruststoreFile); - } - if (httpsTruststorePassword != null) { - piranha.setHttpsTruststorePassword(httpsTruststorePassword); - } - piranha.setWebAppsDir(new File(webAppsDir)); return piranha; } @@ -209,7 +187,7 @@ public ServerPiranhaBuilder httpsServerClass(String httpsServerClass) { * @return the builder. */ public ServerPiranhaBuilder httpsTruststoreFile(String httpsTruststoreFile) { - this.httpsTruststoreFile = httpsTruststoreFile; + piranha.getConfiguration().setString("httpsTruststoreFile", httpsTruststoreFile); return this; } @@ -220,7 +198,7 @@ public ServerPiranhaBuilder httpsTruststoreFile(String httpsTruststoreFile) { * @return the builder. */ public ServerPiranhaBuilder httpsTruststorePassword(String httpsTruststorePassword) { - this.httpsTruststorePassword = httpsTruststorePassword; + piranha.getConfiguration().setString("httpsTruststorePassword", httpsTruststorePassword); return this; } @@ -268,9 +246,9 @@ private void showArguments() { piranha.getConfiguration().getString("httpsKeystoreFile"), piranha.getConfiguration().getInteger("httpsPort"), piranha.getConfiguration().getString("httpsServerClass"), - httpsTruststoreFile, + piranha.getConfiguration().getString("httpsTruststoreFile"), piranha.getConfiguration().getBoolean("jpmsEnabled", false), - webAppsDir + piranha.getConfiguration().getFile("webAppsDir") )); } @@ -292,7 +270,9 @@ public ServerPiranhaBuilder verbose(boolean verbose) { * @return the builder. */ public ServerPiranhaBuilder webAppsDir(String webAppsDir) { - this.webAppsDir = webAppsDir; + if (webAppsDir != null) { + piranha.getConfiguration().setFile("webAppsDir", new File(webAppsDir)); + } return this; } } diff --git a/dist/server/src/main/java/cloud/piranha/dist/server/ServerPiranhaMain.java b/dist/server/src/main/java/cloud/piranha/dist/server/ServerPiranhaMain.java index 5d2028012..30b5aca32 100644 --- a/dist/server/src/main/java/cloud/piranha/dist/server/ServerPiranhaMain.java +++ b/dist/server/src/main/java/cloud/piranha/dist/server/ServerPiranhaMain.java @@ -126,18 +126,24 @@ protected static void showHelp() { LOGGER.log(Level.INFO, ""); LOGGER.log(Level.INFO, """ - --extension - Set the extension class - --help - Show this help - --http-port - Set the HTTP port (use -1 to disable) - --http-server-class - Set the HTTP server class - --https-keystore-file - Set the HTTPS keystore file - --https-keystore-password - Set the HTTPS keystore password - --https-port - Set the HTTPS port (disabled by default) - --https-server-class - Set the HTTPS server class - --https-truststore-file - Set the SSL keystore file - --https-truststore-password - Set the SSL keystore password - --jpms - Enable Java Platform Module System - --webapps-dir - Set the web applications directory - """); + --extension-class - Set the extension to use + --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 + --verbose - Shows the runtime parameters + --webapps-dir - Set the web applications directory + """); } } diff --git a/dist/servlet/src/main/java/cloud/piranha/dist/servlet/ServletPiranha.java b/dist/servlet/src/main/java/cloud/piranha/dist/servlet/ServletPiranha.java index bc60e8e66..ba08c09b7 100644 --- a/dist/servlet/src/main/java/cloud/piranha/dist/servlet/ServletPiranha.java +++ b/dist/servlet/src/main/java/cloud/piranha/dist/servlet/ServletPiranha.java @@ -170,6 +170,8 @@ public void run() { httpsFeature.setHttpsKeystoreFile(configuration.getString("httpsKeystoreFile")); httpsFeature.setHttpsKeystorePassword(configuration.getString("httpsKeystorePassword")); httpsFeature.setHttpsServerClass(configuration.getString("httpsServerClass")); + httpsFeature.setHttpsTruststoreFile(configuration.getString("httpsTruststoreFile")); + httpsFeature.setHttpsTruststorePassword(configuration.getString("httpsTruststorePassword")); httpsFeature.setPort(configuration.getInteger("httpsPort")); httpsFeature.init(); httpsFeature.getHttpsServer().setHttpServerProcessor(webAppFeature.getHttpServerProcessor()); diff --git a/dist/servlet/src/main/java/cloud/piranha/dist/servlet/ServletPiranhaBuilder.java b/dist/servlet/src/main/java/cloud/piranha/dist/servlet/ServletPiranhaBuilder.java index 323842aa2..6226d94ef 100644 --- a/dist/servlet/src/main/java/cloud/piranha/dist/servlet/ServletPiranhaBuilder.java +++ b/dist/servlet/src/main/java/cloud/piranha/dist/servlet/ServletPiranhaBuilder.java @@ -27,6 +27,7 @@ */ package cloud.piranha.dist.servlet; +import cloud.piranha.core.api.PiranhaConfiguration; import cloud.piranha.core.api.WebApplicationExtension; import java.io.File; import java.lang.System.Logger; @@ -195,6 +196,28 @@ public ServletPiranhaBuilder httpsServerClass(String httpsServerClass) { return this; } + /** + * Set the HTTPS truststore file. + * + * @param httpsTruststoreFile the HTTPS truststore file. + * @return the builder. + */ + public ServletPiranhaBuilder httpsTruststoreFile(String httpsTruststoreFile) { + piranha.getConfiguration().setString("httpsTruststoreFile", httpsTruststoreFile); + return this; + } + + /** + * Set the HTTPS truststore password. + * + * @param httpsTruststorePassword the HTTPS truststore password. + * @return the builder. + */ + public ServletPiranhaBuilder httpsTruststorePassword(String httpsTruststorePassword) { + piranha.getConfiguration().setString("httpsTruststorePassword", httpsTruststorePassword); + return this; + } + /** * Enable/disable JPMS. * @@ -221,6 +244,8 @@ public ServletPiranhaBuilder pid(Long pid) { * Show the arguments used. */ private void showArguments() { + PiranhaConfiguration configuration = piranha.getConfiguration(); + LOGGER.log(Level.INFO, """ @@ -229,32 +254,38 @@ private void showArguments() { Arguments ========= - Context path : %s - Extension class : %s - Exit on stop : %s - HTTP port : %s - HTTP server class : %s - HTTPS keystore file : %s - HTTPS keystore password : **** - HTTPS port : %s - HTTPS server class : %s - JPMS enabled : %s - PID : %s - WAR filename : %s - Web application dir : %s + Context path : %s + Extension class : %s + Exit on stop : %s + HTTP port : %s + HTTP server class : %s + HTTPS keystore file : %s + HTTPS keystore password : **** + HTTPS port : %s + HTTPS server class : %s + HTTPS truststore file : %s + HTTPS truststore password : **** + JPMS enabled : %s + PID : %s + WAR filename : %s + Web application dir : %s + """.formatted( - piranha.getConfiguration().getString("contextPath"), - piranha.getConfiguration().getClass("extensionClass"), - piranha.getConfiguration().getBoolean("exitOnStop", false), - piranha.getConfiguration().getInteger("httpPort"), - piranha.getConfiguration().getString("httpServerClass"), - piranha.getConfiguration().getString("httpsKeystoreFile"), - piranha.getConfiguration().getInteger("httpsPort"), - piranha.getConfiguration().getString("httpsServerClass"), - piranha.getConfiguration().getBoolean("jpmsEnabled", false), - piranha.getConfiguration().getLong("pid"), - piranha.getConfiguration().getFile("warFile"), - piranha.getConfiguration().getFile("webAppDir"))); + configuration.getString("contextPath"), + configuration.getClass("extensionClass"), + configuration.getBoolean("exitOnStop", false), + configuration.getInteger("httpPort"), + configuration.getString("httpServerClass"), + configuration.getString("httpsKeystoreFile"), + configuration.getInteger("httpsPort"), + configuration.getString("httpsServerClass"), + configuration.getString("httpsTruststoreFile"), + configuration.getBoolean("jpms", false), + configuration.getLong("pid"), + configuration.getFile("warFile"), + configuration.getFile("webAppDir") + ) + ); } /** diff --git a/dist/servlet/src/main/java/cloud/piranha/dist/servlet/ServletPiranhaMain.java b/dist/servlet/src/main/java/cloud/piranha/dist/servlet/ServletPiranhaMain.java index db93e2ba1..669d25449 100644 --- a/dist/servlet/src/main/java/cloud/piranha/dist/servlet/ServletPiranhaMain.java +++ b/dist/servlet/src/main/java/cloud/piranha/dist/servlet/ServletPiranhaMain.java @@ -106,6 +106,12 @@ private ServletPiranhaBuilder processArguments(String[] arguments) { 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); } @@ -141,25 +147,31 @@ private static void showHelp() { LOGGER.log(Level.INFO, ""); LOGGER.log(Level.INFO, """ - --extension-class - Set the extension to use - --help - Show this help - --context-path - Set the Servlet context path - --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 SSL keystore file (applies to the - whole JVM) - --https-keystore-password - Set the SSL 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 - --jpms - Enable Java Platform Module System - --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 + --extension-class - Set the extension to use + --help - Show this help + --context-path - Set the Servlet context path + --enable-crac - Enable Project CRaC support + --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 + --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/webprofile/src/main/java/cloud/piranha/dist/webprofile/WebProfilePiranha.java b/dist/webprofile/src/main/java/cloud/piranha/dist/webprofile/WebProfilePiranha.java index 4f783ee41..d5dfbdb78 100644 --- a/dist/webprofile/src/main/java/cloud/piranha/dist/webprofile/WebProfilePiranha.java +++ b/dist/webprofile/src/main/java/cloud/piranha/dist/webprofile/WebProfilePiranha.java @@ -149,6 +149,8 @@ public void run() { httpsFeature.setHttpsKeystoreFile(configuration.getString("httpsKeystoreFile")); httpsFeature.setHttpsKeystorePassword(configuration.getString("httpsKeystorePassword")); httpsFeature.setHttpsServerClass(configuration.getString("httpsServerClass")); + httpsFeature.setHttpsTruststoreFile(configuration.getString("httpsTruststoreFile")); + httpsFeature.setHttpsTruststorePassword(configuration.getString("httpsTruststorePassword")); httpsFeature.setPort(configuration.getInteger("httpsPort")); httpsFeature.init(); httpsFeature.getHttpsServer().setHttpServerProcessor(webAppFeature.getHttpServerProcessor()); diff --git a/dist/webprofile/src/main/java/cloud/piranha/dist/webprofile/WebProfilePiranhaBuilder.java b/dist/webprofile/src/main/java/cloud/piranha/dist/webprofile/WebProfilePiranhaBuilder.java index cf100a3d0..bd522c7c2 100644 --- a/dist/webprofile/src/main/java/cloud/piranha/dist/webprofile/WebProfilePiranhaBuilder.java +++ b/dist/webprofile/src/main/java/cloud/piranha/dist/webprofile/WebProfilePiranhaBuilder.java @@ -27,6 +27,7 @@ */ package cloud.piranha.dist.webprofile; +import cloud.piranha.core.api.PiranhaConfiguration; import cloud.piranha.core.api.WebApplicationExtension; import java.io.File; import java.lang.System.Logger; @@ -183,6 +184,28 @@ public WebProfilePiranhaBuilder httpsServerClass(String httpsServerClass) { return this; } + /** + * Set the HTTPS truststore file. + * + * @param httpsTruststoreFile the HTTPS truststore file. + * @return the builder. + */ + public WebProfilePiranhaBuilder httpsTruststoreFile(String httpsTruststoreFile) { + piranha.getConfiguration().setString("httpsTruststoreFile", httpsTruststoreFile); + return this; + } + + /** + * Set the HTTPS truststore password. + * + * @param httpsTruststorePassword the HTTPS truststore password. + * @return the builder. + */ + public WebProfilePiranhaBuilder httpsTruststorePassword(String httpsTruststorePassword) { + piranha.getConfiguration().setString("httpsTruststorePassword", httpsTruststorePassword); + return this; + } + /** * Enable/disable JPMS. * @@ -209,6 +232,8 @@ public WebProfilePiranhaBuilder pid(Long pid) { * Show the arguments used. */ private void showArguments() { + PiranhaConfiguration configuration = piranha.getConfiguration(); + LOGGER.log(Level.INFO, """ @@ -217,33 +242,38 @@ private void showArguments() { Arguments ========= - Context path : %s - Extension class : %s - Exit on stop : %s - HTTP port : %s - HTTP server class : %s - HTTPS keystore file : %s - HTTPS keystore password : **** - HTTPS port : %s - HTTPS server class : %s - JPMS enabled : %s - PID : %s - WAR filename : %s - Web application dir : %s + Context path : %s + Extension class : %s + Exit on stop : %s + HTTP port : %s + HTTP server class : %s + HTTPS keystore file : %s + HTTPS keystore password : **** + HTTPS port : %s + HTTPS server class : %s + HTTPS truststore file : %s + HTTPS truststore password : **** + JPMS enabled : %s + PID : %s + WAR filename : %s + Web application dir : %s """.formatted( - piranha.getConfiguration().getString("contextPath"), - piranha.getConfiguration().getClass("extensionClass"), - piranha.getConfiguration().getBoolean("exitOnStop", false), - piranha.getConfiguration().getInteger("httpPort"), - piranha.getConfiguration().getString("httpServerClass"), - piranha.getConfiguration().getString("httpsKeystoreFile"), - piranha.getConfiguration().getInteger("httpsPort"), - piranha.getConfiguration().getString("httpsServerClass"), - piranha.getConfiguration().getBoolean("jpmsEnabled", false), - piranha.getConfiguration().getLong("pid"), - piranha.getConfiguration().getFile("warFile"), - piranha.getConfiguration().getFile("webAppDir"))); + configuration.getString("contextPath"), + configuration.getClass("extensionClass"), + configuration.getBoolean("exitOnStop", false), + configuration.getInteger("httpPort"), + configuration.getString("httpServerClass"), + configuration.getString("httpsKeystoreFile"), + configuration.getInteger("httpsPort"), + configuration.getString("httpsServerClass"), + configuration.getString("httpsTruststoreFile"), + configuration.getBoolean("jpms", false), + configuration.getLong("pid"), + configuration.getFile("warFile"), + configuration.getFile("webAppDir") + ) + ); } /** diff --git a/dist/webprofile/src/main/java/cloud/piranha/dist/webprofile/WebProfilePiranhaMain.java b/dist/webprofile/src/main/java/cloud/piranha/dist/webprofile/WebProfilePiranhaMain.java index 40bd7362e..8a919575f 100644 --- a/dist/webprofile/src/main/java/cloud/piranha/dist/webprofile/WebProfilePiranhaMain.java +++ b/dist/webprofile/src/main/java/cloud/piranha/dist/webprofile/WebProfilePiranhaMain.java @@ -103,6 +103,12 @@ private WebProfilePiranhaBuilder processArguments(String[] arguments) { 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); } @@ -138,25 +144,30 @@ private static void showHelp() { LOGGER.log(Level.INFO, ""); LOGGER.log(Level.INFO, """ - --extension-class - Set the extension to use - --help - Show this help - --context-path - Set the Servlet context path - --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 SSL keystore file (applies to the - whole JVM) - --https-keystore-password - Set the SSL 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 - --jpms - Enable Java Platform Module System - --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 doe - not exist, if omitted runtime will us - the filename portion of --war-file) - --write-pid - Write out a PID file + --extension-class - Set the extension to use + --help - Show this help + --context-path - Set the Servlet context path + --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 + --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/feature/exitonstop/pom.xml b/feature/exitonstop/pom.xml index 060b8d48a..4f68a948f 100644 --- a/feature/exitonstop/pom.xml +++ b/feature/exitonstop/pom.xml @@ -16,5 +16,23 @@ ${project.version} compile + + org.junit.jupiter + junit-jupiter-api + 5.6.0 + test + + + org.junit.jupiter + junit-jupiter-params + 5.6.0 + test + + + org.junit.jupiter + junit-jupiter-engine + 5.6.0 + test + diff --git a/feature/exitonstop/src/main/java/cloud/piranha/feature/exitonstop/ExitOnStopFeature.java b/feature/exitonstop/src/main/java/cloud/piranha/feature/exitonstop/ExitOnStopFeature.java index bd3b6bd3f..ad1f62abc 100644 --- a/feature/exitonstop/src/main/java/cloud/piranha/feature/exitonstop/ExitOnStopFeature.java +++ b/feature/exitonstop/src/main/java/cloud/piranha/feature/exitonstop/ExitOnStopFeature.java @@ -41,16 +41,31 @@ * @author Manfred Riem (mriem@manorrock.com) */ public class ExitOnStopFeature extends DefaultFeature { + + /** + * Stores the exiting flag. + */ + private boolean exiting = false; + + /** + * Are we in the process of exiting? + * + * @return true if we are, false otherwise. + */ + public boolean isExiting() { + return exiting; + } @Override public void stop() { Thread thread = new Thread() { @Override public void run() { + exiting = true; try { Thread.sleep(2000); } catch(InterruptedException ie) { - // nothing needed here + Thread.currentThread().interrupt(); } System.exit(0); } diff --git a/feature/exitonstop/src/test/java/cloud/piranha/feature/exitonstop/ExitOnStopFeatureTest.java b/feature/exitonstop/src/test/java/cloud/piranha/feature/exitonstop/ExitOnStopFeatureTest.java new file mode 100644 index 000000000..8eebfa751 --- /dev/null +++ b/feature/exitonstop/src/test/java/cloud/piranha/feature/exitonstop/ExitOnStopFeatureTest.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2002-2023 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.feature.exitonstop; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; + +/** + * The JUnit tests for the ExitOnStopFeature class. + * + * @author Manfred Riem (mriem@manorrock.com) + */ +public class ExitOnStopFeatureTest { + + /** + * Test stop method. + * + * @throws Exception when an error occurs. + */ + @Test + public void testStop() throws Exception { + ExitOnStopFeature feature = new ExitOnStopFeature(); + feature.stop(); + Thread.sleep(500); + assertTrue(feature.isExiting()); + } +} diff --git a/feature/https/pom.xml b/feature/https/pom.xml index 18c6a5882..fdee4761c 100644 --- a/feature/https/pom.xml +++ b/feature/https/pom.xml @@ -22,5 +22,20 @@ ${project.version} compile + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter-params + test + + + org.junit.jupiter + junit-jupiter-engine + test + diff --git a/feature/https/src/main/java/cloud/piranha/feature/https/HttpsFeature.java b/feature/https/src/main/java/cloud/piranha/feature/https/HttpsFeature.java index 0f6246ddc..86c0d33b3 100644 --- a/feature/https/src/main/java/cloud/piranha/feature/https/HttpsFeature.java +++ b/feature/https/src/main/java/cloud/piranha/feature/https/HttpsFeature.java @@ -27,7 +27,6 @@ */ package cloud.piranha.feature.https; -import cloud.piranha.feature.api.FeatureManager; import cloud.piranha.feature.impl.DefaultFeature; import cloud.piranha.http.api.HttpServer; import cloud.piranha.http.impl.DefaultHttpServer; @@ -47,11 +46,6 @@ public class HttpsFeature extends DefaultFeature { */ private static final Logger LOGGER = System.getLogger(HttpsFeature.class.getName()); - /** - * Stores the feature manager. - */ - private FeatureManager featureManager; - /** * Stores the HTTPS server. */ @@ -108,6 +102,23 @@ public String getHttpsServerClass() { return httpsServerClass; } + /** + * Get the HTTPS truststore file. + * + * @return the HTTPS truststore file. + */ + public String getHttpsTruststoreFile() { + return System.getProperty("javax.net.ssl.trustStore"); + } + + /** + * Get the HTTPS truststore password. + * + * @return the HTTPS truststore password. + */ + public String getHttpsTruststorePassword() { + return System.getProperty("javax.net.ssl.trustStorePassword"); + } /** * Get the port. * @@ -192,6 +203,40 @@ public void setHttpsServerClass(String httpsServerClass) { } } + /** + * Set the HTTPS truststore file. + * + *

+ * This is currently a convenience wrapper around the + * javax.net.ssl.trustStore system property. Note using this + * method sets the property for the entire JVM. + *

+ * + * @param httpsTruststoreFile the HTTPS truststore file. + */ + public void setHttpsTruststoreFile(String httpsTruststoreFile) { + if (httpsTruststoreFile != null) { + System.setProperty("javax.net.ssl.trustStore", httpsTruststoreFile); + } + } + + /** + * Set the HTTPS truststore password. + * + *

+ * This is currently a convenience wrapper around the + * javax.net.ssl.trustStorePassword system property. Note using + * this method sets the property for the entire JVM. + *

+ * + * @param httpsTruststorePassword the HTTPS truststore password. + */ + public void setHttpsTruststorePassword(String httpsTruststorePassword) { + if (httpsTruststorePassword != null) { + System.setProperty("javax.net.ssl.trustStorePassword", httpsTruststorePassword); + } + } + /** * Set the port. * diff --git a/feature/https/src/test/java/cloud/piranha/feature/https/HttpsFeatureTest.java b/feature/https/src/test/java/cloud/piranha/feature/https/HttpsFeatureTest.java new file mode 100644 index 000000000..a8efcc580 --- /dev/null +++ b/feature/https/src/test/java/cloud/piranha/feature/https/HttpsFeatureTest.java @@ -0,0 +1,151 @@ +/* + * Copyright (c) 2002-2023 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.feature.https; + +import cloud.piranha.http.api.HttpServer; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; + +/** + * The JUnit tests for the HttpsFeature class. + * + * @author Manfred Riem (mriem@manorrock.com) + */ +public class HttpsFeatureTest { + + /** + * Test destroy method. + */ + @Test + public void testDestroy() { + HttpsFeature feature = new HttpsFeature(); + feature.init(); + assertNotNull(feature.getHttpsServer()); + feature.destroy(); + assertNull(feature.getHttpsServer()); + feature.setHttpsServerClass("BOGUS"); + feature.init(); + assertNull(feature.getHttpsServer()); + feature.destroy(); + assertNull(feature.getHttpsServer()); + } + + /** + * Test getHttpsKeystoreFile method. + */ + @Test + public void testGetHttpsKeystoreFile() { + HttpsFeature feature = new HttpsFeature(); + feature.setHttpsKeystoreFile("keystore"); + assertEquals("keystore", feature.getHttpsKeystoreFile()); + } + + /** + * Test getHttpsKeystorePassword method. + */ + @Test + public void testGetHttpsKeystorePassword() { + HttpsFeature feature = new HttpsFeature(); + feature.setHttpsKeystorePassword("password"); + assertEquals("password", feature.getHttpsKeystorePassword()); + } + + /** + * Test getHttpServer method. + */ + @Test + public void testGetHttpServerMethod() { + HttpsFeature feature = new HttpsFeature(); + feature.init(); + HttpServer httpsServer = feature.getHttpsServer(); + feature.setHttpsServer(null); + assertNull(feature.getHttpsServer()); + feature.setHttpsServer(httpsServer); + assertEquals(httpsServer, feature.getHttpsServer()); + } + + /** + * Test getHttpServerClass method. + */ + @Test + public void testGetHttpServerClass() { + HttpsFeature feature = new HttpsFeature(); + feature.setHttpsServerClass("BOGUS"); + assertEquals("BOGUS", feature.getHttpsServerClass()); + feature.setHttpsServerClass(null); + assertNotNull(feature.getHttpsServerClass()); + } + + /** + * Test getHttpsTruststoreFile method. + */ + @Test + public void testGetHttpsTruststoreFile() { + HttpsFeature feature = new HttpsFeature(); + feature.setHttpsTruststoreFile("truststore"); + assertEquals("truststore", feature.getHttpsTruststoreFile()); + } + + /** + * Test getHttpsTruststorePassword method. + */ + @Test + public void testGetHttpsTruststorePassword() { + HttpsFeature feature = new HttpsFeature(); + feature.setHttpsTruststorePassword("password"); + assertEquals("password", feature.getHttpsTruststorePassword()); + } + + /** + * Test getPort method. + */ + @Test + public void testGetPort() { + HttpsFeature feature = new HttpsFeature(); + feature.setPort(1234); + assertEquals(1234, feature.getPort()); + } + + /** + * Test stop method. + */ + @Test + public void testStop() { + HttpsFeature feature = new HttpsFeature(); + feature.init(); + assertFalse(feature.getHttpsServer().isRunning()); + feature.start(); + assertTrue(feature.getHttpsServer().isRunning()); + feature.stop(); + assertFalse(feature.getHttpsServer().isRunning()); + } +}