| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| MessageServletDispatcher |
|
| 1.25;1.25 |
| 1 | /* | |
| 2 | * Copyright (C) The Apache Software Foundation. All rights reserved. | |
| 3 | * | |
| 4 | * This software is published under the terms of the Apache Software License | |
| 5 | * version 1.1, a copy of which has been included with this distribution in | |
| 6 | * the LICENSE file. | |
| 7 | * | |
| 8 | * $Id: MessageServletDispatcher.java 155459 2005-02-26 13:24:44Z dirkv $ | |
| 9 | */ | |
| 10 | package org.apache.commons.messagelet.impl; | |
| 11 | ||
| 12 | import javax.jms.JMSException; | |
| 13 | import javax.jms.Message; | |
| 14 | import javax.servlet.ServletException; | |
| 15 | ||
| 16 | import org.apache.commons.messagelet.MessageDrivenObjectSupport; | |
| 17 | import org.apache.commons.messenger.Messenger; | |
| 18 | import org.apache.commons.messenger.MessengerListener; | |
| 19 | ||
| 20 | /** <p><code>MessageDispatcher</code> dispatches JMS Messages | |
| 21 | * into a Servlet engine for procesing.</p> | |
| 22 | * | |
| 23 | * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> | |
| 24 | * @version $Revision: 155459 $ | |
| 25 | */ | |
| 26 | public class MessageServletDispatcher extends MessageDrivenObjectSupport implements MessengerListener { | |
| 27 | ||
| 28 | /** | |
| 29 | * The ServletRequest object we will pass to the servlet engine | |
| 30 | */ | |
| 31 | private MessageletRequestImpl request; | |
| 32 | ||
| 33 | /** | |
| 34 | * The ServletResponse object we will pass to the servlet engine | |
| 35 | */ | |
| 36 | private MessageletResponseImpl response; | |
| 37 | ||
| 38 | /** | |
| 39 | * The reply to messenger | |
| 40 | */ | |
| 41 | private Messenger messenger; | |
| 42 | ||
| 43 | /** Holds value of property path. */ | |
| 44 | private String path; | |
| 45 | ||
| 46 | ||
| 47 | 0 | public MessageServletDispatcher() { |
| 48 | //request.setResponse(response); | |
| 49 | //response.setRequest(request); | |
| 50 | 0 | } |
| 51 | ||
| 52 | public MessageServletDispatcher(String path) { | |
| 53 | 0 | this(); |
| 54 | 0 | this.path = path; |
| 55 | 0 | } |
| 56 | ||
| 57 | ||
| 58 | public void init() throws ServletException { | |
| 59 | 0 | request = new MessageletRequestImpl( new ServletRequestImpl( getServletContext() ) ); |
| 60 | 0 | response = new MessageletResponseImpl( new ServletResponseImpl() ); |
| 61 | 0 | } |
| 62 | ||
| 63 | // MessengerListener methods | |
| 64 | //------------------------------------------------------------------------- | |
| 65 | public void setMessenger(Messenger messenger) { | |
| 66 | 0 | response.setReplyMessenger( messenger ); |
| 67 | 0 | } |
| 68 | ||
| 69 | /** | |
| 70 | * Process the incoming JMS Message. | |
| 71 | * | |
| 72 | * @param message is the message to be processed | |
| 73 | */ | |
| 74 | public void onMessage(Message message) { | |
| 75 | try { | |
| 76 | 0 | response.setReplyToDestination( message.getJMSReplyTo() ); |
| 77 | } | |
| 78 | 0 | catch (JMSException e) { |
| 79 | 0 | log( "Could not find JMS replyTo destination", e ); |
| 80 | 0 | response.setReplyToDestination( null ); |
| 81 | 0 | } |
| 82 | ||
| 83 | // Ask our Container to process this request | |
| 84 | try { | |
| 85 | // initialise the request | |
| 86 | 0 | request.setMessage( message ); |
| 87 | 0 | response.reset(); |
| 88 | ||
| 89 | // dispatch the servlet | |
| 90 | 0 | getServletContext().getRequestDispatcher( getPath() ).include(request, response); |
| 91 | ||
| 92 | // finish up the response, sending a reply if necessary | |
| 93 | 0 | response.finish(); |
| 94 | } | |
| 95 | 0 | catch (Throwable e) { |
| 96 | 0 | handleException( message, e ); |
| 97 | 0 | } |
| 98 | 0 | } |
| 99 | ||
| 100 | ||
| 101 | // Properties | |
| 102 | //------------------------------------------------------------------------- | |
| 103 | /** Getter for property path. | |
| 104 | * @return Value of property path. | |
| 105 | */ | |
| 106 | public String getPath() { | |
| 107 | 0 | return path; |
| 108 | } | |
| 109 | ||
| 110 | /** Setter for property path. | |
| 111 | * @param path New value of property path. | |
| 112 | */ | |
| 113 | public void setPath(String path) { | |
| 114 | 0 | this.path = path; |
| 115 | ||
| 116 | //request.setRequestURI(path); | |
| 117 | 0 | } |
| 118 | ||
| 119 | ||
| 120 | ||
| 121 | // Implementation methods | |
| 122 | //------------------------------------------------------------------------- | |
| 123 | protected void handleException(Message message, Throwable t) { | |
| 124 | 0 | log( "Caught exception processing message: " + message, t); |
| 125 | 0 | } |
| 126 | } |