Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/demo/management/JTop/JTopPlugin.java
38829 views
/*1* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/383940/*41*42* Example of a JConsole Plugin. This loads JTop as a JConsole tab.43*44* @author Mandy Chung45*/4647import java.beans.PropertyChangeEvent;48import java.beans.PropertyChangeListener;49import java.util.LinkedHashMap;50import java.util.Map;5152import javax.swing.JPanel;53import javax.swing.SwingWorker;5455import com.sun.tools.jconsole.JConsoleContext;56import com.sun.tools.jconsole.JConsoleContext.ConnectionState;57import com.sun.tools.jconsole.JConsolePlugin;5859/**60* JTopPlugin is a subclass to com.sun.tools.jconsole.JConsolePlugin61*62* JTopPlugin is loaded and instantiated by JConsole. One instance63* is created for each window that JConsole creates. It listens to64* the connected property change so that it will update JTop with65* the valid MBeanServerConnection object. JTop is a JPanel object66* displaying the thread and its CPU usage information.67*/68public class JTopPlugin extends JConsolePlugin implements PropertyChangeListener69{70private JTop jtop = null;71private Map<String, JPanel> tabs = null;7273public JTopPlugin() {74// register itself as a listener75addContextPropertyChangeListener(this);76}7778/*79* Returns a JTop tab to be added in JConsole.80*/81@Override82public synchronized Map<String, JPanel> getTabs() {83if (tabs == null) {84jtop = new JTop();85jtop.setMBeanServerConnection(86getContext().getMBeanServerConnection());87// use LinkedHashMap if you want a predictable order88// of the tabs to be added in JConsole89tabs = new LinkedHashMap<String, JPanel>();90tabs.put("JTop", jtop);91}92return tabs;93}9495/*96* Returns a SwingWorker which is responsible for updating the JTop tab.97*/98@Override99public SwingWorker<?,?> newSwingWorker() {100return jtop.newSwingWorker();101}102103// You can implement the dispose() method if you need to release104// any resource when the plugin instance is disposed when the JConsole105// window is closed.106//107// public void dispose() {108// }109110/*111* Property listener to reset the MBeanServerConnection112* at reconnection time.113*/114@Override115public void propertyChange(PropertyChangeEvent ev) {116String prop = ev.getPropertyName();117if (prop == JConsoleContext.CONNECTION_STATE_PROPERTY) {118ConnectionState newState = (ConnectionState)ev.getNewValue();119// JConsole supports disconnection and reconnection120// The MBeanServerConnection will become invalid when121// disconnected. Need to use the new MBeanServerConnection object122// created at reconnection time.123if (newState == ConnectionState.CONNECTED && jtop != null) {124jtop.setMBeanServerConnection(125getContext().getMBeanServerConnection());126}127}128}129}130131132