KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > console > remote > InvokerServlet


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.console.remote;
23
24 import javax.servlet.ServletException JavaDoc;
25
26
27 /** This servlet accepts a post containing a MarshalledInvocation, extracts
28  * the Invocation object, and then routes the invocation via JMX to either:
29  * 1. the MBean specified via the invokerName ini parameter
30  * 2. the MBean whose object name hash is specified by the invocation.getObjectName()
31  * value. This name's hash must have been entered into the Registry.
32  *
33  * The method signature of the invoker must be Object invoke(org.jboss.invocation.Invocation).
34  *
35  * @see org.jboss.system.Registry
36  * @see org.jboss.invocation.Invocation
37  *
38  * @author Scott.Stark@jboss.org
39  * @version $Revision: 37459 $
40  */

41 public class InvokerServlet extends javax.servlet.http.HttpServlet JavaDoc
42 {
43    private static org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger (InvokerServlet.class);
44    /** A serialized MarshalledInvocation */
45    private static String JavaDoc REQUEST_CONTENT_TYPE =
46    "application/x-java-serialized-object; class=org.jboss.console.remote.RemoteMBeanInvocation";
47    /** A serialized MarshalledValue */
48    private static String JavaDoc RESPONSE_CONTENT_TYPE =
49    "application/x-java-serialized-object; class=org.jboss.invocation.MarshalledValue";
50    private javax.management.MBeanServer JavaDoc mbeanServer;
51    
52    /** Initializes the servlet.
53     */

54    public void init (javax.servlet.ServletConfig JavaDoc config) throws ServletException JavaDoc
55    {
56       super.init (config);
57       
58       // Lookup the MBeanServer
59
mbeanServer = org.jboss.mx.util.MBeanServerLocator.locateJBoss();
60       if( mbeanServer == null )
61          throw new ServletException JavaDoc ("Failed to locate the MBeanServer");
62    }
63    
64    /** Destroys the servlet.
65     */

66    public void destroy ()
67    {
68       
69    }
70    
71    /** Read a MarshalledInvocation and dispatch it to the target JMX object
72     * invoke(Invocation) object.
73     *
74     * @param request servlet request
75     * @param response servlet response
76     */

77    protected void processRequest (javax.servlet.http.HttpServletRequest JavaDoc request, javax.servlet.http.HttpServletResponse JavaDoc response) throws ServletException JavaDoc, java.io.IOException JavaDoc
78    {
79       boolean trace = log.isTraceEnabled ();
80       if( trace )
81       {
82          log.trace ("processRequest, ContentLength: "+request.getContentLength ());
83          log.trace ("processRequest, ContentType: "+request.getContentType ());
84       }
85       
86       try
87       {
88          response.setContentType (RESPONSE_CONTENT_TYPE);
89          // See if the request already has the MarshalledInvocation
90
Object JavaDoc mi = request.getAttribute ("RemoteMBeanInvocation");
91          if( mi == null )
92          {
93             // Get the invocation from the post
94
javax.servlet.ServletInputStream JavaDoc sis = request.getInputStream ();
95             java.io.ObjectInputStream JavaDoc ois = new java.io.ObjectInputStream JavaDoc (sis);
96             mi = ois.readObject ();
97             ois.close ();
98          }
99          
100          // Forward the invocation onto the JMX bus
101
Object JavaDoc value = null;
102          if (mi instanceof RemoteMBeanInvocation)
103          {
104             RemoteMBeanInvocation invocation = (RemoteMBeanInvocation)mi;
105             value = mbeanServer.invoke (invocation.targetObjectName, invocation.actionName, invocation.params, invocation.signature);
106          }
107          else
108          {
109             RemoteMBeanAttributeInvocation invocation = (RemoteMBeanAttributeInvocation)mi;
110             value = mbeanServer.getAttribute(invocation.targetObjectName, invocation.attributeName);
111          }
112          org.jboss.invocation.MarshalledValue mv = new org.jboss.invocation.MarshalledValue (value);
113          javax.servlet.ServletOutputStream JavaDoc sos = response.getOutputStream ();
114          java.io.ObjectOutputStream JavaDoc oos = new java.io.ObjectOutputStream JavaDoc (sos);
115          oos.writeObject (mv);
116          oos.close ();
117       }
118       catch(Throwable JavaDoc t)
119       {
120          t = org.jboss.mx.util.JMXExceptionDecoder.decode (t);
121          org.jboss.invocation.InvocationException appException = new org.jboss.invocation.InvocationException (t);
122          log.debug ("Invoke threw exception", t);
123          // Marshall the exception
124
response.resetBuffer ();
125          org.jboss.invocation.MarshalledValue mv = new org.jboss.invocation.MarshalledValue (appException);
126          javax.servlet.ServletOutputStream JavaDoc sos = response.getOutputStream ();
127          java.io.ObjectOutputStream JavaDoc oos = new java.io.ObjectOutputStream JavaDoc (sos);
128          oos.writeObject (mv);
129          oos.close ();
130       }
131    }
132    
133    /** Handles the HTTP <code>GET</code> method.
134     * @param request servlet request
135     * @param response servlet response
136     */

137    protected void doGet (javax.servlet.http.HttpServletRequest JavaDoc request, javax.servlet.http.HttpServletResponse JavaDoc response) throws ServletException JavaDoc, java.io.IOException JavaDoc
138    {
139       processRequest (request, response);
140    }
141    
142    /** Handles the HTTP <code>POST</code> method.
143     * @param request servlet request
144     * @param response servlet response
145     */

146    protected void doPost (javax.servlet.http.HttpServletRequest JavaDoc request, javax.servlet.http.HttpServletResponse JavaDoc response) throws ServletException JavaDoc, java.io.IOException JavaDoc
147    {
148       processRequest (request, response);
149    }
150    
151    /** Returns a short description of the servlet.
152     */

153    public String JavaDoc getServletInfo ()
154    {
155       return "An HTTP to JMX MBeanServer servlet";
156    }
157    
158 }
159
Popular Tags