1 12 13 package org.ejtools.jmx.browser.model.service; 14 15 16 17 import java.awt.Component ; 18 19 import java.beans.beancontext.BeanContextChildComponentProxy ; 20 21 import java.beans.beancontext.BeanContextServices ; 22 23 import java.util.Collection ; 24 25 import java.util.Iterator ; 26 27 import java.util.ResourceBundle ; 28 29 import java.util.Vector ; 30 31 32 33 import javax.management.Notification ; 34 35 import javax.management.NotificationListener ; 36 37 import javax.management.ObjectName ; 38 39 import javax.swing.JTable ; 40 41 import javax.swing.table.AbstractTableModel ; 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 70 71 public class NotificationServiceProvider extends CustomBeanContextServiceProvider implements BeanContextChildComponentProxy , NotificationService, NotificationListener 72 73 { 74 75 76 77 protected NotificationTableModel model = null; 78 79 80 81 protected LimitedStack notifications = new LimitedStack(500); 82 83 84 85 protected Vector objectNames = new Vector (); 86 87 88 89 protected NotificationService service = null; 90 91 92 93 protected JTable table = null; 94 95 96 97 private static Logger logger = Logger.getLogger(NotificationServiceProvider.class); 98 99 100 101 private static ResourceBundle resources = ResourceBundle.getBundle("org.ejtools.jmx.browser.Resources"); 102 103 104 105 106 107 108 109 public NotificationServiceProvider() 110 111 { 112 113 this.service = this; 114 115 } 116 117 118 119 120 121 130 131 public void addObjectName(ObjectName object) 132 133 { 134 135 BeanContextServices context = (BeanContextServices ) 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 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 192 193 public void clearAll() 194 195 { 196 197 this.notifications.clear(); 198 199 } 200 201 202 203 204 205 210 211 public Component 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 (sorter); 224 225 sorter.addMouseListenerToHeaderInTable(this.table); 226 227 } 228 229 return this.table; 230 231 } 232 233 234 235 236 237 250 251 public Iterator getCurrentServiceSelectors(BeanContextServices bcs, java.lang.Class serviceClass) 252 253 { 254 255 return (new Vector ()).iterator(); 256 257 } 258 259 260 261 262 263 280 281 public Object getService(BeanContextServices bcs, java.lang.Object requestor, java.lang.Class serviceClass, java.lang.Object serviceSelector) 282 283 { 284 285 return this.service; 286 287 } 288 289 290 291 292 293 304 305 public void handleNotification(Notification notification, java.lang.Object 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 338 339 public boolean hasObjectName(ObjectName object) 340 341 { 342 343 return this.objectNames.contains(object.getCanonicalName()); 344 345 } 346 347 348 349 350 351 360 361 public Collection listAll() 362 363 { 364 365 return this.reverse(notifications); 366 367 } 368 369 370 371 372 373 386 387 public void releaseService(BeanContextServices bcs, java.lang.Object requestor, java.lang.Object service) { } 388 389 390 391 392 393 402 403 public void removeObjectName(ObjectName object) 404 405 { 406 407 BeanContextServices context = (BeanContextServices ) 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 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 464 465 public void test() 466 467 { 468 469 for (int i = 0; i < 50; i++) 470 471 { 472 473 Notification n = new Notification ("aze", "qsd", 1000); 474 475 this.notifications.add(n); 476 477 } 478 479 } 480 481 482 483 484 485 490 491 protected Class [] getServiceClass() 492 493 { 494 495 return new Class []{NotificationService.class}; 496 497 } 498 499 500 501 502 503 516 517 protected Collection reverse(Collection collection) 518 519 { 520 521 Vector result = new Vector (); 522 523 524 525 Iterator 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 560 561 private class NotificationTableModel extends AbstractTableModel 562 563 { 564 565 566 567 private String COLUMN_MESSAGE = resources.getString("notification.table.column.message"); 568 569 570 571 private String COLUMN_SEQUENCE = resources.getString("notification.table.column.sequence"); 572 573 574 575 private String COLUMN_SOURCE = resources.getString("notification.table.column.source"); 576 577 578 579 private String COLUMN_TIMESTAMP = resources.getString("notification.table.column.timestamp"); 580 581 582 583 private String COLUMN_TYPE = resources.getString("notification.table.column.type"); 584 585 586 587 588 589 596 597 public Class getColumnClass(int columnIndex) 598 599 { 600 601 switch (columnIndex) 602 603 { 604 605 case 0: 606 607 return Object .class; 608 609 case 1: 610 611 return String .class; 612 613 case 2: 614 615 return String .class; 616 617 case 3: 618 619 return String .class; 620 621 case 4: 622 623 return String .class; 624 625 } 626 627 return Object .class; 628 629 } 630 631 632 633 634 635 640 641 public int getColumnCount() 642 643 { 644 645 return 5; 646 647 } 648 649 650 651 652 653 660 661 public String 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 704 705 public int getRowCount() 706 707 { 708 709 return NotificationServiceProvider.this.notifications.size(); 710 711 } 712 713 714 715 716 717 726 727 public Object getValueAt(int rowIndex, int columnIndex) 728 729 { 730 731 Object o = null; 732 733 if (rowIndex < NotificationServiceProvider.this.notifications.size()) 734 735 { 736 737 Notification notif = (Notification ) 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 ("" + notif.getTimeStamp()); 752 753 break; 754 755 case 2: 756 757 o = new String ("" + 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 (); 782 783 } 784 785 return o; 786 787 } 788 789 } 790 791 } 792 793 | Popular Tags |