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