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