Path: blob/master/src/jdk.jconsole/share/classes/sun/tools/jconsole/OutputViewer.java
40948 views
/*1* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jconsole;2627import java.awt.Font;28import java.awt.event.WindowAdapter;29import java.awt.event.WindowEvent;30import java.io.*;3132import javax.swing.*;3334/**35* A simple console window to display messages sent to System.out and36* System.err.37*38* A stop-gap solution until an error dialog is implemented.39*/40public class OutputViewer {41private static JFrame frame;42private static JTextArea ta;4344static {45System.setOut(PipeListener.create("System.out"));46System.setErr(PipeListener.create("System.err"));47}4849// Dummy to cause class to be loaded50public static void init() { }5152private static void append(String s) {53if (frame == null) {54// FIXME: The frame title should be a localized string.55frame = new JFrame("JConsole: Output");56ta = new JTextArea();57ta.setEditable(false);58frame.getContentPane().add(new JScrollPane(ta));59ta.setFont(new Font("Monospaced", Font.BOLD, 14));60frame.setSize(500, 600);61frame.setLocation(1024-500, 768-600);62// Exit JConsole if no window remains.63// e.g. jconsole -version only creates the OutputViewer64// but no other window.65frame.addWindowListener(new WindowAdapter() {66public void windowClosing(WindowEvent e) {67if (JFrame.getFrames().length == 1) {68System.exit(0);69}70}71});72}73ta.append(s);74ta.setCaretPosition(ta.getText().length());75frame.setVisible(true);76}7778private static void appendln(String s) {79append(s+"\n");80}8182private static class PipeListener extends Thread {83public PrintStream ps;84private String name;85private PipedInputStream inPipe;86private BufferedReader br;8788public static PrintStream create(String name) {89return new PipeListener(name).ps;90}9192private PipeListener(String name) {93this.name = name;9495try {96inPipe = new PipedInputStream();97ps = new PrintStream(new PipedOutputStream(inPipe));98br = new BufferedReader(new InputStreamReader(inPipe));99} catch (IOException e) {100appendln("PipeListener<init>("+name+"): " + e);101}102start();103}104105public void run() {106try {107String str;108while ((str = br.readLine()) != null) {109appendln(str);110111// Hack: Turn off thread check in PipedInputStream.112// Any thread should be allowed to write except this one113// but we just use this one to keep the pipe alive.114try {115java.lang.reflect.Field f =116PipedInputStream.class.getDeclaredField("writeSide");117f.setAccessible(true);118f.set(inPipe, this);119} catch (Exception e) {120appendln("PipeListener("+name+").run: "+e);121}122}123appendln("-- "+name+" closed --");124br.close();125} catch (IOException e) {126appendln("PipeListener("+name+").run: "+e);127}128}129}130}131132133