KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > jmx > browser > model > service > NotificationServiceProvider


1 /*
2
3  * EJTools, the Enterprise Java Tools
4
5  *
6
7  * Distributable under LGPL license.
8
9  * See terms of license at www.gnu.org.
10
11  */

12
13 package org.ejtools.jmx.browser.model.service;
14
15
16
17 import java.awt.Component JavaDoc;
18
19 import java.beans.beancontext.BeanContextChildComponentProxy JavaDoc;
20
21 import java.beans.beancontext.BeanContextServices JavaDoc;
22
23 import java.util.Collection JavaDoc;
24
25 import java.util.Iterator JavaDoc;
26
27 import java.util.ResourceBundle JavaDoc;
28
29 import java.util.Vector JavaDoc;
30
31
32
33 import javax.management.Notification JavaDoc;
34
35 import javax.management.NotificationListener JavaDoc;
36
37 import javax.management.ObjectName JavaDoc;
38
39 import javax.swing.JTable JavaDoc;
40
41 import javax.swing.table.AbstractTableModel JavaDoc;
42
43
44
45 import org.apache.log4j.Logger;
46
47 import org.ejtools.beans.beancontext.CustomBeanContextServiceProvider;
48
49 import org.ejtools.swing.table.TableModelSorter;
50
51 import org.ejtools.util.LimitedStack;
52
53
54
55 /**
56
57  * Description of the Class
58
59  *
60
61  * @author letiemble
62
63  * @created 13 décembre 2001
64
65  * @version $Revision: 1.6 $
66
67  * @todo Javadoc to complete
68
69  */

70
71 public class NotificationServiceProvider extends CustomBeanContextServiceProvider implements BeanContextChildComponentProxy JavaDoc, NotificationService, NotificationListener JavaDoc
72
73 {
74
75    /** Description of the Field */
76
77    protected NotificationTableModel model = null;
78
79    /** Description of the Field */
80
81    protected LimitedStack notifications = new LimitedStack(500);
82
83    /** Description of the Field */
84
85    protected Vector JavaDoc objectNames = new Vector JavaDoc();
86
87    /** Description of the Field */
88
89    protected NotificationService service = null;
90
91    /** Description of the Field */
92
93    protected JTable JavaDoc table = null;
94
95    /** Description of the Field */
96
97    private static Logger logger = Logger.getLogger(NotificationServiceProvider.class);
98
99    /** Description of the Field */
100
101    private static ResourceBundle JavaDoc resources = ResourceBundle.getBundle("org.ejtools.jmx.browser.Resources");
102
103
104
105
106
107    /** Constructor for the ConnectionServiceProvider object */
108
109    public NotificationServiceProvider()
110
111    {
112
113       this.service = this;
114
115    }
116
117
118
119
120
121    /**
122
123     * Adds a feature to the ObjectName attribute of the NotificationService object
124
125     *
126
127     * @param object The feature to be added to the ObjectName attribute
128
129     */

130
131    public void addObjectName(ObjectName JavaDoc object)
132
133    {
134
135       BeanContextServices JavaDoc context = (BeanContextServices JavaDoc) getBeanContext();
136
137
138
139       if (this.hasObjectName(object))
140
141       {
142
143          return;
144
145       }
146
147
148
149       if (context.hasService(ConnectionService.class))
150
151       {
152
153          logger.debug("Using service ConnectionService...");
154
155          try
156
157          {
158
159             ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this);
160
161             service.getMBeanServer().addNotificationListener(object, this, null, "EJTOOLS JMX BROWSER");
162
163             this.objectNames.add(object.getCanonicalName());
164
165          }
166
167          catch (Exception JavaDoc e)
168
169          {
170
171             logger.error("Error during utilisation of service ConnectionService", e);
172
173          }
174
175          finally
176
177          {
178
179             context.releaseService(this, this, ConnectionService.class);
180
181          }
182
183       }
184
185    }
186
187
188
189
190
191    /** Description of the Method */
192
193    public void clearAll()
194
195    {
196
197       this.notifications.clear();
198
199    }
200
201
202
203
204
205    /**
206
207     * @return The component value
208
209     */

210
211    public Component JavaDoc getComponent()
212
213    {
214
215       if (this.table == null)
216
217       {
218
219          this.model = new NotificationServiceProvider.NotificationTableModel();
220
221          TableModelSorter sorter = new TableModelSorter(this.model);
222
223          this.table = new JTable JavaDoc(sorter);
224
225          sorter.addMouseListenerToHeaderInTable(this.table);
226
227       }
228
229       return this.table;
230
231    }
232
233
234
235
236
237    /**
238
239     * Getter for the currentServiceSelectors attribute
240
241     *
242
243     * @param bcs Description of the Parameter
244
245     * @param serviceClass Description of the Parameter
246
247     * @return The currentServiceSelectors value
248
249     */

