J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | |
| 2 | /* |
| 3 | * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. |
| 4 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | * |
| 6 | * This code is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 only, as |
| 8 | * published by the Free Software Foundation. Sun designates this |
| 9 | * particular file as subject to the "Classpath" exception as provided |
| 10 | * by Sun in the LICENSE file that accompanied this code. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 | * version 2 for more details (a copy is included in the LICENSE file that |
| 16 | * accompanied this code). |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License version |
| 19 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 23 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 24 | * have any questions. |
| 25 | */ |
| 26 | |
| 27 | package sun.launcher; |
| 28 | |
| 29 | /* |
| 30 | * |
| 31 | * <p><b>This is NOT part of any API supported by Sun Microsystems. |
| 32 | * If you write code that depends on this, you do so at your own |
| 33 | * risk. This code and its internal interfaces are subject to change |
| 34 | * or deletion without notice.</b> |
| 35 | * |
| 36 | */ |
| 37 | |
| 38 | /** |
| 39 | * A utility package for the java(1), javaw(1) launchers. |
| 40 | */ |
| 41 | import java.io.File; |
| 42 | import java.io.PrintStream; |
| 43 | import java.util.ResourceBundle; |
| 44 | import java.text.MessageFormat; |
| 45 | |
| 46 | public class LauncherHelp { |
| 47 | |
| 48 | private static final String defaultBundleName = "sun.launcher.resources.launcher"; |
| 49 | private static ResourceBundle javarb = ResourceBundle.getBundle(defaultBundleName); |
| 50 | |
| 51 | private static StringBuilder outBuf = new StringBuilder(); |
| 52 | |
| 53 | /** Creates a new instance of LauncherHelp, keep it a singleton */ |
| 54 | private LauncherHelp(){} |
| 55 | |
| 56 | |
| 57 | /** |
| 58 | * A private helper method to get a localized message and also |
| 59 | * apply any arguments that we might pass. |
| 60 | */ |
| 61 | private static String getLocalizedMessage(String key, Object... args) { |
| 62 | String msg = javarb.getString(key); |
| 63 | return (args != null) ? MessageFormat.format(msg, args) : msg; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * The java -help message is split into 3 parts, an invariant, followed |
| 68 | * by a set of platform dependent variant messages, finally an invariant |
| 69 | * set of lines. |
| 70 | * This method initializes the help message for the first time, and also |
| 71 | * assembles the invariant header part of the message. |
| 72 | */ |
| 73 | static void initHelpMessage(String progname) { |
| 74 | outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.header", (progname == null) ? "java" : progname )); |
| 75 | outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.datamodel", 32)); |
| 76 | outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.datamodel", 64)); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Appends the vm selection messages to the header, already created. |
| 81 | * initHelpSystem must already be called. |
| 82 | */ |
| 83 | static void appendVmSelectMessage(String vm1, String vm2) { |
| 84 | outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.vmselect", vm1, vm2)); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Appends the vm synoym message to the header, already created. |
| 89 | * initHelpSystem must be called before using this method. |
| 90 | */ |
| 91 | static void appendVmSynonymMessage(String vm1, String vm2) { |
| 92 | outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.hotspot", vm1, vm2)); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Appends the vm Ergo message to the header, already created. |
| 97 | * initHelpSystem must be called before using this method. |
| 98 | */ |
| 99 | static void appendVmErgoMessage(boolean isServerClass, String vm) { |
| 100 | outBuf = outBuf.append(getLocalizedMessage("java.launcher.ergo.message1", vm)); |
| 101 | outBuf = (isServerClass) |
| 102 | ? outBuf.append(",\n" + getLocalizedMessage("java.launcher.ergo.message2") + "\n\n") |
| 103 | : outBuf.append(".\n\n"); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Appends the last invariant part to the previously created messages, |
| 108 | * and finishes up the printing to the desired output stream. |
| 109 | * initHelpSystem must be called before using this method. |
| 110 | */ |
| 111 | static void printHelpMessage(boolean printToStderr) { |
| 112 | PrintStream ostream = (printToStderr) ? System.err : System.out; |
| 113 | outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.footer", File.pathSeparator)); |
| 114 | ostream.println(outBuf.toString()); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Prints the Xusage text to the desired output stream. |
| 119 | */ |
| 120 | static void printXUsageMessage(boolean printToStderr) { |
| 121 | PrintStream ostream = (printToStderr) ? System.err : System.out; |
| 122 | ostream.println(getLocalizedMessage("java.launcher.X.usage", File.pathSeparator)); |
| 123 | } |
| 124 | |
| 125 | /* Test code */ |
| 126 | public static void main(String[] args) { |
| 127 | initHelpMessage("java"); |
| 128 | appendVmSelectMessage("-client", "client"); |
| 129 | appendVmSelectMessage("-server", "server"); |
| 130 | appendVmSynonymMessage("-hotspot", "client"); |
| 131 | appendVmErgoMessage(true, "server"); |
| 132 | printHelpMessage(true); |
| 133 | |
| 134 | System.err.println("------------------------------------"); |
| 135 | |
| 136 | printXUsageMessage(true); |
| 137 | } |
| 138 | } |