Path: blob/master/src/jdk.jconsole/share/classes/sun/tools/jconsole/MBeansTab.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.BorderLayout;28import java.awt.EventQueue;29import java.awt.event.MouseAdapter;30import java.awt.event.MouseEvent;31import java.awt.event.MouseListener;32import java.beans.*;33import java.io.*;34import java.util.Set;35import javax.management.*;36import javax.swing.*;37import javax.swing.event.*;38import javax.swing.tree.*;39import sun.tools.jconsole.ProxyClient.SnapshotMBeanServerConnection;40import sun.tools.jconsole.inspector.*;4142import com.sun.tools.jconsole.JConsoleContext;4344@SuppressWarnings("serial")45public class MBeansTab extends Tab implements46NotificationListener, PropertyChangeListener,47TreeSelectionListener, TreeWillExpandListener {4849private XTree tree;50private XSheet sheet;51private XDataViewer viewer;5253public static String getTabName() {54return Messages.MBEANS;55}5657public MBeansTab(final VMPanel vmPanel) {58super(vmPanel, getTabName());59addPropertyChangeListener(this);60setupTab();61}6263public XDataViewer getDataViewer() {64return viewer;65}6667public XTree getTree() {68return tree;69}7071public XSheet getSheet() {72return sheet;73}7475@Override76public void dispose() {77super.dispose();78sheet.dispose();79}8081public int getUpdateInterval() {82return vmPanel.getUpdateInterval();83}8485private void buildMBeanServerView() {86new SwingWorker<Set<ObjectName>, Void>() {87@Override88public Set<ObjectName> doInBackground() {89// Register listener for MBean registration/unregistration90//91try {92getMBeanServerConnection().addNotificationListener(93MBeanServerDelegate.DELEGATE_NAME,94MBeansTab.this,95null,96null);97} catch (InstanceNotFoundException e) {98// Should never happen because the MBeanServerDelegate99// is always present in any standard MBeanServer100//101if (JConsole.isDebug()) {102e.printStackTrace();103}104} catch (IOException e) {105if (JConsole.isDebug()) {106e.printStackTrace();107}108vmPanel.getProxyClient().markAsDead();109return null;110}111// Retrieve MBeans from MBeanServer112//113Set<ObjectName> mbeans = null;114try {115mbeans = getMBeanServerConnection().queryNames(null, null);116} catch (IOException e) {117if (JConsole.isDebug()) {118e.printStackTrace();119}120vmPanel.getProxyClient().markAsDead();121return null;122}123return mbeans;124}125@Override126protected void done() {127try {128// Wait for mbsc.queryNames() result129Set<ObjectName> mbeans = get();130// Do not display anything until the new tree has been built131//132tree.setVisible(false);133// Cleanup current tree134//135tree.removeAll();136// Add MBeans to tree137//138tree.addMBeansToView(mbeans);139// Display the new tree140//141tree.setVisible(true);142} catch (Exception e) {143Throwable t = Utils.getActualException(e);144if (JConsole.isDebug()) {145System.err.println("Problem at MBean tree construction");146t.printStackTrace();147}148}149}150}.execute();151}152153public MBeanServerConnection getMBeanServerConnection() {154return vmPanel.getProxyClient().getMBeanServerConnection();155}156157public SnapshotMBeanServerConnection getSnapshotMBeanServerConnection() {158return vmPanel.getProxyClient().getSnapshotMBeanServerConnection();159}160161@Override162public void update() {163// Ping the connection to see if it is still alive. At164// some point the ProxyClient class should centralize165// the connection aliveness monitoring and no longer166// rely on the custom tabs to ping the connections.167//168try {169getMBeanServerConnection().getDefaultDomain();170} catch (IOException ex) {171vmPanel.getProxyClient().markAsDead();172}173}174175private void setupTab() {176// set up the split pane with the MBean tree and MBean sheet panels177setLayout(new BorderLayout());178JSplitPane mainSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);179mainSplit.setDividerLocation(160);180mainSplit.setBorder(BorderFactory.createEmptyBorder());181182// set up the MBean tree panel (left pane)183tree = new XTree(this);184tree.setCellRenderer(new XTreeRenderer());185tree.getSelectionModel().setSelectionMode(186TreeSelectionModel.SINGLE_TREE_SELECTION);187tree.addTreeSelectionListener(this);188tree.addTreeWillExpandListener(this);189tree.addMouseListener(ml);190JScrollPane theScrollPane = new JScrollPane(191tree,192JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,193JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);194JPanel treePanel = new JPanel(new BorderLayout());195treePanel.add(theScrollPane, BorderLayout.CENTER);196mainSplit.add(treePanel, JSplitPane.LEFT, 0);197198// set up the MBean sheet panel (right pane)199viewer = new XDataViewer(this);200sheet = new XSheet(this);201mainSplit.add(sheet, JSplitPane.RIGHT, 0);202203add(mainSplit);204}205206/* notification listener: handleNotification */207public void handleNotification(208final Notification notification, Object handback) {209EventQueue.invokeLater(new Runnable() {210public void run() {211if (notification instanceof MBeanServerNotification) {212ObjectName mbean =213((MBeanServerNotification) notification).getMBeanName();214if (notification.getType().equals(215MBeanServerNotification.REGISTRATION_NOTIFICATION)) {216tree.addMBeanToView(mbean);217} else if (notification.getType().equals(218MBeanServerNotification.UNREGISTRATION_NOTIFICATION)) {219tree.removeMBeanFromView(mbean);220}221}222}223});224}225226/* property change listener: propertyChange */227public void propertyChange(PropertyChangeEvent evt) {228if (JConsoleContext.CONNECTION_STATE_PROPERTY.equals(evt.getPropertyName())) {229boolean connected = (Boolean) evt.getNewValue();230if (connected) {231buildMBeanServerView();232} else {233sheet.dispose();234}235}236}237238/* tree selection listener: valueChanged */239public void valueChanged(TreeSelectionEvent e) {240DefaultMutableTreeNode node =241(DefaultMutableTreeNode) tree.getLastSelectedPathComponent();242sheet.displayNode(node);243}244/* tree mouse listener: mousePressed */245private MouseListener ml = new MouseAdapter() {246@Override247public void mousePressed(MouseEvent e) {248if (e.getClickCount() == 1) {249int selRow = tree.getRowForLocation(e.getX(), e.getY());250if (selRow != -1) {251TreePath selPath =252tree.getPathForLocation(e.getX(), e.getY());253DefaultMutableTreeNode node =254(DefaultMutableTreeNode) selPath.getLastPathComponent();255if (sheet.isMBeanNode(node)) {256tree.expandPath(selPath);257}258}259}260}261};262263/* tree will expand listener: treeWillExpand */264public void treeWillExpand(TreeExpansionEvent e)265throws ExpandVetoException {266TreePath path = e.getPath();267if (!tree.hasBeenExpanded(path)) {268DefaultMutableTreeNode node =269(DefaultMutableTreeNode) path.getLastPathComponent();270if (sheet.isMBeanNode(node) && !tree.hasMetadataNodes(node)) {271tree.addMetadataNodes(node);272}273}274}275276/* tree will expand listener: treeWillCollapse */277public void treeWillCollapse(TreeExpansionEvent e)278throws ExpandVetoException {279}280}281282283