001: /*
002: * Copyright 2004-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.jconsole;
027:
028: import java.awt.*;
029: import java.awt.event.*;
030: import java.util.List;
031: import java.util.TreeSet;
032: import java.util.Comparator;
033:
034: import javax.swing.*;
035: import javax.swing.Timer;
036: import javax.swing.border.*;
037: import javax.swing.event.*;
038:
039: import javax.management.MBeanServerConnection;
040: import javax.management.ObjectName;
041: import javax.management.InstanceAlreadyExistsException;
042: import javax.management.InstanceNotFoundException;
043:
044: import static sun.tools.jconsole.Resources.*;
045: import static sun.tools.jconsole.Utilities.*;
046:
047: @SuppressWarnings("serial")
048: public class CreateMBeanDialog extends InternalDialog implements
049: ActionListener {
050: JConsole jConsole;
051: JComboBox connections;
052: JButton createMBeanButton, unregisterMBeanButton, cancelButton;
053:
054: private static final String HOTSPOT_MBEAN = "sun.management.HotspotInternal";
055: private static final String HOTSPOT_MBEAN_OBJECTNAME = "sun.management:type=HotspotInternal";
056:
057: public CreateMBeanDialog(JConsole jConsole) {
058: super (jConsole, "JConsole: Hotspot MBeans", true);
059:
060: this .jConsole = jConsole;
061: setAccessibleDescription(this ,
062: getText("Hotspot MBeans.dialog.accessibleDescription"));
063: Container cp = getContentPane();
064: ((JComponent) cp).setBorder(new EmptyBorder(10, 10, 4, 10));
065:
066: JPanel centerPanel = new JPanel(new VariableGridLayout(0, 1, 4,
067: 4, false, true));
068: cp.add(centerPanel, BorderLayout.CENTER);
069: connections = new JComboBox();
070: updateConnections();
071:
072: centerPanel.add(new LabeledComponent(Resources
073: .getText("Manage Hotspot MBeans " + "in: "),
074: connections));
075:
076: JPanel bottomPanel = new JPanel(new BorderLayout());
077: cp.add(bottomPanel, BorderLayout.SOUTH);
078:
079: JPanel buttonPanel = new JPanel();
080: bottomPanel.add(buttonPanel, BorderLayout.NORTH);
081: buttonPanel.add(createMBeanButton = new JButton(Resources
082: .getText("Create")));
083: buttonPanel.add(unregisterMBeanButton = new JButton(Resources
084: .getText("Unregister")));
085: buttonPanel.add(cancelButton = new JButton(Resources
086: .getText("Cancel")));
087:
088: statusBar = new JLabel(" ", JLabel.CENTER);
089: bottomPanel.add(statusBar, BorderLayout.SOUTH);
090:
091: createMBeanButton.addActionListener(this );
092: unregisterMBeanButton.addActionListener(this );
093: cancelButton.addActionListener(this );
094:
095: LabeledComponent.layout(centerPanel);
096: pack();
097: setLocationRelativeTo(jConsole);
098: }
099:
100: private void updateConnections() {
101: List<VMInternalFrame> frames = jConsole.getInternalFrames();
102: TreeSet<ProxyClient> data = new TreeSet<ProxyClient>(
103: new Comparator<ProxyClient>() {
104: public int compare(ProxyClient o1, ProxyClient o2) {
105: // TODO: Need to understand how this method being used?
106: return o1.connectionName().compareTo(
107: o2.connectionName());
108: }
109: });
110:
111: if (frames.size() == 0) {
112: JComponent cp = (JComponent) jConsole.getContentPane();
113: Component comp = ((BorderLayout) cp.getLayout())
114: .getLayoutComponent(BorderLayout.CENTER);
115: if (comp instanceof VMPanel) {
116: VMPanel vmpanel = (VMPanel) comp;
117: ProxyClient client = vmpanel.getProxyClient(false);
118: if (client != null && client.hasPlatformMXBeans()) {
119: data.add(client);
120: }
121: }
122: } else {
123: for (VMInternalFrame f : frames) {
124: ProxyClient client = f.getVMPanel().getProxyClient(
125: false);
126: if (client != null && client.hasPlatformMXBeans()) {
127: data.add(client);
128: }
129: }
130: }
131: connections.invalidate();
132: connections.setModel(new DefaultComboBoxModel(data.toArray()));
133: connections.validate();
134: }
135:
136: public void actionPerformed(final ActionEvent ev) {
137: setVisible(false);
138: statusBar.setText("");
139: if (ev.getSource() != cancelButton) {
140: new Thread("CreateMBeanDialog.actionPerformed") {
141: public void run() {
142: try {
143: StringBuffer buff = null;
144: Object c = connections.getSelectedItem();
145: if (c == null)
146: return;
147: if (ev.getSource() == createMBeanButton) {
148: MBeanServerConnection connection = ((ProxyClient) c)
149: .getMBeanServerConnection();
150: connection.createMBean(HOTSPOT_MBEAN, null);
151: } else {
152: if (ev.getSource() == unregisterMBeanButton) {
153: MBeanServerConnection connection = ((ProxyClient) c)
154: .getMBeanServerConnection();
155: connection
156: .unregisterMBean(new ObjectName(
157: HOTSPOT_MBEAN_OBJECTNAME));
158: }
159: }
160: return;
161: } catch (InstanceAlreadyExistsException e) {
162: statusBar.setText(Resources
163: .getText("Error: MBeans already "
164: + "exist"));
165: } catch (InstanceNotFoundException e) {
166: statusBar.setText(Resources
167: .getText("Error: MBeans do not "
168: + "exist"));
169: } catch (Exception e) {
170: statusBar.setText(e.toString());
171: }
172: setVisible(true);
173: }
174: }.start();
175: }
176: }
177:
178: public void setVisible(boolean b) {
179: boolean wasVisible = isVisible();
180:
181: if (b) {
182: setLocationRelativeTo(jConsole);
183: invalidate();
184: updateConnections();
185: validate();
186: repaint();
187: }
188:
189: super .setVisible(b);
190:
191: if (b && !wasVisible) {
192: // Need to delay this to make focus stick
193: SwingUtilities.invokeLater(new Runnable() {
194: public void run() {
195: connections.requestFocus();
196: }
197: });
198: }
199: }
200: }
|