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