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.io.Externalizable;
020 import java.io.IOException;
021 import java.io.ObjectInput;
022 import java.io.ObjectOutput;
023 import java.lang.reflect.InvocationTargetException;
024 import java.lang.reflect.Method;
025 import java.util.Arrays;
026
027 import org.apache.camel.Exchange;
028 import org.apache.camel.util.IOHelper;
029 import org.apache.camel.util.ObjectHelper;
030 import org.apache.commons.logging.Log;
031 import org.apache.commons.logging.LogFactory;
032
033 /**
034 * Invocation of beans that can handle being serialized.
035 */
036 public class BeanInvocation implements Externalizable {
037 private static final transient Log LOG = LogFactory.getLog(BeanInvocation.class);
038 private Object[] args;
039 private MethodBean methodBean;
040 private transient Method method;
041
042 public BeanInvocation() {
043 }
044
045 public BeanInvocation(Method method, Object[] args) {
046 this.method = method;
047 this.args = args;
048 }
049
050 @Override
051 public String toString() {
052 Object list = null;
053 if (args != null) {
054 list = Arrays.asList(args);
055 }
056 return "BeanInvocation " + method + " with " + list + "]";
057 }
058
059 public Object[] getArgs() {
060 return args;
061 }
062
063 public Method getMethod() {
064 return method;
065 }
066
067 public void setMethod(Method method) {
068 this.method = method;
069 }
070
071 public void setArgs(Object[] args) {
072 this.args = args;
073 }
074
075 /**
076 * This causes us to invoke the endpoint Pojo using reflection.
077 *
078 * @param pojo the bean on which to perform this invocation
079 * @param exchange the exchange carrying the method invocation
080 */
081 public void invoke(Object pojo, Exchange exchange) {
082 try {
083 if (LOG.isTraceEnabled()) {
084 LOG.trace("Invoking method: " + getMethod() + " with args: " + getArgs());
085 }
086 Object response = getMethod().invoke(pojo, getArgs());
087 if (LOG.isTraceEnabled()) {
088 LOG.trace("Got response: " + response);
089 }
090 exchange.getOut().setBody(response);
091 } catch (InvocationTargetException e) {
092 exchange.setException(ObjectHelper.wrapRuntimeCamelException(e.getCause()));
093 } catch (Exception e) {
094 throw ObjectHelper.wrapRuntimeCamelException(e);
095 }
096 }
097
098 public void readExternal(ObjectInput objectInput) throws IOException, ClassNotFoundException {
099 methodBean = ObjectHelper.cast(MethodBean.class, objectInput.readObject());
100 try {
101 method = methodBean.getMethod();
102 } catch (NoSuchMethodException e) {
103 throw IOHelper.createIOException(e);
104 }
105 args = ObjectHelper.cast(Object[].class, objectInput.readObject());
106 }
107
108 public void writeExternal(ObjectOutput objectOutput) throws IOException {
109 if (methodBean == null) {
110 methodBean = new MethodBean(method);
111 }
112 objectOutput.writeObject(methodBean);
113 objectOutput.writeObject(args);
114 }
115 }