Download NetBeans!

20090427 Monday April 27, 2009

Letting VisualVM Distinguish Between NetBeans Platform Applications

A typical factory for the com.sun.tools.visualvm.application.type.ApplicationType class specifies that the aforementioned class should only be created for an application with a specific main class. So, for example, if your application's main class is "com.toy.anagrams.ui.Anagrams", then the factory will extend MainClassApplicationTypeFactory and it will have a method override like this:

@Override
public ApplicationType createApplicationTypeFor(Application app, Jvm jvm, String mainClass) {
    if ("com.toy.anagrams.ui.Anagrams".equals(mainClass)) {
        return new WonderfulApplicationType(app.getPid());
    }
    return null;
}

However, what about NetBeans Platform applications? Except for the extremely rare situation where you create your own main class that overrides the one provided by the runtime container, your NetBeans Platform application will always have "org.netbeans.Main" as its main class.

So, how can VisualVM distinguish between NetBeans Platform applications? Instead of extending MainClassApplicationTypeFactory, you will need to extend NetBeansApplicationTypeFactory, in one of two ways, which could also be combined, if necessary: (1) identify the application via its unique branding token, (2) identify the application via its unique cluster.

Below code is shown for distinguishing via the unique branding name, which here is "wonderful", defined in the application's project.properties file, via the branding.token key:

public class WonderfulApplicationTypeProvider extends NetBeansApplicationTypeFactory {

    private static final String WONDERFUL_ID = "wonderful"; // NOI18N

    @Override
    public ApplicationType createApplicationTypeFor(Application app, Jvm jvm, String mainClass) {
        String branding = getBranding(jvm);
        if (WONDERFUL_ID.equals(branding)) {
            return new WonderfulApplicationType(app.getPid());
        }
        return null;
    }
    
}

The result, as you can see from the highlighted part below, is that even though the main class of my Wonderful application is the same as all other NetBeans Platform applications, the icon and display name of the node in VisualVM is distinct, which in this case is thanks to the branding token:

For details on the other approach, i.e., using the application's unique cluster, look at the sources of com.sun.tools.visualvm.application.type.NetBeansApplicationTypeFactory.

In other news. Blogs.sun.com is 5 years old today! Hurray and thanks for all the work done by the people behind it.

Apr 27 2009, 09:59:45 AM PDT Permalink

Trackback URL: http://blogs.sun.com/geertjan/entry/letting_visualvm_distinguish_between_netbeans
Comments:

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed