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.Proxy; 020 021 import org.apache.camel.Endpoint; 022 import org.apache.camel.Producer; 023 024 /** 025 * A helper class for creating proxies which delegate to Camel 026 * 027 * @version $Revision: 52554 $ 028 */ 029 public final class ProxyHelper { 030 031 /** 032 * Utility classes should not have a public constructor. 033 */ 034 private ProxyHelper() { 035 } 036 037 038 /** 039 * Creates a Proxy which sends PojoExchange to the endpoint. 040 */ 041 @SuppressWarnings("unchecked") 042 public static Object createProxyObject(Endpoint endpoint, Producer producer, ClassLoader classLoader, Class[] interfaces, MethodInfoCache methodCache) { 043 return Proxy.newProxyInstance(classLoader, interfaces.clone(), new CamelInvocationHandler(endpoint, producer, methodCache)); 044 } 045 046 047 /** 048 * Creates a Proxy which sends PojoExchange to the endpoint. 049 * 050 * @throws Exception 051 */ 052 @SuppressWarnings("unchecked") 053 public static <T> T createProxy(Endpoint endpoint, ClassLoader cl, Class[] interfaces, MethodInfoCache methodCache) throws Exception { 054 return (T) createProxyObject(endpoint, endpoint.createProducer(), cl, interfaces, methodCache); 055 } 056 057 /** 058 * Creates a Proxy which sends PojoExchange to the endpoint. 059 * 060 * @throws Exception 061 */ 062 @SuppressWarnings("unchecked") 063 public static <T> T createProxy(Endpoint endpoint, ClassLoader cl, Class<T>... interfaceClasses) throws Exception { 064 return (T) createProxy(endpoint, cl, interfaceClasses, createMethodInfoCache(endpoint)); 065 } 066 067 068 /** 069 * Creates a Proxy which sends PojoExchange to the endpoint. 070 * 071 * @throws Exception 072 */ 073 @SuppressWarnings("unchecked") 074 public static <T> T createProxy(Endpoint endpoint, Class<T>... interfaceClasses) throws Exception { 075 return (T) createProxy(endpoint, getClassLoader(interfaceClasses), interfaceClasses); 076 } 077 078 /** 079 * Creates a Proxy which sends PojoExchange to the endpoint. 080 * 081 * @throws Exception 082 */ 083 @SuppressWarnings("unchecked") 084 public static <T> T createProxy(Endpoint endpoint, Producer producer, Class<T>... interfaceClasses) throws Exception { 085 return (T) createProxyObject(endpoint, producer, getClassLoader(interfaceClasses), interfaceClasses, createMethodInfoCache(endpoint)); 086 } 087 088 089 /** 090 * Returns the class loader of the first interface or throws {@link IllegalArgumentException} if there are no interfaces specified 091 * 092 * @return the class loader 093 */ 094 protected static ClassLoader getClassLoader(Class... interfaces) { 095 if (interfaces == null || interfaces.length < 1) { 096 throw new IllegalArgumentException("You must provide at least 1 interface class."); 097 } 098 return interfaces[0].getClassLoader(); 099 } 100 101 102 protected static MethodInfoCache createMethodInfoCache(Endpoint endpoint) { 103 return new MethodInfoCache(endpoint.getCamelContext()); 104 } 105 106 }