001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  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    package org.apache.camel.component.bean;
018    
019    import java.lang.reflect.InvocationHandler;
020    import java.lang.reflect.InvocationTargetException;
021    import java.lang.reflect.Method;
022    
023    import org.apache.camel.Endpoint;
024    import org.apache.camel.Exchange;
025    import org.apache.camel.ExchangePattern;
026    import org.apache.camel.Producer;
027    import org.apache.camel.RuntimeCamelException;
028    import org.apache.camel.impl.DefaultExchange;
029    import org.apache.commons.logging.Log;
030    import org.apache.commons.logging.LogFactory;
031    
032    /**
033     * An {@link java.lang.reflect.InvocationHandler} which invokes a
034     * message exchange on a camel {@link Endpoint}
035     *
036     * @version $Revision: 889160 $
037     */
038    public class CamelInvocationHandler implements InvocationHandler {
039        private static final transient Log LOG = LogFactory.getLog(CamelInvocationHandler.class);
040    
041        private final Endpoint endpoint;
042        private final Producer producer;
043        private final MethodInfoCache methodInfoCache;
044    
045        public CamelInvocationHandler(Endpoint endpoint, Producer producer, MethodInfoCache methodInfoCache) {
046            this.endpoint = endpoint;
047            this.producer = producer;
048            this.methodInfoCache = methodInfoCache;
049        }
050    
051        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
052            BeanInvocation invocation = new BeanInvocation(method, args);
053            ExchangePattern pattern = ExchangePattern.InOut;
054            MethodInfo methodInfo = methodInfoCache.getMethodInfo(method);
055            if (methodInfo != null) {
056                pattern = methodInfo.getPattern();
057            }
058            Exchange exchange = new DefaultExchange(endpoint, pattern);
059            exchange.getIn().setBody(invocation);
060    
061            // process the exchange
062            if (LOG.isTraceEnabled()) {
063                LOG.trace("Proxied method call " + method.getName() + " invoking producer: " + producer);
064            }
065            producer.process(exchange);
066    
067            // check if we had an exception
068            Throwable fault = exchange.getException();
069            if (fault != null) {
070                if (fault instanceof RuntimeCamelException) {
071                    // if the inner cause is a runtime exception we can throw it directly
072                    if (fault.getCause() instanceof RuntimeException) {
073                        throw (RuntimeException) ((RuntimeCamelException) fault).getCause();
074                    }
075                    throw (RuntimeCamelException) fault;
076                }
077                throw new InvocationTargetException(fault);
078            }
079    
080            // do not return a reply if the method is VOID or the MEP is not OUT capable
081            Class<?> to = method.getReturnType();
082            if (to == Void.TYPE || !pattern.isOutCapable()) {
083                return null;
084            }
085    
086            // only convert if there is a body
087            if (!exchange.hasOut() || exchange.getOut().getBody() == null) {
088                // there is no body so return null
089                return null;
090            }
091    
092            // use type converter so we can convert output in the desired type defined by the method
093            // and let it be mandatory so we know wont return null if we cant convert it to the defined type
094            Object answer = exchange.getOut().getMandatoryBody(to);
095            if (LOG.isTraceEnabled()) {
096                LOG.trace("Proxied method call " + method.getName() + " returning: " + answer);
097            }
098            return answer;
099        }
100    }
101