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.impl;
19
20 import edu.emory.mathcs.backport.java.util.concurrent.FutureTask;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.logicblaze.lingo.MetadataStrategy;
25 import org.logicblaze.lingo.jms.JmsServiceExporterMessageListener;
26 import org.logicblaze.lingo.jms.ReplyHandler;
27 import org.logicblaze.lingo.jms.marshall.Marshaller;
28 import org.springframework.remoting.support.RemoteInvocation;
29
30 import javax.jms.JMSException;
31 import javax.jms.Message;
32
33 import java.lang.reflect.Method;
34
35 /***
36 * @version $Revision: 1.7 $
37 */
38 public class AsyncReplyHandler extends JmsServiceExporterMessageListener implements ReplyHandler {
39 private static final Log log = LogFactory.getLog(AsyncReplyHandler.class);
40
41 private Marshaller marshaller;
42 private final MetadataStrategy metadataStrategy;
43 private ReplyHandler parent;
44
45 public AsyncReplyHandler(Object pojo, Marshaller marshaller, MetadataStrategy metadataStrategy) {
46 super(pojo);
47 this.marshaller = marshaller;
48 this.metadataStrategy = metadataStrategy;
49 }
50
51 public boolean handle(Message message) throws JMSException {
52 if (parent != null) {
53 parent.handle(message);
54 }
55 RemoteInvocation invocation = marshaller.readRemoteInvocation(message);
56 doInvoke(message, invocation);
57 return isEndSessionMethod(invocation);
58 }
59
60 public ReplyHandler getParent() {
61 return parent;
62 }
63
64 public void setParent(ReplyHandler parent) {
65 this.parent = parent;
66 }
67
68 public FutureTask newResultHandler() {
69 FutureHandler futureResult = new FutureHandler();
70 setParent(futureResult);
71 return futureResult;
72 }
73
74 protected boolean isEndSessionMethod(RemoteInvocation invocation) {
75 Method method;
76 try {
77 method = getProxy().getClass().getMethod(invocation.getMethodName(), invocation.getParameterTypes());
78 }
79 catch (Exception e) {
80 onException(invocation, e);
81 return false;
82 }
83 return metadataStrategy.getMethodMetadata(method).isEndSession();
84 }
85
86 protected void onException(RemoteInvocation invocation, Exception e) {
87 log.error("Failed to invoke: " + invocation + " on: " + getProxy() + ". Reason: " + e, e);
88 }
89 }