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: 747062 $ 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 * Creates a Proxy which sends PojoExchange to the endpoint. 039 */ 040 @SuppressWarnings("unchecked") 041 public static Object createProxyObject(Endpoint endpoint, Producer producer, ClassLoader classLoader, Class[] interfaces, MethodInfoCache methodCache) { 042 return Proxy.newProxyInstance(classLoader, interfaces.clone(), new CamelInvocationHandler(endpoint, producer, methodCache)); 043 } 044 045 046 /** 047 * Creates a Proxy which sends PojoExchange to the endpoint. 048 */ 049 @SuppressWarnings("unchecked") 050 public static <T> T createProxy(Endpoint endpoint, ClassLoader cl, Class[] interfaces, MethodInfoCache methodCache) throws Exception { 051 return (T) createProxyObject(endpoint, endpoint.createProducer(), cl, interfaces, methodCache); 052 } 053 054 /** 055 * Creates a Proxy which sends PojoExchange to the endpoint. 056 */ 057 @SuppressWarnings("unchecked") 058 public static <T> T createProxy(Endpoint endpoint, ClassLoader cl, Class<T>... interfaceClasses) throws Exception { 059 return (T) createProxy(endpoint, cl, interfaceClasses, createMethodInfoCache(endpoint)); 060 } 061 062 063 /** 064 * Creates a Proxy which sends PojoExchange to the endpoint. 065 */ 066 @SuppressWarnings("unchecked") 067 public static <T> T createProxy(Endpoint endpoint, Class<T>... interfaceClasses) throws Exception { 068 return (T) createProxy(endpoint, getClassLoader(interfaceClasses), interfaceClasses); 069 } 070 071 /** 072 * Creates a Proxy which sends PojoExchange to the endpoint. 073 */ 074 @SuppressWarnings("unchecked") 075 public static <T> T createProxy(Endpoint endpoint, Producer producer, Class<T>... interfaceClasses) throws Exception { 076 return (T) createProxyObject(endpoint, producer, getClassLoader(interfaceClasses), interfaceClasses, createMethodInfoCache(endpoint)); 077 } 078 079 /** 080 * Returns the class loader of the first interface or throws {@link IllegalArgumentException} if there are no interfaces specified 081 */ 082 protected static ClassLoader getClassLoader(Class... interfaces) { 083 if (interfaces == null || interfaces.length < 1) { 084 throw new IllegalArgumentException("You must provide at least 1 interface class."); 085 } 086 return interfaces[0].getClassLoader(); 087 } 088 089 protected static MethodInfoCache createMethodInfoCache(Endpoint endpoint) { 090 return new MethodInfoCache(endpoint.getCamelContext()); 091 } 092 093 }