J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.tools.jconsole.inspector; |
| 27 | |
| 28 | import javax.swing.*; |
| 29 | import javax.swing.event.*; |
| 30 | import javax.swing.table.*; |
| 31 | import javax.swing.tree.*; |
| 32 | import java.awt.BorderLayout; |
| 33 | import java.awt.GridLayout; |
| 34 | import java.awt.FlowLayout; |
| 35 | import java.awt.Component; |
| 36 | import java.awt.EventQueue; |
| 37 | import java.awt.event.*; |
| 38 | import java.awt.Insets; |
| 39 | import java.awt.Dimension; |
| 40 | import java.util.*; |
| 41 | import java.io.*; |
| 42 | |
| 43 | import javax.management.*; |
| 44 | |
| 45 | import sun.tools.jconsole.Resources; |
| 46 | import sun.tools.jconsole.MBeansTab; |
| 47 | import sun.tools.jconsole.JConsole; |
| 48 | |
| 49 | public abstract class XOperations extends JPanel implements ActionListener { |
| 50 | |
| 51 | public final static String OPERATION_INVOCATION_EVENT = |
| 52 | "jam.xoperations.invoke.result"; |
| 53 | private java.util.List<NotificationListener> notificationListenersList; |
| 54 | |
| 55 | private Hashtable<JButton, OperationEntry> operationEntryTable; |
| 56 | |
| 57 | private XMBean mbean; |
| 58 | private MBeanInfo mbeanInfo; |
| 59 | private MBeansTab mbeansTab; |
| 60 | public XOperations(MBeansTab mbeansTab) { |
| 61 | super(new GridLayout(1,1)); |
| 62 | this.mbeansTab = mbeansTab; |
| 63 | operationEntryTable = new Hashtable<JButton, OperationEntry>(); |
| 64 | ArrayList<NotificationListener> l = |
| 65 | new ArrayList<NotificationListener>(1); |
| 66 | notificationListenersList = |
| 67 | Collections.synchronizedList(l); |
| 68 | } |
| 69 | |
| 70 | public void removeOperations() { |
| 71 | removeAll(); |
| 72 | } |
| 73 | |
| 74 | public void loadOperations(XMBean mbean,MBeanInfo mbeanInfo) { |
| 75 | this.mbean = mbean; |
| 76 | this.mbeanInfo = mbeanInfo; |
| 77 | // add operations information |
| 78 | MBeanOperationInfo operations[] = mbeanInfo.getOperations(); |
| 79 | invalidate(); |
| 80 | |
| 81 | // remove listeners, if any |
| 82 | Component listeners[] = getComponents(); |
| 83 | for (int i = 0; i < listeners.length; i++) |
| 84 | if (listeners[i] instanceof JButton) |
| 85 | ((JButton)listeners[i]).removeActionListener(this); |
| 86 | |
| 87 | removeAll(); |
| 88 | setLayout(new BorderLayout()); |
| 89 | |
| 90 | JButton methodButton; |
| 91 | JLabel methodLabel; |
| 92 | JPanel innerPanelLeft,innerPanelRight; |
| 93 | JPanel outerPanelLeft,outerPanelRight; |
| 94 | outerPanelLeft = new JPanel(new GridLayout(operations.length,1)); |
| 95 | outerPanelRight = new JPanel(new GridLayout(operations.length,1)); |
| 96 | |
| 97 | for (int i=0;i<operations.length;i++) { |
| 98 | innerPanelLeft = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
| 99 | innerPanelRight = new JPanel(new FlowLayout(FlowLayout.LEFT)); |
| 100 | innerPanelLeft.add(methodLabel = |
| 101 | new JLabel(Utils. |
| 102 | getReadableClassName(operations[i]. |
| 103 | getReturnType()), |
| 104 | JLabel.RIGHT)); |
| 105 | if (methodLabel.getText().length()>20) { |
| 106 | methodLabel.setText(methodLabel.getText(). |
| 107 | substring(methodLabel.getText(). |
| 108 | lastIndexOf(".")+1, |
| 109 | methodLabel.getText().length())); |
| 110 | } |
| 111 | |
| 112 | methodButton = new JButton(operations[i].getName()); |
| 113 | methodButton.setToolTipText(operations[i].getDescription()); |
| 114 | boolean callable = isCallable(operations[i].getSignature()); |
| 115 | if(callable) |
| 116 | methodButton.addActionListener(this); |
| 117 | else |
| 118 | methodButton.setEnabled(false); |
| 119 | |
| 120 | MBeanParameterInfo[] signature = operations[i].getSignature(); |
| 121 | OperationEntry paramEntry = new OperationEntry(operations[i], |
| 122 | callable, |
| 123 | methodButton, |
| 124 | this); |
| 125 | operationEntryTable.put(methodButton, paramEntry); |
| 126 | innerPanelRight.add(methodButton); |
| 127 | if(signature.length==0) |
| 128 | innerPanelRight.add(new JLabel("( )",JLabel.CENTER)); |
| 129 | else |
| 130 | innerPanelRight.add(paramEntry); |
| 131 | |
| 132 | outerPanelLeft.add(innerPanelLeft,BorderLayout.WEST); |
| 133 | outerPanelRight.add(innerPanelRight,BorderLayout.CENTER); |
| 134 | } |
| 135 | add(outerPanelLeft,BorderLayout.WEST); |
| 136 | add(outerPanelRight,BorderLayout.CENTER); |
| 137 | validate(); |
| 138 | } |
| 139 | |
| 140 | private boolean isCallable(MBeanParameterInfo[] signature) { |
| 141 | for(int i = 0; i < signature.length; i++) { |
| 142 | if(!Utils.isEditableType(signature[i].getType())) |
| 143 | return false; |
| 144 | } |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | public void actionPerformed(final ActionEvent e) { |
| 149 | performInvokeRequest((JButton)e.getSource()); |
| 150 | } |
| 151 | |
| 152 | void performInvokeRequest(final JButton button) { |
| 153 | mbeansTab.workerAdd(new Runnable() { |
| 154 | public void run() { |
| 155 | try { |
| 156 | OperationEntry entryIf = operationEntryTable.get(button); |
| 157 | Object result = null; |
| 158 | result = mbean.invoke(button.getText(), |
| 159 | entryIf.getParameters(), |
| 160 | entryIf.getSignature()); |
| 161 | // sends result notification to upper level if |
| 162 | // there is a return value |
| 163 | if (entryIf.getReturnType() != null && |
| 164 | !entryIf.getReturnType().equals(Void.TYPE.getName()) && |
| 165 | !entryIf.getReturnType().equals(Void.class.getName())) |
| 166 | fireChangedNotification(OPERATION_INVOCATION_EVENT, |
| 167 | button, |
| 168 | result); |
| 169 | else |
| 170 | EventQueue.invokeLater(new ThreadDialog( |
| 171 | button, |
| 172 | Resources.getText("Method successfully invoked"), |
| 173 | Resources.getText("Info"), |
| 174 | JOptionPane.INFORMATION_MESSAGE)); |
| 175 | } catch (Throwable ex) { |
| 176 | if (JConsole.isDebug()) { |
| 177 | ex.printStackTrace(); |
| 178 | } |
| 179 | ex = Utils.getActualException(ex); |
| 180 | String message = ex.toString(); |
| 181 | EventQueue.invokeLater(new ThreadDialog( |
| 182 | button, |
| 183 | Resources.getText("Problem invoking") + " " + |
| 184 | button.getText() + " : " + message, |
| 185 | Resources.getText("Error"), |
| 186 | JOptionPane.ERROR_MESSAGE)); |
| 187 | } |
| 188 | } |
| 189 | }); |
| 190 | } |
| 191 | |
| 192 | public void addOperationsListener(NotificationListener nl) { |
| 193 | notificationListenersList.add(nl); |
| 194 | } |
| 195 | |
| 196 | public void removeOperationsListener(NotificationListener nl) { |
| 197 | notificationListenersList.remove(nl); |
| 198 | } |
| 199 | |
| 200 | private void fireChangedNotification(String type, |
| 201 | Object source, |
| 202 | Object handback) { |
| 203 | Notification e = new Notification(type,source,0); |
| 204 | for(NotificationListener nl : notificationListenersList) |
| 205 | nl.handleNotification(e,handback); |
| 206 | } |
| 207 | |
| 208 | protected abstract MBeanOperationInfo[] |
| 209 | updateOperations(MBeanOperationInfo[] operations); |
| 210 | } |