1 22 package org.jboss.mq.il.http.servlet; 23 24 import java.io.IOException ; 25 import java.io.ObjectInputStream ; 26 import java.io.ObjectOutputStream ; 27 import java.lang.reflect.InvocationTargetException ; 28 import java.lang.reflect.Method ; 29 30 import javax.management.MBeanServer ; 31 import javax.management.ObjectName ; 32 import javax.servlet.ServletConfig ; 33 import javax.servlet.ServletException ; 34 import javax.servlet.http.HttpServlet ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 37 import javax.transaction.xa.Xid ; 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 64 public class HTTPServerILServlet extends HttpServlet 65 { 66 67 private static final String 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 server; 71 private JMSServerInvoker invoker; 72 73 public void init(ServletConfig config) throws ServletException 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 ("Failed to locate the MBeanServer"); 84 } 85 String invokerName = config.getInitParameter("Invoker"); 86 if (invokerName == null) 87 { 88 throw new ServletException ("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 (invokerName), "Invoker"); 94 } 95 catch (Exception exception) 96 { 97 throw new ServletException ("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 request, HttpServletResponse response) throws ServletException , IOException 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 outputStream = new ObjectOutputStream (response.getOutputStream()); 117 try 118 { 119 ObjectInputStream inputStream = new ObjectInputStream (request.getInputStream()); 120 HTTPILRequest httpIlRequest = (HTTPILRequest)inputStream.readObject(); 121 String methodName = httpIlRequest.getMethodName(); 122 if (methodName.equals("clientListening")) 123 { 124 if (log.isTraceEnabled()) 125 { 126 log.trace("clientListening(HTTPILRequest " + httpIlRequest.toString() + ")"); 127 } 128 String clientIlId = (String )httpIlRequest.getArguments()[0]; 129 long timeout = ((Long )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 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 clientIlId = (String )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 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 )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 )httpIlRequest.getArguments()[1]).intValue(); 190 long wait = ((Long )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 )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 )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 )httpIlRequest.getArguments()[1]).intValue(); 226 Xid [] 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 method = this.invoker.getClass().getMethod(methodName, httpIlRequest.getArgumentTypes()); 236 Object 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 invocatonTargetException) 249 { 250 Throwable 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 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 request, HttpServletResponse response) throws ServletException , IOException 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 request, HttpServletResponse response) throws ServletException , IOException 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 getServletInfo() 287 { 288 return "Provides an HTTP/S interface to JBossMQ"; 289 } 290 } | Popular Tags |