001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.invocation.http.servlet;
023:
024: import java.io.IOException;
025: import java.io.ObjectOutputStream;
026: import javax.servlet.ServletConfig;
027: import javax.servlet.ServletException;
028: import javax.servlet.ServletOutputStream;
029: import javax.servlet.http.HttpServlet;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032: import javax.management.ObjectName;
033: import javax.management.MalformedObjectNameException;
034: import javax.management.MBeanServer;
035:
036: import org.jboss.invocation.MarshalledValue;
037: import org.jboss.logging.Logger;
038: import org.jboss.mx.util.MBeanServerLocator;
039:
040: /** Create a Naming interface proxy that uses HTTP to communicate with the
041: * JBoss JNDI naming service. Any request to this servlet receives a
042: * serialized object stream containing a MarshalledValue with the Naming proxy
043: * as its content. The proxy is obtained from the MBean named by the
044: * namingProxyMBean init-param.
045: *
046: * @author Scott.Stark@jboss.org
047: * @version $Revision: 57210 $
048: */
049: public class NamingFactoryServlet extends HttpServlet {
050: /** A serialized MarshalledValue */
051: private static String RESPONSE_CONTENT_TYPE = "application/x-java-serialized-object; class=org.jboss.invocation.MarshalledValue";
052: private Logger log;
053:
054: /** The Naming proxy instance obtained from the MBean */
055: private Object namingProxy;
056: /** The JMX ObjectName that provides the Naming proxy for the servlet */
057: private ObjectName namingProxyMBean;
058: /** The name of the attribute of namingProxyMBean used to obtain the proxy */
059: private String proxyAttribute;
060:
061: /** Initializes the servlet.
062: */
063: public void init(ServletConfig config) throws ServletException {
064: super .init(config);
065: String category = getClass().getName() + '.'
066: + config.getServletName();
067: log = Logger.getLogger(category);
068:
069: // Get the name of the MBean that provides the Naming proxy
070: String name = config.getInitParameter("namingProxyMBean");
071: if (name == null)
072: throw new ServletException(
073: "An namingProxyMBean must be specified");
074: proxyAttribute = config.getInitParameter("proxyAttribute");
075: if (proxyAttribute == null)
076: proxyAttribute = "Proxy";
077:
078: try {
079: namingProxyMBean = new ObjectName(name);
080: } catch (MalformedObjectNameException e) {
081: throw new ServletException("Failed to create object name: "
082: + name, e);
083: }
084: }
085:
086: /** Destroys the servlet.
087: */
088: public void destroy() {
089: }
090:
091: /** Returns a short description of the servlet.
092: */
093: public String getServletInfo() {
094: return "A factory servlet for Naming proxies";
095: }
096:
097: /** Return a Naming service proxy for any GET/POST made against this servlet
098: * @param response servlet response
099: */
100: protected void processRequest(HttpServletRequest request,
101: HttpServletResponse response) throws ServletException,
102: IOException {
103: boolean trace = log.isTraceEnabled();
104: if (trace)
105: log.trace("processRequest");
106: // Lazy load of the proxy
107: lookupNamingProxy();
108: try {
109: response.setContentType(RESPONSE_CONTENT_TYPE);
110: MarshalledValue mv = new MarshalledValue(namingProxy);
111: if (trace)
112: log.trace("Serialized Naming proxy, size=" + mv.size());
113: //response.setContentLength(mv.size());
114: ServletOutputStream sos = response.getOutputStream();
115: ObjectOutputStream oos = new ObjectOutputStream(sos);
116: oos.writeObject(mv);
117: oos.flush();
118: oos.close();
119: } catch (Throwable t) {
120: log.debug("Invoke failed", t);
121: // Marshall the exception
122: response.resetBuffer();
123: MarshalledValue mv = new MarshalledValue(t);
124: ServletOutputStream sos = response.getOutputStream();
125: ObjectOutputStream oos = new ObjectOutputStream(sos);
126: oos.writeObject(mv);
127: oos.close();
128: }
129:
130: }
131:
132: /** Handles the HTTP <code>GET</code> method.
133: * @param request servlet request
134: * @param response servlet response
135: */
136: protected void doGet(HttpServletRequest request,
137: HttpServletResponse response) throws ServletException,
138: IOException {
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(HttpServletRequest request,
147: HttpServletResponse response) throws ServletException,
148: IOException {
149: processRequest(request, response);
150: }
151:
152: /** If the namingProxy has not been loaded, query the namingProxyMBean for
153: * its proxyAttribute.
154: * @throws ServletException
155: */
156: private synchronized void lookupNamingProxy()
157: throws ServletException {
158: if (namingProxy != null)
159: return;
160:
161: MBeanServer mbeanServer = MBeanServerLocator.locateJBoss();
162: try {
163: namingProxy = mbeanServer.getAttribute(namingProxyMBean,
164: proxyAttribute);
165: } catch (Exception e) {
166: String msg = "Failed to obtain proxy from: "
167: + namingProxyMBean + " via attribute:"
168: + proxyAttribute;
169: log.debug(msg, e);
170: throw new ServletException(msg, e);
171: }
172:
173: }
174: }
|