8311653: Modify -XshowSettings launcher behavior

Reviewed-by: mchung, rriggs
This commit is contained in:
Sean Coffey 2023-07-27 06:33:27 +00:00
parent a9d21c61fb
commit 36d578cddb
4 changed files with 172 additions and 78 deletions

View file

@ -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;
@ -61,17 +52,19 @@ import java.nio.file.Path;
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;
@ -131,11 +124,19 @@ public final class LauncherHelper {
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
*
@ -158,44 +159,68 @@ public final class LauncherHelper {
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);
}
}
/*
* prints the main vm settings subopt/section
* 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;
}
/*
* 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) {
@ -227,11 +252,8 @@ public final class LauncherHelper {
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();
}
@ -280,9 +302,9 @@ public final class LauncherHelper {
/*
* 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:");
@ -297,7 +319,7 @@ public final class LauncherHelper {
Locale.getDefault(Category.FORMAT).getDisplayName());
ostream.println(INDENT + "tzdata version = " +
ZoneInfoFile.getVersion());
if (!summaryMode) {
if (verbose) {
printLocales();
}
ostream.println();