1 /*
   2  * Copyright (c) 2001, 2011, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * This source code is provided to illustrate the usage of a given feature
  28  * or technique and has been deliberately simplified. Additional steps
  29  * required for a production-quality application, such as security checks,
  30  * input validation and proper error handling, might not be present in
  31  * this sample code.
  32  */
  33 
  34 package com.sun.tools.example.debug.tty;
  35 
  36 import java.util.*;
  37 import java.text.MessageFormat;
  38 /**
  39  * Internationalization (i18n) convenience methods for jdb.
  40  *
  41  * All program output should flow through these methods, and this is
  42  * the only class that should be printing directly or otherwise
  43  * accessing System.[out,err].
  44  *
  45  * @bug 4348376
  46  * @author Tim Bell
  47  */
  48 public class MessageOutput {
  49     /**
  50      * The resource bundle containing localizable message content.
  51      * This is loaded by TTY.main() at start-up
  52      */
  53     static ResourceBundle textResources;
  54 
  55     /** Our message formatter.  Allocated once, used many times */
  56     private static MessageFormat messageFormat;
  57 
  58     /**
  59      * Fatal shutdown notification.  This is sent to System.err
  60      * instead of System.out
  61      */
  62     static void fatalError(String messageKey) {
  63         System.err.println();
  64         System.err.println(format("Fatal error"));
  65         System.err.println(format(messageKey));
  66         Env.shutdown();
  67     }
  68 
  69     /**
  70      * "Format" a string by doing a simple key lookup.
  71      */
  72     static String format(String key) {
  73         return (textResources.getString(key));
  74     }
  75 
  76     /**
  77      * Fetch and format a message with one string argument.
  78      * This is the most common usage.
  79      */
  80     static String format(String key, String argument) {
  81         return format(key, new Object [] {argument});
  82     }
  83 
  84     /**
  85      * Fetch a string by key lookup and format in the arguments.
  86      */
  87     static synchronized String format(String key, Object [] arguments) {
  88         if (messageFormat == null) {
  89             messageFormat = new MessageFormat (textResources.getString(key));
  90         } else {
  91             messageFormat.applyPattern (textResources.getString(key));
  92         }
  93         return (messageFormat.format (arguments));
  94     }
  95 
  96     /**
  97      * Print directly to System.out.
  98      * Every rule has a few exceptions.
  99      * The exceptions to "must use the MessageOutput formatters" are:
 100      *     VMConnection.dumpStream()
 101      *     TTY.monitorCommand()
 102      *     TTY.TTY() (for the '!!' command only)
 103      *     Commands.java (multiple locations)
 104      * These are the only sites that should be calling this
 105      * method.
 106      */
 107     static void printDirectln(String line) {
 108         System.out.println(line);
 109     }
 110     static void printDirect(String line) {
 111         System.out.print(line);
 112     }
 113     static void printDirect(char c) {
 114         System.out.print(c);
 115     }
 116 
 117     /**
 118      * Print a newline.
 119      * Use this instead of '\n'
 120      */
 121     static void println() {
 122         System.out.println();
 123     }
 124 
 125     /**
 126      * Format and print a simple string.
 127      */
 128     static void print(String key) {
 129         System.out.print(format(key));
 130     }
 131     /**
 132      * Format and print a simple string.
 133      */
 134     static void println(String key) {
 135         System.out.println(format(key));
 136     }
 137 
 138 
 139     /**
 140      * Fetch, format and print a message with one string argument.
 141      * This is the most common usage.
 142      */
 143     static void print(String key, String argument) {
 144         System.out.print(format(key, argument));
 145     }
 146     static void println(String key, String argument) {
 147         System.out.println(format(key, argument));
 148     }
 149 
 150     /**
 151      * Fetch, format and print a message with an arbitrary
 152      * number of message arguments.
 153      */
 154     static void println(String key, Object [] arguments) {
 155         System.out.println(format(key, arguments));
 156     }
 157 
 158     /**
 159      * Print a newline, followed by the string.
 160      */
 161     static void lnprint(String key) {
 162         System.out.println();
 163         System.out.print(textResources.getString(key));
 164     }
 165 
 166     static void lnprint(String key, String argument) {
 167         System.out.println();
 168         System.out.print(format(key, argument));
 169     }
 170 
 171     static void lnprint(String key, Object [] arguments) {
 172         System.out.println();
 173         System.out.print(format(key, arguments));
 174     }
 175 
 176     /**
 177      * Print an exception message with a stack trace.
 178      */
 179     static void printException(String key, Exception e) {
 180         if (key != null) {
 181             try {
 182                 println(key);
 183             } catch (MissingResourceException mex) {
 184                 printDirectln(key);
 185             }
 186         }
 187         System.out.flush();
 188         e.printStackTrace();
 189     }
 190 
 191     static void printPrompt() {
 192         printPrompt(false);
 193     }
 194 
 195     static void printPrompt(boolean simple) {
 196         ThreadInfo threadInfo = ThreadInfo.getCurrentThreadInfo();
 197         if (simple || threadInfo == null) {
 198             System.out.print
 199                 (MessageOutput.format("jdb prompt with no current thread"));
 200         } else {
 201             System.out.print
 202                 (MessageOutput.format("jdb prompt thread name and current stack frame",
 203                                       new Object [] {
 204                                           threadInfo.getThread().name(),
 205                                           Integer.valueOf(threadInfo.getCurrentFrameIndex() + 1)}));
 206         }
 207         System.out.flush();
 208     }
 209 }