KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > il > http > servlet > HTTPServerILServlet


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.mq.il.http.servlet;
23
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectInputStream JavaDoc;
26 import java.io.ObjectOutputStream JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29
30 import javax.management.MBeanServer JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.servlet.ServletConfig JavaDoc;
33 import javax.servlet.ServletException JavaDoc;
34 import javax.servlet.http.HttpServlet JavaDoc;
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37 import javax.transaction.xa.Xid JavaDoc;
38
39 import org.jboss.logging.Logger;
40 import org.jboss.mq.ConnectionToken;
41 import org.jboss.mq.SpyMessage;
42 import org.jboss.mq.il.http.HTTPClientILStorageQueue;
43 import org.jboss.mq.il.http.HTTPILRequest;
44 import org.jboss.mq.il.http.HTTPILResponse;
45 import org.jboss.mq.server.JMSServerInvoker;
46 import org.jboss.mx.util.MBeanServerLocator;
47
48 /**
49  * This Servlet acts as a proxy to the JMS Server for the HTTP IL clients.
50  * It receives posts from the HTTPServerIL in the form of HTTPILRequests,
51  * invokes the approprate method on the server, and returns a HTTPILResponse.
52  *
53  * This also acts as a delegate for the HTTPClientILService, by receiving
54  * POSTS, looking in HTTPILStorageQueue, retrieving HTTPRequests bound for the
55  * client, packaging them in a HTTPILResponse. These requests are generally
56  * long lived to simulate asynch messaging. When the HTTPClientILService POSTS
57  * a HTTPILRequest the payload of the request includes timeout value which represents
58  * the max time the request will last, before returning to the client.
59  *
60  * @author Nathan Phelps (nathan@jboss.org)
61  * @version $Revision: 46444 $
62  * @since January 15, 2003
63  */

