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.InvocationTargetException; 020 import java.lang.reflect.Method; 021 022 import org.apache.camel.Exchange; 023 024 public class BeanInvocation { 025 026 private final Object proxy; 027 private final Method method; 028 private final Object[] args; 029 030 public BeanInvocation(Object proxy, Method method, Object[] args) { 031 this.proxy = proxy; 032 this.method = method; 033 this.args = args; 034 } 035 036 public Object[] getArgs() { 037 return args; 038 } 039 040 public Method getMethod() { 041 return method; 042 } 043 044 public Object getProxy() { 045 return proxy; 046 } 047 048 /** 049 * This causes us to invoke the endpoint Pojo using reflection. 050 * 051 * @param pojo 052 */ 053 public void invoke(Object pojo, Exchange exchange) { 054 try { 055 Object response = getMethod().invoke(pojo, getArgs()); 056 exchange.getOut().setBody(response); 057 } catch (InvocationTargetException e) { 058 exchange.setException(e.getCause()); 059 } catch (RuntimeException e) { 060 throw e; 061 } catch (Throwable e) { 062 throw new RuntimeException(e); 063 } 064 } 065 066 }