KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOAPMonitorApplet


1 /*
2  * Copyright 2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 import java.awt.*;
18 import java.awt.event.*;
19 import java.io.*;
20 import java.net.*;
21 import java.text.*;
22 import java.util.*;
23 import javax.swing.*;
24 import javax.swing.border.*;
25 import javax.swing.event.*;
26 import javax.swing.table.*;
27
28 import org.apache.axis.monitor.SOAPMonitorConstants;
29
30 /**
31  * This is a SOAP Mointor Applet class. This class provides
32  * the user interface for displaying data from the SOAP
33  * monitor service.
34  *
35  * @author Brian Price (pricebe@us.ibm.com)
36  *
37  */

38 public class SOAPMonitorApplet extends JApplet {
39
40     /**
41      * Private data
42      */

43     private JPanel main_panel = null;
44     private JTabbedPane tabbed_pane = null;
45     private int port = 0;
46     private Vector pages = null;
47
48     /**
49      * Constructor
50      */

51     public SOAPMonitorApplet() {
52     }
53
54     /**
55      * Applet initialization
56      */

57     public void init() {
58         // Get the port to be used
59
String JavaDoc port_str = getParameter("port");
60         if (port_str != null) {
61             port = Integer.parseInt(port_str);
62         }
63         // Try to use the system look and feel
64
try {
65            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
66         } catch (Exception JavaDoc e){
67         }
68         // Create main panel to hold notebook
69
main_panel = new JPanel();
70         main_panel.setBackground(Color.white);
71         main_panel.setLayout(new BorderLayout());
72         setContentPane(main_panel);
73         // Create the notebook
74
tabbed_pane = new JTabbedPane(JTabbedPane.TOP);
75         main_panel.add(tabbed_pane,BorderLayout.CENTER);
76         // Add notebook page for default host connection
77
pages = new Vector();
78         addPage(new SOAPMonitorPage(getCodeBase().getHost()));
79     }
80
81     /**
82      * Add a page to the notebook
83      */

84     private void addPage(SOAPMonitorPage pg) {
85         tabbed_pane.addTab(" "+pg.getHost()+" ", pg);
86         pages.addElement(pg);
87     }
88
89     /**
90      * Applet is being displayed
91      */

92     public void start() {
93         // Tell all pages to start talking to the server
94
Enumeration e = pages.elements();
95         while (e.hasMoreElements()) {
96             SOAPMonitorPage pg = (SOAPMonitorPage) e.nextElement();
97             if (pg != null) {
98                 pg.start();
99             }
100         }
101     }
102
103     /*
104      * Applet is no longer displayed
105      */

106     public void stop() {
107         // Tell all pages to stop talking to the server
108
Enumeration e = pages.elements();
109         while (e.hasMoreElements()) {
110             SOAPMonitorPage pg = (SOAPMonitorPage) e.nextElement();
111             if (pg != null) {
112                 pg.stop();
113             }
114         }
115     }
116
117     /**
118      * Applet cleanup
119      */

120     public void destroy() {
121         tabbed_pane = null;
122         main_panel = null;
123     }
124
125     /**
126      * This class provides the contents of a notebook page
127      * representing a server connection.
128      */

129     class SOAPMonitorPage extends JPanel
130                           implements Runnable JavaDoc,
131                                      ListSelectionListener,
132                                      ActionListener {
133
134         /**
135          * Status Strings
136          */

137         private final String JavaDoc STATUS_ACTIVE = "The SOAP Monitor is started.";
138         private final String JavaDoc STATUS_STOPPED = "The SOAP Monitor is stopped.";
139         private final String JavaDoc STATUS_CLOSED = "The server communication has been terminated.";
140         private final String JavaDoc STATUS_NOCONNECT = "The SOAP Monitor is unable to communcate with the server.";
141
142         /**
143          * Private data
144          */

145         private String JavaDoc host = null;
146         private Socket socket = null;
147         private ObjectInputStream in = null;
148         private ObjectOutputStream out = null;
149         private SOAPMonitorTableModel model = null;
150         private JTable table = null;
151         private JScrollPane scroll = null;
152         private JPanel list_panel = null;
153         private JPanel list_buttons = null;
154         private JButton remove_button = null;
155         private JButton remove_all_button = null;
156         private JButton filter_button = null;
157         private JPanel details_panel = null;
158         private JPanel details_header = null;
159         private JSplitPane details_soap = null;
160         private JPanel details_buttons = null;
161         private JLabel details_time = null;
162         private JLabel details_target = null;
163         private JLabel details_status = null;
164         private JLabel details_time_value = null;
165         private JLabel details_target_value = null;
166         private JLabel details_status_value = null;
167         private EmptyBorder empty_border = null;
168         private EtchedBorder etched_border = null;
169         private JPanel request_panel = null;
170         private JPanel response_panel = null;
171         private JLabel request_label = null;
172         private JLabel response_label = null;
173         private SOAPMonitorTextArea request_text = null;
174         private SOAPMonitorTextArea response_text = null;
175         private JScrollPane request_scroll = null;
176         private JScrollPane response_scroll = null;
177         private JButton layout_button = null;
178         private JSplitPane split = null;
179         private JPanel status_area = null;
180         private JPanel status_buttons = null;
181         private JButton start_button = null;
182         private JButton stop_button = null;
183         private JLabel status_text = null;
184         private JPanel status_text_panel = null;
185         private SOAPMonitorFilter filter = null;
186         private GridBagLayout details_header_layout = null;
187         private GridBagConstraints details_header_constraints = null;
188         private JCheckBox reflow_xml = null;
189
190         /**
191          * Constructor (create and layout page)
192          */

193         public SOAPMonitorPage(String JavaDoc host_name) {
194             host = host_name;
195             // Set up default filter (show all messages)
196
filter = new SOAPMonitorFilter();
197             // Use borders to help improve appearance
198
etched_border = new EtchedBorder();
199             // Build top portion of split (list panel)
200
model = new SOAPMonitorTableModel();
201             table = new JTable(model);
202             table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
203             table.setRowSelectionInterval(0,0);
204             table.setPreferredScrollableViewportSize(new Dimension(600, 96));
205             table.getSelectionModel().addListSelectionListener(this);
206             scroll = new JScrollPane(table);
207             remove_button = new JButton("Remove");
208             remove_button.addActionListener(this);
209             remove_button.setEnabled(false);
210             remove_all_button = new JButton("Remove All");
211             remove_all_button.addActionListener(this);
212             filter_button = new JButton("Filter ...");
213             filter_button.addActionListener(this);
214             list_buttons = new JPanel();
215             list_buttons.setLayout(new FlowLayout());
216             list_buttons.add(remove_button);
217             list_buttons.add(remove_all_button);
218             list_buttons.add(filter_button);
219             list_panel = new JPanel();
220             list_panel.setLayout(new BorderLayout());
221             list_panel.add(scroll,BorderLayout.CENTER);
222             list_panel.add(list_buttons, BorderLayout.SOUTH);
223             list_panel.setBorder(empty_border);
224             // Build bottom portion of split (message details)
225
details_time = new JLabel("Time: ", SwingConstants.RIGHT);
226             details_target = new JLabel("Target Service: ", SwingConstants.RIGHT);
227             details_status = new JLabel("Status: ", SwingConstants.RIGHT);
228             details_time_value = new JLabel();
229             details_target_value = new JLabel();
230             details_status_value = new JLabel();
231             Dimension preferred_size = details_time.getPreferredSize();
232             preferred_size.width = 1;
233             details_time.setPreferredSize(preferred_size);
234             details_target.setPreferredSize(preferred_size);
235             details_status.setPreferredSize(preferred_size);
236             details_time_value.setPreferredSize(preferred_size);
237             details_target_value.setPreferredSize(preferred_size);
238             details_status_value.setPreferredSize(preferred_size);
239             details_header = new JPanel();
240             details_header_layout = new GridBagLayout();
241             details_header.setLayout(details_header_layout);
242             details_header_constraints = new GridBagConstraints();
243             details_header_constraints.fill=GridBagConstraints.BOTH;
244             details_header_constraints.weightx=0.5;
245             details_header_layout.setConstraints(details_time,details_header_constraints);
246             details_header.add(details_time);
247             details_header_layout.setConstraints(details_time_value,details_header_constraints);
248             details_header.add(details_time_value);
249             details_header_layout.setConstraints(details_target,details_header_constraints);
250             details_header.add(details_target);
251             details_header_constraints.weightx=1.0;
252             details_header_layout.setConstraints(details_target_value,details_header_constraints);
253             details_header.add(details_target_value);
254             details_header_constraints.weightx=.5;
255             details_header_layout.setConstraints(details_status,details_header_constraints);
256             details_header.add(details_status);
257             details_header_layout.setConstraints(details_status_value,details_header_constraints);
258             details_header.add(details_status_value);
259             details_header.setBorder(etched_border);
260             request_label = new JLabel("SOAP Request", SwingConstants.CENTER);
261             request_text = new SOAPMonitorTextArea();
262             request_text.setEditable(false);
263             request_scroll = new JScrollPane(request_text);
264             request_panel = new JPanel();
265             request_panel.setLayout(new BorderLayout());
266             request_panel.add(request_label, BorderLayout.NORTH);
267             request_panel.add(request_scroll, BorderLayout.CENTER);
268             response_label = new JLabel("SOAP Response", SwingConstants.CENTER);
269             response_text = new SOAPMonitorTextArea();
270             response_text.setEditable(false);
271             response_scroll = new JScrollPane(response_text);
272             response_panel = new JPanel();
273             response_panel.setLayout(new BorderLayout());
274             response_panel.add(response_label, BorderLayout.NORTH);
275             response_panel.add(response_scroll, BorderLayout.CENTER);
276             details_soap = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
277             details_soap.setTopComponent(request_panel);
278             details_soap.setRightComponent(response_panel);
279             details_soap.setResizeWeight(.5);
280             details_panel = new JPanel();
281             layout_button = new JButton("Switch Layout");
282             layout_button.addActionListener(this);
283             reflow_xml = new JCheckBox("Reflow XML text");
284             reflow_xml.addActionListener(this);
285             details_buttons = new JPanel();
286             details_buttons.setLayout(new FlowLayout());
287             details_buttons.add(reflow_xml);
288             details_buttons.add(layout_button);
289             details_panel.setLayout(new BorderLayout());
290             details_panel.add(details_header,BorderLayout.NORTH);
291             details_panel.add(details_soap,BorderLayout.CENTER);
292             details_panel.add(details_buttons,BorderLayout.SOUTH);
293             details_panel.setBorder(empty_border);
294             // Add the two parts to the age split pane
295
split = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
296             split.setTopComponent(list_panel);
297             split.setRightComponent(details_panel);
298             // Build status area
299
start_button = new JButton("Start");
300             start_button.addActionListener(this);
301             stop_button = new JButton("Stop");
302             stop_button.addActionListener(this);
303             status_buttons = new JPanel();
304             status_buttons.setLayout(new FlowLayout());
305             status_buttons.add(start_button);
306             status_buttons.add(stop_button);
307             status_text = new JLabel();
308             status_text.setBorder(new BevelBorder(BevelBorder.LOWERED));
309             status_text_panel = new JPanel();
310             status_text_panel.setLayout(new BorderLayout());
311             status_text_panel.add(status_text, BorderLayout.CENTER);
312             status_text_panel.setBorder(empty_border);
313             status_area = new JPanel();
314             status_area.setLayout(new BorderLayout());
315             status_area.add(status_buttons, BorderLayout.WEST);
316             status_area.add(status_text_panel, BorderLayout.CENTER);
317             status_area.setBorder(etched_border);
318             // Add the split and status area to page
319
setLayout(new BorderLayout());
320             add(split, BorderLayout.CENTER);
321             add(status_area, BorderLayout.SOUTH);
322         }
323
324         /**
325          * Get the name of the host we are displaying
326          */

327         public String JavaDoc getHost() {
328             return host;
329         }
330
331         /**
332          * Set the status text
333          */

334         public void setStatus(String JavaDoc txt) {
335             status_text.setForeground(Color.black);
336             status_text.setText(" "+txt);
337         }
338
339         /**
340          * Set the status text to an error
341          */

342         public void setErrorStatus(String JavaDoc txt) {
343             status_text.setForeground(Color.red);
344             status_text.setText(" "+txt);
345         }
346
347         /**
348          * Start talking to the server
349          */

350         public void start() {
351             String JavaDoc codehost = getCodeBase().getHost();
352             if (socket == null) {
353                 try {
354                     // Open the socket to the server
355
socket = new Socket(codehost, port);
356                     // Create output stream
357
out = new ObjectOutputStream(socket.getOutputStream());
358                     out.flush();
359                     // Create input stream and start background
360
// thread to read data from the server
361
in = new ObjectInputStream(socket.getInputStream());
362                     new Thread JavaDoc(this).start();
363                 } catch (Exception JavaDoc e) {
364                     // Exceptions here are unexpected, but we can't
365
// really do anything (so just write it to stdout
366
// in case someone cares and then ignore it)
367
System.out.println("Exception! "+e.toString());
368                     e.printStackTrace();
369                     setErrorStatus(STATUS_NOCONNECT);
370                     socket = null;
371                 }
372             } else {
373                 // Already started
374
}
375             if (socket != null) {
376                 // Make sure the right buttons are enabled
377
start_button.setEnabled(false);
378                 stop_button.setEnabled(true);
379                 setStatus(STATUS_ACTIVE);
380             }
381         }
382
383         /**
384          * Stop talking to the server
385          */

386         public void stop() {
387             if (socket != null) {
388                 // Close all the streams and socket
389
if (out != null) {
390                     try {
391                         out.close();
392                     } catch (IOException ioe) {
393                     }
394                     out = null;
395                 }
396                 if (in != null) {
397                     try {
398                         in.close();
399                     } catch (IOException ioe) {
400                     }
401                     in = null;
402                 }
403                 if (socket != null) {
404                     try {
405                         socket.close();
406                     } catch (IOException ioe) {
407                     }
408                     socket = null;
409                 }
410             } else {
411                 // Already stopped
412
}
413             // Make sure the right buttons are enabled
414
start_button.setEnabled(true);
415             stop_button.setEnabled(false);
416             setStatus(STATUS_STOPPED);
417         }
418
419         /**
420          * Background thread used to receive data from
421          * the server.
422          */

423         public void run() {
424             Long JavaDoc id;
425             Integer JavaDoc message_type;
426             String JavaDoc target;
427             String JavaDoc soap;
428             SOAPMonitorData data;
429             int selected;
430             int row;
431             boolean update_needed;
432             while (socket != null) {
433                 try {
434                     // Get the data from the server
435
message_type = (Integer JavaDoc) in.readObject();
436                     // Process the data depending on its type
437
switch (message_type.intValue()) {
438                         case SOAPMonitorConstants.SOAP_MONITOR_REQUEST:
439                             // Get the id, target and soap info
440
id = (Long JavaDoc) in.readObject();
441                             target = (String JavaDoc) in.readObject();
442                             soap = (String JavaDoc) in.readObject();
443                             // Add new request data to the table
444
data = new SOAPMonitorData(id,target,soap);
445                             model.addData(data);
446                             // If "most recent" selected then update
447
// the details area if needed
448
selected = table.getSelectedRow();
449                             if ((selected == 0) && model.filterMatch(data)) {
450                                 valueChanged(null);
451                             }
452                             break;
453                         case SOAPMonitorConstants.SOAP_MONITOR_RESPONSE:
454                             // Get the id and soap info
455
id = (Long JavaDoc) in.readObject();
456                             soap = (String JavaDoc) in.readObject();
457                             data = model.findData(id);
458                             if (data != null) {
459                                 update_needed = false;
460                                 // Get the selected row
461
selected = table.getSelectedRow();
462                                 // If "most recent", then always
463
// update details area
464
if (selected == 0) {
465                                     update_needed = true;
466                                 }
467                                 // If the data being updated is
468
// selected then update details
469
row = model.findRow(data);
470                                 if ((row != -1) && (row == selected)) {
471                                     update_needed = true;
472                                 }
473                                 // Set the response and update table
474
data.setSOAPResponse(soap);
475                                 model.updateData(data);
476                                 // Refresh details area (if needed)
477
if (update_needed) {
478                                     valueChanged(null);
479                                 }
480                             }
481                             break;
482                     }
483
484                 } catch (Exception JavaDoc e) {
485                     // Exceptions are expected here when the
486
// server communication has been terminated.
487
if (stop_button.isEnabled()) {
488                         stop();
489                         setErrorStatus(STATUS_CLOSED);
490                     }
491                 }
492             }
493         }
494
495         /**
496          * Listener to handle table selection changes
497          */

498         public void valueChanged(ListSelectionEvent e) {
499             int row = table.getSelectedRow();
500             // Check if they selected a specific row
501
if (row > 0) {
502                 remove_button.setEnabled(true);
503             } else {
504                 remove_button.setEnabled(false);
505             }
506             // Check for "most recent" selection
507
if (row == 0) {
508                 row = model.getRowCount() - 1;
509                 if (row == 0) {
510                     row = -1;
511                 }
512             }
513             if (row == -1) {
514                 // Clear the details panel
515
details_time_value.setText("");
516                 details_target_value.setText("");
517                 details_status_value.setText("");
518                 request_text.setText("");
519                 response_text.setText("");
520             } else {
521                 // Show the details for the row
522
SOAPMonitorData soap = model.getData(row);
523                 details_time_value.setText(soap.getTime());
524                 details_target_value.setText(soap.getTargetService());
525                 details_status_value.setText(soap.getStatus());
526                 if (soap.getSOAPRequest() == null) {
527                     request_text.setText("");
528                 } else {
529                     request_text.setText(soap.getSOAPRequest());
530                     request_text.setCaretPosition(0);
531                 }
532                 if (soap.getSOAPResponse() == null) {
533                     response_text.setText("");
534                 } else {
535                     response_text.setText(soap.getSOAPResponse());
536                     response_text.setCaretPosition(0);
537                 }
538             }
539         }
540
541         /**
542          * Listener to handle button actions
543          */

544         public void actionPerformed(ActionEvent e) {
545             // Check if the user pressed the remove button
546
if (e.getSource() == remove_button) {
547                 int row = table.getSelectedRow();
548                 model.removeRow(row);
549                 table.clearSelection();
550                 table.repaint();
551                 valueChanged(null);
552             }
553             // Check if the user pressed the remove all button
554
if (e.getSource() == remove_all_button) {
555                 model.clearAll();
556                 table.setRowSelectionInterval(0,0);
557                 table.repaint();
558                 valueChanged(null);
559             }
560             // Check if the user pressed the filter button
561
if (e.getSource() == filter_button) {
562                 filter.showDialog();
563                 if (filter.okPressed()) {
564                     // Update the display with new filter
565
model.setFilter(filter);
566                     table.repaint();
567                 }
568             }
569             // Check if the user pressed the start button
570
if (e.getSource() == start_button) {
571                 start();
572             }
573             // Check if the user pressed the stop button
574
if (e.getSource() == stop_button) {
575                 stop();
576             }
577             // Check if the user wants to switch layout
578
if (e.getSource() == layout_button) {
579                 details_panel.remove(details_soap);
580                 details_soap.removeAll();
581                 if (details_soap.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
582                     details_soap = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
583                 } else {
584                     details_soap = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
585                 }
586                 details_soap.setTopComponent(request_panel);
587                 details_soap.setRightComponent(response_panel);
588                 details_soap.setResizeWeight(.5);
589                 details_panel.add(details_soap, BorderLayout.CENTER);
590                 details_panel.validate();
591                 details_panel.repaint();
592             }
593             // Check if the user is changing the reflow option
594
if (e.getSource() == reflow_xml) {
595                 request_text.setReflowXML(reflow_xml.isSelected());
596                 response_text.setReflowXML(reflow_xml.isSelected());
597             }
598         }
599     }
600
601     /**
602      * This class represend the data for a SOAP request/response pair
603      */

604     class SOAPMonitorData {
605
606         /**
607          * Private data
608          */

609         private Long JavaDoc id;
610         private String JavaDoc time;
611         private String JavaDoc target;
612         private String JavaDoc soap_request;
613         private String JavaDoc soap_response;
614
615         /**
616          * Constructor
617          */

618         public SOAPMonitorData(Long JavaDoc id, String JavaDoc target, String JavaDoc soap_request) {
619             this.id = id;
620             // A null id is used to signal that the "most recent" entry
621
// is being created.
622
if (id == null) {
623                 this.time = "Most Recent";
624                 this.target = "---";
625                 this.soap_request = null;
626                 this.soap_response = null;
627             } else {
628                 this.time = DateFormat.getTimeInstance().format(new Date());
629                 this.target = target;
630                 this.soap_request = soap_request;
631                 this.soap_response = null;
632             }
633         }
634
635         /**
636          * Get the id for the SOAP message
637          */

638         public Long JavaDoc getId() {
639             return id;
640         }
641
642         /**
643          * Get the time the SOAP request was received by the applet
644          */

645         public String JavaDoc getTime() {
646             return time;
647         }
648
649         /**
650          * Get the SOAP request target service name
651          */

652         public String JavaDoc getTargetService() {
653             return target;
654         }
655
656         /**
657          * Get the status of the request
658          */

659         public String JavaDoc getStatus() {
660             String JavaDoc status = "---";
661             if (id != null) {
662                 status = "Complete";
663                 if (soap_response == null) {
664                     status = "Active";
665                 }
666             }
667             return status;
668         }
669
670         /**
671          * Get the request SOAP contents
672          */

673         public String JavaDoc getSOAPRequest() {
674             return soap_request;
675         }
676
677         /**
678          * Set the resposne SOAP contents
679          */

680         public void setSOAPResponse(String JavaDoc response) {
681             soap_response = response;
682         }
683
684         /**
685          * Get the response SOAP contents
686          */

687         public String JavaDoc getSOAPResponse() {
688             return soap_response;
689         }
690     }
691
692     /**
693      * This table model is used to manage the table displayed
694      * at the top of the page to show all the SOAP messages
695      * we have received and to control which message details are
696      * to be displayed on the bottom of the page.
697      */

698     class SOAPMonitorTableModel extends AbstractTableModel {
699
700         /**
701          * Column titles
702          */

703         private final String JavaDoc[] column_names = { "Time",
704                                                 "Target Service",
705                                                 "Status" };
706         /**
707          * Private data
708          */

709         private Vector data;
710         private Vector filter_include;
711         private Vector filter_exclude;
712         private boolean filter_active;
713         private boolean filter_complete;
714         private Vector filter_data;
715
716         /**
717          * Constructor
718          */

719         public SOAPMonitorTableModel() {
720             data = new Vector();
721             // Add "most recent" entry to top of table
722
SOAPMonitorData soap = new SOAPMonitorData(null,null,null);
723             data.addElement(soap);
724             filter_include = null;
725             filter_exclude = null;
726             filter_active = false;
727             filter_complete = false;
728             filter_data = null;
729             // By default, exclude NotificationService and
730
// EventViewerService messages
731
filter_exclude = new Vector();
732             filter_exclude.addElement("NotificationService");
733             filter_exclude.addElement("EventViewerService");
734             filter_data = new Vector();
735             filter_data.addElement(soap);
736         }
737
738         /**
739          * Get column count (part of table model interface)
740          */

741         public int getColumnCount() {
742             return column_names.length;
743         }
744         
745         /**
746          * Get row count (part of table model interface)
747          */

748         public int getRowCount() {
749             int count = data.size();
750             if (filter_data != null) {
751                 count = filter_data.size();
752             }
753             return count;
754         }
755
756         /**
757          * Get column name (part of table model interface)
758          */

759         public String JavaDoc getColumnName(int col) {
760             return column_names[col];
761         }
762
763         /**
764          * Get value at (part of table model interface)
765          */

766         public Object JavaDoc getValueAt(int row, int col) {
767             SOAPMonitorData soap;
768             String JavaDoc value = null;
769             soap = (SOAPMonitorData) data.elementAt(row);
770             if (filter_data != null) {
771                 soap = (SOAPMonitorData) filter_data.elementAt(row);
772             }
773             switch (col) {
774                 case 0:
775                     value = soap.getTime();
776                     break;
777                 case 1:
778                     value = soap.getTargetService();
779                     break;
780                 case 2:
781                     value = soap.getStatus();
782                     break;
783             }
784             return value;
785         }
786
787         /**
788          * Check if soap data matches filter
789          */

790         public boolean filterMatch(SOAPMonitorData soap) {
791             boolean match = true;
792             if (filter_include != null) {
793                 // Check for service match
794
Enumeration e = filter_include.elements();
795                 match = false;
796                 while (e.hasMoreElements() && !match) {
797                     String JavaDoc service = (String JavaDoc) e.nextElement();
798                     if (service.equals(soap.getTargetService())) {
799                         match = true;
800                     }
801                 }
802             }
803             if (filter_exclude != null) {
804                 // Check for service match
805
Enumeration e = filter_exclude.elements();
806                 while (e.hasMoreElements() && match) {
807                     String JavaDoc service = (String JavaDoc) e.nextElement();
808                     if (service.equals(soap.getTargetService())) {
809                         match = false;
810                     }
811                 }
812             }
813             if (filter_active) {
814                 // Check for active status match
815
if (soap.getSOAPResponse() != null) {
816                     match = false;
817                 }
818             }
819             if (filter_complete) {
820                 // Check for complete status match
821
if (soap.getSOAPResponse() == null) {
822                     match = false;
823                 }
824             }
825             // The "most recent" is always a match
826
if (soap.getId() == null) {
827                 match = true;
828             }
829             return match;
830         }
831
832         /**
833          * Add data to the table as a new row
834          */

835         public void addData(SOAPMonitorData soap) {
836             int row = data.size();
837             data.addElement(soap);
838             if (filter_data != null) {
839                 if (filterMatch(soap)) {
840                     row = filter_data.size();
841                     filter_data.addElement(soap);
842                     fireTableRowsInserted(row,row);
843                 }
844             } else {
845                 fireTableRowsInserted(row,row);
846             }
847         }
848
849         /**
850          * Find the data for a given id
851          */

852         public SOAPMonitorData findData(Long JavaDoc id) {
853             SOAPMonitorData soap = null;
854             for (int row=data.size(); (row > 0) && (soap == null); row--) {
855                 soap = (SOAPMonitorData) data.elementAt(row-1);
856                 if (soap.getId().longValue() != id.longValue()) {
857                     soap = null;
858                 }
859             }
860             return soap;
861         }
862
863         /**
864          * Find the row in the table for a given message id
865          */

866         public int findRow(SOAPMonitorData soap) {
867             int row = -1;
868             if (filter_data != null) {
869                 row = filter_data.indexOf(soap);
870             } else {
871                 row = data.indexOf(soap);
872             }
873             return row;
874         }
875
876         /**
877          * Remove all messages from the table (but leave "most recent")
878          */

879         public void clearAll() {
880             int last_row = data.size() - 1;
881             if (last_row > 0) {
882                 data.removeAllElements();
883                 SOAPMonitorData soap = new SOAPMonitorData(null,null,null);
884                 data.addElement(soap);
885                 if (filter_data != null) {
886                     filter_data.removeAllElements();
887                     filter_data.addElement(soap);
888                 }
889                 fireTableDataChanged();
890             }
891         }
892
893         /**
894          * Remove a message from the table
895          */

896         public void removeRow(int row) {
897             SOAPMonitorData soap = null;
898             if (filter_data == null) {
899                 soap = (SOAPMonitorData) data.elementAt(row);
900                 data.remove(soap);
901             } else {
902                 soap = (SOAPMonitorData) filter_data.elementAt(row);
903                 filter_data.remove(soap);
904                 data.remove(soap);
905             }
906             fireTableRowsDeleted(row,row);
907         }
908
909         /**
910          * Set a new filter
911          */

912         public void setFilter(SOAPMonitorFilter filter) {
913             // Save new filter criteria
914
filter_include = filter.getFilterIncludeList();
915             filter_exclude = filter.getFilterExcludeList();
916             filter_active = filter.getFilterActive();
917             filter_complete = filter.getFilterComplete();
918             applyFilter();
919         }
920
921         /**
922          * Refilter the list of messages
923          */

924         public void applyFilter() {
925             // Re-filter using new criteria
926
filter_data = null;
927             if ((filter_include != null) ||
928                 (filter_exclude != null) ||
929                  filter_active || filter_complete ) {
930                 filter_data = new Vector();
931                 Enumeration e = data.elements();
932                 SOAPMonitorData soap;
933                 while (e.hasMoreElements()) {
934                     soap = (SOAPMonitorData) e.nextElement();
935                     if (filterMatch(soap)) {
936                         filter_data.addElement(soap);
937                     }
938                 }
939             }
940             fireTableDataChanged();
941         }
942
943         /**
944          * Get the data for a row
945          */

946         public SOAPMonitorData getData(int row) {
947             SOAPMonitorData soap = null;
948             if (filter_data == null) {
949                 soap = (SOAPMonitorData) data.elementAt(row);
950             } else {
951                 soap = (SOAPMonitorData) filter_data.elementAt(row);
952             }
953             return soap;
954         }
955
956         /**
957          * Update a message
958          */

959         public void updateData (SOAPMonitorData soap) {
960            int row;
961            if (filter_data == null) {
962                // No filter, so just fire table updated
963
row = data.indexOf(soap);
964                if (row != -1) {
965                    fireTableRowsUpdated(row,row);
966                }
967            } else {
968                // Check if the row was being displayed
969
row = filter_data.indexOf(soap);
970                if (row == -1) {
971                    // Row was not displayed, so check for if it
972
// now needs to be displayed
973
if (filterMatch(soap)) {
974                        int index = -1;
975                        row = data.indexOf(soap) + 1;
976                        while ((row < data.size()) && (index == -1)) {
977                            index = filter_data.indexOf(data.elementAt(row));
978                            if (index != -1) {
979                                // Insert at this location
980
filter_data.add(index,soap);
981                            }
982                            row++;
983                        }
984                        if (index == -1) {
985                            // Insert at end
986
index = filter_data.size();
987                            filter_data.addElement(soap);
988                        }
989                        fireTableRowsInserted(index,index);
990                    }
991                } else {
992                    // Row was displayed, so check if it needs to
993
// be updated or removed
994
if (filterMatch(soap)) {
995                        fireTableRowsUpdated(row,row);
996                    } else {
997                        filter_data.remove(soap);
998                        fireTableRowsDeleted(row,row);
999                    }
1000               }
1001           }
1002        }
1003
1004    }
1005
1006    /**
1007     * Panel with checkbox and list
1008     */

1009    class ServiceFilterPanel extends JPanel
1010                             implements ActionListener,
1011                                        ListSelectionListener,
1012                                        DocumentListener {
1013
1014        private JCheckBox service_box = null;
1015        private Vector filter_list = null;
1016        private Vector service_data = null;
1017        private JList service_list = null;
1018        private JScrollPane service_scroll = null;
1019        private JButton remove_service_button = null;
1020        private JPanel remove_service_panel = null;
1021        private EmptyBorder indent_border = null;
1022        private EmptyBorder empty_border = null;
1023        private JPanel service_area = null;
1024        private JPanel add_service_area = null;
1025        private JTextField add_service_field = null;
1026        private JButton add_service_button = null;
1027        private JPanel add_service_panel = null;
1028
1029        /**
1030         * Constructor
1031         */

1032        public ServiceFilterPanel(String JavaDoc text, Vector list) {
1033            empty_border = new EmptyBorder(5,5,0,5);
1034            indent_border = new EmptyBorder(5,25,5,5);
1035            service_box = new JCheckBox(text);
1036            service_box.addActionListener(this);
1037            service_data = new Vector();
1038            if (list != null) {
1039                service_box.setSelected(true);
1040                service_data = (Vector) list.clone();
1041            }
1042            service_list = new JList(service_data);
1043            service_list.setBorder(new EtchedBorder());
1044            service_list.setVisibleRowCount(5);
1045            service_list.addListSelectionListener(this);
1046            service_list.setEnabled(service_box.isSelected());
1047            service_scroll = new JScrollPane(service_list);
1048            service_scroll.setBorder(new EtchedBorder());
1049            remove_service_button = new JButton("Remove");
1050            remove_service_button.addActionListener(this);
1051            remove_service_button.setEnabled(false);
1052            remove_service_panel = new JPanel();
1053            remove_service_panel.setLayout(new FlowLayout());
1054            remove_service_panel.add(remove_service_button);
1055            service_area = new JPanel();
1056            service_area.setLayout(new BorderLayout());
1057            service_area.add(service_scroll, BorderLayout.CENTER);
1058            service_area.add(remove_service_panel, BorderLayout.EAST);
1059            service_area.setBorder(indent_border);
1060            add_service_field = new JTextField();
1061            add_service_field.addActionListener(this);
1062            add_service_field.getDocument().addDocumentListener(this);
1063            add_service_field.setEnabled(service_box.isSelected());
1064            add_service_button = new JButton("Add");
1065            add_service_button.addActionListener(this);
1066            add_service_button.setEnabled(false);
1067            add_service_panel = new JPanel();
1068            add_service_panel.setLayout(new BorderLayout());
1069            JPanel dummy = new JPanel();
1070            dummy.setBorder(empty_border);
1071            add_service_panel.add(dummy, BorderLayout.WEST);
1072            add_service_panel.add(add_service_button, BorderLayout.EAST);
1073            add_service_area = new JPanel();
1074            add_service_area.setLayout(new BorderLayout());
1075            add_service_area.add(add_service_field, BorderLayout.CENTER);
1076            add_service_area.add(add_service_panel, BorderLayout.EAST);
1077            add_service_area.setBorder(indent_border);
1078            setLayout(new BorderLayout());
1079            add(service_box, BorderLayout.NORTH);
1080            add(service_area, BorderLayout.CENTER);
1081            add(add_service_area, BorderLayout.SOUTH);
1082            setBorder(empty_border);
1083        }
1084
1085        /**
1086         * Get the current list of services
1087         */

1088        public Vector getServiceList() {
1089            Vector list = null;
1090            if (service_box.isSelected()) {
1091                list = service_data;
1092            }
1093            return list;
1094        }
1095
1096        /**
1097         * Listener to handle button actions
1098         */

1099        public void actionPerformed(ActionEvent e) {
1100            // Check if the user changed the service filter option
1101
if (e.getSource() == service_box) {
1102                service_list.setEnabled(service_box.isSelected());
1103                service_list.clearSelection();
1104                remove_service_button.setEnabled(false);
1105                add_service_field.setEnabled(service_box.isSelected());
1106                add_service_field.setText("");
1107                add_service_button.setEnabled(false);
1108            }
1109            // Check if the user pressed the add service button
1110
if ((e.getSource() == add_service_button) ||
1111                (e.getSource() == add_service_field)) {
1112                String JavaDoc text = add_service_field.getText();
1113                if ((text != null) && (text.length() > 0)) {
1114                    service_data.addElement(text);
1115                    service_list.setListData(service_data);
1116                }
1117                add_service_field.setText("");
1118                add_service_field.requestFocus();
1119            }
1120            // Check if the user pressed the remove service button
1121
if (e.getSource() == remove_service_button) {
1122                Object JavaDoc[] sels = service_list.getSelectedValues();
1123                for (int i=0; i<sels.length; i++) {
1124                    service_data.removeElement(sels[i]);
1125                }
1126                service_list.setListData(service_data);
1127                service_list.clearSelection();
1128            }
1129        }
1130
1131        /**
1132         * Handle changes to the text field
1133         */

1134        public void changedUpdate(DocumentEvent e) {
1135            String JavaDoc text = add_service_field.getText();
1136            if ((text != null) && (text.length() > 0)) {
1137                add_service_button.setEnabled(true);
1138            } else {
1139                add_service_button.setEnabled(false);
1140            }
1141        }
1142
1143        /**
1144         * Handle changes to the text field
1145         */

1146        public void insertUpdate(DocumentEvent e) {
1147            changedUpdate(e);
1148        }
1149
1150        /**
1151         * Handle changes to the text field
1152         */

1153        public void removeUpdate(DocumentEvent e) {
1154            changedUpdate(e);
1155        }
1156
1157        /**
1158         * Listener to handle service list selection changes
1159         */

1160        public void valueChanged(ListSelectionEvent e) {
1161            if (service_list.getSelectedIndex() == -1) {
1162                remove_service_button.setEnabled(false);
1163            } else {
1164                remove_service_button.setEnabled(true);
1165            }
1166        }
1167    }
1168
1169    /**
1170     * Class for showing the filter dialog
1171     */

1172    class SOAPMonitorFilter implements ActionListener {
1173
1174        /**
1175         * Private data
1176         */

1177        private JDialog dialog = null;
1178        private JPanel panel = null;
1179        private JPanel buttons = null;
1180        private JButton ok_button = null;
1181        private JButton cancel_button = null;
1182        private ServiceFilterPanel include_panel = null;
1183        private ServiceFilterPanel exclude_panel = null;
1184        private JPanel status_panel = null;
1185        private JCheckBox status_box = null;
1186        private EmptyBorder empty_border = null;
1187        private EmptyBorder indent_border = null;
1188        private JPanel status_options = null;
1189        private ButtonGroup status_group = null;
1190        private JRadioButton status_active = null;
1191        private JRadioButton status_complete = null;
1192        private Vector filter_include_list = null;
1193        private Vector filter_exclude_list = null;
1194        private boolean filter_active = false;
1195        private boolean filter_complete = false;
1196        private boolean ok_pressed = false;
1197
1198        /**
1199         * Constructor
1200         */

1201        public SOAPMonitorFilter() {
1202            // By default, exclude NotificationService and
1203
// EventViewerService messages
1204
filter_exclude_list = new Vector();
1205            filter_exclude_list.addElement("NotificationService");
1206            filter_exclude_list.addElement("EventViewerService");
1207        }
1208
1209        /**
1210         * Get list of services to be included
1211         */

1212        public Vector getFilterIncludeList() {
1213            return filter_include_list;
1214        }
1215
1216        /**
1217         * Get list of services to be excluded
1218         */

1219        public Vector getFilterExcludeList() {
1220            return filter_exclude_list;
1221        }
1222
1223        /**
1224         * Check if filter active messages
1225         */

1226        public boolean getFilterActive() {
1227            return filter_active;
1228        }
1229
1230        /**
1231         * Check if filter complete messages
1232         */

1233        public boolean getFilterComplete() {
1234            return filter_complete;
1235        }
1236
1237        /**
1238         * Show the filter dialog
1239         */

1240        public void showDialog() {
1241            empty_border = new EmptyBorder(5,5,0,5);
1242            indent_border = new EmptyBorder(5,25,5,5);
1243            include_panel = new ServiceFilterPanel("Include messages based on target service:",
1244                                                   filter_include_list);
1245            exclude_panel = new ServiceFilterPanel("Exclude messages based on target service:",
1246                                                   filter_exclude_list);
1247            status_box = new JCheckBox("Filter messages based on status:");
1248            status_box.addActionListener(this);
1249            status_active = new JRadioButton("Active messages only");
1250            status_active.setSelected(true);
1251            status_active.setEnabled(false);
1252            status_complete = new JRadioButton("Complete messages only");
1253            status_complete.setEnabled(false);
1254            status_group = new ButtonGroup();
1255            status_group.add(status_active);
1256            status_group.add(status_complete);
1257            if (filter_active || filter_complete) {
1258                status_box.setSelected(true);
1259                status_active.setEnabled(true);
1260                status_complete.setEnabled(true);
1261                if (filter_complete) {
1262                    status_complete.setSelected(true);
1263                }
1264            }
1265            status_options = new JPanel();
1266            status_options.setLayout(new BoxLayout(status_options, BoxLayout.Y_AXIS));
1267            status_options.add(status_active);
1268            status_options.add(status_complete);
1269            status_options.setBorder(indent_border);
1270            status_panel = new JPanel();
1271            status_panel.setLayout(new BorderLayout());
1272            status_panel.add(status_box, BorderLayout.NORTH);
1273            status_panel.add(status_options, BorderLayout.CENTER);
1274            status_panel.setBorder(empty_border);
1275            ok_button = new JButton("Ok");
1276            ok_button.addActionListener(this);
1277            cancel_button = new JButton("Cancel");
1278            cancel_button.addActionListener(this);
1279            buttons = new JPanel();
1280            buttons.setLayout(new FlowLayout());
1281            buttons.add(ok_button);
1282            buttons.add(cancel_button);
1283            panel = new JPanel();
1284            panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
1285            panel.add(include_panel);
1286            panel.add(exclude_panel);
1287            panel.add(status_panel);
1288            panel.add(buttons);
1289            dialog = new JDialog();
1290            dialog.setTitle("SOAP Monitor Filter");
1291            dialog.setContentPane(panel);
1292            dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
1293            dialog.setModal(true);
1294            dialog.pack();
1295            Dimension d = dialog.getToolkit().getScreenSize();
1296            dialog.setLocation((d.width-dialog.getWidth())/2,
1297                               (d.height-dialog.getHeight())/2);
1298            ok_pressed = false;
1299            dialog.show();
1300        }
1301
1302        /**
1303         * Listener to handle button actions
1304         */

1305        public void actionPerformed(ActionEvent e) {
1306            // Check if the user pressed the ok button
1307
if (e.getSource() == ok_button) {
1308                filter_include_list = include_panel.getServiceList();
1309                filter_exclude_list = exclude_panel.getServiceList();
1310                if (status_box.isSelected()) {
1311                    filter_active = status_active.isSelected();
1312                    filter_complete = status_complete.isSelected();
1313                } else {
1314                    filter_active = false;
1315                    filter_complete = false;
1316                }
1317                ok_pressed = true;
1318                dialog.dispose();
1319            }
1320            // Check if the user pressed the cancel button
1321
if (e.getSource() == cancel_button) {
1322                dialog.dispose();
1323            }
1324            // Check if the user changed the status filter option
1325
if (e.getSource() == status_box) {
1326                status_active.setEnabled(status_box.isSelected());
1327                status_complete.setEnabled(status_box.isSelected());
1328            }
1329        }
1330
1331        /**
1332         * Check if the user pressed the ok button
1333         */

1334        public boolean okPressed() {
1335            return ok_pressed;
1336        }
1337    }
1338
1339    /**
1340     * Text panel class that supports XML reflow
1341     */

1342    class SOAPMonitorTextArea extends JTextArea {
1343
1344        /**
1345         * Private data
1346         */

1347        private boolean format = false;
1348        private String JavaDoc original = "";
1349        private String JavaDoc formatted = null;
1350
1351        /**
1352         * Constructor
1353         */

1354        public SOAPMonitorTextArea() {
1355        }
1356
1357        /**
1358         * Override setText to do formatting
1359         */

1360        public void setText(String JavaDoc text) {
1361            original = text;
1362            formatted = null;
1363            if (format) {
1364                doFormat();
1365                super.setText(formatted);
1366            } else {
1367                super.setText(original);
1368            }
1369        }
1370
1371        /**
1372         * Turn reflow on or off
1373         */

1374        public void setReflowXML(boolean reflow) {
1375            format = reflow;
1376            if (format) {
1377                if (formatted == null) {
1378                    doFormat();
1379                }
1380                super.setText(formatted);
1381            } else {
1382                super.setText(original);
1383            }
1384        }
1385
1386        /**
1387         * Reflow XML
1388         */

1389        public void doFormat() {
1390            Vector parts = new Vector();
1391            char[] chars = original.toCharArray();
1392            int index = 0;
1393            int first = 0;
1394            String JavaDoc part = null;
1395            while (index < chars.length) {
1396                // Check for start of tag
1397
if (chars[index] == '<') {
1398                    // Did we have data before this tag?
1399
if (first < index) {
1400                        part = new String JavaDoc(chars,first,index-first);
1401                        part = part.trim();
1402                        // Save non-whitespace data
1403
if (part.length() > 0) {
1404                            parts.addElement(part);
1405                        }
1406                    }
1407                    // Save the start of tag
1408
first = index;
1409                }
1410                // Check for end of tag
1411
if (chars[index] == '>') {
1412                    // Save the tag
1413
part = new String JavaDoc(chars,first,index-first+1);
1414                    parts.addElement(part);
1415                    first = index+1;
1416                }
1417                // Check for end of line
1418
if ((chars[index] == '\n') || (chars[index] == '\r')) {
1419                    // Was there data on this line?
1420
if (first < index) {
1421                        part = new String JavaDoc(chars,first,index-first);
1422                        part = part.trim();
1423                        // Save non-whitespace data
1424
if (part.length() > 0) {
1425                            parts.addElement(part);
1426                        }
1427                    }
1428                    first = index+1;
1429                }
1430                index++;
1431            }
1432            // Reflow as XML
1433
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
1434            Object JavaDoc[] list = parts.toArray();
1435            int indent = 0;
1436            int pad = 0;
1437            index = 0;
1438            while (index < list.length) {
1439                part = (String JavaDoc) list[index];
1440                if (buf.length() == 0) {
1441                    // Just add first tag (should be XML header)
1442
buf.append(part);
1443                } else {
1444                    // All other parts need to start on a new line
1445
buf.append('\n');
1446                    // If we're at an end tag then decrease indent
1447
if (part.startsWith("</")) {
1448                        indent--;
1449                    }
1450                    // Add any indent
1451
for (pad = 0; pad < indent; pad++) {
1452                        buf.append(" ");
1453                    }
1454                    // Add the tag or data
1455
buf.append(part);
1456                    // If this is a start tag then increase indent
1457
if (part.startsWith("<") &&
1458                        !part.startsWith("</") &&
1459                        !part.endsWith("/>")) {
1460                        indent++;
1461                        // Check for special <tag>data</tag> case
1462
if ((index + 2) < list.length) {
1463                            part = (String JavaDoc) list[index+2];
1464                            if (part.startsWith("</")) {
1465                                part = (String JavaDoc) list[index+1];
1466                                if (!part.startsWith("<")) {
1467                                    buf.append(part);
1468                                    part = (String JavaDoc) list[index+2];
1469                                    buf.append(part);
1470                                    index = index + 2;
1471                                    indent--;
1472                                }
1473                            }
1474                        }
1475                    }
1476                }
1477                index++;
1478            }
1479            formatted = new String JavaDoc(buf);
1480        }
1481    }
1482}
1483
Popular Tags