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.InvocationTargetException;
020    import java.lang.reflect.Method;
021    
022    import org.apache.camel.CamelContext;
023    import org.apache.camel.Exchange;
024    import org.apache.camel.Message;
025    import org.apache.camel.Processor;
026    import org.apache.camel.impl.ServiceSupport;
027    import org.apache.camel.util.ObjectHelper;
028    import org.apache.camel.util.ServiceHelper;
029    import org.apache.commons.logging.Log;
030    import org.apache.commons.logging.LogFactory;
031    
032    /**
033     * A {@link Processor} which converts the inbound exchange to a method
034     * invocation on a POJO
035     *
036     * @version $Revision: 42414 $
037     */
038    public class BeanProcessor extends ServiceSupport implements Processor {
039        public static final String METHOD_NAME = "org.apache.camel.MethodName";
040        private static final transient Log LOG = LogFactory.getLog(BeanProcessor.class);
041    
042        private Method methodObject;
043        private String method;
044        private BeanHolder beanHolder;
045    
046        public BeanProcessor(Object pojo, BeanInfo beanInfo) {
047            this(new ConstantBeanHolder(pojo, beanInfo));
048        }
049    
050        public BeanProcessor(Object pojo, CamelContext camelContext, ParameterMappingStrategy parameterMappingStrategy) {
051            this(pojo, new BeanInfo(camelContext, pojo.getClass(), parameterMappingStrategy));
052        }
053    
054        public BeanProcessor(Object pojo, CamelContext camelContext) {
055            this(pojo, camelContext, BeanInfo.createParameterMappingStrategy(camelContext));
056        }
057    
058        public BeanProcessor(BeanHolder beanHolder) {
059            this.beanHolder = beanHolder;
060        }
061    
062        @Override
063        public String toString() {
064            String description = methodObject != null ? " " + methodObject : "";
065            return "BeanProcessor[" + beanHolder + description + "]";
066        }
067    
068        public void process(Exchange exchange) throws Exception {
069            if (LOG.isDebugEnabled()) {
070                LOG.debug(">>>> invoking method for: " + exchange);
071            }
072    
073            Object bean = beanHolder.getBean();
074            exchange.setProperty("org.apache.camel.bean.BeanHolder", beanHolder);
075    
076            Processor processor = getProcessor();
077            BeanInfo beanInfo = beanHolder.getBeanInfo();
078    
079            // do we have a custom adapter for this POJO to a Processor
080            if (processor != null) {
081                processor.process(exchange);
082                return;
083            }
084            Message in = exchange.getIn();
085            BeanInvocation beanInvoke = in.getBody(BeanInvocation.class);
086            if (beanInvoke != null) {
087                beanInvoke.invoke(bean, exchange);
088                return;
089            }
090    
091            boolean isExplicitMethod = false;
092            String prevMethod = null;
093            MethodInvocation invocation;
094            if (methodObject != null) {
095                invocation = beanInfo.createInvocation(methodObject, bean, exchange);
096            } else {
097                // we just override the bean's invocation method name here
098                if (ObjectHelper.isNotNullAndNonEmpty(method)) {
099                    prevMethod = in.getHeader(METHOD_NAME, String.class);
100                    in.setHeader(METHOD_NAME, method);
101                    isExplicitMethod = true;
102                }
103                invocation = beanInfo.createInvocation(bean, exchange);
104            }
105            if (invocation == null) {
106                throw new IllegalStateException("No method invocation could be created, "
107                                                + "no maching method could be found on: " + bean);
108            }
109            try {
110                Object value = invocation.proceed();
111                if (value != null) {
112                    exchange.getOut().setBody(value);
113                }
114            } catch (InvocationTargetException e) {
115                // lets unwrap the exception
116                Throwable cause = e.getCause();
117                if (cause instanceof Exception) {
118                    throw (Exception) cause;
119                } else {
120                    // do not handle errors!
121                    throw e;
122                }
123            } catch (Exception e) {
124                throw e;
125            } catch (Throwable throwable) {
126                throw new Exception(throwable);
127            } finally {
128                if (isExplicitMethod) {
129                    in.setHeader(METHOD_NAME, prevMethod);
130                }
131            }
132        }
133    
134        protected Processor getProcessor() {
135            return beanHolder.getProcessor();
136        }
137    
138        // Properties
139        // -----------------------------------------------------------------------
140    
141        public Method getMethodObject() {
142            return methodObject;
143        }
144    
145        public void setMethodObject(Method methodObject) {
146            this.methodObject = methodObject;
147        }
148    
149        public String getMethod() {
150            return method;
151        }
152    
153        /**
154         * Sets the method name to use
155         */
156        public void setMethod(String method) {
157            this.method = method;
158        }
159    
160        /**
161         * Kept around for backwards compatibility, please use {@link #setMethod(String)}
162         * in future instead.
163         *
164         * @deprecated use {@link #setMethod(String)}. Will be removed in Camel 2.0.
165         */
166        @Deprecated
167        public void setMethodName(String method) {
168            setMethod(method);
169        }
170    
171        // Implementation methods
172        //-------------------------------------------------------------------------
173        protected void doStart() throws Exception {
174            ServiceHelper.startService(getProcessor());
175        }
176    
177        protected void doStop() throws Exception {
178            ServiceHelper.stopService(getProcessor());
179        }
180    }