Service Boss Level Part 2: Custom Web Console Plugins

By Daniel Klco Daniel Klco on 
This was also posted on: http://labs.sixdimensions.com/blog/dan-klco/2013-09-11/service-boss-level-part-2-custom-web-console-plugins, however 6D Global Technologies decided to remove my authoring credit which seems unethical.

You’ve got your awesome service.  It performs all sorts of complex tasks and maybe even uses a Sling Service Factory to provide multiple configurations.  But how do you know what it’s doing?  Well, of course you need to have appropriate logging, but another tool you can leverage is the Apache Felix Web Console, by creating a custom Web Console Plugin.  Your plugin can be accessed through Adobe CQ and Apache Sling’s Web Console and can display any information you can render in HTML.

Apache Felix provides a really simple class you can extend for creating custom Web Console screens, the AbstractWebConsolePlugin.  This class will automatically leverage the look and feel of the Apache Felix console and makes it easy for you to create your own console screen.  There are three main methods to override:

  • getLabel - This method should return the URL for the console page, ex: helloworld
  • getTitle - This method should return the text to display to the user in the Felix Console navigation and title bar
  • renderContent - This method will be executed when your Web Console page is accessed, it should write out the Web Console page content

Your WebConsole plugin class, should extend the AbstractWebConsolePlugin class and should be a standard OSGi Service.  You should extend the Servlet interface and should have properties exposing the label and title for your Web Console plugin as properties. Generally, I would recommend using constants to ensure that the method and properties use the same value.

@Component
@Service(value={Servlet.class})
@Properties({
  @Property(name=WebConsoleConstants.PLUGIN_LABEL,value=HelloWorldWebConsole.LABEL),
  @Property(name = WebConsoleConstants.PLUGIN_TITLE,value=HelloWorldWebConsole.TITLE)})
public class HelloWorldWebConsole extends AbstractWebConsolePlugin {
  [... code ...]
}

Since the Web Console plugin is an OSGi service, you can inject references to any other Service or Component.  You can simply retrieve the Service you want to monitor and retrieve the information you need from it.

When you go to to write the HTML to the page, retrieve the PrintWriter from the response, ex: res.getWriter().  The console supports easily styled tables, with styled header rows, make sure to add the CSS class ‘content’ to all cells containing content to get the correct padding.  Additionally, you can add the class ‘container’ to make a column header with a grey background.

Once your Web Console Plugin is complete, you should be able to access it from the Apache Felix Web Console.  If you are using CQ 5.6, it will be in the Main dropdown.

Web Console Plugin Screen

As you can see Custom Web Console Plugins are easy to create and can allow administrators valuable views into the functioning of OSGi Services and the health of your application.


Tags


comments powered by Disqus