1 22 package org.enhydra.multiServer.launch; 23 24 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 import java.io.IOException ; 31 import java.io.File ; 32 import java.io.FileWriter ; 33 import java.io.PrintWriter ; 34 import java.awt.*; 35 import javax.swing.*; 36 import java.beans.Beans ; 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 ex) { 49 ex.printStackTrace(); 50 } 51 } 52 53 public void onPrint(PrintEvent event) { 54 final String 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 out) { 76 String message = new String (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 sendOutput = new Runnable () { 95 public void run() { 96 textOutput.append(out + "\n"); 97 } 98 99 }; 100 101 try { 102 SwingUtilities.invokeLater(sendOutput); 103 } catch (Exception e) { 104 e.printStackTrace(); 105 } 106 } 107 108 private void appendToFile(String message) { 109 if (getApp() != null) { 110 try { 111 PrintWriter logWriter = getApp().getLogWriter(); 112 113 logWriter.println(message); 114 } catch (IOException e) { 115 getApp().freeOutput(); 116 e.printStackTrace(); 117 captureOutput(); 118 } 119 } 120 } 121 122 private void jbInit() throws Exception { 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 |