jdk/src/share/classes/sun/launcher/LauncherHelper.java
changeset 7297 906c58a8b849
parent 5982 de622fe4f7d8
child 7810 d4730191e53c
--- a/jdk/src/share/classes/sun/launcher/LauncherHelper.java	Wed Nov 24 07:43:06 2010 +0800
+++ b/jdk/src/share/classes/sun/launcher/LauncherHelper.java	Tue Nov 23 16:52:39 2010 -0800
@@ -44,8 +44,16 @@
 import java.io.PrintStream;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
+import java.math.BigDecimal;
+import java.math.MathContext;
+import java.math.RoundingMode;
 import java.util.ResourceBundle;
 import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Locale;
+import java.util.Properties;
 import java.util.jar.Attributes;
 import java.util.jar.JarFile;
 import java.util.jar.Manifest;
@@ -59,6 +67,17 @@
     private static StringBuilder outBuf = new StringBuilder();
 
     private static ResourceBundle javarb = null;
+
+    private static final String INDENT = "    ";
+    private static final String VM_SETTINGS     = "VM settings:";
+    private static final String PROP_SETTINGS   = "Property settings:";
+    private static final String LOCALE_SETTINGS = "Locale settings:";
+
+    private static final long K = 1024;
+    private static final long M = K * K;
+    private static final long G = M * K;
+    private static final long T = G * K;
+
     private static synchronized ResourceBundle getLauncherResourceBundle() {
         if (javarb == null) {
             javarb = ResourceBundle.getBundle(defaultBundleName);
@@ -66,6 +85,184 @@
         return javarb;
     }
 
+    /*
+     * 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.
+     *
+     * printToStderr: choose between stdout and stderr
+     *
+     * optionFlag: specifies which options to print default is all other
+     *    possible values are vm, properties, locale.
+     *
+     * maxHeapSize: in bytes, as set by the launcher, a zero-value indicates
+     *    this code should determine this value, using a suitable method.
+     *
+     * stackSize: in bytes, as set by the launcher, a zero-value indicates
+     * this code determine this value, using a suitable method.
+     */
+    static void showSettings(boolean printToStderr, String optionFlag,
+            long maxHeapSize, long stackSize, boolean isServer) {
+
+        PrintStream ostream = (printToStderr) ? System.err : System.out;
+        String opts[] = optionFlag.split(":");
+        String optStr = (opts.length > 1 && opts[1] != null)
+                ? opts[1].trim()
+                : "all";
+        switch (optStr) {
+            case "vm":
+                printVmSettings(ostream, maxHeapSize, stackSize, isServer);
+                break;
+            case "properties":
+                printProperties(ostream);
+                break;
+            case "locale":
+                printLocale(ostream);
+                break;
+            default:
+                printVmSettings(ostream, maxHeapSize, stackSize, isServer);
+                printProperties(ostream);
+                printLocale(ostream);
+                break;
+        }
+    }
+
+    /*
+     * prints the main vm settings subopt/section
+     */
+    private static void printVmSettings(PrintStream ostream, long maxHeapSize,
+            long stackSize, boolean isServer) {
+
+        ostream.println(VM_SETTINGS);
+        if (stackSize != 0L) {
+            ostream.println(INDENT + "Stack Size: " + scaleValue(stackSize));
+        }
+        if (maxHeapSize != 0L) {
+            ostream.println(INDENT + "Max. Heap Size: " + scaleValue(maxHeapSize));
+        } else {
+            ostream.println(INDENT + "Max. Heap Size (Estimated): "
+                    + scaleValue(Runtime.getRuntime().maxMemory()));
+        }
+        ostream.println(INDENT + "Ergonomics Machine Class: "
+                + ((isServer) ? "server" : "client"));
+        ostream.println(INDENT + "Using VM: "
+                + System.getProperty("java.vm.name"));
+        ostream.println();
+    }
+
+    /*
+     * scale the incoming values to a human readable form, represented as
+     * K, M, G and T, see java.c parse_size for the scaled values and
+     * suffixes.
+     */
+
+    private static String scaleValue(double v) {
+        MathContext mc2 = new MathContext(3, RoundingMode.HALF_EVEN);
+
+        if (v >= K && v < M) {
+            return (new BigDecimal(v / K, mc2)).toPlainString() + "K";
+        } else if (v >= M && v < G) {
+            return (new BigDecimal(v / M, mc2)).toPlainString() + "M";
+        } else if (v >= G && v < T) {
+            return (new BigDecimal(v / G, mc2)).toPlainString() + "G";
+        } else if (v >= T) {
+            return (new BigDecimal(v / T, mc2)).toPlainString() + "T";
+        } else {
+            return String.format("%.0f", v);
+        }
+    }
+
+    /*
+     * prints the properties subopt/section
+     */
+    private static void printProperties(PrintStream ostream) {
+        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(ostream, x, p.getProperty(x));
+        }
+        ostream.println();
+    }
+
+    private static boolean isPath(String key) {
+        return key.endsWith(".dirs") || key.endsWith(".path");
+    }
+
+    private static void printPropertyValue(PrintStream ostream,
+            String key, String value) {
+        ostream.print(INDENT + key + " = ");
+        if (key.equals("line.separator")) {
+            byte[] bytes = value.getBytes();
+            for (byte b : bytes) {
+                switch (b) {
+                    case 0xd:
+                        ostream.print("CR ");
+                        break;
+                    case 0xa:
+                        ostream.print("LF ");
+                        break;
+                    default:
+                        ostream.printf("0x%02X", b & 0xff);
+                        break;
+                }
+            }
+            ostream.println();
+            return;
+        }
+        if (!isPath(key)) {
+            ostream.println(value);
+            return;
+        }
+        // pretty print the path values as a list
+        String[] values = value.split(System.getProperty("path.separator"));
+        int len = values.length;
+        for (int i = 0 ; i < len ; i++) {
+            if (i == 0) { // first line treated specially
+                ostream.println(values[i]);
+            } else { // following lines prefix with indents
+                ostream.print(INDENT + INDENT);
+                ostream.println(values[i]);
+            }
+        }
+    }
+
+    /*
+     * prints the locale subopt/section
+     */
+    private static void printLocale(PrintStream ostream) {
+        Locale locale = Locale.getDefault();
+        ostream.println(LOCALE_SETTINGS);
+        ostream.println(INDENT + "default locale = " + locale.getDisplayLanguage());
+        printLocales(ostream);
+        ostream.println();
+    }
+
+    private static void printLocales(PrintStream ostream) {
+        Locale[] locales = Locale.getAvailableLocales();
+        final int len = locales == null ? 0 : locales.length;
+        if (len < 1 ) {
+            return;
+        }
+        ostream.print(INDENT + "available locales = ");
+        final int last = len - 1 ;
+        for (int i = 0; i < last ; i++) {
+            ostream.print(locales[i]);
+            if (i != last) {
+                ostream.print(", ");
+            }
+            // print columns of 8
+            if ((i + 1) % 8 == 0) {
+                ostream.println();
+                ostream.print(INDENT + INDENT);
+            }
+        }
+        ostream.println(locales[last]);
+    }
+
     /**
      * A private helper method to get a localized message and also
      * apply any arguments that we might pass.