KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > chainsaw > Main


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.log4j.chainsaw;
17
18 import java.awt.BorderLayout JavaDoc;
19 import java.awt.Dimension JavaDoc;
20 import java.awt.event.WindowAdapter JavaDoc;
21 import java.awt.event.WindowEvent JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Properties JavaDoc;
24 import javax.swing.BorderFactory JavaDoc;
25 import javax.swing.JFrame JavaDoc;
26 import javax.swing.JMenu JavaDoc;
27 import javax.swing.JMenuBar JavaDoc;
28 import javax.swing.JMenuItem JavaDoc;
29 import javax.swing.JOptionPane JavaDoc;
30 import javax.swing.JPanel JavaDoc;
31 import javax.swing.JScrollPane JavaDoc;
32 import javax.swing.JSplitPane JavaDoc;
33 import javax.swing.JTable JavaDoc;
34 import javax.swing.ListSelectionModel JavaDoc;
35 import org.apache.log4j.Logger;
36 import org.apache.log4j.PropertyConfigurator;
37
38 /**
39  * The main application.
40  *
41  * @author <a HREF="mailto:oliver@puppycrawl.com">Oliver Burn</a>
42  */

43 public class Main
44     extends JFrame JavaDoc
45 {
46     /** the default port number to listen on **/
47     private static final int DEFAULT_PORT = 4445;
48
49     /** name of property for port name **/
50     public static final String JavaDoc PORT_PROP_NAME = "chainsaw.port";
51
52     /** use to log messages **/
53     private static final Logger LOG = Logger.getLogger(Main.class);
54
55
56     /**
57      * Creates a new <code>Main</code> instance.
58      */

59     private Main() {
60         super("CHAINSAW - Log4J Log Viewer");
61         // create the all important model
62
final MyTableModel model = new MyTableModel();
63
64         //Create the menu bar.
65
final JMenuBar JavaDoc menuBar = new JMenuBar JavaDoc();
66         setJMenuBar(menuBar);
67         final JMenu JavaDoc menu = new JMenu JavaDoc("File");
68         menuBar.add(menu);
69
70         try {
71             final LoadXMLAction lxa = new LoadXMLAction(this, model);
72             final JMenuItem JavaDoc loadMenuItem = new JMenuItem JavaDoc("Load file...");
73             menu.add(loadMenuItem);
74             loadMenuItem.addActionListener(lxa);
75         } catch (NoClassDefFoundError JavaDoc 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 JavaDoc 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 JavaDoc exitMenuItem = new JMenuItem JavaDoc("Exit");
92         menu.add(exitMenuItem);
93         exitMenuItem.addActionListener(ExitAction.INSTANCE);
94
95         // Add control panel
96
final ControlPanel cp = new ControlPanel(model);
97         getContentPane().add(cp, BorderLayout.NORTH);
98
99         // Create the table
100
final JTable JavaDoc table = new JTable JavaDoc(model);
101         table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
102         final JScrollPane JavaDoc scrollPane = new JScrollPane JavaDoc(table);
103         scrollPane.setBorder(BorderFactory.createTitledBorder("Events: "));
104         scrollPane.setPreferredSize(new Dimension JavaDoc(900, 300));
105
106         // Create the details
107
final JPanel JavaDoc details = new DetailPanel(table, model);
108         details.setPreferredSize(new Dimension JavaDoc(900, 300));
109
110         // Add the table and stack trace into a splitter
111
final JSplitPane JavaDoc jsp =
112             new JSplitPane JavaDoc(JSplitPane.VERTICAL_SPLIT, scrollPane, details);
113         getContentPane().add(jsp, BorderLayout.CENTER);
114
115         addWindowListener(new WindowAdapter JavaDoc() {
116                 public void windowClosing(WindowEvent JavaDoc aEvent) {
117                     ExitAction.INSTANCE.actionPerformed(null);
118                 }
119             });
120
121         pack();
122         setVisible(true);
123
124         setupReceiver(model);
125     }
126
127     /**
128      * Setup recieving messages.
129      *
130      * @param aModel a <code>MyTableModel</code> value
131      */

132     private void setupReceiver(MyTableModel aModel) {
133         int port = DEFAULT_PORT;
134         final String JavaDoc strRep = System.getProperty(PORT_PROP_NAME);
135         if (strRep != null) {
136             try {
137                 port = Integer.parseInt(strRep);
138             } catch (NumberFormatException JavaDoc 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 JavaDoc 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     ////////////////////////////////////////////////////////////////////////////
167
// static methods
168
////////////////////////////////////////////////////////////////////////////
169

170
171     /** initialise log4j **/
172     private static void initLog4J() {
173         final Properties JavaDoc props = new Properties JavaDoc();
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     /**
183      * The main method.
184      *
185      * @param aArgs ignored
186      */

187     public static void main(String JavaDoc[] aArgs) {
188         initLog4J();
189         new Main();
190     }
191 }
192
Popular Tags