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.impl;
019    
020    import org.logicblaze.lingo.jms.ReplyHandler;
021    import org.logicblaze.lingo.jms.marshall.Marshaller;
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    import org.springframework.remoting.support.RemoteInvocation;
025    import org.springframework.remoting.support.RemoteInvocationBasedExporter;
026    
027    import javax.jms.JMSException;
028    import javax.jms.Message;
029    import java.lang.reflect.InvocationTargetException;
030    
031    /**
032     * @version $Revision: 1.1 $
033     */
034    public class AsyncReplyHandler extends RemoteInvocationBasedExporter implements ReplyHandler {
035        private static final Log log = LogFactory.getLog(AsyncReplyHandler.class);
036    
037        private Object pojo;
038        private Marshaller marshaller;
039    
040        public AsyncReplyHandler(Object pojo, Marshaller marshaller) {
041            this.pojo = pojo;
042            this.marshaller = marshaller;
043        }
044    
045        public boolean handle(Message message) throws JMSException {
046            RemoteInvocation invocation = marshaller.readRemoteInvocation(message);
047            try {
048                invoke(invocation, pojo);
049            }
050            catch (NoSuchMethodException e) {
051                onException(invocation, e);
052            }
053            catch (IllegalAccessException e) {
054                onException(invocation, e);
055            }
056            catch (InvocationTargetException e) {
057                onException(invocation, e);
058            }
059            return false;
060        }
061    
062    
063        protected void onException(RemoteInvocation invocation, Exception e) {
064            log.error("Failed to invoke: " + invocation + " on: " + pojo + ". Reason: " + e, e);
065        }
066    }