package sun.tools.jconsole.inspector;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.util.*;
import javax.management.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.tree.*;
import sun.tools.jconsole.JConsole;
import sun.tools.jconsole.Messages;
import sun.tools.jconsole.inspector.XNodeInfo.Type;
import static sun.tools.jconsole.Utilities.*;
@SuppressWarnings("serial")
public class XMBeanInfo extends JPanel {
private static final Color lightYellow = new Color(255, 255, 128);
private final int NAME_COLUMN = 0;
private final int VALUE_COLUMN = 1;
private final String[] columnNames = {
Messages.NAME,
Messages.VALUE
};
private JTable infoTable = new JTable();
private JTable descTable = new JTable();
private JPanel infoBorderPanel = new JPanel(new BorderLayout());
private JPanel descBorderPanel = new JPanel(new BorderLayout());
private static class ReadOnlyDefaultTableModel extends DefaultTableModel {
public void setValueAt(Object value, int row, int col) {
}
}
private static class TableRowDivider {
private String tableRowDividerText;
public TableRowDivider(String tableRowDividerText) {
this.tableRowDividerText = tableRowDividerText;
}
public String toString() {
return tableRowDividerText;
}
}
private static MBeanInfoTableCellRenderer renderer =
new MBeanInfoTableCellRenderer();
private static class MBeanInfoTableCellRenderer
extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
Component comp = super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
if (value instanceof TableRowDivider) {
JLabel label = new JLabel(value.toString());
label.setBackground(ensureContrast(lightYellow,
label.getForeground()));
label.setOpaque(true);
return label;
}
return comp;
}
}
private static TableCellEditor editor =
new MBeanInfoTableCellEditor(new JTextField());
private static class MBeanInfoTableCellEditor
extends Utils.ReadOnlyTableCellEditor {
public MBeanInfoTableCellEditor(JTextField tf) {
super(tf);
}
public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected,
int row, int column) {
Component comp = super.getTableCellEditorComponent(
table, value, isSelected, row, column);
if (value instanceof TableRowDivider) {
JLabel label = new JLabel(value.toString());
label.setBackground(ensureContrast(lightYellow,
label.getForeground()));
label.setOpaque(true);
return label;
}
return comp;
}
}
public XMBeanInfo() {
super(new GridLayout(2, 1));
infoTable.setModel(new ReadOnlyDefaultTableModel());
infoTable.setRowSelectionAllowed(false);
infoTable.setColumnSelectionAllowed(false);
infoTable.getTableHeader().setReorderingAllowed(false);
((DefaultTableModel) infoTable.getModel()).setColumnIdentifiers(columnNames);
infoTable.getColumnModel().getColumn(NAME_COLUMN).setPreferredWidth(140);
infoTable.getColumnModel().getColumn(NAME_COLUMN).setMaxWidth(140);
infoTable.getColumnModel().getColumn(NAME_COLUMN).setCellRenderer(renderer);
infoTable.getColumnModel().getColumn(VALUE_COLUMN).setCellRenderer(renderer);
infoTable.getColumnModel().getColumn(NAME_COLUMN).setCellEditor(editor);
infoTable.getColumnModel().getColumn(VALUE_COLUMN).setCellEditor(editor);
infoTable.addKeyListener(new Utils.CopyKeyAdapter());
infoTable.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
JScrollPane infoTableScrollPane = new JScrollPane(infoTable);
infoBorderPanel.setBorder(
BorderFactory.createTitledBorder("MBeanInfoPlaceHolder"));
infoBorderPanel.add(infoTableScrollPane);
descTable.setModel(new ReadOnlyDefaultTableModel());
descTable.setRowSelectionAllowed(false);
descTable.setColumnSelectionAllowed(false);
descTable.getTableHeader().setReorderingAllowed(false);
((DefaultTableModel) descTable.getModel()).setColumnIdentifiers(columnNames);
descTable.getColumnModel().getColumn(NAME_COLUMN).setPreferredWidth(140);
descTable.getColumnModel().getColumn(NAME_COLUMN).setMaxWidth(140);
descTable.getColumnModel().getColumn(NAME_COLUMN).setCellRenderer(renderer);
descTable.getColumnModel().getColumn(VALUE_COLUMN).setCellRenderer(renderer);
descTable.getColumnModel().getColumn(NAME_COLUMN).setCellEditor(editor);
descTable.getColumnModel().getColumn(VALUE_COLUMN).setCellEditor(editor);
descTable.addKeyListener(new Utils.CopyKeyAdapter());
descTable.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
JScrollPane descTableScrollPane = new JScrollPane(descTable);
descBorderPanel.setBorder(
BorderFactory.createTitledBorder(Messages.DESCRIPTOR));
descBorderPanel.add(descTableScrollPane);
add(infoBorderPanel);
add(descBorderPanel);
}
public void emptyInfoTable() {
DefaultTableModel tableModel = (DefaultTableModel) infoTable.getModel();
while (tableModel.getRowCount() > 0) {
tableModel.removeRow(0);
}
}
public void emptyDescTable() {
DefaultTableModel tableModel = (DefaultTableModel) descTable.getModel();
while (tableModel.getRowCount() > 0) {
tableModel.removeRow(0);
}
}
private void addDescriptor(Descriptor desc, String text) {
if (desc != null && desc.getFieldNames().length > 0) {
DefaultTableModel tableModel = (DefaultTableModel) descTable.getModel();
Object rowData[] = new Object[2];
rowData[0] = new TableRowDivider(text);
rowData[1] = new TableRowDivider("");
tableModel.addRow(rowData);
for (String fieldName : desc.getFieldNames()) {
rowData[0] = fieldName;
Object fieldValue = desc.getFieldValue(fieldName);
if (fieldValue instanceof boolean[]) {
rowData[1] = Arrays.toString((boolean[]) fieldValue);
} else if (fieldValue instanceof byte[]) {
rowData[1] = Arrays.toString((byte[]) fieldValue);
} else if (fieldValue instanceof char[]) {
rowData[1] = Arrays.toString((char[]) fieldValue);
} else if (fieldValue instanceof double[]) {
rowData[1] = Arrays.toString((double[]) fieldValue);
} else if (fieldValue instanceof float[]) {
rowData[1] = Arrays.toString((float[]) fieldValue);
} else if (fieldValue instanceof int[]) {
rowData[1] = Arrays.toString((int[]) fieldValue);
} else if (fieldValue instanceof long[]) {
rowData[1] = Arrays.toString((long[]) fieldValue);
} else if (fieldValue instanceof short[]) {
rowData[1] = Arrays.toString((short[]) fieldValue);
} else if (fieldValue instanceof Object[]) {
rowData[1] = Arrays.toString((Object[]) fieldValue);
} else {
rowData[1] = fieldValue;
}
tableModel.addRow(rowData);
}
tableModel.newDataAvailable(new TableModelEvent(tableModel));
}
}
public void addMBeanInfo(XMBean mbean, MBeanInfo mbeanInfo) {
emptyInfoTable();
emptyDescTable();
((TitledBorder) infoBorderPanel.getBorder()).setTitle(
Messages.MBEAN_INFO);
String text = Messages.INFO + ":";
DefaultTableModel tableModel = (DefaultTableModel) infoTable.getModel();
Object rowData[] = new Object[2];
rowData[0] = new TableRowDivider(text);
rowData[1] = new TableRowDivider("");
tableModel.addRow(rowData);
rowData[0] = Messages.OBJECT_NAME;
rowData[1] = mbean.getObjectName();
tableModel.addRow(rowData);
rowData[0] = Messages.CLASS_NAME;
rowData[1] = mbeanInfo.getClassName();
tableModel.addRow(rowData);
rowData[0] = Messages.DESCRIPTION;
rowData[1] = mbeanInfo.getDescription();
tableModel.addRow(rowData);
addDescriptor(mbeanInfo.getDescriptor(), text);
int i = 0;
for (MBeanConstructorInfo mbci : mbeanInfo.getConstructors()) {
addMBeanConstructorInfo(mbci,
Messages.CONSTRUCTOR + "-" + i + ":");
int j = 0;
for (MBeanParameterInfo mbpi : mbci.getSignature()) {
addMBeanParameterInfo(mbpi,
Messages.PARAMETER + "-" + i + "-" + j + ":");
j++;
}
i++;
}
tableModel.newDataAvailable(new TableModelEvent(tableModel));
}
public void addMBeanAttributeInfo(MBeanAttributeInfo mbai) {
emptyInfoTable();
emptyDescTable();
((TitledBorder) infoBorderPanel.getBorder()).setTitle(
Messages.MBEAN_ATTRIBUTE_INFO);
String text = Messages.ATTRIBUTE + ":";
DefaultTableModel tableModel = (DefaultTableModel) infoTable.getModel();
Object rowData[] = new Object[2];
rowData[0] = new TableRowDivider(text);
rowData[1] = new TableRowDivider("");
tableModel.addRow(rowData);
rowData[0] = Messages.NAME;
rowData[1] = mbai.getName();
tableModel.addRow(rowData);
rowData[0] = Messages.DESCRIPTION;
rowData[1] = mbai.getDescription();
tableModel.addRow(rowData);
rowData[0] = Messages.READABLE;
rowData[1] = mbai.isReadable();
tableModel.addRow(rowData);
rowData[0] = Messages.WRITABLE;
rowData[1] = mbai.isWritable();
tableModel.addRow(rowData);
rowData[0] = Messages.IS;
rowData[1] = mbai.isIs();
tableModel.addRow(rowData);
rowData[0] = Messages.TYPE;
rowData[1] = mbai.getType();
tableModel.addRow(rowData);
addDescriptor(mbai.getDescriptor(), text);
tableModel.newDataAvailable(new TableModelEvent(tableModel));
}
public void addMBeanOperationInfo(MBeanOperationInfo mboi) {
emptyInfoTable();
emptyDescTable();
((TitledBorder) infoBorderPanel.getBorder()).setTitle(
Messages.MBEAN_OPERATION_INFO);
String text = Messages.OPERATION + ":";
DefaultTableModel tableModel = (DefaultTableModel) infoTable.getModel();
Object rowData[] = new Object[2];
rowData[0] = new TableRowDivider(text);
rowData[1] = new TableRowDivider("");
tableModel.addRow(rowData);
rowData[0] = Messages.NAME;
rowData[1] = mboi.getName();
tableModel.addRow(rowData);
rowData[0] = Messages.DESCRIPTION;
rowData[1] = mboi.getDescription();
tableModel.addRow(rowData);
rowData[0] = Messages.IMPACT;
switch (mboi.getImpact()) {
case MBeanOperationInfo.INFO:
rowData[1] = Messages.INFO_CAPITALIZED;
break;
case MBeanOperationInfo.ACTION:
rowData[1] = Messages.ACTION_CAPITALIZED;
break;
case MBeanOperationInfo.ACTION_INFO:
rowData[1] = Messages.ACTION_INFO_CAPITALIZED;
break;
case MBeanOperationInfo.UNKNOWN:
rowData[1] = Messages.UNKNOWN_CAPITALIZED;
break;
}
tableModel.addRow(rowData);
rowData[0] = Messages.RETURN_TYPE;
rowData[1] = mboi.getReturnType();
tableModel.addRow(rowData);
addDescriptor(mboi.getDescriptor(), text);
int i = 0;
for (MBeanParameterInfo mbpi : mboi.getSignature()) {
addMBeanParameterInfo(mbpi,
Messages.PARAMETER + "-" + i++ + ":");
}
tableModel.newDataAvailable(new TableModelEvent(tableModel));
}
public void addMBeanNotificationInfo(MBeanNotificationInfo mbni) {
emptyInfoTable();
emptyDescTable();
((TitledBorder) infoBorderPanel.getBorder()).setTitle(
Messages.MBEAN_NOTIFICATION_INFO);
String text = Messages.NOTIFICATION + ":";
DefaultTableModel tableModel = (DefaultTableModel) infoTable.getModel();
Object rowData[] = new Object[2];
rowData[0] = new TableRowDivider(text);
rowData[1] = new TableRowDivider("");
tableModel.addRow(rowData);
rowData[0] = Messages.NAME;
rowData[1] = mbni.getName();
tableModel.addRow(rowData);
rowData[0] = Messages.DESCRIPTION;
rowData[1] = mbni.getDescription();
tableModel.addRow(rowData);
rowData[0] = Messages.NOTIF_TYPES;
rowData[1] = Arrays.toString(mbni.getNotifTypes());
tableModel.addRow(rowData);
addDescriptor(mbni.getDescriptor(), text);
tableModel.newDataAvailable(new TableModelEvent(tableModel));
}
private void addMBeanConstructorInfo(MBeanConstructorInfo mbci, String text) {
DefaultTableModel tableModel = (DefaultTableModel) infoTable.getModel();
Object rowData[] = new Object[2];
rowData[0] = new TableRowDivider(text);
rowData[1] = new TableRowDivider("");
tableModel.addRow(rowData);
rowData[0] = Messages.NAME;
rowData[1] = mbci.getName();
tableModel.addRow(rowData);
rowData[0] = Messages.DESCRIPTION;
rowData[1] = mbci.getDescription();
tableModel.addRow(rowData);
addDescriptor(mbci.getDescriptor(), text);
tableModel.newDataAvailable(new TableModelEvent(tableModel));
}
private void addMBeanParameterInfo(MBeanParameterInfo mbpi, String text) {
DefaultTableModel tableModel = (DefaultTableModel) infoTable.getModel();
Object rowData[] = new Object[2];
rowData[0] = new TableRowDivider(text);
rowData[1] = new TableRowDivider("");
tableModel.addRow(rowData);
rowData[0] = Messages.NAME;
rowData[1] = mbpi.getName();
tableModel.addRow(rowData);
rowData[0] = Messages.DESCRIPTION;
rowData[1] = mbpi.getDescription();
tableModel.addRow(rowData);
rowData[0] = Messages.TYPE;
rowData[1] = mbpi.getType();
tableModel.addRow(rowData);
addDescriptor(mbpi.getDescriptor(), text);
tableModel.newDataAvailable(new TableModelEvent(tableModel));
}
public static void loadInfo(DefaultMutableTreeNode root) {
XMBean mbean = (XMBean) ((XNodeInfo) root.getUserObject()).getData();
final MBeanInfo mbeanInfo;
try {
mbeanInfo = mbean.getMBeanInfo();
} catch (Exception e) {
if (JConsole.isDebug()) {
e.printStackTrace();
}
return;
}
MBeanAttributeInfo[] ai = mbeanInfo.getAttributes();
MBeanOperationInfo[] oi = mbeanInfo.getOperations();
MBeanNotificationInfo[] ni = mbeanInfo.getNotifications();
if (ai != null && ai.length > 0) {
DefaultMutableTreeNode attributes = new DefaultMutableTreeNode();
XNodeInfo attributesUO = new XNodeInfo(Type.ATTRIBUTES, mbean,
Messages.ATTRIBUTES, null);
attributes.setUserObject(attributesUO);
root.add(attributes);
for (MBeanAttributeInfo mbai : ai) {
DefaultMutableTreeNode attribute = new DefaultMutableTreeNode();
XNodeInfo attributeUO = new XNodeInfo(Type.ATTRIBUTE,
new Object[] {mbean, mbai}, mbai.getName(), null);
attribute.setUserObject(attributeUO);
attributes.add(attribute);
}
}
if (oi != null && oi.length > 0) {
DefaultMutableTreeNode operations = new DefaultMutableTreeNode();
XNodeInfo operationsUO = new XNodeInfo(Type.OPERATIONS, mbean,
Messages.OPERATIONS, null);
operations.setUserObject(operationsUO);
root.add(operations);
for (MBeanOperationInfo mboi : oi) {
StringBuilder sb = new StringBuilder();
for (MBeanParameterInfo mbpi : mboi.getSignature()) {
sb.append(mbpi.getType() + ",");
}
String signature = sb.toString();
if (signature.length() > 0) {
signature = signature.substring(0, signature.length() - 1);
}
String toolTipText = mboi.getName() + "(" + signature + ")";
DefaultMutableTreeNode operation = new DefaultMutableTreeNode();
XNodeInfo operationUO = new XNodeInfo(Type.OPERATION,
new Object[] {mbean, mboi}, mboi.getName(), toolTipText);
operation.setUserObject(operationUO);
operations.add(operation);
}
}
if (mbean.isBroadcaster()) {
DefaultMutableTreeNode notifications = new DefaultMutableTreeNode();
XNodeInfo notificationsUO = new XNodeInfo(Type.NOTIFICATIONS, mbean,
Messages.NOTIFICATIONS, null);
notifications.setUserObject(notificationsUO);
root.add(notifications);
if (ni != null && ni.length > 0) {
for (MBeanNotificationInfo mbni : ni) {
DefaultMutableTreeNode notification =
new DefaultMutableTreeNode();
XNodeInfo notificationUO = new XNodeInfo(Type.NOTIFICATION,
mbni, mbni.getName(), null);
notification.setUserObject(notificationUO);
notifications.add(notification);
}
}
}
}
}