X

Geertjan's Blog

  • February 29, 2008

Getting Started Extending VisualVM (Part 4)

Geertjan Wielenga
Product Manager
Update on 28 May 2008: The code and steps in this blog entry have been updated to reflect the VisualVM 1.0 APIs. If you want to download the complete sources of the sample discussed here, get the VisualVM Sample Collection from the Plugin Portal.

In part 2 of this series, I talked about extending the Overview tab in VisualVM. At the time, I didn't realize that you can also extend the Monitor tab and the Threads tab. In each case, when you initialize the plugin that provides the subtab (i.e., the tab within the Overview tab, Monitor tab, or Threads tab), you need to use either ApplicationViewsSupport.sharedInstance().getOverviewView(), ApplicationViewsSupport.sharedInstance().getMonitorView(), or ApplicationViewsSupport.sharedInstance().getThreadsView(), like this, for example:

public static void initialize() {
ApplicationViewsSupport.sharedInstance().getOverviewView().
registerPluginProvider(new AnagramViewPluginProvider());
}

Call the above from the ModuleInstall class and then your subtab will end up in the tab you specified (i.e., either "Overview", "Monitor", or "Threads"), as is shown here for "MonitorViewSupport", where I added a "User Interface" tab to the bottom right (the tab simply contains a screenshot of the ui of the application in question):

That's nice, because someone can create a whole separate module that does nothing other than provide a subtab:

However, what's even more interesting than that (much more interesting, in fact) is that you can make your own tabs extendable. So, while "Overview", "Monitor", and "Threads" are provided by VisualVM, you can (as explained at the start of this series) provide your own tab. The cool thing I discovered today is that YOU CAN CREATE YOUR TAB IN SUCH A WAY THAT OTHERS CAN EXTEND IT. Sorry, I never use uppercase, but here I think it is warranted. That same line of code above is rewritten to the following, in the plugin shown in the screenshot above:

public static void initialize() {
DemoViewSupport.getInstance().getApplicationPluggableView().
registerPluginProvider(new DemoViewPluginProvider());
}

Where does "DemoViewSupport" come from? Look in the sources of VisualVM and you will NOT find it. I created a module called "DemoPluggableView", which looks like this:

The two files you see highlighted are the two classes I needed to add in order to make the tab defined by the provider and datasource view extendable. The DemoViewSupport is defined as follows:

package org.visualvm.demopluggableview;
import com.sun.tools.visualvm.application.Application;
import com.sun.tools.visualvm.core.ui.DataSourceViewsManager;
import com.sun.tools.visualvm.core.ui.PluggableDataSourceViewProvider;
public class DemoViewSupport {
private static DemoViewSupport instance;
private DemoViewProvider DemoPluggableView = new DemoViewProvider();
public static synchronized DemoViewSupport getInstance() {
if (instance == null) {
instance = new DemoViewSupport();
}
return instance;
}
public PluggableDataSourceViewProvider getApplicationPluggableView() {
return DemoPluggableView;
}
public DemoViewSupport() {
DataSourceViewsManager.sharedInstance().addViewProvider(DemoPluggableView, Application.class);
}
}

And this is the DemoPluggableView:

package org.visualvm.demopluggableview;
import com.sun.tools.visualvm.application.Application;
import com.sun.tools.visualvm.core.ui.DataSourceView;
import com.sun.tools.visualvm.core.ui.DataSourceViewProvider;
import com.sun.tools.visualvm.core.ui.DataSourceViewsManager;
import com.sun.tools.visualvm.core.ui.PluggableDataSourceViewProvider;
import java.util.Set;
public class DemoPluggableView extends PluggableDataSourceViewProvider<Application> {
private static DataSourceViewProvider<Application> instance = new DemoPluggableView();
@Override
public boolean supportsViewFor(Application application) {
//Always shown:
return true;
}
@Override
protected DataSourceView createView(Application application) {
return new DemoDataSourceView(application);
}
static void initialize() {
DataSourceViewsManager.sharedInstance().addViewProvider(instance, Application.class);
}
static void unregister() {
DataSourceViewsManager.sharedInstance().removeViewProvider(instance);
}
@Override
public Set<Integer> getPluggableLocations(DataSourceView arg0) {
return ALL_LOCATIONS;
}
}

Update on 28 May 2008: The code and steps in this blog entry have been updated to reflect the VisualVM 1.0 APIs. If you want to download the complete sources of the sample discussed here, get the VisualVM Sample Collection from the Plugin Portal.

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.