KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smack > debugger > ConsoleDebugger


1 package org.jivesoftware.smack.debugger;
2
3 import org.jivesoftware.smack.ConnectionListener;
4 import org.jivesoftware.smack.PacketListener;
5 import org.jivesoftware.smack.XMPPConnection;
6 import org.jivesoftware.smack.packet.Packet;
7 import org.jivesoftware.smack.util.*;
8
9 import java.io.Reader JavaDoc;
10 import java.io.Writer JavaDoc;
11 import java.text.SimpleDateFormat JavaDoc;
12 import java.util.Date JavaDoc;
13
14 /**
15  * Very simple debugger that prints to the console (stdout) the sent and received stanzas. Use
16  * this debugger with caution since printing to the console is an expensive operation that may
17  * even block the thread since only one thread may print at a time.<p>
18  * <p/>
19  * It is possible to not only print the raw sent and received stanzas but also the interpreted
20  * packets by Smack. By default interpreted packets won't be printed. To enable this feature
21  * just change the <tt>printInterpreted</tt> static variable to <tt>true</tt>.
22  *
23  * @author Gaston Dombiak
24  */

25 public class ConsoleDebugger implements SmackDebugger {
26
27     public static boolean printInterpreted = false;
28     private SimpleDateFormat JavaDoc dateFormatter = new SimpleDateFormat JavaDoc("hh:mm:ss aaa");
29
30     private XMPPConnection connection = null;
31
32     private PacketListener listener = null;
33     private ConnectionListener connListener = null;
34
35     private Writer JavaDoc writer;
36     private Reader JavaDoc reader;
37     private ReaderListener readerListener;
38     private WriterListener writerListener;
39
40     public ConsoleDebugger(XMPPConnection connection, Writer JavaDoc writer, Reader JavaDoc reader) {
41         this.connection = connection;
42         this.writer = writer;
43         this.reader = reader;
44         createDebug();
45     }
46
47     /**
48      * Creates the listeners that will print in the console when new activity is detected.
49      */

50     private void createDebug() {
51         // Create a special Reader that wraps the main Reader and logs data to the GUI.
52
ObservableReader debugReader = new ObservableReader(reader);
53         readerListener = new ReaderListener() {
54             public void read(String JavaDoc str) {
55                 System.out.println(
56                         dateFormatter.format(new Date JavaDoc()) + " RCV (" + connection.hashCode() +
57                         "): " +
58                         str);
59             }
60         };
61         debugReader.addReaderListener(readerListener);
62
63         // Create a special Writer that wraps the main Writer and logs data to the GUI.
64
ObservableWriter debugWriter = new ObservableWriter(writer);
65         writerListener = new WriterListener() {
66             public void write(String JavaDoc str) {
67                 System.out.println(
68                         dateFormatter.format(new Date JavaDoc()) + " SENT (" + connection.hashCode() +
69                         "): " +
70                         str);
71             }
72         };
73         debugWriter.addWriterListener(writerListener);
74
75         // Assign the reader/writer objects to use the debug versions. The packet reader
76
// and writer will use the debug versions when they are created.
77
reader = debugReader;
78         writer = debugWriter;
79
80         // Create a thread that will listen for all incoming packets and write them to
81
// the GUI. This is what we call "interpreted" packet data, since it's the packet
82
// data as Smack sees it and not as it's coming in as raw XML.
83
listener = new PacketListener() {
84             public void processPacket(Packet packet) {
85                 if (printInterpreted) {
86                     System.out.println(
87                             dateFormatter.format(new Date JavaDoc()) + " RCV PKT (" +
88                             connection.hashCode() +
89                             "): " +
90                             packet.toXML());
91                 }
92             }
93         };
94
95         connListener = new ConnectionListener() {
96             public void connectionClosed() {
97                 System.out.println(
98                         dateFormatter.format(new Date JavaDoc()) + " Connection closed (" +
99                         connection.hashCode() +
100                         ")");
101             }
102
103             public void connectionClosedOnError(Exception JavaDoc e) {
104                 System.out.println(
105                         dateFormatter.format(new Date JavaDoc()) +
106                         " Connection closed due to an exception (" +
107                         connection.hashCode() +
108                         ")");
109                 e.printStackTrace();
110             }
111         };
112     }
113
114     public Reader JavaDoc newConnectionReader(Reader JavaDoc newReader) {
115         ((ObservableReader)reader).removeReaderListener(readerListener);
116         ObservableReader debugReader = new ObservableReader(newReader);
117         debugReader.addReaderListener(readerListener);
118         reader = debugReader;
119         return reader;
120     }
121
122     public Writer JavaDoc newConnectionWriter(Writer JavaDoc newWriter) {
123         ((ObservableWriter)writer).removeWriterListener(writerListener);
124         ObservableWriter debugWriter = new ObservableWriter(newWriter);
125         debugWriter.addWriterListener(writerListener);
126         writer = debugWriter;
127         return writer;
128     }
129
130     public void userHasLogged(String JavaDoc user) {
131         boolean isAnonymous = "".equals(StringUtils.parseName(user));
132         String JavaDoc title =
133                 "User logged (" + connection.hashCode() + "): "
134                 + (isAnonymous ? "" : StringUtils.parseBareAddress(user))
135                 + "@"
136                 + connection.getServiceName()
137                 + ":"
138                 + connection.getPort();
139         title += "/" + StringUtils.parseResource(user);
140         System.out.println(title);
141         // Add the connection listener to the connection so that the debugger can be notified
142
// whenever the connection is closed.
143
connection.addConnectionListener(connListener);
144     }
145
146     public Reader JavaDoc getReader() {
147         return reader;
148     }
149
150     public Writer JavaDoc getWriter() {
151         return writer;
152     }
153
154     public PacketListener getReaderListener() {
155         return listener;
156     }
157
158     public PacketListener getWriterListener() {
159         return null;
160     }
161 }
162
Popular Tags