Write a custom plugin for Sirona GUI


Apache Sirona is a brand new project in Apache Software foundation world. It targets Java applications monitoring. One awesome feature of this library is the fact the reporting GUI is easily extensible and you don’t need to write tons of code/pages to add your info in this GUI.

Here is a sample simply reporting the JVM system properties.

First you need to write a plugin (org.apache.sirona.reporting.web.plugin.Plugin). This uses Java SPI mecanism (so you need a META-INF/services/org.apache.sirona.reporting.web.plugin.Plugin file – this is mainly cause Sirona targets Servlet 2.5 containers too). This interface defines three methods:

  • the name of your plugin
  • the class with your plugin endpoints: the list of accessible sub-urls for your plugin
  • the mapping of your plugin

Basically you can see a plugin as a sub context handler of the monitoring reporting GUI.

Here is the plugin:

import org.apache.sirona.reporting.web.plugin.Plugin;

public class SystemPropertiesPlugin implements Plugin {
    @Override
    public String name() {
        return "System Properties";
    }

    @Override
    public Class<?> endpoints() {
        return SystemPropertiesEndpoint.class;
    }

    @Override
    public String mapping() {
        return "/system-properties";
    }
}

Don’t forget the META-INF/services/org.apache.sirona.reporting.web.plugin.Plugin file:

com.github.rmannibucau.demo.sirona.plugin.SystemPropertiesPlugin

Then you need to write the endpoint class. Endpoint classes are kind of very light JAXRS endpoints. It doesn’t reuse JAXRS to avoid conflicts as much as possible with applications since Sirona is embeddable (and its needs are small). It is based on the @Regex annotation (a bit like @Path for JAXRS). I’ll not detail this annotation here since that’s our “Hello Sirona” plugin but note it is possible to match parts of the url path thanks to it.

The endpoint methods (decorated with @Regex) can get the http request and response injected too. Finally Template instances can be returned. It represent a velocity template with a list of variables accessible in the template. We’ll use it to pass the list of system properties.

So here is our endpoint class:

import org.apache.sirona.reporting.web.handler.api.Regex;
import org.apache.sirona.reporting.web.handler.api.Template;
import org.apache.sirona.reporting.web.template.MapBuilder;

public class SystemPropertiesEndpoint {
    @Regex
    public Template list() {
        return new Template("system-properties.vm",
            new MapBuilder<String, Object>()
                .set("props", System.getProperties())
                .build());
    }
}

The system-properties.vm file needs to be in /templates/ directory (in the classloader) – note: this can be changed customizing the base template but this is another story.

Here a propsal for the system-properties.vm file:

<ul>
    #foreach ( $item in $props.entrySet() )
        <li>$item.key = $item.value</li>
    #end
</ul>

And here we are. To test it just add it to reporting war of sirona or create a war with it and the reporting jar of sirona. In servlet 3 container it will be deployed under /monitoring sub context automatically (in Servlet 2.5 containers just declare the MonitoringController in your web.xml).

Here is a screenshot of our page:

sirona-system-props

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s