| 1 12 13 package org.ejtools.jmx.browser.ui; 14 15 16 17 import java.awt.Color ; 18 19 import java.awt.Component ; 20 21 import java.awt.GridBagConstraints ; 22 23 import java.awt.GridBagLayout ; 24 25 import java.awt.GridLayout ; 26 27 import java.awt.Insets ; 28 29 import java.awt.event.ActionEvent ; 30 31 import java.awt.event.ActionListener ; 32 33 import java.beans.Customizer ; 34 35 import java.beans.PropertyChangeEvent ; 36 37 import java.beans.PropertyChangeListener ; 38 39 import java.beans.PropertyEditor ; 40 41 import java.util.ArrayList ; 42 43 import java.util.Arrays ; 44 45 import java.util.Hashtable ; 46 47 import java.util.Iterator ; 48 49 import java.util.List ; 50 51 import java.util.ResourceBundle ; 52 53 54 55 import javax.management.Attribute ; 56 57 import javax.management.MBeanAttributeInfo ; 58 59 import javax.management.MBeanConstructorInfo ; 60 61 import javax.management.MBeanInfo ; 62 63 import javax.management.MBeanNotificationInfo ; 64 65 import javax.management.MBeanOperationInfo ; 66 67 import javax.management.MBeanParameterInfo ; 68 69 import javax.swing.BorderFactory ; 70 71 import javax.swing.JButton ; 72 73 import javax.swing.JComponent ; 74 75 import javax.swing.JLabel ; 76 77 import javax.swing.JPanel ; 78 79 import javax.swing.JScrollPane ; 80 81 import javax.swing.JTextArea ; 82 83 import javax.swing.SwingConstants ; 84 85 86 87 import org.apache.log4j.Logger; 88 89 import org.ejtools.adwt.AwtToolkit; 90 91 import org.ejtools.adwt.editor.ArrayEditor; 92 93 import org.ejtools.adwt.jmx.MBeanMethodDialog; 94 95 import org.ejtools.beans.CustomPropertyEditorManager; 96 97 import org.ejtools.graph.service.GraphConsumer; 98 99 import org.ejtools.graph.service.GraphConsumerMediator; 100 101 import org.ejtools.graph.service.GraphConsumerSelector; 102 103 import org.ejtools.jmx.MBeanAccessor; 104 105 import org.ejtools.jmx.MBeanSorter; 106 107 import org.ejtools.jmx.browser.model.Resource; 108 109 import org.ejtools.util.ClassTools; 110 111 112 113 import com.dreambean.awt.GenericPropertyCustomizer; 114 115 116 117 136 137 public class MBeanCustomizer extends JScrollPane implements Customizer , GraphConsumerMediator 138 139 { 140 141 142 143 private Hashtable invocationDialogs = new Hashtable (); 144 145 146 147 private JPanel mbeanAttributes = null; 148 149 150 151 private JPanel mbeanConstructors = null; 152 153 154 155 private JPanel mbeanInfo = null; 156 157 158 159 private JPanel mbeanNotifications = null; 160 161 162 163 private JPanel mbeanOperations = null; 164 165 166 167 private Resource object = null; 168 169 170 171 private JPanel p = null; 172 173 174 175 private JComponent previous; 176 177 178 179 private ArrayList updaters = new ArrayList (); 180 181 182 183 private static Logger logger = Logger.getLogger(MBeanCustomizer.class); 184 185 186 187 private static ResourceBundle resources = ResourceBundle.getBundle("org.ejtools.jmx.browser.Resources"); 188 189 190 191 192 193 194 195 196 197 public MBeanCustomizer() 198 199 { 200 201 super(new JPanel ()); 202 203 this.setBorder(BorderFactory.createEmptyBorder()); 204 205 this.previous = null; 206 207 this.p = (JPanel ) getViewport().getView(); 208 209 this.p.setLayout(new GridBagLayout ()); 210 211 } 212 213 214 215 216 217 226 227 public MBeanCustomizer(Object obj) 228 229 { 230 231 this(); 232 233 this.setObject(obj); 234 235 } 236 237 238 239 240 241 250 251 public GraphConsumer[] getGraphConsumers() 252 253 { 254 255 return this.object.getGraphConsumers(); 256 257 } 258 259 260 261 262 263 272 273 public void setObject(Object obj) 274 275 { 276 277 try 278 279 { 280 281 this.p.removeAll(); 282 283 284 285 if (obj == null) 286 287 { 288 289 this.validate(); 290 291 this.repaint(); 292 293 return; 294 295 } 296 297 298 299 this.object = (Resource) obj; 300 301 MBeanInfo beaninfo = object.getMBeanInfo(); 302 303 304 305 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 306 307 gridbagconstraints.insets = new Insets (3, 3, 3, 3); 308 309 gridbagconstraints.anchor = GridBagConstraints.NORTH; 310 311 gridbagconstraints.weightx = 1.0D; 312 313 gridbagconstraints.weighty = 0.0D; 314 315 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 316 317 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 318 319 320 321 this.buildInfosPanel(beaninfo, object); 322 323 this.buildConstructorsPanel(beaninfo); 324 325 this.buildNotificationsPanel(beaninfo); 326 327 this.buildAttributesPanel(beaninfo); 328 329 this.buildOperationsPanel(beaninfo); 330 331 332 333 this.p.add(mbeanInfo, gridbagconstraints); 334 335 if (this.mbeanConstructors != null) 336 337 { 338 339 this.p.add(this.mbeanConstructors, gridbagconstraints); 340 341 } 342 343 if (this.mbeanNotifications != null) 344 345 { 346 347 this.p.add(this.mbeanNotifications, gridbagconstraints); 348 349 } 350 351 if (this.mbeanAttributes != null) 352 353 { 354 355 this.p.add(this.mbeanAttributes, gridbagconstraints); 356 357 } 358 359 if (this.mbeanOperations != null) 360 361 { 362 363 this.p.add(this.mbeanOperations, gridbagconstraints); 364 365 } 366 367 368 369 371 gridbagconstraints.weighty = 1.0D; 372 373 gridbagconstraints.fill = GridBagConstraints.BOTH; 374 375 this.p.add(new JLabel (" "), gridbagconstraints); 376 377 378 379 this.validate(); 380 381 this.repaint(); 382 383 } 384 385 catch (Exception exception) 386 387 { 388 389 System.out.println("Exception occurred"); 390 391 exception.printStackTrace(); 392 393 } 394 395 } 396 397 398 399 400 401 412 413 protected void addProperty(PropertyEditor propertyeditor, MBeanAttributeInfo attributeInfo) 414 415 { 416 417 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 418 419 gridbagconstraints.insets = new Insets (2, 2, 2, 2); 420 421 422 423 try 424 425 { 426 427 if (attributeInfo.isReadable()) 428 429 { 430 431 propertyeditor.setValue(object.getAttribute(attributeInfo.getName())); 432 433 } 434 435 } 436 437 catch (Throwable throwable) 438 439 { 440 441 logger.warn("Can't read the property from MBean", throwable); 442 443 } 444 445 446 447 449 JLabel label = new JLabel (attributeInfo.getName() + " :", SwingConstants.RIGHT); 450 451 label.setToolTipText(attributeInfo.getDescription()); 452 453 label.setForeground(AwtToolkit.DARK_BLUE); 454 455 456 457 459 JButton button = null; 460 461 Class c = ClassTools.getClass(attributeInfo.getType()); 462 463 if (ClassTools.isNumeric(c)) 464 465 { 466 467 button = new JButton (resources.getString("customizer.button.graph")); 468 469 final String name = attributeInfo.getName(); 470 471 button.addActionListener( 472 473 new ActionListener () 474 475 { 476 477 public void actionPerformed(ActionEvent e) 478 479 { 480 481 MBeanCustomizer.this.addToGraph(name); 482 483 } 484 485 } 486 487 ); 488 489 } 490 491 492 493 Object obj1; 494 495 if (propertyeditor.supportsCustomEditor()) 496 497 { 498 499 obj1 = propertyeditor.getCustomEditor(); 500 501 if (obj1 instanceof JComponent ) 502 503 { 504 505 if (previous != null) 506 507 { 508 509 previous.setNextFocusableComponent(((Component ) (obj1))); 510 511 } 512 513 previous = (JComponent ) obj1; 514 515 } 516 517 } 518 519 else 520 521 { 522 523 String [] tags = propertyeditor.getTags(); 524 525 if (tags != null) 526 527 { 528 529 obj1 = new GenericPropertyCustomizer(propertyeditor, tags); 530 531 if (previous != null) 532 533 { 534 535 previous.setNextFocusableComponent((Component ) (obj1)); 536 537 } 538 539 previous = (JComponent ) obj1; 540 541 } 542 543 else 544 545 { 546 547 if (attributeInfo.isWritable()) 548 549 { 550 551 obj1 = new GenericPropertyCustomizer(propertyeditor); 552 553 if (previous != null) 554 555 { 556 557 previous.setNextFocusableComponent((Component ) (obj1)); 558 559 } 560 561 previous = (JComponent ) obj1; 562 563 } 564 565 else 566 567 { 568 569 String content = "" + propertyeditor.getAsText(); 570 571 573 575 577 if (content.indexOf('\n') >= 0) 578 579 { 580 581 final JTextArea area = new JTextArea (propertyeditor.getAsText()); 582 583 area.setEditable(false); 584 585 final PropertyEditor pcEditor = propertyeditor; 586 587 obj1 = area; 588 589 pcEditor.addPropertyChangeListener( 590 591 new PropertyChangeListener () 592 593 { 594 595 public void propertyChange(PropertyChangeEvent evt) 596 597 { 598 599 area.setText(pcEditor.getAsText()); 600 601 } 602 603 }); 604 605 } 606 607 else 608 609 { 610 611 final JLabel lbl = new JLabel (propertyeditor.getAsText()); 612 613 final PropertyEditor pcEditor = propertyeditor; 614 615 obj1 = lbl; 616 617 pcEditor.addPropertyChangeListener( 618 619 new PropertyChangeListener () 620 621 { 622 623 public void propertyChange(PropertyChangeEvent evt) 624 625 { 626 627 lbl.setText(pcEditor.getAsText()); 628 629 } 630 631 }); 632 633 } 634 635 } 636 637 } 638 639 } 640 641 642 643 gridbagconstraints.anchor = GridBagConstraints.NORTH; 644 645 gridbagconstraints.weighty = 1.0D; 646 647 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 648 649 650 651 if (button != null) 652 653 { 654 655 gridbagconstraints.weightx = 0.0D; 656 657 gridbagconstraints.gridwidth = 1; 658 659 this.mbeanAttributes.add(label, gridbagconstraints); 660 661 662 663 gridbagconstraints.weightx = 1.0D; 664 665 gridbagconstraints.gridwidth = GridBagConstraints.RELATIVE; 666 667 this.mbeanAttributes.add(((Component ) (obj1)), gridbagconstraints); 668 669 670 671 gridbagconstraints.weightx = 0.0D; 672 673 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 674 675 this.mbeanAttributes.add(button, gridbagconstraints); 676 677 } 678 679 else 680 681 { 682 683 gridbagconstraints.weightx = 0.0D; 684 685 gridbagconstraints.gridwidth = 1; 686 687 this.mbeanAttributes.add(label, gridbagconstraints); 688 689 690 691 gridbagconstraints.weightx = 1.0D; 692 693 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 694 695 this.mbeanAttributes.add(((Component ) (obj1)), gridbagconstraints); 696 697 } 698 699 701 Updater updater = new Updater(propertyeditor, attributeInfo); 702 703 this.getUpdaters().add(updater); 704 705 } 706 707 708 709 710 711 720 721 protected void addToGraph(String attribute) 722 723 { 724 725 GraphConsumer consumer = GraphConsumerSelector.selectWithDialog(this); 726 727 if (consumer != null) 728 729 { 730 731 this.object.registerForGraph(consumer, attribute); 732 733 } 734 735 } 736 737 738 739 740 741 754 755 protected void addUnsupportedProperty(MBeanAttributeInfo attributeInfo) 756 757 { 758 759 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 760 761 gridbagconstraints.insets = new Insets (2, 2, 2, 2); 762 763 764 765 JLabel jlabel = new JLabel (attributeInfo.getName() + " :", SwingConstants.RIGHT); 766 767 jlabel.setToolTipText(attributeInfo.getDescription()); 768 769 jlabel.setForeground(AwtToolkit.DARK_BLUE); 770 771 772 773 gridbagconstraints.anchor = GridBagConstraints.NORTH; 774 775 gridbagconstraints.weightx = 0.0D; 776 777 gridbagconstraints.weighty = 1.0D; 778 779 gridbagconstraints.gridwidth = 1; 780 781 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 782 783 784 785 this.mbeanAttributes.add(jlabel, gridbagconstraints); 786 787 788 789 JLabel lbl = new JLabel (resources.getString("customizer.text.unloadable.class") + " " + ClassTools.classDisplay(attributeInfo.getType())); 790 791 lbl.setForeground(AwtToolkit.DARK_RED); 792 793 794 795 gridbagconstraints.weightx = 1.0D; 796 797 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 798 799 800 801 this.mbeanAttributes.add(lbl, gridbagconstraints); 802 803 } 804 805 806 807 808 809 818 819 protected void buildAttributesPanel(MBeanInfo info) 820 821 { 822 823 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 824 825 gridbagconstraints.insets = new Insets (0, 0, 0, 0); 826 827 gridbagconstraints.anchor = GridBagConstraints.NORTH; 828 829 gridbagconstraints.weightx = 0.0D; 830 831 gridbagconstraints.weighty = 1.0D; 832 833 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 834 835 836 837 MBeanAttributeInfo [] infosArray = info.getAttributes(); 838 839 if (infosArray.length == 0) 840 841 { 842 843 return; 844 845 } 846 847 List infos = Arrays.asList(infosArray); 848 849 MBeanSorter.sortByName(infos); 850 851 852 853 this.mbeanAttributes = new JPanel (new GridBagLayout ()); 854 855 this.mbeanAttributes.setBorder(BorderFactory.createTitledBorder(resources.getString("customizer.text.attributes"))); 856 857 for (int i = 0; i < infos.size(); i++) 858 859 { 860 861 MBeanAttributeInfo attributeInfo = (MBeanAttributeInfo ) infos.get(i); 862 863 PropertyEditor propertyeditor = null; 864 865 Class c = ClassTools.getClass(attributeInfo.getType()); 866 867 868 869 if (c == null) 870 871 { 872 873 this.addUnsupportedProperty(attributeInfo); 874 875 } 876 877 else 878 879 { 880 881 if (c.isArray()) 882 883 { 884 885 Class componentType = c.getComponentType(); 886 887 propertyeditor = new ArrayEditor(componentType); 888 889 this.addProperty(propertyeditor, attributeInfo); 890 891 } 892 893 else 894 895 { 896 897 propertyeditor = CustomPropertyEditorManager.findEditor(c); 898 899 if (propertyeditor == null) 900 901 { 902 903 propertyeditor = CustomPropertyEditorManager.findEditor(String .class); 904 905 } 906 907 this.addProperty(propertyeditor, attributeInfo); 908 909 } 910 911 gridbagconstraints.weighty = 1.0D; 912 913 } 914 915 } 916 917 918 919 gridbagconstraints.weightx = 1.0D; 920 921 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 922 923 924 925 JPanel buttonPanel = new JPanel (new GridLayout (1, 2, 1, 1)); 926 927 JButton button1 = new JButton (resources.getString("customizer.button.refresh")); 928 929 button1.addActionListener( 930 931 new ActionListener () 932 933 { 934 935 public void actionPerformed(ActionEvent e) 936 937 { 938 939 try 940 941 { 942 943 Iterator it = MBeanCustomizer.this.getUpdaters().iterator(); 944 945 while (it.hasNext()) 946 947 { 948 949 Updater updater = (Updater) it.next(); 950 951 updater.updateFromBean(); 952 953 } 954 955 } 956 957 catch (Exception ex) 958 959 { 960 961 logger.error("Error while refreshing" + ex); 962 963 } 964 965 } 966 967 } 968 969 ); 970 971 JButton button2 = new JButton (resources.getString("customizer.button.update")); 972 973 button2.addActionListener( 974 975 new ActionListener () 976 977 { 978 979 public void actionPerformed(ActionEvent e) 980 981 { 982 983 try 984 985 { 986 987 Iterator it = updaters.iterator(); 988 989 while (it.hasNext()) 990 991 { 992 993 Updater updater = (Updater) it.next(); 994 995 updater.updateFromEditor(); 996 997 updater.updateFromBean(); 998 999 } 1000 1001 } 1002 1003 catch (Exception ex) 1004 1005 { 1006 1007 logger.error("Error while updating" + ex); 1008 1009 } 1010 1011 } 1012 1013 } 1014 1015 ); 1016 1017 buttonPanel.add(button1); 1018 1019 buttonPanel.add(button2); 1020 1021 this.mbeanAttributes.add(buttonPanel, gridbagconstraints); 1022 1023 } 1024 1025 1026 1027 1028 1029 1038 1039 protected void buildConstructorsPanel(MBeanInfo info) 1040 1041 { 1042 1043 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 1044 1045 gridbagconstraints.insets = new Insets (0, 0, 0, 0); 1046 1047 gridbagconstraints.anchor = GridBagConstraints.NORTH; 1048 1049 gridbagconstraints.weightx = 1.0D; 1050 1051 gridbagconstraints.weighty = 1.0D; 1052 1053 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 1054 1055 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 1056 1057 1058 1059 MBeanConstructorInfo [] infosArray = info.getConstructors(); 1060 1061 if (infosArray.length == 0) 1062 1063 { 1064 1065 return; 1066 1067 } 1068 1069 List infos = Arrays.asList(infosArray); 1070 1071 MBeanSorter.sortByName(infos); 1072 1073 1074 1075 this.mbeanConstructors = new JPanel (new GridBagLayout ()); 1076 1077 this.mbeanConstructors.setBorder(BorderFactory.createTitledBorder(resources.getString("customizer.text.constructors"))); 1078 1079 for (int i = 0; i < infos.size(); i++) 1080 1081 { 1082 1083 MBeanConstructorInfo ctorInfo = (MBeanConstructorInfo ) infos.get(i); 1084 1085 StringBuffer display = new StringBuffer (); 1086 1087 display.append(ctorInfo.getName()); 1088 1089 display.append("("); 1090 1091 MBeanParameterInfo [] parameters = ctorInfo.getSignature(); 1092 1093 if (parameters.length > 0) 1094 1095 { 1096 1097 for (int j = 0; j < parameters.length; j++) 1098 1099 { 1100 1101 if (j > 0) 1102 1103 { 1104 1105 display.append(", "); 1106 1107 } 1108 1109 display.append(ClassTools.classDisplay(parameters[j].getType())); 1110 1111 } 1112 1113 } 1114 1115 display.append(")"); 1116 1117 1118 1119 JLabel label = new JLabel (display.toString(), SwingConstants.LEFT); 1120 1121 label.setToolTipText(ctorInfo.getDescription()); 1122 1123 label.setForeground(Color.black); 1124 1125 this.mbeanConstructors.add(label, gridbagconstraints); 1126 1127 } 1128 1129 } 1130 1131 1132 1133 1134 1135 |