src/jdk.jconsole/share/classes/sun/tools/jconsole/CreateMBeanDialog.java
changeset 47216 71c04702a3d5
parent 25859 3317bb8137f4
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2004, 2012, 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;
       
    27 
       
    28 import java.awt.*;
       
    29 import java.awt.event.*;
       
    30 import java.util.List;
       
    31 import java.util.TreeSet;
       
    32 import java.util.Comparator;
       
    33 
       
    34 import javax.swing.*;
       
    35 import javax.swing.border.*;
       
    36 
       
    37 import javax.management.MBeanServerConnection;
       
    38 import javax.management.ObjectName;
       
    39 import javax.management.InstanceAlreadyExistsException;
       
    40 import javax.management.InstanceNotFoundException;
       
    41 
       
    42 
       
    43 import static sun.tools.jconsole.Utilities.*;
       
    44 
       
    45 @SuppressWarnings("serial")
       
    46 public class CreateMBeanDialog extends InternalDialog
       
    47                 implements ActionListener {
       
    48     JConsole jConsole;
       
    49     JComboBox<ProxyClient> connections;
       
    50     JButton createMBeanButton, unregisterMBeanButton, cancelButton;
       
    51 
       
    52     private static final String HOTSPOT_MBEAN =
       
    53         "sun.management.HotspotInternal";
       
    54     private static final String HOTSPOT_MBEAN_OBJECTNAME =
       
    55         "sun.management:type=HotspotInternal";
       
    56     public CreateMBeanDialog(JConsole jConsole) {
       
    57         super(jConsole, "JConsole: Hotspot MBeans", true);
       
    58 
       
    59         this.jConsole = jConsole;
       
    60         setAccessibleDescription(this,
       
    61                                  Messages.HOTSPOT_MBEANS_DIALOG_ACCESSIBLE_DESCRIPTION);
       
    62         Container cp = getContentPane();
       
    63         ((JComponent)cp).setBorder(new EmptyBorder(10, 10, 4, 10));
       
    64 
       
    65         JPanel centerPanel = new JPanel(new VariableGridLayout(0,
       
    66                                                         1,
       
    67                                                         4,
       
    68                                                         4,
       
    69                                                         false,
       
    70                                                         true));
       
    71         cp.add(centerPanel, BorderLayout.CENTER);
       
    72         connections = new JComboBox<ProxyClient>();
       
    73         updateConnections();
       
    74 
       
    75         centerPanel.add(new LabeledComponent(Resources.format(Messages.MANAGE_HOTSPOT_MBEANS_IN_COLON_),
       
    76                                              connections));
       
    77 
       
    78         JPanel bottomPanel = new JPanel(new BorderLayout());
       
    79         cp.add(bottomPanel, BorderLayout.SOUTH);
       
    80 
       
    81         JPanel buttonPanel = new JPanel();
       
    82         bottomPanel.add(buttonPanel, BorderLayout.NORTH);
       
    83         buttonPanel.add(createMBeanButton =
       
    84                         new JButton(Messages.CREATE));
       
    85         buttonPanel.add(unregisterMBeanButton =
       
    86                         new JButton(Messages.UNREGISTER));
       
    87         buttonPanel.add(cancelButton =
       
    88                         new JButton(Messages.CANCEL));
       
    89 
       
    90         statusBar = new JLabel(" ", JLabel.CENTER);
       
    91         bottomPanel.add(statusBar, BorderLayout.SOUTH);
       
    92 
       
    93         createMBeanButton.addActionListener(this);
       
    94         unregisterMBeanButton.addActionListener(this);
       
    95         cancelButton.addActionListener(this);
       
    96 
       
    97         LabeledComponent.layout(centerPanel);
       
    98         pack();
       
    99         setLocationRelativeTo(jConsole);
       
   100     }
       
   101 
       
   102     private void updateConnections() {
       
   103         List<VMInternalFrame> frames = jConsole.getInternalFrames();
       
   104         TreeSet<ProxyClient> data =
       
   105             new TreeSet<ProxyClient>(new Comparator<ProxyClient>() {
       
   106             public int compare(ProxyClient o1, ProxyClient o2) {
       
   107                 // TODO: Need to understand how this method being used?
       
   108                 return o1.connectionName().compareTo(o2.connectionName());
       
   109             }
       
   110         });
       
   111 
       
   112         if (frames.size() == 0) {
       
   113             JComponent cp = (JComponent)jConsole.getContentPane();
       
   114             Component comp = ((BorderLayout)cp.getLayout()).
       
   115                 getLayoutComponent(BorderLayout.CENTER);
       
   116             if (comp instanceof VMPanel) {
       
   117                 VMPanel vmpanel = (VMPanel) comp;
       
   118                 ProxyClient client = vmpanel.getProxyClient(false);
       
   119                 if (client != null && client.hasPlatformMXBeans()) {
       
   120                     data.add(client);
       
   121                 }
       
   122             }
       
   123         } else {
       
   124             for (VMInternalFrame f : frames) {
       
   125                 ProxyClient client = f.getVMPanel().getProxyClient(false);
       
   126                 if (client != null && client.hasPlatformMXBeans()) {
       
   127                     data.add(client);
       
   128                 }
       
   129             }
       
   130         }
       
   131         connections.invalidate();
       
   132         connections.setModel(new DefaultComboBoxModel<ProxyClient>
       
   133             (data.toArray(new ProxyClient[data.size()])));
       
   134         connections.validate();
       
   135     }
       
   136 
       
   137     public void actionPerformed(final ActionEvent ev) {
       
   138         setVisible(false);
       
   139         statusBar.setText("");
       
   140         if (ev.getSource() != cancelButton) {
       
   141             new Thread("CreateMBeanDialog.actionPerformed") {
       
   142                     public void run() {
       
   143                         try {
       
   144                             Object c = connections.getSelectedItem();
       
   145                             if(c == null) return;
       
   146                             if(ev.getSource() == createMBeanButton) {
       
   147                                 MBeanServerConnection connection =
       
   148                                     ((ProxyClient) c).
       
   149                                     getMBeanServerConnection();
       
   150                                 connection.createMBean(HOTSPOT_MBEAN, null);
       
   151                             } else {
       
   152                                 if(ev.getSource() == unregisterMBeanButton) {
       
   153                                     MBeanServerConnection connection =
       
   154                                         ((ProxyClient) c).
       
   155                                         getMBeanServerConnection();
       
   156                                     connection.unregisterMBean(new
       
   157                                         ObjectName(HOTSPOT_MBEAN_OBJECTNAME));
       
   158                                 }
       
   159                             }
       
   160                             return;
       
   161                         } catch(InstanceAlreadyExistsException e) {
       
   162                             statusBar.setText(Messages.ERROR_COLON_MBEANS_ALREADY_EXIST);
       
   163                         } catch(InstanceNotFoundException e) {
       
   164                             statusBar.setText(Messages.ERROR_COLON_MBEANS_DO_NOT_EXIST);
       
   165                         } catch(Exception e) {
       
   166                             statusBar.setText(e.toString());
       
   167                         }
       
   168                         setVisible(true);
       
   169                     }
       
   170                 }.start();
       
   171         }
       
   172     }
       
   173 
       
   174     public void setVisible(boolean b) {
       
   175         boolean wasVisible = isVisible();
       
   176 
       
   177         if(b) {
       
   178             setLocationRelativeTo(jConsole);
       
   179             invalidate();
       
   180             updateConnections();
       
   181             validate();
       
   182             repaint();
       
   183         }
       
   184 
       
   185         super.setVisible(b);
       
   186 
       
   187 
       
   188         if (b && !wasVisible) {
       
   189             // Need to delay this to make focus stick
       
   190             SwingUtilities.invokeLater(new Runnable() {
       
   191                 public void run() {
       
   192                     connections.requestFocus();
       
   193                 }
       
   194             });
       
   195         }
       
   196     }
       
   197 }