250
251    public Iterator JavaDoc getCurrentServiceSelectors(BeanContextServices JavaDoc bcs, java.lang.Class JavaDoc serviceClass)
252
253    {
254
255       return (new Vector JavaDoc()).iterator();
256
257    }
258
259
260
261
262
263    /**
264
265     * Getter for the service attribute
266
267     *
268
269     * @param bcs Description of the Parameter
270
271     * @param requestor Description of the Parameter
272
273     * @param serviceClass Description of the Parameter
274
275     * @param serviceSelector Description of the Parameter
276
277     * @return The service value
278
279     */

280
281    public Object JavaDoc getService(BeanContextServices JavaDoc bcs, java.lang.Object JavaDoc requestor, java.lang.Class JavaDoc serviceClass, java.lang.Object JavaDoc serviceSelector)
282
283    {
284
285       return this.service;
286
287    }
288
289
290
291
292
293    /**
294
295     * Description of the Method
296
297     *
298
299     * @param notification Description of the Parameter
300
301     * @param handback Description of the Parameter
302
303     */

304
305    public void handleNotification(Notification JavaDoc notification, java.lang.Object JavaDoc handback)
306
307    {
308
309       logger.debug("Notification received (" + handback + ")");
310
311       this.notifications.push(notification);
312
313       if (this.model != null)
314
315       {
316
317          this.model.fireTableRowsInserted(this.notifications.size(), this.notifications.size());
318
319       }
320
321    }
322
323
324
325
326
327    /**
328
329     * Description of the Method
330
331     *
332
333     * @param object Description of the Parameter
334
335     * @return Description of the Return Value
336
337     */

338
339    public boolean hasObjectName(ObjectName JavaDoc object)
340
341    {
342
343       return this.objectNames.contains(object.getCanonicalName());
344
345    }
346
347
348
349
350
351    /**
352
353     * Description of the Method
354
355     *
356
357     * @return Description of the Return Value
358
359     */

360
361    public Collection JavaDoc listAll()
362
363    {
364
365       return this.reverse(notifications);
366
367    }
368
369
370
371
372
373    /**
374
375     * Description of the Method
376
377     *
378
379     * @param bcs Description of the Parameter
380
381     * @param requestor Description of the Parameter
382
383     * @param service Description of the Parameter
384
385     */

386
387    public void releaseService(BeanContextServices JavaDoc bcs, java.lang.Object JavaDoc requestor, java.lang.Object JavaDoc service) { }
388
389
390
391
392
393    /**
394
395     * Description of the Method
396
397     *
398
399     * @param object Description of the Parameter
400
401     */

402
403    public void removeObjectName(ObjectName JavaDoc object)
404
405    {
406
407       BeanContextServices JavaDoc context = (BeanContextServices JavaDoc) getBeanContext();
408
409
410
411       if (!this.hasObjectName(object))
412
413       {
414
415          return;
416
417       }
418
419
420
421       if (context.hasService(ConnectionService.class))
422
423       {
424
425          logger.debug("Using service ConnectionService...");
426
427          try
428
429          {
430
431             ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this);
432
433             service.getMBeanServer().removeNotificationListener(object, this);
434
435          }
436
437          catch (Exception JavaDoc e)
438
439          {
440
441             logger.error("Error during utilisation of service ConnectionService", e);
442
443          }
444
445          finally
446
447          {
448
449             this.objectNames.remove(object.getCanonicalName());
450
451             context.releaseService(this, this, ConnectionService.class);
452
453          }
454
455       }
456
457    }
458
459
460
461
462
463    /** A unit test for JUnit */
464
465    public void test()
466
467    {
468
469       for (int i = 0; i < 50; i++)
470
471       {
472
473          Notification JavaDoc n = new Notification JavaDoc("aze", "qsd", 1000);
474
475          this.notifications.add(n);
476
477       }
478
479    }
480
481
482
483
484
485    /**
486
487     * @return The serviceClass value
488
489     */

490
491    protected Class JavaDoc[] getServiceClass()
492
493    {
494
495       return new Class JavaDoc[]{NotificationService.class};
496
497    }
498
499
500
501
502
503    /**
504
505     * Description of the Method
506
507     *
508
509     * @param collection Description of the Parameter
510
511     * @return Description of the Return Value
512
513     * @todo Move elsewhere
514
515     */

