KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JTopPlugin


1 /*
2  * @(#)JTopPlugin.java 1.2 06/05/08
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)JTopPlugin.java 1.2 06/05/08
39  *
40  * Example of a JConsole Plugin. This loads JTop as a JConsole tab.
41  *
42  * @author Mandy Chung
43  */

44
45 import java.beans.PropertyChangeEvent JavaDoc;
46 import java.beans.PropertyChangeListener JavaDoc;
47 import java.util.LinkedHashMap JavaDoc;
48 import java.util.Map JavaDoc;
49 import javax.management.MBeanServerConnection JavaDoc;
50 import javax.swing.JPanel JavaDoc;
51 import javax.swing.SwingWorker JavaDoc;
52
53 import com.sun.tools.jconsole.JConsolePlugin;
54 import com.sun.tools.jconsole.JConsoleContext;
55 import com.sun.tools.jconsole.JConsoleContext.ConnectionState;
56
57 /**
58  * JTopPlugin is a subclass to com.sun.tools.jconsole.JConsolePlugin
59  *
60  * JTopPlugin is loaded and instantiated by JConsole. One instance
61  * is created for each window that JConsole creates. It listens to
62  * the connected property change so that it will update JTop with
63  * the valid MBeanServerConnection object. JTop is a JPanel object
64  * displaying the thread and its CPU usage information.
65  */

66 public class JTopPlugin extends JConsolePlugin implements PropertyChangeListener JavaDoc
67 {
68     private JTop jtop = null;
69     private Map JavaDoc<String JavaDoc, JPanel JavaDoc> tabs = null;
70
71     public JTopPlugin() {
72         // register itself as a listener
73
addContextPropertyChangeListener(this);
74     }
75
76     /*
77      * Returns a JTop tab to be added in JConsole.
78      */

79     public synchronized Map JavaDoc<String JavaDoc, JPanel JavaDoc> getTabs() {
80         if (tabs == null) {
81             jtop = new JTop();
82             jtop.setMBeanServerConnection(
83                 getContext().getMBeanServerConnection());
84             // use LinkedHashMap if you want a predictable order
85
// of the tabs to be added in JConsole
86
tabs = new LinkedHashMap JavaDoc<String JavaDoc, JPanel JavaDoc>();
87             tabs.put("JTop", jtop);
88         }
89         return tabs;
90     }
91
92     /*
93      * Returns a SwingWorker which is responsible for updating the JTop tab.
94      */

95     public SwingWorker JavaDoc<?,?> newSwingWorker() {
96         return jtop.newSwingWorker();
97     }
98
99     // You can implement the dispose() method if you need to release
100
// any resource when the plugin instance is disposed when the JConsole
101
// window is closed.
102
//
103
// public void dispose() {
104
// }
105

106     /*
107      * Property listener to reset the MBeanServerConnection
108      * at reconnection time.
109      */

110     public void propertyChange(PropertyChangeEvent JavaDoc ev) {
111         String JavaDoc prop = ev.getPropertyName();
112         if (prop == JConsoleContext.CONNECTION_STATE_PROPERTY) {
113             ConnectionState oldState = (ConnectionState)ev.getOldValue();
114             ConnectionState newState = (ConnectionState)ev.getNewValue();
115             // JConsole supports disconnection and reconnection
116
// The MBeanServerConnection will become invalid when
117
// disconnected. Need to use the new MBeanServerConnection object
118
// created at reconnection time.
119
if (newState == ConnectionState.CONNECTED && jtop != null) {
120                 jtop.setMBeanServerConnection(
121                     getContext().getMBeanServerConnection());
122             }
123         }
124     }
125 }
126
Popular Tags