| 1 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 18 public class LogViewerApplet extends java.applet.Applet  19 implements HTTPConnectionClosedInterface 20 { 21 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 authCode = ""; 32 private int port = 8087; 33 private int plugin = 3; 34 35 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 } 49 50 public void stop() 51 { 52 disconnect(); 53 } 54 55 private void initParam() 56 { 57 String 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 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 ex) 84 { 85 return; 86 } 87 } 88 } 89 90 95 private void initComponents() { 97 java.awt.GridBagConstraints gridBagConstraints; 98 99 buttonPanel = new java.awt.Panel (); 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 (); 106 107 setLayout(new java.awt.GridBagLayout ()); 108 109 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (); 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 (); 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 } 137 138 private void doLogin() 139 { 140 142 if (connect() == false) 143 { 144 statusBar.setText("Connection to " + port + " failed"); 145 return; 146 } 147 148 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(); } 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 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 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 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 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 content_type, 248 int http_status, 249 String reason, 250 String message) 251 { 252 if (status == HTTPMessageListenerInterface.TIMEOUT) 253 { 254 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 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 { 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 content_type, 342 int http_status, 343 String reason, 344 String message) 345 { 346 OAMPFrameworkMessageParser parser = null; 348 349 try 350 { 351 parser = new OAMPFrameworkMessageParser(); 352 } 353 catch (Exception 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 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 } 415 } 416 417 private java.awt.TextField statusBar; 419 private java.awt.Panel buttonPanel; 420 private java.awt.ScrollPane logPane; 421 423 } 424 | Popular Tags |