Path: blob/master/src/jdk.jconsole/share/classes/com/sun/tools/jconsole/JConsolePlugin.java
40948 views
/*1* Copyright (c) 2006, 2013, 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 com.sun.tools.jconsole;2627import java.beans.PropertyChangeEvent;28import java.beans.PropertyChangeListener;29import java.util.ArrayList;30import java.util.List;31import javax.swing.JPanel;32import javax.swing.SwingWorker;3334/**35* A JConsole plugin class. JConsole uses the36* {@link java.util.ServiceLoader service provider}37* mechanism to search the JConsole plugins.38* Users can provide their JConsole plugins in a jar file39* containing a file named40*41* <blockquote><pre>42* META-INF/services/com.sun.tools.jconsole.JConsolePlugin</pre></blockquote>43*44* <p> This file contains one line for each plugin, for example,45*46* <blockquote><pre>47* com.sun.example.JTop</pre></blockquote>48* <p> which is the fully qualified class name of the class implementing49* {@code JConsolePlugin}.50*51* <p> To load the JConsole plugins in JConsole, run:52*53* <blockquote><pre>54* jconsole -pluginpath <plugin-path> </pre></blockquote>55*56* <p> where {@code <plugin-path>} specifies the paths of JConsole57* plugins to look up which can be a directory or a jar file. Multiple58* paths are separated by the path separator character of the platform.59*60* <p> When a new JConsole window is created for a connection,61* an instance of each {@code JConsolePlugin} will be created.62* The {@code JConsoleContext} object is not available at its63* construction time.64* JConsole will set the {@link JConsoleContext} object for65* a plugin after the plugin object is created. It will then66* call its {@link #getTabs getTabs} method and add the returned67* tabs to the JConsole window.68*69* @see java.util.ServiceLoader70*71* @since 1.672*/73public abstract class JConsolePlugin {74private volatile JConsoleContext context = null;75private List<PropertyChangeListener> listeners = null;7677/**78* Constructor.79*/80protected JConsolePlugin() {81}8283/**84* Sets the {@link JConsoleContext JConsoleContext} object representing85* the connection to an application. This method will be called86* only once after the plugin is created and before the {@link #getTabs}87* is called. The given {@code context} can be in any88* {@link JConsoleContext#getConnectionState connection state} when89* this method is called.90*91* @param context a {@code JConsoleContext} object92*/93public final synchronized void setContext(JConsoleContext context) {94this.context = context;95if (listeners != null) {96for (PropertyChangeListener l : listeners) {97context.addPropertyChangeListener(l);98}99// throw away the listener list100listeners = null;101}102}103104/**105* Returns the {@link JConsoleContext JConsoleContext} object representing106* the connection to an application. This method may return {@code null}107* if it is called before the {@link #setContext context} is initialized.108*109* @return the {@link JConsoleContext JConsoleContext} object representing110* the connection to an application.111*/112public final JConsoleContext getContext() {113return context;114}115116/**117* Returns the tabs to be added in JConsole window.118* <p>119* The returned map contains one entry for each tab120* to be added in the tabbed pane in a JConsole window with121* the tab name as the key122* and the {@link JPanel} object as the value.123* This method returns an empty map if no tab is added by this plugin.124* This method will be called from the <i>Event Dispatch Thread</i>125* once at the new connection time.126*127* @return a map of a tab name and a {@link JPanel} object128* representing the tabs to be added in the JConsole window;129* or an empty map.130*/131public abstract java.util.Map<String, JPanel> getTabs();132133/**134* Returns a {@link SwingWorker} to perform135* the GUI update for this plugin at the same interval136* as JConsole updates the GUI.137* <p>138* JConsole schedules the GUI update at an interval specified139* for a connection. This method will be called at every140* update to obtain a {@code SwingWorker} for each plugin.141* <p>142* JConsole will invoke the {@link SwingWorker#execute execute()}143* method to schedule the returned {@code SwingWorker} for execution144* if:145* <ul>146* <li> the {@code SwingWorker} object has not been executed147* (i.e. the {@link SwingWorker#getState} method148* returns {@link javax.swing.SwingWorker.StateValue#PENDING PENDING}149* state); and</li>150* <li> the {@code SwingWorker} object returned in the previous151* update has completed the task if it was not {@code null}152* (i.e. the {@link SwingWorker#isDone SwingWorker.isDone} method153* returns {@code true}).</li>154* </ul>155* <br>156* Otherwise, {@code SwingWorker} object will not be scheduled to work.157*158* <p>159* A plugin can schedule its own GUI update and this method160* will return {@code null}.161*162* @return a {@code SwingWorker} to perform the GUI update; or163* {@code null}.164*/165public abstract SwingWorker<?,?> newSwingWorker();166167/**168* Dispose this plugin. This method is called by JConsole to inform169* that this plugin will be discarded and that it should free170* any resources that it has allocated.171* The {@link #getContext JConsoleContext} can be in any172* {@link JConsoleContext#getConnectionState connection state} when173* this method is called.174*/175public void dispose() {176// Default nop implementation177}178179/**180* Adds a {@link PropertyChangeListener PropertyChangeListener}181* to the {@link #getContext JConsoleContext} object for this plugin.182* This method is a convenient method for this plugin to register183* a listener when the {@code JConsoleContext} object may or184* may not be available.185*186* <p>For example, a plugin constructor can187* call this method to register a listener to listen to the188* {@link JConsoleContext.ConnectionState connectionState}189* property changes and the listener will be added to the190* {@link JConsoleContext#addPropertyChangeListener JConsoleContext}191* object when it is available.192*193* @param listener The {@code PropertyChangeListener} to be added194*195* @throws NullPointerException if {@code listener} is {@code null}.196*/197public final void addContextPropertyChangeListener(PropertyChangeListener listener) {198if (listener == null) {199throw new NullPointerException("listener is null");200}201202if (context == null) {203// defer registration of the listener until setContext() is called204synchronized (this) {205// check again if context is not set206if (context == null) {207// maintain a listener list to be added later208if (listeners == null) {209listeners = new ArrayList<PropertyChangeListener>();210}211listeners.add(listener);212return;213}214}215}216context.addPropertyChangeListener(listener);217}218219/**220* Removes a {@link PropertyChangeListener PropertyChangeListener}221* from the listener list of the {@link #getContext JConsoleContext}222* object for this plugin.223* If {@code listener} was never added, no exception is224* thrown and no action is taken.225*226* @param listener the {@code PropertyChangeListener} to be removed227*228* @throws NullPointerException if {@code listener} is {@code null}.229*/230public final void removeContextPropertyChangeListener(PropertyChangeListener listener) {231if (listener == null) {232throw new NullPointerException("listener is null");233}234235if (context == null) {236// defer registration of the listener until setContext() is called237synchronized (this) {238// check again if context is not set239if (context == null) {240if (listeners != null) {241listeners.remove(listener);242}243return;244}245}246}247context.removePropertyChangeListener(listener);248}249}250251252