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.BorderLayout;
029: import java.awt.event.MouseAdapter;
030: import java.awt.event.MouseEvent;
031: import java.awt.event.MouseListener;
032: import java.beans.*;
033: import java.io.*;
034: import java.util.Set;
035: import javax.management.*;
036: import javax.swing.*;
037: import javax.swing.event.*;
038: import javax.swing.tree.*;
039: import sun.tools.jconsole.inspector.*;
040:
041: import com.sun.tools.jconsole.JConsoleContext;
042:
043: @SuppressWarnings("serial")
044: public class MBeansTab extends Tab implements NotificationListener,
045: PropertyChangeListener, TreeSelectionListener {
046:
047: private XTree tree;
048: private XSheet sheet;
049: private XDataViewer viewer;
050:
051: public static String getTabName() {
052: return Resources.getText("MBeans");
053: }
054:
055: public MBeansTab(final VMPanel vmPanel) {
056: super (vmPanel, getTabName());
057: addPropertyChangeListener(this );
058: setupTab();
059: }
060:
061: public XDataViewer getDataViewer() {
062: return viewer;
063: }
064:
065: public XTree getTree() {
066: return tree;
067: }
068:
069: public XSheet getSheet() {
070: return sheet;
071: }
072:
073: public void dispose() {
074: super .dispose();
075: sheet.dispose();
076: }
077:
078: public int getUpdateInterval() {
079: return vmPanel.getUpdateInterval();
080: }
081:
082: void synchroniseMBeanServerView() {
083: // Register listener for MBean registration/unregistration
084: //
085: try {
086: getMBeanServerConnection()
087: .addNotificationListener(
088: MBeanServerDelegate.DELEGATE_NAME, this ,
089: null, null);
090: } catch (InstanceNotFoundException e) {
091: // Should never happen because the MBeanServerDelegate
092: // is always present in any standard MBeanServer
093: //
094: if (JConsole.isDebug()) {
095: e.printStackTrace();
096: }
097: } catch (IOException e) {
098: if (JConsole.isDebug()) {
099: e.printStackTrace();
100: }
101: vmPanel.getProxyClient().markAsDead();
102: return;
103: }
104: // Retrieve MBeans from MBeanServer
105: //
106: Set<ObjectName> newSet = null;
107: try {
108: newSet = getMBeanServerConnection().queryNames(null, null);
109: } catch (IOException e) {
110: if (JConsole.isDebug()) {
111: e.printStackTrace();
112: }
113: vmPanel.getProxyClient().markAsDead();
114: return;
115: }
116: // Cleanup current tree
117: //
118: tree.removeAll();
119: // Do not display anything until the new tree has been built
120: //
121: tree.setVisible(false);
122: // Add MBeans to tree
123: //
124: for (ObjectName mbean : newSet) {
125: tree.addMBeanToView(mbean);
126: }
127: // Display the new tree
128: //
129: tree.setVisible(true);
130: }
131:
132: public MBeanServerConnection getMBeanServerConnection() {
133: return vmPanel.getProxyClient().getMBeanServerConnection();
134: }
135:
136: public void update() {
137: // Ping the connection to see if it is still alive. At
138: // some point the ProxyClient class should centralize
139: // the connection aliveness monitoring and no longer
140: // rely on the custom tabs to ping the connections.
141: //
142: try {
143: getMBeanServerConnection().getDefaultDomain();
144: } catch (IOException ex) {
145: vmPanel.getProxyClient().markAsDead();
146: }
147: }
148:
149: private void setupTab() {
150: // set up the split pane with the MBean tree and MBean sheet panels
151: setLayout(new BorderLayout());
152: JSplitPane mainSplit = new JSplitPane(
153: JSplitPane.HORIZONTAL_SPLIT);
154: mainSplit.setDividerLocation(160);
155: mainSplit.setBorder(BorderFactory.createEmptyBorder());
156:
157: // set up the MBean tree panel (left pane)
158: tree = new XTree(this );
159: tree.setCellRenderer(new XTreeRenderer());
160: tree.getSelectionModel().setSelectionMode(
161: TreeSelectionModel.SINGLE_TREE_SELECTION);
162: tree.addTreeSelectionListener(this );
163: tree.addMouseListener(ml);
164: JScrollPane theScrollPane = new JScrollPane(tree,
165: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
166: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
167: JPanel treePanel = new JPanel(new BorderLayout());
168: treePanel.add(theScrollPane, BorderLayout.CENTER);
169: mainSplit.add(treePanel, JSplitPane.LEFT, 0);
170:
171: // set up the MBean sheet panel (right pane)
172: viewer = new XDataViewer(this );
173: sheet = new XSheet(this );
174: mainSplit.add(sheet, JSplitPane.RIGHT, 0);
175:
176: add(mainSplit);
177: }
178:
179: /* notification listener */
180: public void handleNotification(Notification notification,
181: Object handback) {
182: if (notification instanceof MBeanServerNotification) {
183: ObjectName mbean = ((MBeanServerNotification) notification)
184: .getMBeanName();
185: if (notification.getType().equals(
186: MBeanServerNotification.REGISTRATION_NOTIFICATION)) {
187: tree.addMBeanToView(mbean);
188: } else if (notification
189: .getType()
190: .equals(
191: MBeanServerNotification.UNREGISTRATION_NOTIFICATION)) {
192: tree.delMBeanFromView(mbean);
193: }
194: }
195: }
196:
197: /* property change listener */
198: public void propertyChange(PropertyChangeEvent evt) {
199: if (evt.getPropertyName() == JConsoleContext.CONNECTION_STATE_PROPERTY) {
200: boolean connected = (Boolean) evt.getNewValue();
201: if (connected) {
202: workerAdd(new Runnable() {
203: public void run() {
204: synchroniseMBeanServerView();
205: }
206: });
207: } else {
208: sheet.dispose();
209: }
210: }
211:
212: }
213:
214: /* tree selection listener */
215: public void valueChanged(TreeSelectionEvent e) {
216: DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree
217: .getLastSelectedPathComponent();
218: sheet.displayNode(node);
219: }
220:
221: /* tree mouse listener */
222: private MouseListener ml = new MouseAdapter() {
223: public void mousePressed(MouseEvent e) {
224: if (e.getClickCount() == 1) {
225: int selRow = tree.getRowForLocation(e.getX(), e.getY());
226: if (selRow != -1) {
227: TreePath selPath = tree.getPathForLocation(
228: e.getX(), e.getY());
229: DefaultMutableTreeNode node = (DefaultMutableTreeNode) selPath
230: .getLastPathComponent();
231: if (sheet.isMBeanNode(node)) {
232: tree.expandPath(selPath);
233: }
234: }
235: }
236: }
237: };
238: }
|