001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.jconsole;
027:
028: import java.awt.Font;
029: import java.awt.event.WindowAdapter;
030: import java.awt.event.WindowEvent;
031: import java.io.*;
032:
033: import javax.swing.*;
034:
035: /**
036: * A simple console window to display messages sent to System.out and
037: * System.err.
038: *
039: * A stop-gap solution until an error dialog is implemented.
040: */
041: public class OutputViewer {
042: private static JFrame frame;
043: private static JTextArea ta;
044:
045: static {
046: System.setOut(PipeListener.create("System.out"));
047: System.setErr(PipeListener.create("System.err"));
048: }
049:
050: // Dummy to cause class to be loaded
051: public static void init() {
052: }
053:
054: private static void append(String s) {
055: if (frame == null) {
056: // FIXME: The frame title should be a localized string.
057: frame = new JFrame("JConsole: Output");
058: ta = new JTextArea();
059: ta.setEditable(false);
060: frame.getContentPane().add(new JScrollPane(ta));
061: ta.setFont(new Font("Monospaced", Font.BOLD, 14));
062: frame.setSize(500, 600);
063: frame.setLocation(1024 - 500, 768 - 600);
064: // Exit JConsole if no window remains.
065: // e.g. jconsole -version only creates the OutputViewer
066: // but no other window.
067: frame.addWindowListener(new WindowAdapter() {
068: public void windowClosing(WindowEvent e) {
069: if (JFrame.getFrames().length == 1) {
070: System.exit(0);
071: }
072: }
073: });
074: }
075: ta.append(s);
076: ta.setCaretPosition(ta.getText().length());
077: frame.setVisible(true);
078: }
079:
080: private static void appendln(String s) {
081: append(s + "\n");
082: }
083:
084: private static class PipeListener extends Thread {
085: public PrintStream ps;
086: private String name;
087: private PipedInputStream inPipe;
088: private BufferedReader br;
089:
090: public static PrintStream create(String name) {
091: return new PipeListener(name).ps;
092: }
093:
094: private PipeListener(String name) {
095: this .name = name;
096:
097: try {
098: inPipe = new PipedInputStream();
099: ps = new PrintStream(new PipedOutputStream(inPipe));
100: br = new BufferedReader(new InputStreamReader(inPipe));
101: } catch (IOException e) {
102: appendln("PipeListener<init>(" + name + "): " + e);
103: }
104: start();
105: }
106:
107: public void run() {
108: try {
109: String str;
110: while ((str = br.readLine()) != null) {
111: appendln(str);
112:
113: // Hack: Turn off thread check in PipedInputStream.
114: // Any thread should be allowed to write except this one
115: // but we just use this one to keep the pipe alive.
116: try {
117: java.lang.reflect.Field f = PipedInputStream.class
118: .getDeclaredField("writeSide");
119: f.setAccessible(true);
120: f.set(inPipe, this );
121: } catch (Exception e) {
122: appendln("PipeListener(" + name + ").run: " + e);
123: }
124: }
125: appendln("-- " + name + " closed --");
126: br.close();
127: } catch (IOException e) {
128: appendln("PipeListener(" + name + ").run: " + e);
129: }
130: }
131: }
132: }
|