Skip to content

Commit

Permalink
8311653: Modify -XshowSettings launcher behavior
Browse files Browse the repository at this point in the history
Reviewed-by: mchung, rriggs
  • Loading branch information
coffeys committed Jul 27, 2023
1 parent a9d21c6 commit 36d578c
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 78 deletions.
132 changes: 77 additions & 55 deletions src/java.base/share/classes/sun/launcher/LauncherHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@

package sun.launcher;

/*
*
* <p><b>This is NOT part of any API supported by Sun Microsystems.
* If you write code that depends on this, you do so at your own
* risk. This code and its internal interfaces are subject to change
* or deletion without notice.</b>
*
*/

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
Expand Down Expand Up @@ -61,17 +52,19 @@
import java.text.MessageFormat;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Locale.Category;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Function;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
Expand Down Expand Up @@ -131,11 +124,19 @@ private static class ResourceBundleHolder {
private static PrintStream ostream;
private static Class<?> appClass; // application class, for GUI/reporting purposes

enum Option { DEFAULT, ALL, LOCALE, PROPERTIES, SECURITY,
SECURITY_ALL, SECURITY_PROPERTIES, SECURITY_PROVIDERS,
SECURITY_TLS, SYSTEM, VM };

/*
* A method called by the launcher to print out the standard settings,
* by default -XshowSettings is equivalent to -XshowSettings:all,
* Specific information may be gotten by using suboptions with possible
* values vm, properties and locale.
* A method called by the launcher to print out the standard settings.
* -XshowSettings prints details of all supported components in non-verbose
* mode. -XshowSettings:all prints all settings in verbose mode.
* Specific settings information may be obtained by using suboptions.
*
* Suboption values include "all", "locale", "properties", "security",
* "system" (Linux only) and "vm". A error message is printed for an
* unknown suboption value and the VM launch aborts.
*
* printToStderr: choose between stdout and stderr
*
Expand All @@ -158,44 +159,68 @@ static void showSettings(boolean printToStderr, String optionFlag,
long initialHeapSize, long maxHeapSize, long stackSize) {

initOutput(printToStderr);
String[] opts = optionFlag.split(":");
String optStr = opts.length > 1
? opts[1].trim()
: "all";
switch (optStr) {
case "vm":
printVmSettings(initialHeapSize, maxHeapSize, stackSize);
break;
case "properties":
printProperties();
break;
case "locale":
printLocale(false);
break;
case "security":
var opt = opts.length > 2 ? opts[2].trim() : "all";
SecuritySettings.printSecuritySettings(opt, ostream);
break;
case "system":
if (OperatingSystem.isLinux()) {
printSystemMetrics();
break;
}
default:
printVmSettings(initialHeapSize, maxHeapSize, stackSize);
printProperties();
printLocale(true);
SecuritySettings.printSecuritySummarySettings(ostream);
if (OperatingSystem.isLinux()) {
printSystemMetrics();
}
break;
Option component = validateOption(optionFlag);
switch (component) {
case ALL -> printAllSettings(initialHeapSize, maxHeapSize, stackSize, true);
case LOCALE -> printLocale(true);
case PROPERTIES -> printProperties();
case SECURITY,
SECURITY_ALL,
SECURITY_PROPERTIES,
SECURITY_PROVIDERS,
SECURITY_TLS -> SecuritySettings.printSecuritySettings(component, ostream, true);
case SYSTEM -> printSystemMetrics();
case VM -> printVmSettings(initialHeapSize, maxHeapSize, stackSize);
case DEFAULT -> printAllSettings(initialHeapSize, maxHeapSize, stackSize, false);
}
}

/*
* Validate that the -XshowSettings value is allowed
* If a valid option is parsed, return enum corresponding
* to that option. Abort if a bad option is parsed.
*/
private static Option validateOption(String optionFlag) {
if (optionFlag.equals("-XshowSettings")) {
return Option.DEFAULT;
}

if (optionFlag.equals("-XshowSetings:")) {
abort(null, "java.launcher.bad.option", ":");
}

Map<String, Option> validOpts = Arrays.stream(Option.values())
.filter(o -> !o.equals(Option.DEFAULT)) // non-valid option
.collect(Collectors.toMap(o -> o.name()
.toLowerCase(Locale.ROOT)
.replace("_", ":"), Function.identity()));

String optStr = optionFlag.substring("-XshowSettings:".length());
Option component = validOpts.get(optStr);
if (component == null) {
abort(null, "java.launcher.bad.option", optStr);
}
return component;
}

/*
* prints the main vm settings subopt/section
* Print settings for all supported components.
* verbose value used to determine if verbose information
* should be printed for components that support printing
* in verbose or non-verbose mode.
*/
private static void printAllSettings(long initialHeapSize, long maxHeapSize,
long stackSize, boolean verbose) {
printVmSettings(initialHeapSize, maxHeapSize, stackSize);
printProperties();
printLocale(verbose);
SecuritySettings.printSecuritySettings(
Option.SECURITY_ALL, ostream, verbose);
if (OperatingSystem.isLinux()) {
printSystemMetrics();
}
}

private static void printVmSettings(
long initialHeapSize, long maxHeapSize,
long stackSize) {
Expand Down Expand Up @@ -227,11 +252,8 @@ private static void printVmSettings(
private static void printProperties() {
Properties p = System.getProperties();
ostream.println(PROP_SETTINGS);
List<String> sortedPropertyKeys = new ArrayList<>();
sortedPropertyKeys.addAll(p.stringPropertyNames());
Collections.sort(sortedPropertyKeys);
for (String x : sortedPropertyKeys) {
printPropertyValue(x, p.getProperty(x));
for (String key : p.stringPropertyNames().stream().sorted().toList()) {
printPropertyValue(key, p.getProperty(key));
}
ostream.println();
}
Expand Down Expand Up @@ -280,9 +302,9 @@ private static void printPropertyValue(String key, String value) {
/*
* prints the locale subopt/section
*/
private static void printLocale(boolean summaryMode) {
private static void printLocale(boolean verbose) {
Locale locale = Locale.getDefault();
if (!summaryMode) {
if (verbose) {
ostream.println(LOCALE_SETTINGS);
} else {
ostream.println("Locale settings summary:");
Expand All @@ -297,7 +319,7 @@ private static void printLocale(boolean summaryMode) {
Locale.getDefault(Category.FORMAT).getDisplayName());
ostream.println(INDENT + "tzdata version = " +
ZoneInfoFile.getVersion());
if (!summaryMode) {
if (verbose) {
printLocales();
}
ostream.println();
Expand Down
27 changes: 14 additions & 13 deletions src/java.base/share/classes/sun/launcher/SecuritySettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,25 @@ public final class SecuritySettings {
private static final String PROV_INFO_STRING = "Provider information: ";
private static PrintStream ostream = null;

static void printSecuritySettings(String arg, PrintStream stream) {
static void printSecuritySettings(LauncherHelper.Option o, PrintStream stream, boolean verbose) {
ostream = stream;
switch (arg) {
case "properties" -> printSecurityProperties();
case "providers" -> printSecurityProviderConfig(true);
case "tls" -> printSecurityTLSConfig(true);
case "all" -> printAllSecurityConfig();
default -> ostream.println(
"\nUnrecognized security subcommand. Valid values are " +
"\"all\", \"properties\", \"providers\", \"tls\". See \"java -X\"\n");
if (!verbose) {
printSecuritySummarySettings();
return;
}
switch (o) {
case SECURITY_PROPERTIES -> printSecurityProperties();
case SECURITY_PROVIDERS -> printSecurityProviderConfig(true);
case SECURITY_TLS -> printSecurityTLSConfig(true);
case SECURITY, SECURITY_ALL -> printAllSecurityConfig();
}
}

// A non-verbose description of some core security configuration settings
static void printSecuritySummarySettings(PrintStream stream) {
ostream = stream;
ostream.println("Security settings summary: " + "\n" +
INDENT + "See \"java -X\" for verbose security settings options");
static void printSecuritySummarySettings() {
ostream.println("Security settings summary:");
ostream.println(INDENT + "Use \"-XshowSettings:security\" " +
"option for verbose security settings options");
printSecurityProviderConfig(false);
printSecurityTLSConfig(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ java.launcher.X.usage=\n\
\ failures. It should not be used in production environments.\n\
\ -XshowSettings show all settings and continue\n\
\ -XshowSettings:all\n\
\ show all settings and continue\n\
\ show all settings in verbose detail and continue\n\
\ -XshowSettings:locale\n\
\ show all locale related settings and continue\n\
\ -XshowSettings:properties\n\
Expand Down Expand Up @@ -226,6 +226,14 @@ The following options are macOS specific:\n\
\ -Xdock:icon=<path to icon file>\n\
\ override default icon displayed in dock\n\n

java.launcher.bad.option=\
\n\
Unrecognized showSettings option: {0}\n\
Valid values are \"all\", \"locale\", \"properties\", \"security\", \
\"system\"(Linux only), \"vm\"\n\
Valid \"security\" suboption values are \"all\", \"properties\", \"providers\", \"tls\"\n\
See \"java -X\"\n\
java.launcher.cls.error1=\
Error: Could not find or load main class {0}\n\
Caused by: {1}: {2}
Expand Down

1 comment on commit 36d578c

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.