1 7 package org.ejtools.adwt; 8 9 import java.awt.Component ; 10 import java.awt.Container ; 11 import java.awt.Frame ; 12 import java.awt.GridBagConstraints ; 13 import java.awt.GridBagLayout ; 14 import java.awt.GridLayout ; 15 import java.awt.Insets ; 16 import java.awt.event.ActionEvent ; 17 import java.awt.event.ActionListener ; 18 import java.beans.Customizer ; 19 import java.beans.PropertyChangeEvent ; 20 import java.beans.PropertyChangeListener ; 21 import java.beans.PropertyEditor ; 22 import java.lang.reflect.Method ; 23 24 import javax.management.Attribute ; 25 import javax.management.MBeanAttributeInfo ; 26 import javax.management.MBeanConstructorInfo ; 27 import javax.management.MBeanInfo ; 28 import javax.management.MBeanNotificationInfo ; 29 import javax.management.MBeanOperationInfo ; 30 import javax.management.MBeanParameterInfo ; 31 import javax.swing.BorderFactory ; 32 import javax.swing.JButton ; 33 import javax.swing.JComponent ; 34 import javax.swing.JLabel ; 35 import javax.swing.JOptionPane ; 36 import javax.swing.JPanel ; 37 import javax.swing.JScrollPane ; 38 import javax.swing.SwingConstants ; 39 import javax.swing.SwingUtilities ; 40 41 import org.ejtools.jmx.MBeanAccessor; 42 import org.ejtools.util.ClassTools; 43 44 import com.dreambean.awt.GenericPropertyCustomizer; 45 46 58 public class GenericMBeanCustomizer extends JScrollPane implements Customizer 59 { 60 61 private JPanel mbeanConstructors = null; 62 63 private JPanel mbeanInfo = null; 64 65 private JPanel mbeanNotifications = null; 66 67 private JPanel mbeanOperations = null; 68 69 private MBeanAccessor object; 70 71 private JPanel p; 72 73 private JComponent previous; 74 75 76 77 78 public GenericMBeanCustomizer() 79 { 80 super(new JPanel ()); 81 previous = null; 82 this.p = (JPanel ) getViewport().getView(); 83 this.p.setLayout(new GridBagLayout ()); 84 } 85 86 87 92 public GenericMBeanCustomizer(Object obj) 93 { 94 this(); 95 this.setObject(obj); 96 } 97 98 99 105 public void addArrayProperty(PropertyEditor propertyeditor, MBeanAttributeInfo attributeInfo) 106 { 107 Object obj = null; 108 Object [] array = null; 109 JPanel jpanel = null; 110 111 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 112 gridbagconstraints.insets = new Insets (3, 3, 3, 3); 113 114 try 115 { 116 if (attributeInfo.isReadable()) 117 { 118 obj = object.getAttribute(attributeInfo.getName()); 119 } 120 if (obj != null) 121 { 122 array = (Object []) obj; 123 } 124 } 125 catch (Throwable _ex) 126 { 127 } 128 129 JLabel jlabel = new JLabel (attributeInfo.getName() + " :", SwingConstants.RIGHT); 130 jlabel.setToolTipText(attributeInfo.getDescription()); 131 132 gridbagconstraints.anchor = GridBagConstraints.NORTH; 133 gridbagconstraints.weightx = 0.0D; 134 gridbagconstraints.weighty = 1.0D; 135 gridbagconstraints.gridwidth = GridBagConstraints.RELATIVE; 136 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 137 138 140 if (array == null) 141 { 142 jpanel = new JPanel (); 143 } 144 else 145 { 146 jpanel = new JPanel (new GridLayout (array.length, 1)); 147 for (int i = 0; i < array.length; i++) 148 { 149 try 150 { 151 PropertyEditor propertyeditor1 = (PropertyEditor ) propertyeditor.getClass().newInstance(); 152 propertyeditor1.setValue(array[i]); 153 154 Object obj1; 155 156 if (propertyeditor1.supportsCustomEditor()) 157 { 158 obj1 = propertyeditor1.getCustomEditor(); 159 if (obj1 instanceof JComponent ) 160 { 161 if (previous != null) 162 { 163 previous.setNextFocusableComponent(((Component ) (obj1))); 164 } 165 previous = (JComponent ) obj1; 166 } 167 } 168 else 169 { 170 String as[] = propertyeditor1.getTags(); 171 if (as != null) 172 { 173 obj1 = new GenericPropertyCustomizer(propertyeditor1, as); 174 if (previous != null) 175 { 176 previous.setNextFocusableComponent(((Component ) (obj1))); 177 } 178 previous = (JComponent ) obj1; 179 } 180 else 181 { 182 final JLabel lbl = new JLabel (propertyeditor1.getAsText()); 183 final PropertyEditor pcEditor = propertyeditor1; 184 obj1 = lbl; 185 pcEditor.addPropertyChangeListener( 186 new PropertyChangeListener () 187 { 188 public void propertyChange(PropertyChangeEvent evt) 189 { 190 lbl.setText(pcEditor.getAsText()); 191 } 192 }); 193 } 194 } 195 jpanel.add((Component ) obj1); 196 } 197 catch (Exception _ex) 198 { 199 } 200 } 201 } 202 203 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 204 gridbagconstraints.weightx = 1.0D; 205 } 207 208 209 215 public void addProperty(PropertyEditor propertyeditor, MBeanAttributeInfo attributeInfo) 216 { 217 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 218 gridbagconstraints.insets = new Insets (3, 3, 3, 3); 219 220 try 221 { 222 if (attributeInfo.isReadable()) 223 { 224 propertyeditor.setValue(object.getAttribute(attributeInfo.getName())); 225 } 226 } 227 catch (Throwable _ex) 228 { 229 } 230 231 JLabel jlabel = new JLabel (attributeInfo.getName() + " :", SwingConstants.RIGHT); 232 jlabel.setToolTipText(attributeInfo.getDescription()); 233 234 gridbagconstraints.anchor = GridBagConstraints.NORTH; 235 gridbagconstraints.weightx = 0.0D; 236 gridbagconstraints.weighty = 1.0D; 237 gridbagconstraints.gridwidth = GridBagConstraints.RELATIVE; 238 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 239 240 242 Object obj1; 243 if (propertyeditor.supportsCustomEditor()) 244 { 245 obj1 = propertyeditor.getCustomEditor(); 246 if (obj1 instanceof JComponent ) 247 { 248 if (previous != null) 249 { 250 previous.setNextFocusableComponent(((Component ) (obj1))); 251 } 252 previous = (JComponent ) obj1; 253 } 254 } 255 else 256 { 257 String [] tags = propertyeditor.getTags(); 258 if (tags != null) 259 { 260 obj1 = new GenericPropertyCustomizer(propertyeditor, tags); 261 if (previous != null) 262 { 263 previous.setNextFocusableComponent((Component ) (obj1)); 264 } 265 previous = (JComponent ) obj1; 266 } 267 else 268 { 269 if (attributeInfo.isWritable()) 270 { 271 obj1 = new GenericPropertyCustomizer(propertyeditor); 272 if (previous != null) 273 { 274 previous.setNextFocusableComponent((Component ) (obj1)); 275 } 276 previous = (JComponent ) obj1; 277 } 278 else 279 { 280 final JLabel lbl = new JLabel (propertyeditor.getAsText()); 281 final PropertyEditor pcEditor = propertyeditor; 282 obj1 = lbl; 283 pcEditor.addPropertyChangeListener( 284 new PropertyChangeListener () 285 { 286 public void propertyChange(PropertyChangeEvent evt) 287 { 288 lbl.setText(pcEditor.getAsText()); 289 } 290 }); 291 } 292 } 293 } 294 295 gridbagconstraints.weightx = 1.0D; 296 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 297 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 298 299 301 if (attributeInfo.isWritable()) 302 { 303 propertyeditor.addPropertyChangeListener(new BeanUpdater(attributeInfo)); 304 } 305 306 try 307 { 308 Method method = object.getClass().getMethod("addPropertyChangeListener", new Class []{java.lang.String .class, java.beans.PropertyChangeListener .class}); 309 method.invoke(object, new Object []{attributeInfo.getName(), new EditorUpdater(propertyeditor, attributeInfo.getName())}); 310 } 311 catch (Exception _ex) 312 { 313 try 314 { 315 Method method1 = object.getClass().getMethod("addPropertyChangeListener", new Class []{java.beans.PropertyChangeListener .class}); 316 method1.invoke(object, new Object []{new EditorUpdater(propertyeditor, attributeInfo)}); 317 } 318 catch (Exception _ex2) 319 { 320 } 321 } 322 } 323 324 325 330 public void addUnsupportedProperty(MBeanAttributeInfo attributeInfo) 331 { 332 System.out.println("In addUnsupportedProperty " + attributeInfo.getType()); 333 334 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 335 gridbagconstraints.insets = new Insets (3, 3, 3, 3); 336 337 JLabel jlabel = new JLabel (attributeInfo.getName() + " :", SwingConstants.RIGHT); 338 jlabel.setToolTipText(attributeInfo.getDescription()); 339 340 gridbagconstraints.anchor = GridBagConstraints.NORTH; 341 gridbagconstraints.weightx = 0.0D; 342 gridbagconstraints.weighty = 1.0D; 343 gridbagconstraints.gridwidth = GridBagConstraints.RELATIVE; 344 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 345 346 348 JLabel lbl = new JLabel ("Unloadabled class " + attributeInfo.getType()); 349 350 gridbagconstraints.weightx = 1.0D; 351 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 352 353 } 355 356 357 362 public void setObject(Object obj) 363 { 364 try 365 { 366 this.p.removeAll(); 367 368 if (obj == null) 369 { 370 this.validate(); 371 this.repaint(); 372 return; 373 } 374 375 this.object = (MBeanAccessor) obj; 376 MBeanInfo beaninfo = object.getMBeanInfo(); 377 378 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 379 gridbagconstraints.insets = new Insets (3, 3, 3, 3); 380 gridbagconstraints.anchor = GridBagConstraints.NORTH; 381 gridbagconstraints.weightx = 1.0D; 382 gridbagconstraints.weighty = 0.0D; 383 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 384 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 385 386 this.buildInfosPanel(beaninfo, object); 387 this.buildConstructorsPanel(beaninfo); 388 this.buildNotificationsPanel(beaninfo); 389 this.buildOperationsPanel(beaninfo); 390 391 this.p.add(mbeanInfo, gridbagconstraints); 392 if (mbeanConstructors != null) 393 { 394 this.p.add(mbeanConstructors, gridbagconstraints); 395 } 396 if (mbeanNotifications != null) 397 { 398 this.p.add(mbeanNotifications, gridbagconstraints); 399 } 400 if (mbeanOperations != null) 401 { 402 this.p.add(mbeanOperations, gridbagconstraints); 403 } 404 405 gridbagconstraints.weighty = 1.0D; 407 gridbagconstraints.fill = GridBagConstraints.BOTH; 408 this.p.add(new JLabel (" "), gridbagconstraints); 409 410 validate(); 411 repaint(); 412 } 413 catch (Exception exception) 414 { 415 System.out.println("Exception occurred"); 416 exception.printStackTrace(); 417 } 418 } 419 420 421 428 protected void updated(String s, Object obj, Object obj1) 429 { 430 firePropertyChange(s, obj, obj1); 431 } 432 433 434 439 private void buildConstructorsPanel(MBeanInfo info) 440 { 441 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 442 gridbagconstraints.insets = new Insets (2, 2, 2, 2); 443 gridbagconstraints.anchor = GridBagConstraints.NORTH; 444 gridbagconstraints.weightx = 1.0D; 445 gridbagconstraints.weighty = 1.0D; 446 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 447 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 448 449 MBeanConstructorInfo [] infos = info.getConstructors(); 450 if (infos.length == 0) 451 { 452 return; 453 } 454 this.mbeanConstructors = new JPanel (new GridBagLayout ()); 455 this.mbeanConstructors.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Constructors")); 456 for (int i = 0; i < infos.length; i++) 457 { 458 StringBuffer display = new StringBuffer (); 459 display.append(infos[i].getName()); 460 display.append("("); 461 MBeanParameterInfo [] parameters = infos[i].getSignature(); 462 if (parameters.length > 0) 463 { 464 for (int j = 0; j < parameters.length; j++) 465 { 466 if (j > 0) 467 { 468 display.append(", "); 469 } 470 display.append(ClassTools.classDisplay(parameters[j].getType())); 471 } 472 } 473 display.append(")"); 474 475 JLabel label = new JLabel (display.toString(), SwingConstants.LEFT); 476 label.setToolTipText(infos[i].getDescription()); 477 this.mbeanConstructors.add(label, gridbagconstraints); 478 } 479 } 480 481 482 488 private void buildInfosPanel(MBeanInfo info, MBeanAccessor object) 489 { 490 JLabel label = null; 491 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 492 gridbagconstraints.insets = new Insets (2, 2, 2, 2); 493 gridbagconstraints.anchor = GridBagConstraints.NORTH; 494 gridbagconstraints.weightx = 0.0D; 495 gridbagconstraints.weighty = 1.0D; 496 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 497 498 this.mbeanInfo = new JPanel (new GridBagLayout ()); 499 this.mbeanInfo.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), object.getCanonicalName())); 500 501 gridbagconstraints.weightx = 0.0D; 502 gridbagconstraints.gridwidth = GridBagConstraints.RELATIVE; 503 label = new JLabel ("ClassName : ", SwingConstants.RIGHT); 504 label.setToolTipText("Class Name of the MBean"); 505 label.setForeground(AwtToolkit.DARK_BLUE); 506 this.mbeanInfo.add(label, gridbagconstraints); 507 508 gridbagconstraints.weightx = 1.0D; 509 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 510 label = new JLabel (info.getClassName(), SwingConstants.LEFT); 511 this.mbeanInfo.add(label, gridbagconstraints); 512 513 gridbagconstraints.weightx = 0.0D; 514 gridbagconstraints.gridwidth = GridBagConstraints.RELATIVE; 515 label = new JLabel ("Description : ", SwingConstants.RIGHT); 516 label.setToolTipText("Description of the MBean"); 517 label.setForeground(AwtToolkit.DARK_BLUE); 518 this.mbeanInfo.add(label, gridbagconstraints); 519 520 gridbagconstraints.weightx = 1.0D; 521 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 522 label = new JLabel (info.getDescription(), SwingConstants.LEFT); 523 this.mbeanInfo.add(label, gridbagconstraints); 524 } 525 526 527 532 private void buildNotificationsPanel(MBeanInfo info) 533 { 534 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 535 gridbagconstraints.insets = new Insets (2, 2, 2, 2); 536 gridbagconstraints.anchor = GridBagConstraints.NORTH; 537 gridbagconstraints.weightx = 0.0D; 538 gridbagconstraints.weighty = 1.0D; 539 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 540 541 MBeanNotificationInfo [] infos = info.getNotifications(); 542 if (infos.length == 0) 543 { 544 return; 545 } 546 this.mbeanNotifications = new JPanel (new GridBagLayout ()); 547 this.mbeanNotifications.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Notifications")); 548 for (int i = 0; i < infos.length; i++) 549 { 550 gridbagconstraints.weightx = 0.0D; 551 gridbagconstraints.gridheight = infos[i].getNotifTypes().length; 552 gridbagconstraints.gridwidth = GridBagConstraints.RELATIVE; 553 JLabel label = new JLabel (infos[i].getName(), SwingConstants.RIGHT); 554 label.setToolTipText(infos[i].getDescription()); 555 label.setForeground(AwtToolkit.DARK_BLUE); 556 this.mbeanNotifications.add(label, gridbagconstraints); 557 558 gridbagconstraints.weightx = 1.0D; 559 gridbagconstraints.gridheight = 1; 560 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 561 for (int j = 0; j < infos[i].getNotifTypes().length; j++) 562 { 563 label = new JLabel (infos[i].getNotifTypes()[j], SwingConstants.LEFT); 564 this.mbeanNotifications.add(label, gridbagconstraints); 565 } 566 } 567 if (infos.length > 0) 568 { 569 JPanel buttonPanel = new JPanel (new GridLayout (1, 2, 5, 5)); 570 JButton button1 = new JButton ("Register"); 571 JButton button2 = new JButton ("Unregister"); 572 buttonPanel.add(button1); 573 buttonPanel.add(button2); 574 this.mbeanNotifications.add(buttonPanel, gridbagconstraints); 575 } 576 } 577 578 579 584 private void buildOperationsPanel(MBeanInfo info) 585 { 586 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 587 gridbagconstraints.insets = new Insets (2, 2, 2, 2); 588 gridbagconstraints.anchor = GridBagConstraints.NORTH; 589 gridbagconstraints.weightx = 1.0D; 590 gridbagconstraints.weighty = 1.0D; 591 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 592 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 593 594 MBeanOperationInfo [] infos = info.getOperations(); 595 if (infos.length == 0) 596 { 597 return; 598 } 599 this.mbeanOperations = new JPanel (new GridBagLayout ()); 600 this.mbeanOperations.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Operations")); 601 for (int i = 0; i < infos.length; i++) 602 { 603 gridbagconstraints.weightx = 0.0D; 604 gridbagconstraints.gridwidth = 1; 605 String returnType = ClassTools.classDisplay(infos[i].getReturnType()); 606 JLabel label = new JLabel (returnType, SwingConstants.RIGHT); 607 label.setForeground(AwtToolkit.DARK_BLUE); 608 this.mbeanOperations.add(label, gridbagconstraints); 609 610 StringBuffer display = new StringBuffer (); 611 display.append(infos[i].getName()); 612 display.append("("); 613 MBeanParameterInfo [] parameters = infos[i].getSignature(); 614 if (parameters.length > 0) 615 { 616 for (int j = 0; j < parameters.length; j++) 617 { 618 if (j > 0) 619 { 620 display.append(", "); 621 } 622 display.append(ClassTools.classDisplay(parameters[j].getType())); 623 } 624 } 625 display.append(")"); 626 627 gridbagconstraints.weightx = 1.0D; 628 gridbagconstraints.gridwidth = GridBagConstraints.RELATIVE; 629 label = new JLabel (display.toString(), SwingConstants.LEFT); 630 label.setToolTipText(infos[i].getDescription()); 631 this.mbeanOperations.add(label, gridbagconstraints); 632 633 gridbagconstraints.weightx = 0.0D; 634 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 635 JButton button = new JButton ("Execute"); 636 this.mbeanOperations.add(button, gridbagconstraints); 637 } 638 } 639 640 641 647 class BeanUpdater implements PropertyChangeListener 648 { 649 650 MBeanAttributeInfo info; 651 652 653 658 public BeanUpdater(MBeanAttributeInfo info) 659 { 660 this.info = info; 661 } 662 663 664 669 public void propertyChange(PropertyChangeEvent event) 670 { 671 Object old = null; 672 try 673 { 674 if (info.isReadable()) 675 { 676 old = object.getAttribute(info.getName()); 677 } 678 Object obj = ((PropertyEditor ) event.getSource()).getValue(); 679 680 object.setAttribute(new Attribute (info.getName(), obj)); 681 updated(info.getName(), old, obj); 682 } 683 696 catch (Exception e) 697 { 698 System.err.println(e); 699 } 700 } 701 } 702 703 704 710 class EditorUpdater implements PropertyChangeListener 711 { 712 713 PropertyEditor editor; 714 715 MBeanAttributeInfo info; 716 717 String name; 718 719 720 726 public EditorUpdater(PropertyEditor editor, MBeanAttributeInfo info) 727 { 728 this.editor = editor; 729 this.info = info; 730 this.name = info.getName(); 731 } 732 733 734 740 public EditorUpdater(PropertyEditor editor, String name) 741 { 742 this.editor = editor; 743 this.name = name; 744 } 745 746 747 752 public void propertyChange(PropertyChangeEvent event) 753 { 754 System.out.println("$$$ Attribute has been changed " + event); 755 System.out.println("$$$ Attribute has been changed " + name + "/" + event.getPropertyName()); 756 757 if (name != null && event.getPropertyName() != null) 758 { 759 if (!event.getPropertyName().equals(name)) 760 { 761 return; 762 } 763 } 764 if (event.getPropertyName() != null) 765 { 766 if (!editor.getValue().equals(event.getNewValue())) 767 { 768 editor.setValue(event.getNewValue()); 769 } 770 } 771 else 772 { 773 try 774 { 775 Object value = object.getAttribute(event.getSource() + ""); 776 if (editor != null && !editor.equals(editor.getValue())) 777 { 778 editor.setValue(value); 779 } 780 } 781 catch (Exception e) 782 { 783 } 784 } 785 } 786 } 787 788 789 795 class MethodInvoker implements ActionListener 796 { 797 798 MBeanOperationInfo info; 799 800 801 806 MethodInvoker(MBeanOperationInfo info) 807 { 808 this.info = info; 809 } 810 811 812 817 public void actionPerformed(ActionEvent e) 818 { 819 Container frame = SwingUtilities.getAncestorOfClass(Frame .class, p); 820 821 MBeanParameterInfo [] parameters = info.getSignature(); 823 if (parameters.length == 0) 824 { 825 try 826 { 827 Object obj1 = object.invoke(info.getName(), new Object []{}, new String []{}); 828 829 if (obj1 != null) 830 { 831 JOptionPane.showMessageDialog(frame, obj1.toString(), "Result", 1); 832 } 833 } 834 catch (Exception ex) 835 { 836 System.err.println(ex); 837 JOptionPane.showMessageDialog(frame, "An exception occured. Check log for details", "Error", JOptionPane.ERROR_MESSAGE); 838 } 839 } 840 else 841 { 842 843 new GenericMBeanMethodDialog( 844 object, 845 object.getObjectName(), 846 info, 847 (Frame ) frame 848 ); 849 850 } 851 } 852 } 853 854 855 861 class UnregisterInvoker implements ActionListener 862 { 863 868 public void actionPerformed(ActionEvent e) 869 { 870 Container frame = SwingUtilities.getAncestorOfClass(Frame .class, p); 871 872 try 873 { 874 object.unregisterMBean(); 875 } 876 catch (Exception ex) 877 { 878 System.err.println(ex); 879 JOptionPane.showMessageDialog(frame, "An exception occured. Check log for details", "Error", JOptionPane.ERROR_MESSAGE); 880 } 881 } 882 } 883 } 884 885 | Popular Tags |