64 public class HTTPServerILServlet extends HttpServlet JavaDoc
65 {
66     
67     private static final String JavaDoc RESPONSE_CONTENT_TYPE = "application/x-java-serialized-object; class=org.jboss.mq.il.http.HTTPILResponse";
68     
69     private static Logger log = Logger.getLogger(HTTPServerILServlet.class);
70     private MBeanServer JavaDoc server;
71     private JMSServerInvoker invoker;
72     
73     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc
74     {
75         super.init(config);
76         if (log.isTraceEnabled())
77         {
78             log.trace("init(ServletConfig " + config.toString() + ")");
79         }
80         this.server = MBeanServerLocator.locateJBoss();
81         if (this.server == null)
82         {
83             throw new ServletException JavaDoc("Failed to locate the MBeanServer");
84         }
85         String JavaDoc invokerName = config.getInitParameter("Invoker");
86         if (invokerName == null)
87         {
88             throw new ServletException JavaDoc("Invoker must be specified as servlet init parameter");
89         }
90         log.debug("Invoker set to '" + invokerName + ".'");
91         try
92         {
93             this.invoker = (JMSServerInvoker)server.getAttribute(new ObjectName JavaDoc(invokerName), "Invoker");
94         }
95         catch (Exception JavaDoc exception)
96         {
97             throw new ServletException JavaDoc("Failed to locate the JBossMQ invoker.");
98         }
99     }
100     
101     public void destroy()
102     {
103         if (log.isTraceEnabled())
104         {
105             log.trace("destroy()");
106         }
107     }
108     
109     protected void processRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc
110     {
111         if (log.isTraceEnabled())
112         {
113             log.trace("processRequest(HttpServletRequest " + request.toString() + ", HttpServletResponse " + response.toString() + ")");
114         }
115         response.setContentType(RESPONSE_CONTENT_TYPE);
116         ObjectOutputStream JavaDoc outputStream = new ObjectOutputStream JavaDoc(response.getOutputStream());
117         try
118         {
119             ObjectInputStream JavaDoc inputStream = new ObjectInputStream JavaDoc(request.getInputStream());
120             HTTPILRequest httpIlRequest = (HTTPILRequest)inputStream.readObject();
121             String JavaDoc methodName = httpIlRequest.getMethodName();
122             if (methodName.equals("clientListening"))
123             {
124                 if (log.isTraceEnabled())
125                 {
126                     log.trace("clientListening(HTTPILRequest " + httpIlRequest.toString() + ")");
127                 }
128                 String JavaDoc clientIlId = (String JavaDoc)httpIlRequest.getArguments()[0];
129                 long timeout = ((Long JavaDoc)httpIlRequest.getArguments()[1]).longValue();
130                 log.debug("Listening on behalf of a ClientIL #" + clientIlId + " for " + String.valueOf(timeout) + " milliseconds.");
131                 HTTPILRequest[] responseRequest = HTTPClientILStorageQueue.getInstance().get(clientIlId, timeout);
132                 log.debug("The following lines reflect the HTTPILRequest object to be packaged and returned to ClientIL #" + clientIlId + " as an HTTPILResponse.");
133                 for (int i = 0; i < responseRequest.length; i++)
134                 {
135                     log.debug("Response for ClientIL #" + clientIlId + " contains '" + responseRequest[i].toString() + ".'");
136                 }
137                 outputStream.writeObject(new HTTPILResponse(responseRequest));
138             }
139             else if (methodName.equals("getClientILIdentifer"))
140             {
141                 if (log.isTraceEnabled())
142                 {
143                     log.trace("getClientILIdentifer(HTTPILRequest " + httpIlRequest.toString() + ")");
144                 }
145                 String JavaDoc id = HTTPClientILStorageQueue.getInstance().getID();
146                 log.debug("Provided ClientIL Id #" + id + ".");
147                 outputStream.writeObject(new HTTPILResponse(id));
148             }
149             else if (methodName.equals("stopClientListening"))
150             {
151                 if (log.isTraceEnabled())
152                 {
153                     log.trace("stopClientListening(HTTPILRequest " + httpIlRequest.toString() + ")");
154                 }
155                 String JavaDoc clientIlId = (String JavaDoc)httpIlRequest.getArguments()[0];
156                 if (clientIlId != null)
157                 {
158                     HTTPClientILStorageQueue.getInstance().purgeEntry(clientIlId);
159                 }
160                 outputStream.writeObject(new HTTPILResponse("Storage queue was purged."));
161             }
162             /* The following four 'else ifs' are special ServerIL cases where we can't
163              * simply pass the invocation through due to the way the primitives
164              * must be stored as objects. In JDK 1.4, you can specify the primitive
165              * class (i.e. Long.TYPE or long.class) and it works, however, this
166              * is NOT the case with JDK 1.3. Therefore, in order to support both
167              * we've got to pass the primitive class type in its object form
168              * (i.e. Long.class), and then--in cases where they target object
169              * expects a primitive--handle the unboxing manually.
170              */

171             else if (methodName.equals("ping"))
172             {
173                 if (log.isTraceEnabled())
174                 {
175                     log.trace("ping(HTTPILRequest " + httpIlRequest.toString() + ")");
176                 }
177                 ConnectionToken connectionToken = (ConnectionToken)httpIlRequest.getArguments()[0];
178                 long clientTime = ((Long JavaDoc)httpIlRequest.getArguments()[1]).longValue();
179                 this.invoker.ping(connectionToken, clientTime);
180                 outputStream.writeObject(new HTTPILResponse());
181             }
182             else if (methodName.equals("receive"))
183             {
184                 if (log.isTraceEnabled())
185                 {
186                     log.trace("receive(HTTPILRequest " + httpIlRequest.toString() + ")");
187                 }
188                 ConnectionToken connectionToken = (ConnectionToken)httpIlRequest.getArguments()[0];
189                 int subscriberId = ((Integer JavaDoc)httpIlRequest.getArguments()[1]).intValue();
190                 long wait = ((Long JavaDoc)httpIlRequest.getArguments()[2]).longValue();
191                 SpyMessage message = this.invoker.receive(connectionToken, subscriberId, wait);
192                 outputStream.writeObject(new HTTPILResponse(message));
193                 if (message != null && log.isTraceEnabled())
194                 {
195                     log.debug("Returned an instance of '" + message.getClass().toString() + "' with value of '" + message.toString() + "'");
196                 }
197             }
198             else if (methodName.equals("setEnabled"))
199             {
200                 if (log.isTraceEnabled())
201                 {
202                     log.trace("setEnabled(HTTPILRequest " + httpIlRequest.toString() + ")");
203                 }
204                 ConnectionToken connectionToken = (ConnectionToken)httpIlRequest.getArguments()[0];
205                 boolean enabled = ((Boolean JavaDoc)httpIlRequest.getArguments()[1]).booleanValue();
206                 this.invoker.setEnabled(connectionToken, enabled);
207                 outputStream.writeObject(new HTTPILResponse());
208             }
209             else if (methodName.equals("unsubscribe"))
210             {
211                 if (log.isTraceEnabled())
212                 {
213                     log.trace("unsubscribe(HTTPILRequest " + httpIlRequest.toString() + ")");
214                 }
215                 ConnectionToken connectionToken = (ConnectionToken)httpIlRequest.getArguments()[0];
216                 int subscriberId = ((Integer JavaDoc)httpIlRequest.getArguments()[1]).intValue();
217                 this.invoker.unsubscribe(connectionToken, subscriberId);
218                 outputStream.writeObject(new HTTPILResponse());
219             }
220             else if (methodName.equals("recover"))
221             {
222                 if (log.isTraceEnabled())
223                     log.trace("recover(HTTPILRequest " + httpIlRequest.toString() + ")");
224                 ConnectionToken connectionToken = (ConnectionToken)httpIlRequest.getArguments()[0];
225                 int flags = ((Integer JavaDoc)httpIlRequest.getArguments()[1]).intValue();
226                 Xid JavaDoc[] xids = this.invoker.recover(connectionToken, flags);
227                 outputStream.writeObject(new HTTPILResponse(xids));
228             }
229             else
230             {
231                 if (log.isTraceEnabled())
232                 {
233                     log.trace("HTTPILRequest recieved: " + httpIlRequest.toString());
234                 }
235                 Method JavaDoc method = this.invoker.getClass().getMethod(methodName, httpIlRequest.getArgumentTypes());
236                 Object JavaDoc returnValue = method.invoke(this.invoker, httpIlRequest.getArguments());
237                 if (log.isTraceEnabled())
238                 {
239                     log.debug("Invoked method '" + methodName + ".'");
240                 }
241                 outputStream.writeObject(new HTTPILResponse(returnValue));
242                 if (returnValue != null && log.isTraceEnabled())
243                 {
244                     log.debug("Returned an instance of '" + returnValue.getClass().toString() + "' with value of '" + returnValue.toString() + "'");
245                 }
246             }
247         }
248         catch (InvocationTargetException JavaDoc invocatonTargetException)
249         {
250             Throwable JavaDoc targetException = invocatonTargetException.getTargetException();
251             if (log.isDebugEnabled())
252             {
253                 log.debug("The underlying invoker (i.e. The JMS Server itself) threw in exception of type '" + targetException.getClass().getName() + "' and a message of '" + targetException.getMessage() + ".' This exception is being propogated to the client as a HTTPILResponse.");
254             }
255             outputStream.writeObject(new HTTPILResponse(targetException));
256         }
257         catch (Exception JavaDoc exception)
258         {
259             if (log.isDebugEnabled())
260             {
261                 log.debug("Threw an exception of type '" + exception.getClass().getName() + "' with a message of '" + exception.getMessage() + ".' This exception is being propogated to the client as a HTTPILResponse.");
262             }
263             outputStream.writeObject(new HTTPILResponse(exception));
264         }
265         outputStream.close();
266     }
267     
268     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc
269     {
270         if (log.isTraceEnabled())
271         {
272             log.trace("doGet(HttpServletRequest " + request.toString() + ", HttpServletResponse " + response.toString() + ")");
273         }
274         response.getWriter().print("<html><head><title>JBossMQ HTTP-IL Servlet</title><head><body><h1>This is the JBossMQ HTTP-IL</h1></body></html>");
275     }
276     
277     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc
278     {
279         if (log.isTraceEnabled())
280         {
281             log.trace("doPost() defers to processRequest, see the parameters in its trace.");
282         }
283         processRequest(request, response);
284     }
285     
286     public String JavaDoc getServletInfo()
287     {
288         return "Provides an HTTP/S interface to JBossMQ";
289     }
290 }
Popular Tags