1 /***
2 *
3 * Copyright 2005 LogicBlaze, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.logicblaze.lingo.jms;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.springframework.beans.factory.InitializingBean;
23 import org.springframework.jms.core.JmsTemplate;
24 import org.springframework.jms.core.MessageCreator;
25 import org.springframework.remoting.support.RemoteInvocationResult;
26
27 import javax.jms.JMSException;
28 import javax.jms.Message;
29 import javax.jms.Session;
30
31 /***
32 * A JMS MessageListener that exports the specified service bean as a JMS service
33 * endpoint, accessible via a JMS proxy.
34 * <p/>
35 * <p>Note: JMS services exported with this class can be accessed by
36 * any JMS client, as there isn't any special handling involved.
37 *
38 * @author James Strachan
39 * @see JmsProxyFactoryBean
40 */
41 public class JmsTemplateServiceExporter extends JmsServiceExporterSupport implements InitializingBean {
42 private static final Log log = LogFactory.getLog(JmsTemplateServiceExporter.class);
43
44 private JmsTemplate template;
45
46 public void afterPropertiesSet() throws Exception {
47 super.afterPropertiesSet();
48 if (template == null) {
49 throw new IllegalArgumentException("template is required");
50 }
51 }
52
53 public JmsTemplate getTemplate() {
54 return template;
55 }
56
57 /***
58 * Sets the JMS template used to send replies back for the request
59 *
60 * @param template the JMS template to use
61 */
62 public void setTemplate(JmsTemplate template) {
63 this.template = template;
64 }
65
66
67 /***
68 * Send the given RemoteInvocationResult as a JMS message to the originator
69 *
70 * @param message current HTTP message
71 * @param result the RemoteInvocationResult object
72 * @throws javax.jms.JMSException if thrown by trying to send the message
73 */
74 protected void writeRemoteInvocationResult(final Message message, final RemoteInvocationResult result) throws JMSException {
75 template.send(message.getJMSReplyTo(), new MessageCreator() {
76 public Message createMessage(Session session) throws JMSException {
77 return createResponseMessage(session, message, result);
78 }
79 });
80 }
81
82 }