7 package org.libcsdbg.jtracer;
9 import java.beans.PropertyChangeListener;
11 import java.util.Hashtable;
12 import java.util.Enumeration;
13 import java.util.Vector;
15 import java.io.IOException;
16 import java.io.InputStreamReader;
17 import java.io.BufferedReader;
19 import java.net.Socket;
20 import java.net.ProtocolException;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.awt.Color;
26 import java.awt.Insets;
27 import java.awt.Dimension;
28 import java.awt.Container;
29 import java.awt.BorderLayout;
31 import javax.swing.event.ChangeEvent;
32 import javax.swing.event.ChangeListener;
33 import javax.swing.JFrame;
34 import javax.swing.JToolBar;
35 import javax.swing.JTabbedPane;
36 import javax.swing.JPanel;
37 import javax.swing.JScrollPane;
44 public class Session extends JFrame implements
56 private JTabbedPane
tabs = null;
58 private Vector<TracePane>
traces = null;
66 private Socket
link = null;
69 private BufferedReader
rx = null;
84 Session(JFrame f, Socket sock)
throws IOException
89 Registry conf = Registry.getCurrent();
94 rx =
new BufferedReader(
new InputStreamReader(link.getInputStream()));
100 tools =
new JToolBar();
103 tools.addSeparator();
105 tools.add(
createTool(
"Select previous", (ActionListener) owner));
106 tools.add(
createTool(
"Select next", (ActionListener) owner));
107 tools.add(
createTool(
"Close", (ActionListener) owner));
108 tools.addSeparator();
111 add(
tools, BorderLayout.NORTH);
114 tool.addActionListener(
this);
116 traces =
new Vector<TracePane>();
117 tabs =
new JTabbedPane();
118 tabs.addChangeListener(
this);
119 tabs.setFont((Font) conf.
get(
"component",
"font"));
120 tabs.setForeground((Color) conf.
get(
"component",
"fgcolor"));
121 add(tabs, BorderLayout.CENTER);
124 add(
details, BorderLayout.SOUTH);
126 setSize((Dimension) conf.
get(
"session",
"size"));
127 addPropertyChangeListener(
"sessionRequest", (PropertyChangeListener) owner);
128 addPropertyChangeListener(
"traceCount", (PropertyChangeListener) owner);
130 server =
new Thread(
this);
140 public void addTrace(Hashtable<String, String> rqst)
142 Registry conf = Registry.getCurrent();
155 String host = link.getInetAddress().getCanonicalHostName();
156 String addr = link.getInetAddress().getHostAddress();
157 int port = link.getPort();
160 pane.append(rqst.get(
"trace"));
161 JPanel view =
new JPanel(
new BorderLayout());
163 pane.setMargin(
new Insets(6, 2, 0, 2));
165 JScrollPane scroll =
new JScrollPane(view);
166 scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
167 tabs.addTab(
"Thread " + rqst.get(
"tid"), conf.
loadIcon(
"new.png"), scroll);
169 String path = rqst.get(
"path");
170 int pid = Integer.parseInt(rqst.get(
"pid"), 16);
171 setTitle(path +
" (" + pid +
"@" + addr +
")");
173 int len = rqst.get(
"request").length();
174 StringBuffer msg =
new StringBuffer();
175 msg.append(
"Read " + len +
" bytes ");
176 msg.append(
"for " + path +
" (" + pid +
"@");
177 msg.append(addr +
":" + port +
")");
178 firePropertyChange(
"sessionRequest", null, msg.toString());
179 firePropertyChange(
"traceCount", null,
tabs.getTabCount());
181 tools.getComponent(0).setEnabled(
true);
182 tools.getComponent(7).setEnabled(
true);
193 return (getExtendedState() & ICONIFIED) != 0;
206 int state = getExtendedState();
212 setExtendedState(state);
224 public Hashtable<String, String>
getRequest() throws IOException
226 Hashtable<String, String> retval =
new Hashtable<String, String>();
229 boolean text =
false;
230 StringBuffer buffer =
new StringBuffer();
231 StringBuffer trace =
new StringBuffer();
235 String ln = rx.readLine();
237 throw new IOException(
"The peer disconnected prematurely");
240 buffer.append(ln).append(
"\n");
241 if (ln.length() == 0) {
247 trace.append(ln).append(
"\n");
251 String hdr[] = ln.split(
":");
252 if (hdr.length < 2 || hdr[0].trim().length() == 0) {
253 String msg =
"LDP request format error (" + cnt +
": " + ln +
")";
254 throw new ProtocolException(msg);
257 String field = hdr[1];
258 for (
int i = 2; i < hdr.length; i++)
259 field +=
":" + hdr[i];
261 retval.put(hdr[0].trim(), field.trim());
263 while (!buffer.toString().endsWith(
"}\n\n"));
265 retval.put(
"trace", trace.toString().trim());
266 retval.put(
"request", buffer.toString().trim());
285 catch (Throwable t) {
298 return tabs.getTabCount();
313 String nm = cmd.toLowerCase().replace(
' ',
'_') +
"24.png";
315 retval.setMargin(
new Insets(2, 2, 2, 2));
323 while (
server == Thread.currentThread()) {
325 Hashtable<String, String> rqst =
getRequest();
329 catch (Throwable t) {
345 String cmd = event.getActionCommand();
347 if (cmd.equals(
"Remove trace")) {
348 int i = tabs.getSelectedIndex();
354 int cnt = tabs.getTabCount();
355 firePropertyChange(
"traceCount", null, cnt);
365 tools.getComponent(0).setEnabled(
false);
366 tools.getComponent(7).setEnabled(
false);
369 else if (cmd.equals(
"Lock")) {
372 Registry conf = Registry.getCurrent();
376 tool.setIcon(conf.loadIcon(
"unlock24.png"));
377 tool.setToolTipText(
"Unlock");
381 tool.setIcon(conf.loadIcon(
"lock24.png"));
382 tool.setToolTipText(
"Lock");
386 else if (cmd.equals(
"Find"))
387 Alert.
error(
this,
"Not implemented yet",
false);
390 catch (Throwable t) {
404 int i = tabs.getSelectedIndex();
410 tabs.setIconAt(i, Registry.getCurrent().loadIcon(
"void.png"));
426 String field = pane.getField(
"exception");
428 details.setMessage(field);
430 details.setMessage(
"Thread stack trace");
432 field = pane.getField(
"tstamp");
433 details.setTimestamp(Long.parseLong(field, 16));
435 String host = link.getInetAddress().getCanonicalHostName();
436 int port = link.getPort();
437 details.setAddress(host, port);
440 catch (Throwable t) {
Hashtable< String, String > getRequest()
Read and parse an incoming LDP request.
Object get(String section, String key)
Get an entry.
Vector< TracePane > traces
ImageIcon loadIcon(String nm)
Load an icon from the current theme.
Socket link
Client socket.
int setIconified(boolean how)
Set frame state.
Vector< Image > getProjectIcons()
Get a cross-platform set of project icons.
void stateChanged(ChangeEvent event)
Handler for events fired from the trace enumerator.
JToolBar tools
Trace and frame tools.
BufferedReader rx
Socket buffered reader.
static final long serialVersionUID
Class version.
static void error(JFrame owner, String msg, boolean fatal)
Show an error alert and exit if the error is marked as fatal.
int getTraceCount()
Get the number of traces.
Button createTool(String cmd, ActionListener handler)
Create a toolbar button.
void quit()
Protocol quit.
A bar for various trace details (exception message, timestamp e.t.c) with multiple indicators and con...
JTabbedPane tabs
Trace enumeration pane.
JFrame owner
Main application frame.
User alert or simple prompt dialog with configurable styles.
boolean isLocked
Lock state indicator.
Rich text pane for trace visualization with syntax highlighting.
void addTrace(Hashtable< String, String > rqst)
Add a trace to the tab enumerator.
void actionPerformed(ActionEvent event)
Handler for events fired from the toolbar of the frame.
boolean isIconified()
Check frame state.