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