J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2001-2002 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | package com.sun.tools.example.debug.tty; |
| 26 | |
| 27 | import java.util.*; |
| 28 | import java.text.MessageFormat; |
| 29 | /** |
| 30 | * Internationalization (i18n) convenience methods for jdb. |
| 31 | * |
| 32 | * All program output should flow through these methods, and this is |
| 33 | * the only class that should be printing directly or otherwise |
| 34 | * accessing System.[out,err]. |
| 35 | * |
| 36 | * @bug 4348376 |
| 37 | * @author Tim Bell |
| 38 | */ |
| 39 | public class MessageOutput { |
| 40 | /** |
| 41 | * The resource bundle containing localizable message content. |
| 42 | * This is loaded by TTY.main() at start-up |
| 43 | */ |
| 44 | static ResourceBundle textResources; |
| 45 | |
| 46 | /** Our message formatter. Allocated once, used many times */ |
| 47 | private static MessageFormat messageFormat; |
| 48 | |
| 49 | /** |
| 50 | * Fatal shutdown notification. This is sent to System.err |
| 51 | * instead of System.out |
| 52 | */ |
| 53 | static void fatalError(String messageKey) { |
| 54 | System.err.println(); |
| 55 | System.err.println(format("Fatal error")); |
| 56 | System.err.println(format(messageKey)); |
| 57 | Env.shutdown(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * "Format" a string by doing a simple key lookup. |
| 62 | */ |
| 63 | static String format(String key) { |
| 64 | return (textResources.getString(key)); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Fetch and format a message with one string argument. |
| 69 | * This is the most common usage. |
| 70 | */ |
| 71 | static String format(String key, String argument) { |
| 72 | return format(key, new Object [] {argument}); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Fetch a string by key lookup and format in the arguments. |
| 77 | */ |
| 78 | static synchronized String format(String key, Object [] arguments) { |
| 79 | if (messageFormat == null) { |
| 80 | messageFormat = new MessageFormat (textResources.getString(key)); |
| 81 | } else { |
| 82 | messageFormat.applyPattern (textResources.getString(key)); |
| 83 | } |
| 84 | return (messageFormat.format (arguments)); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Print directly to System.out. |
| 89 | * Every rule has a few exceptions. |
| 90 | * The exceptions to "must use the MessageOutput formatters" are: |
| 91 | * VMConnection.dumpStream() |
| 92 | * TTY.monitorCommand() |
| 93 | * TTY.TTY() (for the '!!' command only) |
| 94 | * Commands.java (multiple locations) |
| 95 | * These are the only sites that should be calling this |
| 96 | * method. |
| 97 | */ |
| 98 | static void printDirectln(String line) { |
| 99 | System.out.println(line); |
| 100 | } |
| 101 | static void printDirect(String line) { |
| 102 | System.out.print(line); |
| 103 | } |
| 104 | static void printDirect(char c) { |
| 105 | System.out.print(c); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Print a newline. |
| 110 | * Use this instead of '\n' |
| 111 | */ |
| 112 | static void println() { |
| 113 | System.out.println(); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Format and print a simple string. |
| 118 | */ |
| 119 | static void print(String key) { |
| 120 | System.out.print(format(key)); |
| 121 | } |
| 122 | /** |
| 123 | * Format and print a simple string. |
| 124 | */ |
| 125 | static void println(String key) { |
| 126 | System.out.println(format(key)); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /** |
| 131 | * Fetch, format and print a message with one string argument. |
| 132 | * This is the most common usage. |
| 133 | */ |
| 134 | static void print(String key, String argument) { |
| 135 | System.out.print(format(key, argument)); |
| 136 | } |
| 137 | static void println(String key, String argument) { |
| 138 | System.out.println(format(key, argument)); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Fetch, format and print a message with an arbitrary |
| 143 | * number of message arguments. |
| 144 | */ |
| 145 | static void println(String key, Object [] arguments) { |
| 146 | System.out.println(format(key, arguments)); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Print a newline, followed by the string. |
| 151 | */ |
| 152 | static void lnprint(String key) { |
| 153 | System.out.println(); |
| 154 | System.out.print(textResources.getString(key)); |
| 155 | } |
| 156 | |
| 157 | static void lnprint(String key, String argument) { |
| 158 | System.out.println(); |
| 159 | System.out.print(format(key, argument)); |
| 160 | } |
| 161 | |
| 162 | static void lnprint(String key, Object [] arguments) { |
| 163 | System.out.println(); |
| 164 | System.out.print(format(key, arguments)); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Print an exception message with a stack trace. |
| 169 | */ |
| 170 | static void printException(String key, Exception e) { |
| 171 | if (key != null) { |
| 172 | try { |
| 173 | println(key); |
| 174 | } catch (MissingResourceException mex) { |
| 175 | printDirectln(key); |
| 176 | } |
| 177 | } |
| 178 | System.out.flush(); |
| 179 | e.printStackTrace(); |
| 180 | } |
| 181 | |
| 182 | static void printPrompt() { |
| 183 | ThreadInfo threadInfo = ThreadInfo.getCurrentThreadInfo(); |
| 184 | if (threadInfo == null) { |
| 185 | System.out.print |
| 186 | (MessageOutput.format("jdb prompt with no current thread")); |
| 187 | } else { |
| 188 | System.out.print |
| 189 | (MessageOutput.format("jdb prompt thread name and current stack frame", |
| 190 | new Object [] { |
| 191 | threadInfo.getThread().name(), |
| 192 | new Integer (threadInfo.getCurrentFrameIndex() + 1)})); |
| 193 | } |
| 194 | System.out.flush(); |
| 195 | } |
| 196 | } |