src/jdk.jconsole/share/classes/sun/tools/jconsole/OutputViewer.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.jconsole;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.WindowAdapter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.event.WindowEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * A simple console window to display messages sent to System.out and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * System.err.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * A stop-gap solution until an error dialog is implemented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class OutputViewer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static JFrame frame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static JTextArea ta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        System.setOut(PipeListener.create("System.out"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        System.setErr(PipeListener.create("System.err"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // Dummy to cause class to be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static void init() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static void append(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (frame == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            // FIXME: The frame title should be a localized string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            frame = new JFrame("JConsole: Output");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            ta = new JTextArea();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            ta.setEditable(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            frame.getContentPane().add(new JScrollPane(ta));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            ta.setFont(new Font("Monospaced", Font.BOLD, 14));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            frame.setSize(500, 600);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            frame.setLocation(1024-500, 768-600);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            // Exit JConsole if no window remains.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            // e.g. jconsole -version only creates the OutputViewer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            // but no other window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            frame.addWindowListener(new WindowAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                public void windowClosing(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    if (JFrame.getFrames().length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                        System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        ta.append(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        ta.setCaretPosition(ta.getText().length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        frame.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static void appendln(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        append(s+"\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static class PipeListener extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        public PrintStream ps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        private PipedInputStream inPipe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        private BufferedReader br;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        public static PrintStream create(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return new PipeListener(name).ps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        private PipeListener(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                inPipe = new PipedInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                ps = new PrintStream(new PipedOutputStream(inPipe));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                br = new BufferedReader(new InputStreamReader(inPipe));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                appendln("PipeListener<init>("+name+"): " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                String str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                while ((str = br.readLine()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    appendln(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    // Hack: Turn off thread check in PipedInputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    // Any thread should be allowed to write except this one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    // but we just use this one to keep the pipe alive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        java.lang.reflect.Field f =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                            PipedInputStream.class.getDeclaredField("writeSide");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        f.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        f.set(inPipe, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        appendln("PipeListener("+name+").run: "+e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                appendln("-- "+name+" closed --");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                br.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                appendln("PipeListener("+name+").run: "+e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
}