001    /**
002     *
003     * Copyright 2005 LogicBlaze, Inc.
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.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import org.springframework.beans.factory.InitializingBean;
023    import org.springframework.jms.core.JmsTemplate;
024    import org.springframework.jms.core.MessageCreator;
025    import org.springframework.remoting.support.RemoteInvocationResult;
026    
027    import javax.jms.JMSException;
028    import javax.jms.Message;
029    import javax.jms.Session;
030    
031    /**
032     * A JMS MessageListener that exports the specified service bean as a JMS service
033     * endpoint, accessible via a JMS proxy.
034     * <p/>
035     * <p>Note: JMS services exported with this class can be accessed by
036     * any JMS client, as there isn't any special handling involved.
037     *
038     * @author James Strachan
039     * @see JmsProxyFactoryBean
040     */
041    public class JmsTemplateServiceExporter extends JmsServiceExporterSupport implements InitializingBean {
042        private static final Log log = LogFactory.getLog(JmsTemplateServiceExporter.class);
043    
044        private JmsTemplate template;
045    
046        public void afterPropertiesSet() throws Exception {
047            super.afterPropertiesSet();
048            if (template == null) {
049                throw new IllegalArgumentException("template is required");
050            }
051        }
052    
053        public JmsTemplate getTemplate() {
054            return template;
055        }
056    
057        /**
058         * Sets the JMS template used to send replies back for the request
059         *
060         * @param template the JMS template to use
061         */
062        public void setTemplate(JmsTemplate template) {
063            this.template = template;
064        }
065    
066    
067        /**
068         * Send the given RemoteInvocationResult as a JMS message to the originator
069         *
070         * @param message current HTTP message
071         * @param result  the RemoteInvocationResult object
072         * @throws javax.jms.JMSException if thrown by trying to send the message
073         */
074        protected void writeRemoteInvocationResult(final Message message, final RemoteInvocationResult result) throws JMSException {
075            template.send(message.getJMSReplyTo(), new MessageCreator() {
076                public Message createMessage(Session session) throws JMSException {
077                    return createResponseMessage(session, message, result);
078                }
079            });
080        }
081    
082    }