1 7 package org.ejtools.management.browser.ui; 8 9 import java.awt.Color ; 10 import java.awt.Component ; 11 import java.awt.GridBagConstraints ; 12 import java.awt.GridBagLayout ; 13 import java.awt.GridLayout ; 14 import java.awt.Insets ; 15 import java.awt.event.ActionEvent ; 16 import java.awt.event.ActionListener ; 17 import java.beans.PropertyChangeEvent ; 18 import java.beans.PropertyChangeListener ; 19 import java.beans.PropertyEditor ; 20 import java.util.ArrayList ; 21 import java.util.Arrays ; 22 import java.util.Comparator ; 23 import java.util.Hashtable ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import javax.management.Attribute ; 28 import javax.management.MBeanAttributeInfo ; 29 import javax.management.MBeanInfo ; 30 import javax.management.MBeanOperationInfo ; 31 import javax.management.MBeanParameterInfo ; 32 import javax.swing.BorderFactory ; 33 import javax.swing.JButton ; 34 import javax.swing.JComponent ; 35 import javax.swing.JLabel ; 36 import javax.swing.JPanel ; 37 import javax.swing.JTextArea ; 38 import javax.swing.SwingConstants ; 39 40 import org.apache.log4j.Logger; 41 import org.ejtools.adwt.AwtToolkit; 42 import org.ejtools.adwt.editor.ArrayEditor; 43 import org.ejtools.adwt.jmx.MBeanMethodDialog; 44 import org.ejtools.beans.CustomPropertyEditorManager; 45 import org.ejtools.jmx.MBeanSorter; 46 import org.ejtools.management.browser.model.ManagedObject; 47 import org.ejtools.util.ClassTools; 48 49 import com.dreambean.awt.GenericPropertyCustomizer; 50 51 57 public class ManagedObjectDetailViewer extends ManagedObjectViewer 58 { 59 60 private Hashtable invocationDialogs = null; 61 62 private JPanel mbeanAttributes = null; 63 64 private JPanel mbeanOperations = null; 65 66 private JPanel mbeanSummary = null; 67 68 private JComponent previous = null; 69 70 private ArrayList updaters; 71 72 private static Logger logger = Logger.getLogger(ManagedObjectDetailViewer.class); 73 74 75 76 public ManagedObjectDetailViewer() 77 { 78 super(); 79 } 80 81 82 87 public ManagedObjectDetailViewer(Object bean) 88 { 89 super(bean); 90 } 91 92 93 97 public MBeanAttributeInfo [] filter(MBeanAttributeInfo [] infos) 98 { 99 ArrayList filteredInfos = new ArrayList (); 100 ArrayList excludes = new ArrayList (); 101 excludes.add(MO_ATTRIBUTE_EVENTPROVIDER); 102 excludes.add(MO_ATTRIBUTE_EVENTTYPES); 103 excludes.add(MO_ATTRIBUTE_STATEMANAGEABLE); 104 excludes.add(MO_ATTRIBUTE_STATISTICSPROVIDER); 105 excludes.add(MO_ATTRIBUTE_STATS); 106 107 for (int i = 0; i < infos.length; i++) 108 { 109 String name = infos[i].getName(); 110 if (!excludes.contains(name.toLowerCase())) 111 { 112 filteredInfos.add(infos[i]); 113 } 114 } 115 116 MBeanAttributeInfo [] result = (MBeanAttributeInfo []) filteredInfos.toArray(new MBeanAttributeInfo [0]); 117 Arrays.sort(result, 118 new Comparator () 119 { 120 public int compare(Object o1, Object o2) 121 { 122 MBeanAttributeInfo info1 = (MBeanAttributeInfo ) o1; 123 MBeanAttributeInfo info2 = (MBeanAttributeInfo ) o2; 124 return info1.getName().compareTo(info2.getName()); 125 } 126 127 128 public boolean equals(Object obj) 129 { 130 return false; 132 } 133 }); 134 135 return result; 136 } 137 138 139 144 public void setObject(Object bean) 145 { 146 try 147 { 148 this.panel.removeAll(); 149 150 if (bean == null) 151 { 152 this.validate(); 153 this.repaint(); 154 return; 155 } 156 157 this.object = (ManagedObject) bean; 158 MBeanInfo beaninfo = this.object.getMBeanInfo(); 159 160 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 161 gridbagconstraints.insets = new Insets (3, 3, 3, 3); 162 gridbagconstraints.anchor = GridBagConstraints.NORTH; 163 gridbagconstraints.weightx = 1.0D; 164 gridbagconstraints.weighty = 0.0D; 165 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 166 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 167 168 this.buildSummaryPanel(beaninfo); 169 this.panel.add(this.mbeanSummary, gridbagconstraints); 170 171 this.buildAttributesPanel(beaninfo); 172 this.panel.add(this.mbeanAttributes, gridbagconstraints); 173 174 gridbagconstraints.weighty = 1.0D; 175 this.buildOperationsPanel(beaninfo); 176 this.panel.add(this.mbeanOperations, gridbagconstraints); 177 178 this.validate(); 179 this.repaint(); 180 } 181 catch (Exception e) 182 { 183 logger.error("Error while setting object" + e); 184 } 185 } 186 187 188 195 protected void add(String text, String toolTip, String value) 196 { 197 JLabel label; 198 199 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 200 gridbagconstraints.insets = new Insets (0, 0, 0, 0); 201 gridbagconstraints.anchor = GridBagConstraints.NORTH; 202 gridbagconstraints.weightx = 0.0D; 203 gridbagconstraints.weighty = 1.0D; 204 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 205 206 gridbagconstraints.weightx = 0.0D; 207 gridbagconstraints.gridwidth = GridBagConstraints.RELATIVE; 208 label = new JLabel (text + " : ", SwingConstants.RIGHT); 209 label.setToolTipText(toolTip); 210 label.setForeground(AwtToolkit.DARK_BLUE); 211 this.mbeanSummary.add(label, gridbagconstraints); 212 213 gridbagconstraints.weightx = 1.0D; 214 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 215 label = new JLabel (value, SwingConstants.LEFT); 216 label.setForeground(Color.black); 217 this.mbeanSummary.add(label, gridbagconstraints); 218 219 } 220 221 222 228 protected void addProperty(PropertyEditor propertyeditor, MBeanAttributeInfo attributeInfo) 229 { 230 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 231 gridbagconstraints.insets = new Insets (2, 2, 2, 2); 232 233 try 234 { 235 if (attributeInfo.isReadable()) 236 { 237 propertyeditor.setValue(object.getAttribute(attributeInfo.getName())); 238 } 239 } 240 catch (Throwable throwable) 241 { 242 logger.warn("Can't read the property from MBean", throwable); 243 } 244 245 JLabel label = new JLabel (attributeInfo.getName() + " : ", SwingConstants.RIGHT); 247 label.setToolTipText(attributeInfo.getDescription()); 248 label.setForeground(AwtToolkit.DARK_BLUE); 249 250 Object obj1; 251 if (propertyeditor.supportsCustomEditor()) 252 { 253 obj1 = propertyeditor.getCustomEditor(); 254 if (obj1 instanceof JComponent ) 255 { 256 if (previous != null) 257 { 258 previous.setNextFocusableComponent(((Component ) (obj1))); 259 } 260 previous = (JComponent ) obj1; 261 } 262 } 263 else 264 { 265 String [] tags = propertyeditor.getTags(); 266 if (tags != null) 267 { 268 obj1 = new GenericPropertyCustomizer(propertyeditor, tags); 269 if (previous != null) 270 { 271 previous.setNextFocusableComponent((Component ) (obj1)); 272 } 273 previous = (JComponent ) obj1; 274 } 275 else 276 { 277 if (attributeInfo.isWritable()) 278 { 279 obj1 = new GenericPropertyCustomizer(propertyeditor); 280 if (previous != null) 281 { 282 previous.setNextFocusableComponent((Component ) (obj1)); 283 } 284 previous = (JComponent ) obj1; 285 } 286 else 287 { 288 String content = "" + propertyeditor.getAsText(); 289 if (content.indexOf('\n') >= 0) 293 { 294 final JTextArea area = new JTextArea (propertyeditor.getAsText()); 295 area.setEditable(false); 296 final PropertyEditor pcEditor = propertyeditor; 297 obj1 = area; 298 pcEditor.addPropertyChangeListener( 299 new PropertyChangeListener () 300 { 301 public void propertyChange(PropertyChangeEvent evt) 302 { 303 area.setText(pcEditor.getAsText()); 304 } 305 }); 306 } 307 else 308 { 309 final JLabel lbl = new JLabel (propertyeditor.getAsText()); 310 final PropertyEditor pcEditor = propertyeditor; 311 obj1 = lbl; 312 pcEditor.addPropertyChangeListener( 313 new PropertyChangeListener () 314 { 315 public void propertyChange(PropertyChangeEvent evt) 316 { 317 lbl.setText(pcEditor.getAsText()); 318 } 319 }); 320 } 321 } 322 } 323 } 324 325 gridbagconstraints.anchor = GridBagConstraints.NORTH; 326 gridbagconstraints.weighty = 1.0D; 327 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 328 329 gridbagconstraints.weightx = 0.0D; 330 gridbagconstraints.gridwidth = 1; 331 this.mbeanAttributes.add(label, gridbagconstraints); 332 333 gridbagconstraints.weightx = 1.0D; 334 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 335 this.mbeanAttributes.add(((Component ) (obj1)), gridbagconstraints); 336 337 Updater updater = new Updater(propertyeditor, attributeInfo); 339 this.getUpdaters().add(updater); 340 } 341 342 343 348 protected void addUnsupportedProperty(MBeanAttributeInfo attributeInfo) 349 { 350 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 351 gridbagconstraints.insets = new Insets (2, 2, 2, 2); 352 353 JLabel jlabel = new JLabel (attributeInfo.getName() + " :", SwingConstants.RIGHT); 354 jlabel.setToolTipText(attributeInfo.getDescription()); 355 jlabel.setForeground(Color.black); 356 357 gridbagconstraints.anchor = GridBagConstraints.NORTH; 358 gridbagconstraints.weightx = 0.0D; 359 gridbagconstraints.weighty = 1.0D; 360 gridbagconstraints.gridwidth = 1; 361 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 362 363 this.mbeanAttributes.add(jlabel, gridbagconstraints); 364 365 JLabel lbl = new JLabel (resources.getString("customizer.tab.detail.text.unloadable.class") + " " + ClassTools.classDisplay(attributeInfo.getType())); 366 lbl.setForeground(AwtToolkit.DARK_RED); 367 368 gridbagconstraints.weightx = 1.0D; 369 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 370 371 this.mbeanAttributes.add(lbl, gridbagconstraints); 372 } 373 374 375 380 protected void buildAttributesPanel(MBeanInfo info) 381 { 382 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 383 gridbagconstraints.insets = new Insets (0, 0, 0, 0); 384 gridbagconstraints.anchor = GridBagConstraints.NORTH; 385 gridbagconstraints.weightx = 0.0D; 386 gridbagconstraints.weighty = 1.0D; 387 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 388 389 this.mbeanAttributes = new JPanel (new GridBagLayout ()); 390 391 MBeanAttributeInfo [] infosArray = this.filter(info.getAttributes()); 392 if (infosArray.length == 0) 393 { 394 return; 395 } 396 List infos = Arrays.asList(infosArray); 397 MBeanSorter.sortByName(infos); 398 399 this.mbeanAttributes.setBorder(BorderFactory.createTitledBorder(resources.getString("customizer.tab.detail.text.attributes"))); 400 for (int i = 0; i < infos.size(); i++) 401 { 402 MBeanAttributeInfo attributeInfo = (MBeanAttributeInfo ) infos.get(i); 403 PropertyEditor propertyeditor = null; 404 Class c = ClassTools.getClass(attributeInfo.getType()); 405 406 if (c == null) 407 { 408 this.addUnsupportedProperty(attributeInfo); 409 } 410 else 411 { 412 if (c.isArray()) 413 { 414 Class componentType = c.getComponentType(); 415 propertyeditor = new ArrayEditor(componentType); 416 this.addProperty(propertyeditor, attributeInfo); 417 } 418 else 419 { 420 propertyeditor = CustomPropertyEditorManager.findEditor(c); 421 if (propertyeditor == null) 422 { 423 propertyeditor = CustomPropertyEditorManager.findEditor(String .class); 424 } 425 this.addProperty(propertyeditor, attributeInfo); 426 } 427 gridbagconstraints.weighty = 1.0D; 428 } 429 } 430 431 gridbagconstraints.weightx = 1.0D; 432 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 433 434 JPanel buttonPanel = new JPanel (new GridLayout (1, 2, 1, 1)); 435 JButton button1 = new JButton (resources.getString("customizer.tab.detail.button.refresh")); 436 button1.addActionListener( 437 new ActionListener () 438 { 439 public void actionPerformed(ActionEvent e) 440 { 441 try 442 { 443 Iterator it = getUpdaters().iterator(); 444 while (it.hasNext()) 445 { 446 Updater updater = (Updater) it.next(); 447 updater.updateFromBean(); 448 } 449 } 450 catch (Exception ex) 451 { 452 logger.error("Error while refreshing" + ex); 453 } 454 } 455 } 456 ); 457 JButton button2 = new JButton (resources.getString("customizer.tab.detail.button.update")); 458 button2.addActionListener( 459 new ActionListener () 460 { 461 public void actionPerformed(ActionEvent e) 462 { 463 try 464 { 465 Iterator it = updaters.iterator(); 466 while (it.hasNext()) 467 { 468 Updater updater = (Updater) it.next(); 469 updater.updateFromEditor(); 470 updater.updateFromBean(); 471 } 472 } 473 catch (Exception ex) 474 { 475 logger.error("Error while updating" + ex); 476 } 477 } 478 } 479 ); 480 buttonPanel.add(button1); 481 buttonPanel.add(button2); 482 this.mbeanAttributes.add(buttonPanel, gridbagconstraints); 483 } 484 485 486 491 protected void buildOperationsPanel(MBeanInfo info) 492 { 493 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 494 gridbagconstraints.insets = new Insets (0, 0, 0, 0); 495 gridbagconstraints.anchor = GridBagConstraints.NORTH; 496 gridbagconstraints.weightx = 1.0D; 497 gridbagconstraints.weighty = 1.0D; 498 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 499 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 500 501 this.mbeanOperations = new JPanel (new GridBagLayout ()); 502 503 MBeanOperationInfo [] infosArray = info.getOperations(); 504 if (infosArray.length == 0) 505 { 506 return; 507 } 508 List infos = Arrays.asList(infosArray); 509 MBeanSorter.sortByName(infos); 510 511 this.mbeanOperations.setBorder(BorderFactory.createTitledBorder(resources.getString("customizer.tab.detail.text.operations"))); 512 for (int i = 0; i < infos.size(); i++) 513 { 514 MBeanOperationInfo operationInfo = (MBeanOperationInfo ) infos.get(i); 515 516 gridbagconstraints.weightx = 0.0D; 517 gridbagconstraints.gridwidth = 1; 518 String returnType = ClassTools.classDisplay(operationInfo.getReturnType() + " "); 519 JLabel label = new JLabel (returnType, SwingConstants.RIGHT); 520 label.setForeground(AwtToolkit.DARK_BLUE); 521 this.mbeanOperations.add(label, gridbagconstraints); 522 523 StringBuffer display = new StringBuffer (); 524 display.append(operationInfo.getName()); 525 display.append("("); 526 MBeanParameterInfo [] parameters = operationInfo.getSignature(); 527 if (parameters.length > 0) 528 { 529 for (int j = 0; j < parameters.length; j++) 530 { 531 if (j > 0) 532 { 533 display.append(", "); 534 } 535 display.append(ClassTools.classDisplay(parameters[j].getType())); 536 } 537 } 538 display.append(")"); 539 540 gridbagconstraints.weightx = 1.0D; 541 gridbagconstraints.gridwidth = GridBagConstraints.RELATIVE; 542 label = new JLabel (display.toString(), SwingConstants.LEFT); 543 label.setToolTipText(operationInfo.getDescription()); 544 label.setForeground(Color.black); 545 this.mbeanOperations.add(label, gridbagconstraints); 546 547 gridbagconstraints.weightx = 0.0D; 548 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 549 JButton button = new JButton (resources.getString("customizer.tab.detail.button.invoke")); 550 final MBeanMethodDialog dialog = new MBeanMethodDialog(this.object, operationInfo, null); 551 552 if (this.invocationDialogs == null) 553 { 554 this.invocationDialogs = new Hashtable (); 555 } 556 this.invocationDialogs.put(operationInfo.getName(), dialog); 557 558 button.addActionListener( 559 new ActionListener () 560 { 561 public void actionPerformed(ActionEvent e) 562 { 563 dialog.show(); 564 } 565 } 566 ); 567 this.mbeanOperations.add(button, gridbagconstraints); 568 } 569 } 570 571 572 577 protected void buildSummaryPanel(MBeanInfo info) 578 { 579 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 580 gridbagconstraints.insets = new Insets (0, 0, 0, 0); 581 gridbagconstraints.anchor = GridBagConstraints.NORTH; 582 gridbagconstraints.weightx = 0.0D; 583 gridbagconstraints.weighty = 1.0D; 584 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 585 586 this.mbeanSummary = new JPanel (new GridBagLayout ()); 587 this.mbeanSummary.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), this.object.getCanonicalName())); 588 589 this.add( 593 resources.getString("customizer.tab.detail.text.mo.name"), 594 resources.getString("customizer.tab.detail.tooltip.mo.name"), 595 object.getName()); 596 this.add( 597 resources.getString("customizer.tab.detail.text.mo.type"), 598 resources.getString("customizer.tab.detail.tooltip.mo.type"), 599 object.getJ2EEType()); 600 601 this.add( 605 resources.getString("customizer.tab.detail.text.class.name"), 606 resources.getString("customizer.tab.detail.tooltip.class.name"), 607 info.getClassName()); 608 this.add( 609 resources.getString("customizer.tab.detail.text.class.description"), 610 resources.getString("customizer.tab.detail.tooltip.class.description"), 611 info.getDescription()); 612 613 this.add( 617 resources.getString("customizer.tab.detail.text.mo.state.manageable"), 618 resources.getString("customizer.tab.detail.tooltip.mo.state.manageable"), 619 "" + this.object.isStateManageable()); 620 this.add( 621 resources.getString("customizer.tab.detail.text.mo.event.provider"), 622 resources.getString("customizer.tab.detail.tooltip.mo.event.provider"), 623 "" + this.object.isEventProvider()); 624 this.add( 625 resources.getString("customizer.tab.detail.text.mo.statistics.provider"), 626 resources.getString("customizer.tab.detail.tooltip.mo.statistics.provider"), 627 "" + this.object.isStatisticsProvider()); 628 } 629 630 631 636 protected ArrayList getUpdaters() 637 { 638 if (this.updaters == null) 639 { 640 this.updaters = new ArrayList (); 641 } 642 return this.updaters; 643 } 644 645 646 653 protected void updated(String s, Object obj, Object obj1) 654 { 655 this.firePropertyChange(s, obj, obj1); 656 } 657 658 659 666 protected class Updater 667 { 668 669 protected PropertyEditor editor; 670 671 protected MBeanAttributeInfo info; 672 673 674 680 public Updater(PropertyEditor editor, MBeanAttributeInfo info) 681 { 682 this.editor = editor; 683 this.info = info; 684 } 685 686 687 688 public void updateFromBean() 689 { 690 if (this.info.isReadable()) 691 { 692 try 693 { 694 this.editor.setValue(ManagedObjectDetailViewer.this.object.getAttribute(this.info.getName())); 695 } 696 catch (Exception e) 697 { 698 logger.error("Error while updating from remote", e); 699 } 700 } 701 } 702 703 704 705 public void updateFromEditor() 706 { 707 if (this.info.isWritable()) 708 { 709 try 710 { 711 Object newValue = this.editor.getValue(); 712 if ((newValue != null) && (this.info.isReadable())) 713 { 714 Object oldValue = ManagedObjectDetailViewer.this.object.getAttribute(this.info.getName()); 715 if (!newValue.equals(oldValue)) 716 { 717 Attribute attr = new Attribute (this.info.getName(), newValue); 718 ManagedObjectDetailViewer.this.object.setAttribute(attr); 719 } 720 } 721 } 722 catch (Exception e) 723 { 724 logger.error("Error while updating from editor", e); 725 } 726 } 727 } 728 } 729 } 730 731 | Popular Tags |