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