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 */
017package org.apache.camel.component.bean;
018
019import java.io.Externalizable;
020import java.io.IOException;
021import java.io.ObjectInput;
022import java.io.ObjectOutput;
023import java.lang.reflect.InvocationTargetException;
024import java.lang.reflect.Method;
025import java.util.Arrays;
026
027import org.apache.camel.Exchange;
028import org.apache.camel.util.ObjectHelper;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032/**
033 * Invocation of beans that can handle being serialized.
034 */
035@Deprecated
036public class BeanInvocation implements Externalizable {
037    private static final Logger LOG = LoggerFactory.getLogger(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            LOG.trace("Invoking method: {} with args: {}", getMethod(), getArgs());
084            Object response = getMethod().invoke(pojo, getArgs());
085            LOG.trace("Got response: {}", response);
086            exchange.getOut().setBody(response);
087        } catch (InvocationTargetException e) {
088            exchange.setException(ObjectHelper.wrapRuntimeCamelException(e.getCause()));
089        } catch (Exception e) {
090            throw ObjectHelper.wrapRuntimeCamelException(e);
091        }
092    }
093
094    public void readExternal(ObjectInput objectInput) throws IOException, ClassNotFoundException {
095        methodBean = ObjectHelper.cast(MethodBean.class, objectInput.readObject());
096        try {
097            method = methodBean.getMethod();
098        } catch (NoSuchMethodException e) {
099            throw new IOException(e);
100        }
101        args = ObjectHelper.cast(Object[].class, objectInput.readObject());
102    }
103
104    public void writeExternal(ObjectOutput objectOutput) throws IOException {
105        if (methodBean == null) {
106            methodBean = new MethodBean(method);
107        }
108        objectOutput.writeObject(methodBean);
109        objectOutput.writeObject(args);
110    }
111}