jdk/src/share/demo/management/JTop/JTopPlugin.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  *
       
     4  * Redistribution and use in source and binary forms, with or without
       
     5  * modification, are permitted provided that the following conditions
       
     6  * are met:
       
     7  *
       
     8  *   - Redistributions of source code must retain the above copyright
       
     9  *     notice, this list of conditions and the following disclaimer.
       
    10  *
       
    11  *   - Redistributions in binary form must reproduce the above copyright
       
    12  *     notice, this list of conditions and the following disclaimer in the
       
    13  *     documentation and/or other materials provided with the distribution.
       
    14  *
       
    15  *   - Neither the name of Sun Microsystems nor the names of its
       
    16  *     contributors may be used to endorse or promote products derived
       
    17  *     from this software without specific prior written permission.
       
    18  *
       
    19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
       
    20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
       
    21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
       
    23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       
    24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
       
    26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
       
    27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
       
    28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
       
    29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    30  */
       
    31 
       
    32 /*
       
    33  *
       
    34  * Example of a JConsole Plugin.  This loads JTop as a JConsole tab.
       
    35  *
       
    36  * @author Mandy Chung
       
    37  */
       
    38 
       
    39 import java.beans.PropertyChangeEvent;
       
    40 import java.beans.PropertyChangeListener;
       
    41 import java.util.LinkedHashMap;
       
    42 import java.util.Map;
       
    43 import javax.management.MBeanServerConnection;
       
    44 import javax.swing.JPanel;
       
    45 import javax.swing.SwingWorker;
       
    46 
       
    47 import com.sun.tools.jconsole.JConsolePlugin;
       
    48 import com.sun.tools.jconsole.JConsoleContext;
       
    49 import com.sun.tools.jconsole.JConsoleContext.ConnectionState;
       
    50 
       
    51 /**
       
    52  * JTopPlugin is a subclass to com.sun.tools.jconsole.JConsolePlugin
       
    53  *
       
    54  * JTopPlugin is loaded and instantiated by JConsole.  One instance
       
    55  * is created for each window that JConsole creates. It listens to
       
    56  * the connected property change so that it will update JTop with
       
    57  * the valid MBeanServerConnection object.  JTop is a JPanel object
       
    58  * displaying the thread and its CPU usage information.
       
    59  */
       
    60 public class JTopPlugin extends JConsolePlugin implements PropertyChangeListener
       
    61 {
       
    62     private JTop jtop = null;
       
    63     private Map<String, JPanel> tabs = null;
       
    64 
       
    65     public JTopPlugin() {
       
    66         // register itself as a listener
       
    67         addContextPropertyChangeListener(this);
       
    68     }
       
    69 
       
    70     /*
       
    71      * Returns a JTop tab to be added in JConsole.
       
    72      */
       
    73     public synchronized Map<String, JPanel> getTabs() {
       
    74         if (tabs == null) {
       
    75             jtop = new JTop();
       
    76             jtop.setMBeanServerConnection(
       
    77                 getContext().getMBeanServerConnection());
       
    78             // use LinkedHashMap if you want a predictable order
       
    79             // of the tabs to be added in JConsole
       
    80             tabs = new LinkedHashMap<String, JPanel>();
       
    81             tabs.put("JTop", jtop);
       
    82         }
       
    83         return tabs;
       
    84     }
       
    85 
       
    86     /*
       
    87      * Returns a SwingWorker which is responsible for updating the JTop tab.
       
    88      */
       
    89     public SwingWorker<?,?> newSwingWorker() {
       
    90         return jtop.newSwingWorker();
       
    91     }
       
    92 
       
    93     // You can implement the dispose() method if you need to release
       
    94     // any resource when the plugin instance is disposed when the JConsole
       
    95     // window is closed.
       
    96     //
       
    97     // public void dispose() {
       
    98     // }
       
    99 
       
   100     /*
       
   101      * Property listener to reset the MBeanServerConnection
       
   102      * at reconnection time.
       
   103      */
       
   104     public void propertyChange(PropertyChangeEvent ev) {
       
   105         String prop = ev.getPropertyName();
       
   106         if (prop == JConsoleContext.CONNECTION_STATE_PROPERTY) {
       
   107             ConnectionState oldState = (ConnectionState)ev.getOldValue();
       
   108             ConnectionState newState = (ConnectionState)ev.getNewValue();
       
   109             // JConsole supports disconnection and reconnection
       
   110             // The MBeanServerConnection will become invalid when
       
   111             // disconnected. Need to use the new MBeanServerConnection object
       
   112             // created at reconnection time.
       
   113             if (newState == ConnectionState.CONNECTED && jtop != null) {
       
   114                 jtop.setMBeanServerConnection(
       
   115                     getContext().getMBeanServerConnection());
       
   116             }
       
   117         }
       
   118     }
       
   119 }