1 /*
   2  * Copyright (c) 2004, 2007, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.tools.jconsole.inspector;
  27 
  28 import javax.swing.*;
  29 import java.awt.BorderLayout;
  30 import java.awt.GridLayout;
  31 import java.awt.FlowLayout;
  32 import java.awt.Component;
  33 import java.awt.EventQueue;
  34 import java.awt.event.*;
  35 import java.awt.Insets;
  36 import java.awt.Dimension;
  37 import java.util.*;
  38 
  39 import javax.management.*;
  40 
  41 import sun.tools.jconsole.MBeansTab;
  42 import sun.tools.jconsole.JConsole;
  43 import sun.tools.jconsole.Messages;
  44 
  45 public abstract class XOperations extends JPanel implements ActionListener {
  46 
  47     public final static String OPERATION_INVOCATION_EVENT =
  48         "jam.xoperations.invoke.result";
  49     private java.util.List<NotificationListener> notificationListenersList;
  50 
  51     private Hashtable<JButton, OperationEntry> operationEntryTable;
  52 
  53     private XMBean mbean;
  54     private MBeanInfo mbeanInfo;
  55     private MBeansTab mbeansTab;
  56     public XOperations(MBeansTab mbeansTab) {
  57         super(new GridLayout(1,1));
  58         this.mbeansTab = mbeansTab;
  59         operationEntryTable = new Hashtable<JButton, OperationEntry>();
  60         ArrayList<NotificationListener> l =
  61             new ArrayList<NotificationListener>(1);
  62         notificationListenersList =
  63             Collections.synchronizedList(l);
  64     }
  65 
  66     public void removeOperations() {
  67         removeAll();
  68     }
  69 
  70     public void loadOperations(XMBean mbean,MBeanInfo mbeanInfo) {
  71         this.mbean = mbean;
  72         this.mbeanInfo = mbeanInfo;
  73         // add operations information
  74         MBeanOperationInfo operations[] = mbeanInfo.getOperations();
  75         invalidate();
  76 
  77         // remove listeners, if any
  78         Component listeners[] = getComponents();
  79         for (int i = 0; i < listeners.length; i++)
  80             if (listeners[i] instanceof JButton)
  81                 ((JButton)listeners[i]).removeActionListener(this);
  82 
  83         removeAll();
  84         setLayout(new BorderLayout());
  85 
  86         JButton methodButton;
  87         JLabel methodLabel;
  88         JPanel innerPanelLeft,innerPanelRight;
  89         JPanel outerPanelLeft,outerPanelRight;
  90         outerPanelLeft  = new JPanel(new GridLayout(operations.length,1));
  91         outerPanelRight = new JPanel(new GridLayout(operations.length,1));
  92 
  93         for (int i=0;i<operations.length;i++) {
  94             innerPanelLeft  = new JPanel(new FlowLayout(FlowLayout.RIGHT));
  95             innerPanelRight = new JPanel(new FlowLayout(FlowLayout.LEFT));
  96             innerPanelLeft.add(methodLabel =
  97                                new JLabel(Utils.
  98                                           getReadableClassName(operations[i].
  99                                                              getReturnType()),
 100                                           JLabel.RIGHT));
 101             if (methodLabel.getText().length()>20) {
 102                 methodLabel.setText(methodLabel.getText().
 103                                     substring(methodLabel.getText().
 104                                               lastIndexOf(".")+1,
 105                                              methodLabel.getText().length()));
 106             }
 107 
 108             methodButton = new JButton(operations[i].getName());
 109             methodButton.setToolTipText(operations[i].getDescription());
 110             boolean callable = isCallable(operations[i].getSignature());
 111             if(callable)
 112                 methodButton.addActionListener(this);
 113             else
 114                 methodButton.setEnabled(false);
 115 
 116             MBeanParameterInfo[] signature = operations[i].getSignature();
 117             OperationEntry paramEntry = new OperationEntry(operations[i],
 118                                                            callable,
 119                                                            methodButton,
 120                                                            this);
 121             operationEntryTable.put(methodButton, paramEntry);
 122             innerPanelRight.add(methodButton);
 123                 if(signature.length==0)
 124                     innerPanelRight.add(new JLabel("( )",JLabel.CENTER));
 125                 else
 126                     innerPanelRight.add(paramEntry);
 127 
 128             outerPanelLeft.add(innerPanelLeft,BorderLayout.WEST);
 129             outerPanelRight.add(innerPanelRight,BorderLayout.CENTER);
 130         }
 131         add(outerPanelLeft,BorderLayout.WEST);
 132         add(outerPanelRight,BorderLayout.CENTER);
 133         validate();
 134     }
 135 
 136     private boolean isCallable(MBeanParameterInfo[] signature) {
 137         for(int i = 0; i < signature.length; i++) {
 138             if(!Utils.isEditableType(signature[i].getType()))
 139                 return false;
 140         }
 141         return true;
 142     }
 143 
 144     public void actionPerformed(final ActionEvent e) {
 145         performInvokeRequest((JButton)e.getSource());
 146     }
 147 
 148     void performInvokeRequest(final JButton button) {
 149         mbeansTab.workerAdd(new Runnable() {
 150             public void run() {
 151                 try {
 152                     OperationEntry entryIf = operationEntryTable.get(button);
 153                     Object result = null;
 154                     result = mbean.invoke(button.getText(),
 155                                           entryIf.getParameters(),
 156                                           entryIf.getSignature());
 157                     // sends result notification to upper level if
 158                     // there is a return value
 159                     if (entryIf.getReturnType() != null &&
 160                         !entryIf.getReturnType().equals(Void.TYPE.getName()) &&
 161                         !entryIf.getReturnType().equals(Void.class.getName()))
 162                         fireChangedNotification(OPERATION_INVOCATION_EVENT,
 163                                                 button,
 164                                                 result);
 165                     else
 166                         EventQueue.invokeLater(new ThreadDialog(
 167                             button,
 168                             Messages.METHOD_SUCCESSFULLY_INVOKED,
 169                             Messages.INFO,
 170                             JOptionPane.INFORMATION_MESSAGE));
 171                 } catch (Throwable ex) {
 172                     if (JConsole.isDebug()) {
 173                         ex.printStackTrace();
 174                     }
 175                     ex = Utils.getActualException(ex);
 176                     String message = ex.toString();
 177                     EventQueue.invokeLater(new ThreadDialog(
 178                         button,
 179                         Messages.PROBLEM_INVOKING + " " +
 180                         button.getText() + " : " + message,
 181                         Messages.ERROR,
 182                         JOptionPane.ERROR_MESSAGE));
 183                 }
 184             }
 185         });
 186     }
 187 
 188     public void addOperationsListener(NotificationListener nl) {
 189             notificationListenersList.add(nl);
 190         }
 191 
 192     public void removeOperationsListener(NotificationListener nl) {
 193         notificationListenersList.remove(nl);
 194     }
 195 
 196     private void fireChangedNotification(String type,
 197                                          Object source,
 198                                          Object handback) {
 199         Notification e = new Notification(type,source,0);
 200         for(NotificationListener nl : notificationListenersList)
 201             nl.handleNotification(e,handback);
 202     }
 203 
 204     protected abstract MBeanOperationInfo[]
 205         updateOperations(MBeanOperationInfo[] operations);
 206 }