001    /*
002     * Copyright (C) The Apache Software Foundation. All rights reserved.
003     *
004     * This software is published under the terms of the Apache Software License
005     * version 1.1, a copy of which has been included with this distribution in
006     * the LICENSE file.
007     * 
008     * $Id: MessageServletDispatcher.java 155459 2005-02-26 13:24:44Z dirkv $
009     */
010    package org.apache.commons.messagelet.impl;
011    
012    import javax.jms.JMSException;
013    import javax.jms.Message;
014    import javax.servlet.ServletException;
015    
016    import org.apache.commons.messagelet.MessageDrivenObjectSupport;
017    import org.apache.commons.messenger.Messenger;
018    import org.apache.commons.messenger.MessengerListener;
019    
020    /** <p><code>MessageDispatcher</code> dispatches JMS Messages
021      * into a Servlet engine for procesing.</p>
022      *
023      * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
024      * @version $Revision: 155459 $
025      */
026    public class MessageServletDispatcher extends MessageDrivenObjectSupport implements MessengerListener {
027    
028        /**
029         * The ServletRequest object we will pass to the servlet engine
030         */
031        private MessageletRequestImpl request;
032    
033        /**
034         * The ServletResponse object we will pass to the servlet engine
035         */
036        private MessageletResponseImpl response;
037        
038        /** 
039         * The reply to messenger
040         */
041        private Messenger messenger;
042        
043        /** Holds value of property path. */
044        private String path;    
045    
046        
047        public MessageServletDispatcher() {
048            //request.setResponse(response);
049            //response.setRequest(request);        
050        }
051    
052        public MessageServletDispatcher(String path) {
053            this();
054            this.path = path;
055        }
056    
057        
058        public void init() throws ServletException {
059            request = new MessageletRequestImpl( new ServletRequestImpl( getServletContext() ) );
060            response = new MessageletResponseImpl( new ServletResponseImpl() );
061        }
062        
063        // MessengerListener methods
064        //-------------------------------------------------------------------------        
065        public void setMessenger(Messenger messenger) {
066            response.setReplyMessenger( messenger );
067        }
068    
069        /**
070         * Process the incoming JMS Message.
071         *
072         * @param message is the message to be processed
073         */
074        public void onMessage(Message message) {
075            try {
076                response.setReplyToDestination( message.getJMSReplyTo() );
077            }
078            catch (JMSException e) {
079                log( "Could not find JMS replyTo destination", e );
080                response.setReplyToDestination( null );
081            }
082                
083            // Ask our Container to process this request
084            try {
085                // initialise the request
086                request.setMessage( message );
087                response.reset();
088                
089                // dispatch the servlet
090                getServletContext().getRequestDispatcher( getPath() ).include(request, response);
091                
092                // finish up the response, sending a reply if necessary
093                response.finish();
094            } 
095            catch (Throwable e) {
096                handleException( message, e );
097            }
098        }
099    
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    }