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