KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > transport > servlet > web > ServerInvokerServlet


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.remoting.transport.servlet.web;
8
9 import java.io.IOException JavaDoc;
10 import javax.management.MBeanServer JavaDoc;
11 import javax.management.MBeanServerInvocationHandler JavaDoc;
12 import javax.management.MalformedObjectNameException JavaDoc;
13 import javax.management.ObjectName JavaDoc;
14 import javax.servlet.ServletConfig JavaDoc;
15 import javax.servlet.ServletException JavaDoc;
16 import javax.servlet.http.HttpServlet JavaDoc;
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.http.HttpServletResponse JavaDoc;
19 import org.jboss.logging.Logger;
20 import org.jboss.mx.util.MBeanServerLocator;
21 import org.jboss.remoting.transport.servlet.ServletServerInvokerMBean;
22
23 /**
24  * The servlet that receives the inital http request for the ServletServerInvoker.
25  *
26  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
27  */

28 public class ServerInvokerServlet extends HttpServlet JavaDoc
29 {
30    private static Logger log = Logger.getLogger(ServerInvokerServlet.class);
31    private ServletServerInvokerMBean servletInvoker;
32
33    /**
34     * Initializes the servlet.
35     */

36    public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc
37    {
38       super.init(config);
39       ObjectName JavaDoc localInvokerName = null;
40       try
41       {
42          // See if the servlet is bound to a particular invoker
43
String JavaDoc name = config.getInitParameter("invokerName");
44          if(name != null)
45          {
46             localInvokerName = new ObjectName JavaDoc(name);
47             log.debug("localInvokerName=" + localInvokerName);
48          }
49       }
50       catch(MalformedObjectNameException JavaDoc e)
51       {
52          throw new ServletException JavaDoc("Failed to build invokerName", e);
53       }
54
55       // Lookup the MBeanServer
56
MBeanServer JavaDoc mbeanServer = MBeanServerLocator.locateJBoss();
57       if(mbeanServer == null)
58       {
59          throw new ServletException JavaDoc("Failed to locate the MBeanServer");
60       }
61       else
62       {
63          servletInvoker = (ServletServerInvokerMBean)
64                MBeanServerInvocationHandler.newProxyInstance(mbeanServer,
65                                                              localInvokerName,
66                                                              ServletServerInvokerMBean.class,
67                                                              false);
68
69       }
70    }
71
72    /**
73     * Destroys the servlet.
74     */

75    public void destroy()
76    {
77
78    }
79
80    /**
81     * Read a MarshalledInvocation and dispatch it to the target JMX object
82     * invoke(Invocation) object.
83     *
84     * @param request servlet request
85     * @param response servlet response
86     */

87    protected void processRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
88          throws ServletException JavaDoc, IOException JavaDoc
89    {
90       boolean trace = log.isTraceEnabled();
91       if(trace)
92       {
93          log.trace("processRequest, ContentLength: " + request.getContentLength());
94          log.trace("processRequest, ContentType: " + request.getContentType());
95       }
96
97       servletInvoker.processRequest(request, response);
98
99    }
100
101    /**
102     * Handles the HTTP <code>GET</code> method.
103     *
104     * @param request servlet request
105     * @param response servlet response
106     */

107    protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
108          throws ServletException JavaDoc, IOException JavaDoc
109    {
110       processRequest(request, response);
111    }
112
113    /**
114     * Handles the HTTP <code>POST</code> method.
115     *
116     * @param request servlet request
117     * @param response servlet response
118     */

119    protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
120          throws ServletException JavaDoc, IOException JavaDoc
121    {
122       processRequest(request, response);
123    }
124
125    /**
126     * Returns a short description of the servlet.
127     */

128    public String JavaDoc getServletInfo()
129    {
130       return "Servlet front to JBossRemoting servlet server invoker.";
131    }
132
133 }
Popular Tags