View Javadoc

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      public MessageServletDispatcher() {
48          //request.setResponse(response);
49          //response.setRequest(request);        
50      }
51  
52      public MessageServletDispatcher(String path) {
53          this();
54          this.path = path;
55      }
56  
57      
58      public void init() throws ServletException {
59          request = new MessageletRequestImpl( new ServletRequestImpl( getServletContext() ) );
60          response = new MessageletResponseImpl( new ServletResponseImpl() );
61      }
62      
63      // MessengerListener methods
64      //-------------------------------------------------------------------------        
65      public void setMessenger(Messenger messenger) {
66          response.setReplyMessenger( messenger );
67      }
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              response.setReplyToDestination( message.getJMSReplyTo() );
77          }
78          catch (JMSException e) {
79              log( "Could not find JMS replyTo destination", e );
80              response.setReplyToDestination( null );
81          }
82              
83          // Ask our Container to process this request
84          try {
85              // initialise the request
86              request.setMessage( message );
87              response.reset();
88              
89              // dispatch the servlet
90              getServletContext().getRequestDispatcher( getPath() ).include(request, response);
91              
92              // finish up the response, sending a reply if necessary
93              response.finish();
94          } 
95          catch (Throwable e) {
96              handleException( message, e );
97          }
98      }
99  
100     
101     // Properties
102     //-------------------------------------------------------------------------        
103     /** Getter for property path.
104      * @return Value of property path.
105      */ 
106     public String getPath() {
107         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         this.path = path;
115         
116         //request.setRequestURI(path);
117     }
118     
119     
120     
121     // Implementation methods
122     //-------------------------------------------------------------------------        
123     protected void handleException(Message message, Throwable t) {
124         log( "Caught exception processing message: " + message, t);
125     }
126 }