package jp.gr.java_conf.skrb.game.trivia.mclient; import java.awt.BorderLayout; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextArea; import javax.swing.border.EmptyBorder; import javax.swing.table.AbstractTableModel; import javax.management.Attribute; import javax.management.AttributeList; import javax.management.MBeanAttributeInfo; import javax.management.MBeanInfo; import javax.management.MBeanOperationInfo; import javax.management.MBeanParameterInfo; import javax.management.Notification; import javax.management.NotificationListener; import javax.management.ObjectName; public class MBeanClientView implements NotificationListener { private MBeanClient parent; private ObjectName name; private MBeanInfo info; private JFrame frame; private AttributeTableModel attributeTableModel; private OperationTableModel operationTableModel; private JTextArea notificationArea; private String[] attributeNames; private ExecutorService worker; private Runnable gettingAttributesTask; private Runnable invokeTask; private String operationName; private Attribute settingAttribute; public MBeanClientView(MBeanClient parent, ObjectName name, MBeanInfo info) { this.parent = parent; this.name = name; this.info = info; gettingAttributesTask = new Runnable() { public void run() { AttributeList attributeList = MBeanClientView.this.parent.getAttributes(MBeanClientView.this.name, attributeNames); attributeTableModel.setValues(attributeList); } }; invokeTask = new Runnable() { public void run() { MBeanClientView.this.parent.invoke(MBeanClientView.this.name, getOperationName(), null, null); } }; initGUI(); worker = Executors.newSingleThreadExecutor(); Thread timer = new Thread(new Runnable() { public void run() { try { while(true) { getAttributes(); Thread.sleep(10000L); } } catch (InterruptedException ex) { return; } } }); timer.start(); } private void initGUI() { frame = new JFrame(name.toString()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(600, 400); frame.getContentPane().add(new JLabel(name.getCanonicalName()), BorderLayout.NORTH); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(3, 0)); initAttributeTable(panel); initOperationTable(panel); frame.getContentPane().add(panel, BorderLayout.CENTER); initNotificationArea(panel); frame.setVisible(true); } private void initAttributeTable(Container container) { JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); JPanel northPanel = new JPanel(); northPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.X_AXIS)); northPanel.add(new JLabel("Attributes:")); northPanel.add(Box.createHorizontalGlue()); JButton button = new JButton("Reload"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { getAttributes(); } }); northPanel.add(button); panel.add(northPanel, BorderLayout.NORTH); MBeanAttributeInfo[] attributes = info.getAttributes(); attributeNames = new String[attributes.length]; for (int i = 0; i < attributes.length; i++) { attributeNames[i] = attributes[i].getName(); } AttributeList attributeList = parent.getAttributes(name, attributeNames); attributeTableModel = new AttributeTableModel(attributes, attributeList); JTable attributeTable = new AttributeTable(attributeTableModel); panel.add(new JScrollPane(attributeTable), BorderLayout.CENTER); container.add(panel); } private void initOperationTable(Container container) { JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.add(new JLabel("Operations:"), BorderLayout.NORTH); MBeanOperationInfo[] operations = info.getOperations(); operationTableModel = new OperationTableModel(operations); JTable operationTable = new OperationTable(operationTableModel); panel.add(new JScrollPane(operationTable), BorderLayout.CENTER); container.add(panel); } private void initNotificationArea(Container container) { JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.add(new JLabel("Notification:"), BorderLayout.NORTH); notificationArea = new JTextArea(); notificationArea.setEditable(false); panel.add(new JScrollPane(notificationArea), BorderLayout.CENTER); container.add(panel); } private void getAttributes() { // Executors.execute(worker, gettingAttributesTask); worker.submit(gettingAttributesTask); } private void requestToInvoke(String operationName) { setOperationName(operationName); // Executors.execute(worker, invokeTask); worker.submit(invokeTask); } private synchronized void setOperationName(String operationName) { this.operationName = operationName; } private synchronized String getOperationName() { return operationName; } private synchronized void setSettingAttribute(String name, Object value) { this.settingAttribute = new Attribute(name, value); } private synchronized Attribute getSettingAttribute() { return settingAttribute; } public void handleNotification(Notification notification, Object handback) { notificationArea.append(notification + "\n"); } class AttributeTableModel extends AbstractTableModel { private List attributes; private String[] columnNames = {"Name", "Type", "Access", "Value"}; class AttributeEntry { private String name; private String type; private String access; private Object value; AttributeEntry(MBeanAttributeInfo attributeInfo) { name = attributeInfo.getName(); type = attributeInfo.getType(); if (attributeInfo.isReadable()) { if (attributeInfo.isWritable()) { access = "RW"; } else { access = "RO"; } } else { access = ""; } } String getName() { return name; } String getType() { return type; } String getAccess() { return access; } synchronized Object getValue() { if (getType().startsWith("[")) { StringBuilder builder = new StringBuilder("["); Object[] values = (Object[])value; int i = 0; for(; i < values.length -1; i++) { builder.append(values[i]); builder.append(", "); } builder.append(values[i]); builder.append("]"); return builder.toString(); } else { return value; } } synchronized void setValue(Object value) { this.value = value; } } AttributeTableModel(MBeanAttributeInfo[] attributesInfo, AttributeList list) { super(); attributes = new ArrayList(); for (MBeanAttributeInfo attribute: attributesInfo) { attributes.add(new AttributeEntry(attribute)); } setValues(list); } void setValues(AttributeList list) { for (Object obj: list) { Attribute attribute = (Attribute)obj; String name = attribute.getName(); for (AttributeEntry entry: attributes) { if (entry.getName().equals(name)) { entry.setValue(attribute.getValue()); break; } } } fireTableDataChanged(); } public int getColumnCount() { return columnNames.length; } public int getRowCount() { return attributes.size(); } public String getColumnName(int index) { return columnNames[index]; } public Object getValueAt(int row, int column) { AttributeEntry attribute = attributes.get(row); switch (column) { case 0: return attribute.getName(); case 1: return attribute.getType(); case 2: return attribute.getAccess(); case 3: return attribute.getValue(); default: return ""; } } } class OperationTableModel extends AbstractTableModel { private List operations; private String[] columnNames = {"Name", "Return Type", "Signature", "Execute"}; class OperationEntry { private String name; private String returnType; private String signature; private Object execute; OperationEntry(String name, String returnType, MBeanParameterInfo[] signature) { this.name = name; this.returnType = returnType; if (signature.length > 0) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < signature.length - 1;i++) { builder.append(signature[i].getType()); builder.append(", "); } builder.append(signature[signature.length - 1].getType()); this.signature = builder.toString(); execute = ""; } else if (signature.length <= 0) { this.signature = "void"; JButton button = new JButton("Execute"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { requestToInvoke(OperationEntry.this.name); } }); execute = button; } } String getName() { return name; } String getReturnType() { return returnType; } String getSignature() { return signature; } Object getExecute() { return execute; } } OperationTableModel(MBeanOperationInfo[] operationInfo) { operations = new ArrayList(); for (MBeanOperationInfo operation: operationInfo) { OperationEntry entry = new OperationEntry(operation.getName(), operation.getReturnType(), operation.getSignature()); operations.add(entry); } } public int getColumnCount() { return columnNames.length; } public int getRowCount() { return operations.size(); } public String getColumnName(int index) { return columnNames[index]; } public boolean isCellEditable(int row, int column) { return column == 3 && operations.get(row).getExecute() instanceof JButton; } public Object getValueAt(int row, int column) { OperationEntry operation = operations.get(row); switch (column) { case 0: return operation.getName(); case 1: return operation.getReturnType(); case 2: return operation.getSignature(); case 3: return operation.getExecute(); default: return ""; } } } }