Path: blob/master/src/jdk.jconsole/share/classes/sun/tools/jconsole/CreateMBeanDialog.java
40948 views
/*1* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jconsole;2627import java.awt.*;28import java.awt.event.*;29import java.util.List;30import java.util.TreeSet;31import java.util.Comparator;3233import javax.swing.*;34import javax.swing.border.*;3536import javax.management.MBeanServerConnection;37import javax.management.ObjectName;38import javax.management.InstanceAlreadyExistsException;39import javax.management.InstanceNotFoundException;404142import static sun.tools.jconsole.Utilities.*;4344@SuppressWarnings("serial")45public class CreateMBeanDialog extends InternalDialog46implements ActionListener {47JConsole jConsole;48JComboBox<ProxyClient> connections;49JButton createMBeanButton, unregisterMBeanButton, cancelButton;5051private static final String HOTSPOT_MBEAN =52"sun.management.HotspotInternal";53private static final String HOTSPOT_MBEAN_OBJECTNAME =54"sun.management:type=HotspotInternal";55public CreateMBeanDialog(JConsole jConsole) {56super(jConsole, "JConsole: Hotspot MBeans", true);5758this.jConsole = jConsole;59setAccessibleDescription(this,60Messages.HOTSPOT_MBEANS_DIALOG_ACCESSIBLE_DESCRIPTION);61Container cp = getContentPane();62((JComponent)cp).setBorder(new EmptyBorder(10, 10, 4, 10));6364JPanel centerPanel = new JPanel(new VariableGridLayout(0,651,664,674,68false,69true));70cp.add(centerPanel, BorderLayout.CENTER);71connections = new JComboBox<ProxyClient>();72updateConnections();7374centerPanel.add(new LabeledComponent(Resources.format(Messages.MANAGE_HOTSPOT_MBEANS_IN_COLON_),75connections));7677JPanel bottomPanel = new JPanel(new BorderLayout());78cp.add(bottomPanel, BorderLayout.SOUTH);7980JPanel buttonPanel = new JPanel();81bottomPanel.add(buttonPanel, BorderLayout.NORTH);82buttonPanel.add(createMBeanButton =83new JButton(Messages.CREATE));84buttonPanel.add(unregisterMBeanButton =85new JButton(Messages.UNREGISTER));86buttonPanel.add(cancelButton =87new JButton(Messages.CANCEL));8889statusBar = new JLabel(" ", JLabel.CENTER);90bottomPanel.add(statusBar, BorderLayout.SOUTH);9192createMBeanButton.addActionListener(this);93unregisterMBeanButton.addActionListener(this);94cancelButton.addActionListener(this);9596LabeledComponent.layout(centerPanel);97pack();98setLocationRelativeTo(jConsole);99}100101private void updateConnections() {102List<VMInternalFrame> frames = jConsole.getInternalFrames();103TreeSet<ProxyClient> data =104new TreeSet<ProxyClient>(new Comparator<ProxyClient>() {105public int compare(ProxyClient o1, ProxyClient o2) {106// TODO: Need to understand how this method being used?107return o1.connectionName().compareTo(o2.connectionName());108}109});110111if (frames.size() == 0) {112JComponent cp = (JComponent)jConsole.getContentPane();113Component comp = ((BorderLayout)cp.getLayout()).114getLayoutComponent(BorderLayout.CENTER);115if (comp instanceof VMPanel) {116VMPanel vmpanel = (VMPanel) comp;117ProxyClient client = vmpanel.getProxyClient(false);118if (client != null && client.hasPlatformMXBeans()) {119data.add(client);120}121}122} else {123for (VMInternalFrame f : frames) {124ProxyClient client = f.getVMPanel().getProxyClient(false);125if (client != null && client.hasPlatformMXBeans()) {126data.add(client);127}128}129}130connections.invalidate();131connections.setModel(new DefaultComboBoxModel<ProxyClient>132(data.toArray(new ProxyClient[data.size()])));133connections.validate();134}135136public void actionPerformed(final ActionEvent ev) {137setVisible(false);138statusBar.setText("");139if (ev.getSource() != cancelButton) {140new Thread("CreateMBeanDialog.actionPerformed") {141public void run() {142try {143Object c = connections.getSelectedItem();144if(c == null) return;145if(ev.getSource() == createMBeanButton) {146MBeanServerConnection connection =147((ProxyClient) c).148getMBeanServerConnection();149connection.createMBean(HOTSPOT_MBEAN, null);150} else {151if(ev.getSource() == unregisterMBeanButton) {152MBeanServerConnection connection =153((ProxyClient) c).154getMBeanServerConnection();155connection.unregisterMBean(new156ObjectName(HOTSPOT_MBEAN_OBJECTNAME));157}158}159return;160} catch(InstanceAlreadyExistsException e) {161statusBar.setText(Messages.ERROR_COLON_MBEANS_ALREADY_EXIST);162} catch(InstanceNotFoundException e) {163statusBar.setText(Messages.ERROR_COLON_MBEANS_DO_NOT_EXIST);164} catch(Exception e) {165statusBar.setText(e.toString());166}167setVisible(true);168}169}.start();170}171}172173public void setVisible(boolean b) {174boolean wasVisible = isVisible();175176if(b) {177setLocationRelativeTo(jConsole);178invalidate();179updateConnections();180validate();181repaint();182}183184super.setVisible(b);185186187if (b && !wasVisible) {188// Need to delay this to make focus stick189SwingUtilities.invokeLater(new Runnable() {190public void run() {191connections.requestFocus();192}193});194}195}196}197198199