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.Method;
020
021 import org.apache.camel.CamelContext;
022 import org.apache.camel.Exchange;
023 import org.apache.camel.Message;
024 import org.apache.camel.Processor;
025 import org.apache.camel.spi.Registry;
026 import org.apache.camel.util.ObjectHelper;
027 import org.apache.commons.logging.Log;
028 import org.apache.commons.logging.LogFactory;
029
030 import static org.apache.camel.util.ObjectHelper.isNullOrBlank;
031
032 /**
033 * A {@link Processor} which converts the inbound exchange to a method
034 * invocation on a POJO
035 *
036 * @version $Revision: $
037 */
038 public class BeanProcessor implements Processor {
039 public static final String METHOD_NAME = "org.apache.camel.MethodName";
040 private static final Log LOG = LogFactory.getLog(BeanProcessor.class);
041
042 private final Object pojo;
043 private final BeanInfo beanInfo;
044 private Method method;
045 private String methodName;
046
047 public BeanProcessor(Object pojo, BeanInfo beanInfo) {
048 this.pojo = pojo;
049 this.beanInfo = beanInfo;
050 }
051
052 public BeanProcessor(Object pojo, ParameterMappingStrategy parameterMappingStrategy) {
053 this(pojo, new BeanInfo(pojo.getClass(), parameterMappingStrategy));
054 }
055
056 public BeanProcessor(Object pojo, CamelContext camelContext) {
057 this(pojo, createParameterMappingStrategy(camelContext));
058 }
059
060 public static ParameterMappingStrategy createParameterMappingStrategy(CamelContext camelContext) {
061 Registry registry = camelContext.getRegistry();
062 ParameterMappingStrategy answer = registry.lookup(ParameterMappingStrategy.class.getName(),
063 ParameterMappingStrategy.class);
064 if (answer == null) {
065 answer = new DefaultParameterMappingStrategy();
066 }
067 return answer;
068 }
069 @Override
070 public String toString() {
071 String description = method != null ? " " + method : "";
072 return "BeanProcessor[" + pojo + description + "]";
073 }
074
075 public void process(Exchange exchange) throws Exception {
076 if (LOG.isDebugEnabled()) {
077 LOG.debug(">>>> invoking method for: " + exchange);
078 }
079 Message in = exchange.getIn();
080 BeanInvocation beanInvoke = in.getBody(BeanInvocation.class);
081 if (beanInvoke != null) {
082 beanInvoke.invoke(pojo, exchange);
083 return;
084 }
085
086 MethodInvocation invocation;
087 if (method != null) {
088 invocation = beanInfo.createInvocation(method, pojo, exchange);
089 } else {
090 // lets pass in the method name to use if its specified
091 if (ObjectHelper.isNotNullAndNonEmpty(methodName)) {
092 if (isNullOrBlank(in.getHeader(METHOD_NAME, String.class))) {
093 in.setHeader(METHOD_NAME, methodName);
094 }
095 }
096 invocation = beanInfo.createInvocation(pojo, exchange);
097 }
098 if (invocation == null) {
099 throw new IllegalStateException("No method invocation could be created");
100 }
101 try {
102 Object value = invocation.proceed();
103 if (value != null) {
104 exchange.getIn().setBody(value);
105 }
106 } catch (Exception e) {
107 throw e;
108 } catch (Throwable throwable) {
109 throw new Exception(throwable);
110 }
111 }
112
113 // Properties
114 // -----------------------------------------------------------------------
115
116 public Method getMethod() {
117 return method;
118 }
119
120 public void setMethod(Method method) {
121 this.method = method;
122 }
123
124 public String getMethodName() {
125 return methodName;
126 }
127
128 public void setMethodName(String methodName) {
129 this.methodName = methodName;
130 }
131 }