Skip to content

Commit

Permalink
Issue #5681 - clearer warning on JVM Arg / System Property use
Browse files Browse the repository at this point in the history
Signed-off-by: Joakim Erdfelt <[email protected]>
  • Loading branch information
joakime committed Mar 22, 2022
1 parent ae5c8e3 commit 132df88
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
4 changes: 4 additions & 0 deletions jetty-start/src/main/java/org/eclipse/jetty/start/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -878,6 +881,11 @@ public Props getProperties()
return properties;
}

public Map<String, String> getSystemProperties()
{
return systemPropertySource;
}

public Set<String> getSkipFileValidationModules()
{
return skipFileValidationModules;
Expand Down
42 changes: 42 additions & 0 deletions jetty-start/src/test/java/org/eclipse/jetty/start/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -91,6 +93,46 @@ public void testListConfig() throws Exception
assertThat("user.dir should indicate that it was specified on the command line", userDirLine, containsString("(<command-line>)"));
}

@Test
public void testUnknownDistroCommand() throws Exception
{
List<String> 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<String> 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<String> warnings = output.stream().filter((line) -> line.startsWith("WARN")).collect(Collectors.toList());
Iterator<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,5 @@ public static void main(String[] args)
}
}
}

System.exit(0);
}
}

0 comments on commit 132df88

Please sign in to comment.