001    /** 
002     * 
003     * Copyright 2005 AgilaSoft Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * You may obtain a copy of the License at 
008     * 
009     * http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS, 
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
014     * See the License for the specific language governing permissions and 
015     * limitations under the License. 
016     * 
017     **/
018    package org.logicblaze.lingo.jms;
019    
020    import org.springframework.remoting.support.RemoteInvocationResult;
021    import org.logicblaze.lingo.jms.impl.DefaultJmsProducer;
022    import org.logicblaze.lingo.jms.impl.OneWayRequestor;
023    
024    import javax.jms.JMSException;
025    import javax.jms.Message;
026    import javax.jms.ConnectionFactory;
027    
028    /**
029     * A regular JMS message listener which can be used from inside a message driven object
030     * container.
031     *
032     * @version $Revision: 1.2 $
033     */
034    public class JmsServiceExporterMessageListener extends JmsServiceExporterSupport {
035        private JmsProducer producer;
036        private ConnectionFactory connectionFactory;
037    
038        public void afterPropertiesSet() throws Exception {
039            if (producer == null) {
040                if (connectionFactory == null) {
041                    throw new IllegalArgumentException("requestor or connectionFactory is required");
042                }
043                else {
044                    producer = DefaultJmsProducer.newInstance(connectionFactory);
045                }
046            }
047            Requestor responseRequestor = getResponseRequestor();
048            if (responseRequestor == null) {
049                setResponseRequestor(new OneWayRequestor(producer, null));
050            }
051            super.afterPropertiesSet();
052        }
053    
054        public JmsProducer getProducer() {
055            return producer;
056        }
057    
058        public void setProducer(JmsProducer producer) {
059            this.producer = producer;
060        }
061    
062        public ConnectionFactory getConnectionFactory() {
063            return connectionFactory;
064        }
065    
066        public void setConnectionFactory(ConnectionFactory connectionFactory) {
067            this.connectionFactory = connectionFactory;
068        }
069    
070        protected void writeRemoteInvocationResult(Message message, RemoteInvocationResult result) throws JMSException {
071            Message responseMessage = createResponseMessage(producer.getSession(), message, result);
072            producer.getMessageProducer().send(message.getJMSReplyTo(), responseMessage);
073        }
074    }