FileDocCategorySizeDatePackage
InvokerServlet.javaAPI DocJBoss 4.2.16398Fri Jul 13 21:02:16 BST 2007org.jboss.console.remote

InvokerServlet

public class InvokerServlet extends HttpServlet
This servlet accepts a post containing a MarshalledInvocation, extracts the Invocation object, and then routes the invocation via JMX to either: 1. the MBean specified via the invokerName ini parameter 2. the MBean whose object name hash is specified by the invocation.getObjectName() value. This name's hash must have been entered into the Registry. The method signature of the invoker must be Object invoke(org.jboss.invocation.Invocation).
see
org.jboss.system.Registry
see
org.jboss.invocation.Invocation
author
Scott.Stark@jboss.org
version
$Revision: 57191 $

Fields Summary
private static Logger
log
private static String
REQUEST_CONTENT_TYPE
A serialized MarshalledInvocation
private static String
RESPONSE_CONTENT_TYPE
A serialized MarshalledValue
private MBeanServer
mbeanServer
Constructors Summary
Methods Summary
public voiddestroy()
Destroys the servlet.

      
   
protected voiddoGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Handles the HTTP GET method.

param
request servlet request
param
response servlet response

      processRequest (request, response);
   
protected voiddoPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Handles the HTTP POST method.

param
request servlet request
param
response servlet response

      processRequest (request, response);
   
public java.lang.StringgetServletInfo()
Returns a short description of the servlet.

      return "An HTTP to JMX MBeanServer servlet";
   
public voidinit(javax.servlet.ServletConfig config)
Initializes the servlet.

   
          
         
   
      super.init (config);
      
      // Lookup the MBeanServer
      mbeanServer = org.jboss.mx.util.MBeanServerLocator.locateJBoss();
      if( mbeanServer == null )
         throw new ServletException ("Failed to locate the MBeanServer");
   
protected voidprocessRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Read a MarshalledInvocation and dispatch it to the target JMX object invoke(Invocation) object.

param
request servlet request
param
response servlet response

      boolean trace = log.isTraceEnabled ();
      if( trace )
      {
         log.trace ("processRequest, ContentLength: "+request.getContentLength ());
         log.trace ("processRequest, ContentType: "+request.getContentType ());
      }
      
      try
      {
         response.setContentType (RESPONSE_CONTENT_TYPE);
         // See if the request already has the MarshalledInvocation
         Object mi = request.getAttribute ("RemoteMBeanInvocation");
         if( mi == null )
         {
            // Get the invocation from the post
            javax.servlet.ServletInputStream sis = request.getInputStream ();
            java.io.ObjectInputStream ois = new java.io.ObjectInputStream (sis);
            mi = ois.readObject ();
            ois.close ();
         }
         
         // Forward the invocation onto the JMX bus
         Object value = null;
         if (mi instanceof RemoteMBeanInvocation)
         {
            RemoteMBeanInvocation invocation = (RemoteMBeanInvocation)mi;
            value = mbeanServer.invoke (invocation.targetObjectName, invocation.actionName, invocation.params, invocation.signature);
         }
         else
         {
            RemoteMBeanAttributeInvocation invocation = (RemoteMBeanAttributeInvocation)mi;
            value = mbeanServer.getAttribute(invocation.targetObjectName, invocation.attributeName);
         }
         org.jboss.invocation.MarshalledValue mv = new org.jboss.invocation.MarshalledValue (value);
         javax.servlet.ServletOutputStream sos = response.getOutputStream ();
         java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream (sos);
         oos.writeObject (mv);
         oos.close ();
      }
      catch(Throwable t)
      {
         t = org.jboss.mx.util.JMXExceptionDecoder.decode (t);
         org.jboss.invocation.InvocationException appException = new org.jboss.invocation.InvocationException (t);
         log.debug ("Invoke threw exception", t);
         // Marshall the exception
         response.resetBuffer ();
         org.jboss.invocation.MarshalledValue mv = new org.jboss.invocation.MarshalledValue (appException);
         javax.servlet.ServletOutputStream sos = response.getOutputStream ();
         java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream (sos);
         oos.writeObject (mv);
         oos.close ();
      }