jdk/src/share/classes/sun/tools/jconsole/CreateMBeanDialog.java
author sla
Tue, 19 Mar 2013 13:26:42 +0100
changeset 18197 ae73e4f50e08
parent 14342 8435a30053c1
permissions -rw-r--r--
8003703: Update RMI connection dialog box Reviewed-by: skoivu, ahgross, mchung, jbachorik
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12851
diff changeset
     2
 * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.jconsole;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.TreeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.MBeanServerConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.InstanceAlreadyExistsException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.InstanceNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    42
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import static sun.tools.jconsole.Utilities.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
@SuppressWarnings("serial")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class CreateMBeanDialog extends InternalDialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    JConsole jConsole;
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    49
    JComboBox<ProxyClient> connections;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    JButton createMBeanButton, unregisterMBeanButton, cancelButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final String HOTSPOT_MBEAN =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        "sun.management.HotspotInternal";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final String HOTSPOT_MBEAN_OBJECTNAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        "sun.management:type=HotspotInternal";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public CreateMBeanDialog(JConsole jConsole) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        super(jConsole, "JConsole: Hotspot MBeans", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this.jConsole = jConsole;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        setAccessibleDescription(this,
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    61
                                 Messages.HOTSPOT_MBEANS_DIALOG_ACCESSIBLE_DESCRIPTION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        Container cp = getContentPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        ((JComponent)cp).setBorder(new EmptyBorder(10, 10, 4, 10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        JPanel centerPanel = new JPanel(new VariableGridLayout(0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                                                        1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                                                        4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                                                        4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                                        false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                                                        true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        cp.add(centerPanel, BorderLayout.CENTER);
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    72
        connections = new JComboBox<ProxyClient>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        updateConnections();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    75
        centerPanel.add(new LabeledComponent(Resources.format(Messages.MANAGE_HOTSPOT_MBEANS_IN_COLON_),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                             connections));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        JPanel bottomPanel = new JPanel(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        cp.add(bottomPanel, BorderLayout.SOUTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        JPanel buttonPanel = new JPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        bottomPanel.add(buttonPanel, BorderLayout.NORTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        buttonPanel.add(createMBeanButton =
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    84
                        new JButton(Messages.CREATE));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        buttonPanel.add(unregisterMBeanButton =
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    86
                        new JButton(Messages.UNREGISTER));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        buttonPanel.add(cancelButton =
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    88
                        new JButton(Messages.CANCEL));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        statusBar = new JLabel(" ", JLabel.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        bottomPanel.add(statusBar, BorderLayout.SOUTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        createMBeanButton.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        unregisterMBeanButton.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        cancelButton.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        LabeledComponent.layout(centerPanel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        setLocationRelativeTo(jConsole);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private void updateConnections() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        List<VMInternalFrame> frames = jConsole.getInternalFrames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        TreeSet<ProxyClient> data =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            new TreeSet<ProxyClient>(new Comparator<ProxyClient>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            public int compare(ProxyClient o1, ProxyClient o2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                // TODO: Need to understand how this method being used?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                return o1.connectionName().compareTo(o2.connectionName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (frames.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            JComponent cp = (JComponent)jConsole.getContentPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            Component comp = ((BorderLayout)cp.getLayout()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                getLayoutComponent(BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (comp instanceof VMPanel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                VMPanel vmpanel = (VMPanel) comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                ProxyClient client = vmpanel.getProxyClient(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                if (client != null && client.hasPlatformMXBeans()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    data.add(client);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            for (VMInternalFrame f : frames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                ProxyClient client = f.getVMPanel().getProxyClient(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                if (client != null && client.hasPlatformMXBeans()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    data.add(client);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        connections.invalidate();
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   132
        connections.setModel(new DefaultComboBoxModel<ProxyClient>
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   133
            (data.toArray(new ProxyClient[data.size()])));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        connections.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public void actionPerformed(final ActionEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        statusBar.setText("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (ev.getSource() != cancelButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            new Thread("CreateMBeanDialog.actionPerformed") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                            Object c = connections.getSelectedItem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                            if(c == null) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                            if(ev.getSource() == createMBeanButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                MBeanServerConnection connection =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                    ((ProxyClient) c).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                    getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                connection.createMBean(HOTSPOT_MBEAN, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                if(ev.getSource() == unregisterMBeanButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                                    MBeanServerConnection connection =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                                        ((ProxyClient) c).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                                        getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                    connection.unregisterMBean(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                        ObjectName(HOTSPOT_MBEAN_OBJECTNAME));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        } catch(InstanceAlreadyExistsException e) {
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   162
                            statusBar.setText(Messages.ERROR_COLON_MBEANS_ALREADY_EXIST);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        } catch(InstanceNotFoundException e) {
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   164
                            statusBar.setText(Messages.ERROR_COLON_MBEANS_DO_NOT_EXIST);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                            statusBar.setText(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                }.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public void setVisible(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        boolean wasVisible = isVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if(b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            setLocationRelativeTo(jConsole);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            updateConnections();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        super.setVisible(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (b && !wasVisible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            // Need to delay this to make focus stick
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            SwingUtilities.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    connections.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
}