516
517    protected Collection JavaDoc reverse(Collection JavaDoc collection)
518
519    {
520
521       Vector JavaDoc result = new Vector JavaDoc();
522
523
524
525       Iterator JavaDoc it = collection.iterator();
526
527       while (it.hasNext())
528
529       {
530
531          result.insertElementAt(it.next(), 0);
532
533       }
534
535
536
537       return result;
538
539    }
540
541
542
543
544
545    /**
546
547     * Description of the Class
548
549     *
550
551     * @author letiembl
552
553     * @created 18 septembre 2002
554
555     * @version $Revision: 1.6 $
556
557     * @todo I18N
558
559     */

560
561    private class NotificationTableModel extends AbstractTableModel JavaDoc
562
563    {
564
565       /** Description of the Field */
566
567       private String JavaDoc COLUMN_MESSAGE = resources.getString("notification.table.column.message");
568
569       /** Description of the Field */
570
571       private String JavaDoc COLUMN_SEQUENCE = resources.getString("notification.table.column.sequence");
572
573       /** Description of the Field */
574
575       private String JavaDoc COLUMN_SOURCE = resources.getString("notification.table.column.source");
576
577       /** Description of the Field */
578
579       private String JavaDoc COLUMN_TIMESTAMP = resources.getString("notification.table.column.timestamp");
580
581       /** Description of the Field */
582
583       private String JavaDoc COLUMN_TYPE = resources.getString("notification.table.column.type");
584
585
586
587
588
589       /**
590
591        * @param columnIndex Description of the Parameter
592
593        * @return The columnClass value
594
595        */

596
597       public Class JavaDoc getColumnClass(int columnIndex)
598
599       {
600
601          switch (columnIndex)
602
603          {
604
605             case 0:
606
607                return Object JavaDoc.class;
608
609             case 1:
610
611                return String JavaDoc.class;
612
613             case 2:
614
615                return String JavaDoc.class;
616
617             case 3:
618
619                return String JavaDoc.class;
620
621             case 4:
622
623                return String JavaDoc.class;
624
625          }
626
627          return Object JavaDoc.class;
628
629       }
630
631
632
633
634
635       /**
636
637        * @return The columnCount value
638
639        */

640
641       public int getColumnCount()
642
643       {
644
645          return 5;
646
647       }
648
649
650
651
652
653       /**
654
655        * @param columnIndex Description of the Parameter
656
657        * @return The columnName value
658
659        */

660
661       public String JavaDoc getColumnName(int columnIndex)
662
663       {
664
665          switch (columnIndex)
666
667          {
668
669             case 0:
670
671                return COLUMN_SOURCE;
672
673             case 1:
674
675                return COLUMN_TIMESTAMP;
676
677             case 2:
678
679                return COLUMN_SEQUENCE;
680
681             case 3:
682
683                return COLUMN_TYPE;
684
685             case 4:
686
687                return COLUMN_MESSAGE;
688
689          }
690
691          return super.getColumnName(columnIndex);
692
693       }
694
695
696
697
698
699       /**
700
701        * @return The rowCount value
702
703        */

704
705       public int getRowCount()
706
707       {
708
709          return NotificationServiceProvider.this.notifications.size();
710
711       }
712
713
714
715
716
717       /**
718
719        * @param rowIndex Description of the Parameter
720
721        * @param columnIndex Description of the Parameter
722
723        * @return The valueAt value
724
725        */

726
727       public Object JavaDoc getValueAt(int rowIndex, int columnIndex)
728
729       {
730
731          Object JavaDoc o = null;
732
733          if (rowIndex < NotificationServiceProvider.this.notifications.size())
734
735          {
736
737             Notification JavaDoc notif = (Notification JavaDoc) NotificationServiceProvider.this.notifications.elementAt(rowIndex);
738
739             switch (columnIndex)
740
741             {
742
743                case 0:
744
745                   o = notif.getSource();
746
747                   break;
748
749                case 1:
750
751                   o = new String JavaDoc("" + notif.getTimeStamp());
752
753                   break;
754
755                case 2:
756
757                   o = new String JavaDoc("" + notif.getSequenceNumber());
758
759                   break;
760
761                case 3:
762
763                   o = notif.getType();
764
765                   break;
766
767                case 4:
768
769                   o = notif.getMessage();
770
771                   break;
772
773             }
774
775          }
776
777          if (o == null)
778
779          {
780
781             o = new String JavaDoc();
782
783          }
784
785          return o;
786
787       }
788
789    }
790
791 }
792
793
Popular Tags