KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > multiServer > launch > OutputPanel


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  */

22 package org.enhydra.multiServer.launch;
23
24 // Kernel imports
25
import org.enhydra.multiServer.launch.capture.Printer;
26 import org.enhydra.multiServer.launch.capture.PrintEvent;
27 import org.enhydra.multiServer.launch.capture.PrintListener;
28
29 // Standard imports
30
import java.io.IOException JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.FileWriter JavaDoc;
33 import java.io.PrintWriter JavaDoc;
34 import java.awt.*;
35 import javax.swing.*;
36 import java.beans.Beans JavaDoc;
37 public class OutputPanel extends JPanel implements PrintListener {
38     private BorderLayout layoutMain;
39     private JScrollPane scrollOutput;
40     private JTextArea textOutput;
41     private boolean critical = false;
42     private Printer errPrinter = new Printer(PrintEvent.ERROR);
43     private Printer outPrinter = new Printer(PrintEvent.OUTPUT);
44
45     public OutputPanel() {
46         try {
47             jbInit();
48         } catch (Exception JavaDoc ex) {
49             ex.printStackTrace();
50         }
51     }
52
53     public void onPrint(PrintEvent event) {
54         final String JavaDoc output = event.getString();
55
56         appendMessage(output);
57     }
58
59     protected void initCapture() {
60         Printer.addPrintListener(this);
61         captureOutput();
62     }
63
64     private void captureOutput() {
65         if (!LaunchApp.debug) {
66             System.setErr(errPrinter);
67             System.setOut(outPrinter);
68         }
69     }
70
71     private LaunchApp getApp() {
72         return LaunchApp.app;
73     }
74
75     protected void appendMessage(final String JavaDoc out) {
76         String JavaDoc message = new String JavaDoc(out);
77
78         appendToFile(message);
79         if (!critical) {
80             if ((message != null)
81                     && (message.indexOf("kernel,CRITICAL") > -1)) {
82                 if (message.length() > 100) {
83                     message = message.substring(0, 75) + "...";
84                 }
85                 message = message + "\n\n See " + getApp().getLogFilePath()
86                           + "\n for more information.";
87                 getApp().runKiller(30);
88                 critical = true;
89                 JOptionPane.showMessageDialog(this, message,
90                                               "Critical Error: Server will shutdown in 30 seconds",
91                                               JOptionPane.ERROR_MESSAGE);
92             }
93         }
94         Runnable JavaDoc sendOutput = new Runnable JavaDoc() {
95             public void run() {
96                 textOutput.append(out + "\n");
97             }
98
99         };
100
101         try {
102             SwingUtilities.invokeLater(sendOutput);
103         } catch (Exception JavaDoc e) {
104             e.printStackTrace();
105         }
106     }
107
108     private void appendToFile(String JavaDoc message) {
109         if (getApp() != null) {
110             try {
111                 PrintWriter JavaDoc logWriter = getApp().getLogWriter();
112
113                 logWriter.println(message);
114             } catch (IOException JavaDoc e) {
115                 getApp().freeOutput();
116                 e.printStackTrace();
117                 captureOutput();
118             }
119         }
120     }
121
122     private void jbInit() throws Exception JavaDoc {
123         layoutMain =
124             (BorderLayout) Beans.instantiate(getClass().getClassLoader(),
125                                              BorderLayout.class.getName());
126         textOutput =
127             (JTextArea) Beans.instantiate(getClass().getClassLoader(),
128                                           JTextArea.class.getName());
129         textOutput.setEditable(false);
130         textOutput.setRows(15);
131         scrollOutput = new JScrollPane(textOutput);
132         this.setLayout(layoutMain);
133         this.add(scrollOutput, BorderLayout.CENTER);
134     }
135
136 }
137
Popular Tags