KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > invocation > http > servlet > NamingFactoryServlet


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.invocation.http.servlet;
23
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectOutputStream JavaDoc;
26 import javax.servlet.ServletConfig JavaDoc;
27 import javax.servlet.ServletException JavaDoc;
28 import javax.servlet.ServletOutputStream JavaDoc;
29 import javax.servlet.http.HttpServlet JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33 import javax.management.MalformedObjectNameException JavaDoc;
34 import javax.management.MBeanServer JavaDoc;
35
36 import org.jboss.invocation.MarshalledValue;
37 import org.jboss.logging.Logger;
38 import org.jboss.mx.util.MBeanServerLocator;
39
40 /** Create a Naming interface proxy that uses HTTP to communicate with the
41  * JBoss JNDI naming service. Any request to this servlet receives a
42  * serialized object stream containing a MarshalledValue with the Naming proxy
43  * as its content. The proxy is obtained from the MBean named by the
44  * namingProxyMBean init-param.
45  *
46  * @author Scott.Stark@jboss.org
47  * @version $Revision: 37459 $
48  */

49 public class NamingFactoryServlet extends HttpServlet JavaDoc
50 {
51    /** A serialized MarshalledValue */
52    private static String JavaDoc RESPONSE_CONTENT_TYPE =
53       "application/x-java-serialized-object; class=org.jboss.invocation.MarshalledValue";
54    private Logger log;
55
56    /** The Naming proxy instance obtained from the MBean */
57    private Object JavaDoc namingProxy;
58    /** The JMX ObjectName that provides the Naming proxy for the servlet */
59    private ObjectName JavaDoc namingProxyMBean;
60    /** The name of the attribute of namingProxyMBean used to obtain the proxy */
61    private String JavaDoc proxyAttribute;
62
63    /** Initializes the servlet.
64     */

65    public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc
66    {
67       super.init(config);
68       String JavaDoc category = getClass().getName() + '.' + config.getServletName();
69       log = Logger.getLogger(category);
70
71       // Get the name of the MBean that provides the Naming proxy
72
String JavaDoc name = config.getInitParameter("namingProxyMBean");
73       if( name == null )
74          throw new ServletException JavaDoc("An namingProxyMBean must be specified");
75       proxyAttribute = config.getInitParameter("proxyAttribute");
76       if( proxyAttribute == null )
77          proxyAttribute = "Proxy";
78
79       try
80       {
81          namingProxyMBean = new ObjectName JavaDoc(name);
82       }
83       catch (MalformedObjectNameException JavaDoc e)
84       {
85          throw new ServletException JavaDoc("Failed to create object name: "+name, e);
86       }
87    }
88
89    /** Destroys the servlet.
90     */

91    public void destroy()
92    {
93    }
94
95    /** Returns a short description of the servlet.
96     */

97    public String JavaDoc getServletInfo()
98    {
99       return "A factory servlet for Naming proxies";
100    }
101
102    /** Return a Naming service proxy for any GET/POST made against this servlet
103     * @param response servlet response
104     */

105    protected void processRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
106       throws ServletException JavaDoc, IOException JavaDoc
107    {
108       boolean trace = log.isTraceEnabled();
109       if( trace )
110          log.trace("processRequest");
111       // Lazy load of the proxy
112
lookupNamingProxy();
113       try
114       {
115          response.setContentType(RESPONSE_CONTENT_TYPE);
116          MarshalledValue mv = new MarshalledValue(namingProxy);
117          if( trace )
118             log.trace("Serialized Naming proxy, size="+mv.size());
119          //response.setContentLength(mv.size());
120
ServletOutputStream JavaDoc sos = response.getOutputStream();
121          ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(sos);
122          oos.writeObject(mv);
123          oos.flush();
124          oos.close();
125       }
126       catch(Throwable JavaDoc t)
127       {
128          log.debug("Invoke failed", t);
129          // Marshall the exception
130
response.resetBuffer();
131          MarshalledValue mv = new MarshalledValue(t);
132          ServletOutputStream JavaDoc sos = response.getOutputStream();
133          ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(sos);
134          oos.writeObject(mv);
135          oos.close();
136       }
137
138    }
139
140    /** Handles the HTTP <code>GET</code> method.
141     * @param request servlet request
142     * @param response servlet response
143     */

144    protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
145       throws ServletException JavaDoc, IOException JavaDoc
146    {
147       processRequest(request, response);
148    }
149    
150    /** Handles the HTTP <code>POST</code> method.
151     * @param request servlet request
152     * @param response servlet response
153     */

154    protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
155       throws ServletException JavaDoc, IOException JavaDoc
156    {
157       processRequest(request, response);
158    }
159
160    /** If the namingProxy has not been loaded, query the namingProxyMBean for
161     * its proxyAttribute.
162     * @throws ServletException
163     */

164    private synchronized void lookupNamingProxy()
165       throws ServletException JavaDoc
166    {
167       if( namingProxy != null )
168          return;
169
170       MBeanServer JavaDoc mbeanServer = MBeanServerLocator.locateJBoss();
171       try
172       {
173          namingProxy = mbeanServer.getAttribute(namingProxyMBean, proxyAttribute);
174       }
175       catch(Exception JavaDoc e)
176       {
177          String JavaDoc msg = "Failed to obtain proxy from: "+namingProxyMBean
178             + " via attribute:" + proxyAttribute;
179          log.debug(msg, e);
180          throw new ServletException JavaDoc(msg, e);
181       }
182
183    }
184 }
185
Popular Tags