From 132df88b760faf7332958eb6aa52c959c4085fae Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Tue, 22 Mar 2022 10:39:11 -0500 Subject: [PATCH] Issue #5681 - clearer warning on JVM Arg / System Property use Signed-off-by: Joakim Erdfelt --- .../java/org/eclipse/jetty/start/Main.java | 4 ++ .../org/eclipse/jetty/start/StartArgs.java | 10 ++++- .../org/eclipse/jetty/start/MainTest.java | 42 +++++++++++++++++++ .../org/eclipse/jetty/start/PropertyDump.java | 2 - 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/jetty-start/src/main/java/org/eclipse/jetty/start/Main.java b/jetty-start/src/main/java/org/eclipse/jetty/start/Main.java index 5a0a14a64a79..2647496dcf99 100644 --- a/jetty-start/src/main/java/org/eclipse/jetty/start/Main.java +++ b/jetty-start/src/main/java/org/eclipse/jetty/start/Main.java @@ -502,6 +502,10 @@ public void run() if (args.hasJvmArgs() || args.hasSystemProperties()) { StartLog.warn("System properties and/or JVM args set. Consider using --dry-run or --exec"); + if (args.hasSystemProperties()) + args.getSystemProperties().forEach((k, v) -> StartLog.warn(" Detected JVM System Property: %s=%s", k, System.getProperty(k))); + if (args.hasJvmArgs()) + args.getJvmArgs().forEach((jvmArg) -> StartLog.warn(" Detected JVM Arg: %s", jvmArg)); } ClassLoader cl = classpath.getClassLoader(); diff --git a/jetty-start/src/main/java/org/eclipse/jetty/start/StartArgs.java b/jetty-start/src/main/java/org/eclipse/jetty/start/StartArgs.java index 80f125176fbe..911395d509b3 100644 --- a/jetty-start/src/main/java/org/eclipse/jetty/start/StartArgs.java +++ b/jetty-start/src/main/java/org/eclipse/jetty/start/StartArgs.java @@ -816,7 +816,10 @@ else if (properties.size() > 0) public String getMainClassname() { String mainClass = System.getProperty("jetty.server", isJPMS() ? MODULE_MAIN_CLASS : MAIN_CLASS); - return System.getProperty("main.class", mainClass); + Prop mainClassProp = properties.getProp("main.class", true); + if (mainClassProp != null) + return mainClassProp.value; + return mainClass; } public String getMavenLocalRepoDir() @@ -878,6 +881,11 @@ public Props getProperties() return properties; } + public Map getSystemProperties() + { + return systemPropertySource; + } + public Set getSkipFileValidationModules() { return skipFileValidationModules; diff --git a/jetty-start/src/test/java/org/eclipse/jetty/start/MainTest.java b/jetty-start/src/test/java/org/eclipse/jetty/start/MainTest.java index da9247ff468f..6e2edc39a6b4 100644 --- a/jetty-start/src/test/java/org/eclipse/jetty/start/MainTest.java +++ b/jetty-start/src/test/java/org/eclipse/jetty/start/MainTest.java @@ -19,7 +19,9 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; +import java.util.stream.Collectors; import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.junit.jupiter.api.BeforeEach; @@ -91,6 +93,46 @@ public void testListConfig() throws Exception assertThat("user.dir should indicate that it was specified on the command line", userDirLine, containsString("()")); } + @Test + public void testUnknownDistroCommand() throws Exception + { + List cmdLineArgs = new ArrayList<>(); + File testJettyHome = MavenTestingUtils.getTestResourceDir("dist-home"); + cmdLineArgs.add("jetty.home=" + testJettyHome); + cmdLineArgs.add("main.class=" + PropertyDump.class.getName()); + cmdLineArgs.add("--module=base"); + cmdLineArgs.add("--foople"); + cmdLineArgs.add("-Dzed.key=0.value"); + + List output; + + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream out = new PrintStream(baos, true, StandardCharsets.UTF_8)) + { + PrintStream originalStream = StartLog.setStream(new PrintStream(out)); + try + { + Main main = new Main(); + StartArgs args = main.processCommandLine(cmdLineArgs.toArray(new String[0])); + main.start(args); + out.flush(); + output = List.of(baos.toString(StandardCharsets.UTF_8).split(System.lineSeparator())); + } + finally + { + StartLog.setStream(originalStream); + } + } + + // Test a System Property that comes from JVM + List warnings = output.stream().filter((line) -> line.startsWith("WARN")).collect(Collectors.toList()); + Iterator warningIter = warnings.iterator(); + + assertThat("Announcement", warningIter.next(), containsString("System properties and/or JVM args set.")); + assertThat("System Prop Detail", warningIter.next(), containsString("Detected JVM System Property: zed.key=0.value")); + assertThat("JVM Arg Detail", warningIter.next(), containsString("Detected JVM Arg: --foople")); + } + @Test @Disabled("Just a bit noisy for general testing") public void testHelp() throws Exception diff --git a/jetty-start/src/test/java/org/eclipse/jetty/start/PropertyDump.java b/jetty-start/src/test/java/org/eclipse/jetty/start/PropertyDump.java index fee9ebef552a..edb5c73c446f 100644 --- a/jetty-start/src/test/java/org/eclipse/jetty/start/PropertyDump.java +++ b/jetty-start/src/test/java/org/eclipse/jetty/start/PropertyDump.java @@ -63,7 +63,5 @@ public static void main(String[] args) } } } - - System.exit(0); } }