The XMOJO Project
<< Prev Chapter 4.1 HTML Adaptor Next >>

HTML Adaptor


HTML Adaptor enables application clients and managers to connect to a JMX agent through HTTP. One of the main advantages for having an HTML adaptor is that there is no need to write a separate client for communicating with the HTML adaptor.

 Communication Mechanism 

The Web browser sends a request to the Web server which will be running at port XXXX. The request will generally be like

http://hostname:htmlPort/jmx_dynamic?fname=index.html

On receiving this HTTP request, the Web server forwards the request to the appropriate Servlet (that is, jmx_dynamic). The Servlet with the help of HTML Adaptor forms the response. The HTML Adaptor is capable of communicating with the MBeanServer and hence the MBean information can be obtained. The response is sent back to the Web browser via Web server. The Web browser displays the received response object in a human readable format.

The image representation of the Communication Mechanism using HTML Adaptor is shown below:

HTML_Adaptor

 HTML adaptor as an MBean 

The HTML adaptor itself is implemented as a Dynamic MBean. It exposes four attributes and two operations. The attributes are Port, State, Active, and ObjectName. Here Port is a read write attribute and the rest three are read only attributes. The Operations are startService and stopService. Since HTML adaptor itself is implemented as an MBean, it can be managed like other MBeans.

 Registering HTML Adaptor 

Registering HTML Adaptor requires only a few lines of code. The following code snippet shows how to register an HTML Adaptor instance with the MBeanServer:

import com.adventnet.adaptors.html.HtmlAdaptor;
import com.adventnet.adaptors.html.HtmlAdaptorServerImpl;
import javax.management.ObjectName;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;

MBeanServer mbs = null;
ObjectName name = null;
private int htmlPort = 8030;

private void registerHTMLAdaptor()
{

try
{
  // create an instance of MBeanServer
  mbs = MBeanServerFactory.createMBeanServer();

  // create a new object name
  name = new ObjectName("Adaptors:type=HTMLAdaptor,port=" + htmlPort);

  HtmlAdaptor htmladaptor = new HtmlAdaptor();
  htmladaptor.addHttpServerInterface(new HtmlAdaptorServerImpl());
  mbs.registerMBean(htmladaptor, name);
  htmladaptor.setParentDir(".");

}
catch (Exception e)
{
  e.printStackTrace();
}

}

 How HTML Adaptor Exposes Notifications ? 

The notifications broadcasted by the MBeans registered with the MBeanServer are available to HTMLAdaptor since HTMLAdaptor is a notification listener to all the registered MBeans. The HTMLAdaptor stores the received notifications in a serialized form and the fileName for storing the notifications can be configured.

When the HTMLAdaptor receives a request for exposing notifications, it reads the serialized notifications from the file and sends them back in human-readable format.

 Plugging in Your Own Web Server 

The sample HTML Adaptor uses Jetty 3.2.4 Web server by default. You can plug in any other Web server instead of the Jetty server. This facility of plugging in a Web server has been enabled by providing the HttpServerInterface. All you have to do to plug in your own Web server is to perform the two steps given below:
  1. Implement the HttpServerInterface
  2. Add your Web server

 Implementing the HttpServerInterface 

The interface defines four methods that are mentioned below, which should be implemented by your class.

public void startHttpServer() throws java.lang.Exception
This method is to start the Web server initially.

public void stopHttpServer() throws java.lang.Exception
This method is to stop the Web server.

public void restartHttpServer() throws java.lang.Exception
This method is to restart the Web server.

public void setConfigFileName(String configFileName)
This method is a setter for the configuration file name.

public String getConfigFileName()
This method gets the configuration file name by which the server is running.

 Adding Your Web Server 

Custom Web server can be plugged in as described below, so that the HTML Adaptor can use that.

Before registering the HTML Adaptor, the instance of the class which implements the HttpServerInterface and wraps your Web server should be added. For example, if XyzServerImpl is the implementation and wrapper for "Xyz web server" then it can be added like following:

htmladaptor.addHttpServerInterface(new XyzServerImpl());

 How to Test HTML Adaptor ? 

The HTML adaptor can be tested by starting the agent with HTML adaptor running. Open any Web browser and type the URL "http://<<server_name>>:<<port>>". Here, server_name is the machine name where the HTML Adaptor is running and port is the Port number at which the HTML Adaptor is listening. This brings up the HTML front end which can be used to manage the application via JMX.

<< Prev Home Next >>
Adaptors Level
RMI Adaptor