1 16 package org.apache.log4j.chainsaw; 17 18 import java.awt.BorderLayout ; 19 import java.awt.Dimension ; 20 import java.awt.event.WindowAdapter ; 21 import java.awt.event.WindowEvent ; 22 import java.io.IOException ; 23 import java.util.Properties ; 24 import javax.swing.BorderFactory ; 25 import javax.swing.JFrame ; 26 import javax.swing.JMenu ; 27 import javax.swing.JMenuBar ; 28 import javax.swing.JMenuItem ; 29 import javax.swing.JOptionPane ; 30 import javax.swing.JPanel ; 31 import javax.swing.JScrollPane ; 32 import javax.swing.JSplitPane ; 33 import javax.swing.JTable ; 34 import javax.swing.ListSelectionModel ; 35 import org.apache.log4j.Logger; 36 import org.apache.log4j.PropertyConfigurator; 37 38 43 public class Main 44 extends JFrame 45 { 46 47 private static final int DEFAULT_PORT = 4445; 48 49 50 public static final String PORT_PROP_NAME = "chainsaw.port"; 51 52 53 private static final Logger LOG = Logger.getLogger(Main.class); 54 55 56 59 private Main() { 60 super("CHAINSAW - Log4J Log Viewer"); 61 final MyTableModel model = new MyTableModel(); 63 64 final JMenuBar menuBar = new JMenuBar (); 66 setJMenuBar(menuBar); 67 final JMenu menu = new JMenu ("File"); 68 menuBar.add(menu); 69 70 try { 71 final LoadXMLAction lxa = new LoadXMLAction(this, model); 72 final JMenuItem loadMenuItem = new JMenuItem ("Load file..."); 73 menu.add(loadMenuItem); 74 loadMenuItem.addActionListener(lxa); 75 } catch (NoClassDefFoundError e) { 76 LOG.info("Missing classes for XML parser", e); 77 JOptionPane.showMessageDialog( 78 this, 79 "XML parser not in classpath - unable to load XML events.", 80 "CHAINSAW", 81 JOptionPane.ERROR_MESSAGE); 82 } catch (Exception e) { 83 LOG.info("Unable to create the action to load XML files", e); 84 JOptionPane.showMessageDialog( 85 this, 86 "Unable to create a XML parser - unable to load XML events.", 87 "CHAINSAW", 88 JOptionPane.ERROR_MESSAGE); 89 } 90 91 final JMenuItem exitMenuItem = new JMenuItem ("Exit"); 92 menu.add(exitMenuItem); 93 exitMenuItem.addActionListener(ExitAction.INSTANCE); 94 95 final ControlPanel cp = new ControlPanel(model); 97 getContentPane().add(cp, BorderLayout.NORTH); 98 99 final JTable table = new JTable (model); 101 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 102 final JScrollPane scrollPane = new JScrollPane (table); 103 scrollPane.setBorder(BorderFactory.createTitledBorder("Events: ")); 104 scrollPane.setPreferredSize(new Dimension (900, 300)); 105 106 final JPanel details = new DetailPanel(table, model); 108 details.setPreferredSize(new Dimension (900, 300)); 109 110 final JSplitPane jsp = 112 new JSplitPane (JSplitPane.VERTICAL_SPLIT, scrollPane, details); 113 getContentPane().add(jsp, BorderLayout.CENTER); 114 115 addWindowListener(new WindowAdapter () { 116 public void windowClosing(WindowEvent aEvent) { 117 ExitAction.INSTANCE.actionPerformed(null); 118 } 119 }); 120 121 pack(); 122 setVisible(true); 123 124 setupReceiver(model); 125 } 126 127 132 private void setupReceiver(MyTableModel aModel) { 133 int port = DEFAULT_PORT; 134 final String strRep = System.getProperty(PORT_PROP_NAME); 135 if (strRep != null) { 136 try { 137 port = Integer.parseInt(strRep); 138 } catch (NumberFormatException nfe) { 139 LOG.fatal("Unable to parse " + PORT_PROP_NAME + 140 " property with value " + strRep + "."); 141 JOptionPane.showMessageDialog( 142 this, 143 "Unable to parse port number from '" + strRep + 144 "', quitting.", 145 "CHAINSAW", 146 JOptionPane.ERROR_MESSAGE); 147 System.exit(1); 148 } 149 } 150 151 try { 152 final LoggingReceiver lr = new LoggingReceiver(aModel, port); 153 lr.start(); 154 } catch (IOException e) { 155 LOG.fatal("Unable to connect to socket server, quiting", e); 156 JOptionPane.showMessageDialog( 157 this, 158 "Unable to create socket on port " + port + ", quitting.", 159 "CHAINSAW", 160 JOptionPane.ERROR_MESSAGE); 161 System.exit(1); 162 } 163 } 164 165 166 170 171 172 private static void initLog4J() { 173 final Properties props = new Properties (); 174 props.setProperty("log4j.rootLogger", "DEBUG, A1"); 175 props.setProperty("log4j.appender.A1", 176 "org.apache.log4j.ConsoleAppender"); 177 props.setProperty("log4j.appender.A1.layout", 178 "org.apache.log4j.TTCCLayout"); 179 PropertyConfigurator.configure(props); 180 } 181 182 187 public static void main(String [] aArgs) { 188 initLog4J(); 189 new Main(); 190 } 191 } 192 | Popular Tags |