KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > web > oamp > logviewer > LogViewerApplet


1 /*
2  * LogViewerApplet.java
3  *
4  * Created on May 31, 2003, 2:11 PM
5  */

6
7 package com.quikj.application.web.oamp.logviewer;
8
9 import java.awt.*;
10 import java.net.*;
11 import com.quikj.client.framework.*;
12 import com.quikj.client.beans.*;
13 import com.quikj.application.web.oamp.messaging.*;
14 /**
15  *
16  * @author amit
17  */

18 public class LogViewerApplet extends java.applet.Applet JavaDoc
19 implements HTTPConnectionClosedInterface
20 {
21     // must match with the framework values
22
public static final int TRACE = 0;
23     public static final int INFORMATIONAL = 1;
24     public static final int WARNING = 2;
25     public static final int ERROR = 3;
26     public static final int FATAL = 4;
27     
28     private RichTextPanel logPanel;
29     private ServerCommunications com = null;
30
31     private String JavaDoc authCode = "";
32     private int port = 8087;
33     private int plugin = 3;
34     
35     /** Initializes the applet LogViewerApplet */
36     public void init()
37     {
38         OAMPFrameworkMessageParser.setParserType(OAMPFrameworkMessageParser.NANOXML_PARSER);
39         initParam();
40         
41         initComponents();
42     }
43     
44     public void start()
45     {
46         doLogin();
47         //logPanel.getTextArea().setFont(new Font("monospaced", Font.PLAIN, 12));
48
}
49     
50     public void stop()
51     {
52         disconnect();
53     }
54     
55     private void initParam()
56     {
57         String JavaDoc parm = getParameter("auth-code");
58         if (parm != null)
59         {
60             authCode = parm;
61         }
62                 
63         parm = getParameter("port");
64         if (parm != null)
65         {
66             try
67             {
68                 port = Integer.parseInt(parm);
69             }
70             catch (NumberFormatException JavaDoc ex)
71             {
72                 return;
73             }
74         }
75         
76         parm = getParameter("plugin");
77         if (parm != null)
78         {
79             try
80             {
81                 plugin = Integer.parseInt(parm);
82             }
83             catch (NumberFormatException JavaDoc ex)
84             {
85                 return;
86             }
87         }
88     }
89     
90     /** This method is called from within the init() method to
91      * initialize the form.
92      * WARNING: Do NOT modify this code. The content of this method is
93      * always regenerated by the Form Editor.
94      */

95     private void initComponents()//GEN-BEGIN:initComponents
96
{
97         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
98
99         buttonPanel = new java.awt.Panel JavaDoc();
100         logPane = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
101         logPanel = new RichTextPanel();
102         logPanel.getTextArea().setBackground(Color.lightGray);
103         logPane.add(logPanel);
104         logPane.setSize(400, 200);
105         statusBar = new java.awt.TextField JavaDoc();
106
107         setLayout(new java.awt.GridBagLayout JavaDoc());
108
109         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
110         gridBagConstraints.gridx = 0;
111         gridBagConstraints.gridy = 0;
112         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
113         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
114         gridBagConstraints.weightx = 100.0;
115         add(buttonPanel, gridBagConstraints);
116
117         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
118         gridBagConstraints.gridx = 0;
119         gridBagConstraints.gridy = 1;
120         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
121         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
122         gridBagConstraints.weightx = 100.0;
123         gridBagConstraints.weighty = 100.0;
124         add(logPane, gridBagConstraints);
125
126         statusBar.setEditable(false);
127         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
128         gridBagConstraints.gridx = 0;
129         gridBagConstraints.gridy = 2;
130         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
131         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
132         gridBagConstraints.weightx = 100.0;
133         add(statusBar, gridBagConstraints);
134
135     }//GEN-END:initComponents
136

137     
138     private void doLogin()
139     {
140         // connect and send the registration message
141

142         if (connect() == false)
143         {
144             statusBar.setText("Connection to " + port + " failed");
145             return;
146         }
147         
148         // send the registration message to the server
149
if (login() == false)
150         {
151             statusBar.setText("Could not login to the server");
152         }
153     }
154         
155     private boolean connect()
156     {
157         if (com != null)
158         {
159             disconnect(); // disconnect, if connected
160
}
161         
162         try
163         {
164             com = new ServerCommunications(getCodeBase().getHost(),
165             port,
166             plugin);
167         }
168         catch (UnknownHostException ex)
169         {
170             return false;
171         }
172         
173         // establish a connection to the server
174
statusBar.setText("Connecting to "
175         + getCodeBase().getHost()
176         + ':'
177         + port
178         + " .....");
179         
180         if (com.connect() == false)
181         {
182             statusBar.setText("Connection to server "
183             + getCodeBase().getHost()
184             + ':'
185             + port
186             + " failed");
187             return false;
188         }
189         
190         // start the message listener thread
191
com.start();
192         
193         statusBar.setText("Connected to "
194         + getCodeBase().getHost()
195         + ':'
196         + port);
197         
198         com.setClosedListener(this);
199         
200         return true;
201     }
202     
203     private void disconnect()
204     {
205         if (com != null)
206         {
207             if (com.isConnected() == true)
208             {
209                 com.disconnect();
210             }
211         }
212         
213         statusBar.setText("Disconnected");
214         
215         // clear log area
216
logPanel.getTextArea().clearText();
217     }
218     
219     private boolean login()
220     {
221         RegistrationRequestMessage message = new RegistrationRequestMessage();
222         message.setAuthCode(authCode);
223                 
224         int req_id = com.sendRequestMessage("text/xml",
225         OAMPFrameworkMessageParser.format(OAMPSystemMessageParser.OAMP_SYSTEM_FEATURE_NAME,
226         message),
227         new LoginResponseListener(),
228         10000);
229         if (req_id == -1)
230         {
231             return false;
232         }
233         
234         return true;
235     }
236     
237     public void connectionClosed(String JavaDoc host, int port, int appl_id)
238     {
239         disconnect();
240         statusBar.setText("Lost connection with the server");
241     }
242     
243     class LoginResponseListener implements HTTPMessageListenerInterface
244     {
245         public void messageReceived(int req_id,
246         int status,
247         String JavaDoc content_type,
248         int http_status,
249         String JavaDoc reason,
250         String JavaDoc message)
251         {
252             if (status == HTTPMessageListenerInterface.TIMEOUT)
253             {
254                 // close the connection
255
disconnect();
256                 
257                 statusBar.setText("Time-out receiving response from server");
258             }
259             else if (status == HTTPMessageListenerInterface.RECEIVED)
260             {
261                 if (http_status == HTTPRspMessage.OK)
262                 {
263                     OAMPFrameworkMessageParser.setParserType(OAMPFrameworkMessageParser.NANOXML_PARSER);
264                     OAMPFrameworkMessageParser parser = null;
265                     try
266                     {
267                         parser = new OAMPFrameworkMessageParser();
268                     }
269                     catch (Exception JavaDoc ex)
270                     {
271                         disconnect();
272                         statusBar.setText("Error obtaining XML parser");
273                         return;
274                     }
275                     
276                     if (parser.parse(message, false) == false)
277                     {
278                         disconnect();
279                         statusBar.setText("Error decoding server response");
280                         return;
281                     }
282                     
283                     if (parser.isSystemMessage() == false)
284                     {
285                         disconnect();
286                         statusBar.setText("Bad server response");
287                         return;
288                     }
289                     
290                     OAMPSystemMessageParser sysparser = new OAMPSystemMessageParser();
291                     if (sysparser.parse(parser.getFirstChild(), false) == false)
292                     {
293                         disconnect();
294                         statusBar.setText("Error decoding server system message response");
295                         return;
296                     }
297                     
298                     OAMPMessageInterface parsed_message = sysparser.getMessage();
299                     if ((parsed_message instanceof
300                     com.quikj.application.web.oamp.messaging.RegistrationResponseMessage) == true)
301                     {
302                         RegistrationResponseMessage response_msg = (RegistrationResponseMessage)parsed_message;
303                        
304                         statusBar.setText("Logged in");
305                         com.setRequestListener(new RequestListener());
306                     }
307                     else
308                     {
309                         disconnect();
310                         statusBar.setText("Bad server response");
311                         return;
312                     }
313                 }
314                 else
315                 {
316                     disconnect();
317                     
318                     if (reason != null)
319                     {
320                         statusBar.setText(reason + " (" + http_status + ")");
321                     }
322                     else
323                     {
324                         statusBar.setText("Server returned error: " + http_status);
325                     }
326                 }
327             }
328             else // unexpected status
329
{
330                 disconnect();
331                 
332                 statusBar.setText("Unknown status: " + status + " received from the server");
333             }
334         }
335     }
336
337     class RequestListener implements HTTPMessageListenerInterface
338     {
339         public void messageReceived(int req_id,
340         int status,
341         String JavaDoc content_type,
342         int http_status,
343         String JavaDoc reason,
344         String JavaDoc message)
345         {
346             // parse the message
347
OAMPFrameworkMessageParser parser = null;
348             
349             try
350             {
351                 parser = new OAMPFrameworkMessageParser();
352             }
353             catch (Exception JavaDoc ex)
354             {
355                 statusBar.setText("Error obtaining XML parser: "
356                 + ex.getClass().getName());
357                 return;
358             }
359             
360             if (parser.parse(message, true) == false)
361             {
362                 statusBar.setText("Received request message could not be parsed: "
363                 + parser.getErrorMessage());
364                 return;
365             }
366             
367             if (parser.isSystemMessage() == false)
368             {
369                 statusBar.setText("Received a non system message");
370                 return;
371             }
372             
373             OAMPSystemMessageParser sparser = new OAMPSystemMessageParser();
374             
375             if (sparser.parse(parser.getFirstChild(), true) == false)
376             {
377                 statusBar.setText("Received request message could not be parsed: "
378                 + parser.getErrorMessage());
379                return;
380             }
381             
382             OAMPMessageInterface parsed_message = sparser.getMessage();
383             if ((parsed_message instanceof LogRequestMessage) == true)
384             {
385                 LogRequestMessage log_message = (LogRequestMessage)parsed_message;
386                 String JavaDoc log = log_message.getMessage();
387                 
388                 int severity = log_message.getSeverity();
389                 Color color = Color.black;
390                 int style = Font.PLAIN;
391                 switch (severity)
392                 {
393                     case TRACE:
394                         color = Color.black;
395                         break;
396                     case INFORMATIONAL:
397                         color = Color.blue;
398                         break;
399                     case WARNING:
400                         color = Color.yellow;
401                         break;
402                     case ERROR:
403                         color = Color.red;
404                         break;
405                     case FATAL:
406                         color = Color.red;
407                         style = Font.BOLD;
408                         break;
409                 }
410                 
411                 logPanel.getTextArea().addText(log, color, style);
412             }
413             // else, ignore
414
}
415     }
416         
417     // Variables declaration - do not modify//GEN-BEGIN:variables
418
private java.awt.TextField JavaDoc statusBar;
419     private java.awt.Panel JavaDoc buttonPanel;
420     private java.awt.ScrollPane JavaDoc logPane;
421     // End of variables declaration//GEN-END:variables
422

423 }
424
Popular Tags