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.lang.reflect.Proxy; 020 021import org.apache.camel.Endpoint; 022import org.apache.camel.Producer; 023import org.apache.camel.processor.DeferServiceFactory; 024 025/** 026 * A helper class for creating proxies which delegate to Camel 027 * 028 * @version 029 */ 030public final class ProxyHelper { 031 032 /** 033 * Utility classes should not have a public constructor. 034 */ 035 private ProxyHelper() { 036 } 037 038 /** 039 * Creates a Proxy which sends the exchange to the endpoint. 040 * 041 * @deprecated use the same method name with binding as parameter 042 */ 043 @Deprecated 044 public static <T> T createProxyObject(Endpoint endpoint, Producer producer, ClassLoader classLoader, Class<T>[] interfaces, MethodInfoCache methodCache) { 045 return createProxyObject(endpoint, true, producer, classLoader, interfaces, methodCache); 046 } 047 048 /** 049 * Creates a Proxy which sends the exchange to the endpoint. 050 */ 051 @SuppressWarnings("unchecked") 052 public static <T> T createProxyObject(Endpoint endpoint, boolean binding, Producer producer, ClassLoader classLoader, Class<T>[] interfaces, MethodInfoCache methodCache) { 053 return (T) Proxy.newProxyInstance(classLoader, interfaces.clone(), new CamelInvocationHandler(endpoint, binding, producer, methodCache)); 054 } 055 056 /** 057 * Creates a Proxy which sends the exchange to the endpoint. 058 * 059 * @deprecated use the same method name with binding as parameter 060 */ 061 @Deprecated 062 public static <T> T createProxy(Endpoint endpoint, ClassLoader cl, Class<T> interfaceClass, MethodInfoCache methodCache) throws Exception { 063 return createProxy(endpoint, true, cl, toArray(interfaceClass), methodCache); 064 } 065 066 /** 067 * Creates a Proxy which sends the exchange to the endpoint. 068 */ 069 public static <T> T createProxy(Endpoint endpoint, boolean binding, ClassLoader cl, Class<T> interfaceClass, MethodInfoCache methodCache) throws Exception { 070 return createProxy(endpoint, binding, cl, toArray(interfaceClass), methodCache); 071 } 072 073 /** 074 * Creates a Proxy which sends the exchange to the endpoint. 075 * 076 * @deprecated use the same method name with binding as parameter 077 */ 078 @Deprecated 079 public static <T> T createProxy(Endpoint endpoint, ClassLoader cl, Class<T>[] interfaceClasses, MethodInfoCache methodCache) throws Exception { 080 return createProxy(endpoint, true, cl, interfaceClasses, methodCache); 081 } 082 083 /** 084 * Creates a Proxy which sends the exchange to the endpoint. 085 */ 086 public static <T> T createProxy(Endpoint endpoint, boolean binding, ClassLoader cl, Class<T>[] interfaceClasses, MethodInfoCache methodCache) throws Exception { 087 Producer producer = DeferServiceFactory.createProducer(endpoint); 088 endpoint.getCamelContext().deferStartService(producer, true); 089 return createProxyObject(endpoint, binding, producer, cl, interfaceClasses, methodCache); 090 } 091 092 /** 093 * Creates a Proxy which sends the exchange to the endpoint. 094 */ 095 public static <T> T createProxy(Endpoint endpoint, ClassLoader cl, Class<T> interfaceClass) throws Exception { 096 return createProxy(endpoint, true, cl, toArray(interfaceClass)); 097 } 098 099 /** 100 * Creates a Proxy which sends the exchange to the endpoint. 101 * 102 * @deprecated use the same method name with binding as parameter 103 */ 104 @Deprecated 105 public static <T> T createProxy(Endpoint endpoint, ClassLoader cl, Class<T>... interfaceClasses) throws Exception { 106 return createProxy(endpoint, true, cl, interfaceClasses); 107 } 108 109 /** 110 * Creates a Proxy which sends the exchange to the endpoint. 111 */ 112 public static <T> T createProxy(Endpoint endpoint, boolean binding, ClassLoader cl, Class<T>... interfaceClasses) throws Exception { 113 return createProxy(endpoint, binding, cl, interfaceClasses, createMethodInfoCache(endpoint)); 114 } 115 116 /** 117 * Creates a Proxy which sends the exchange to the endpoint. 118 */ 119 public static <T> T createProxy(Endpoint endpoint, Class<T> interfaceClass) throws Exception { 120 return createProxy(endpoint, true, toArray(interfaceClass)); 121 } 122 123 /** 124 * Creates a Proxy which sends the exchange to the endpoint. 125 * 126 * @deprecated use the same method name with binding as parameter 127 */ 128 @Deprecated 129 public static <T> T createProxy(Endpoint endpoint, Class<T>... interfaceClasses) throws Exception { 130 return createProxy(endpoint, true, interfaceClasses); 131 } 132 133 /** 134 * Creates a Proxy which sends the exchange to the endpoint. 135 */ 136 public static <T> T createProxy(Endpoint endpoint, boolean binding, Class<T>... interfaceClasses) throws Exception { 137 return createProxy(endpoint, binding, getClassLoader(interfaceClasses), interfaceClasses); 138 } 139 140 /** 141 * Creates a Proxy which sends the exchange to the endpoint. 142 */ 143 public static <T> T createProxy(Endpoint endpoint, Producer producer, Class<T> interfaceClass) throws Exception { 144 return createProxy(endpoint, true, producer, toArray(interfaceClass)); 145 } 146 147 /** 148 * Creates a Proxy which sends the exchange to the endpoint. 149 * 150 * @deprecated use the same method name with binding as parameter 151 */ 152 @Deprecated 153 public static <T> T createProxy(Endpoint endpoint, Producer producer, Class<T>... interfaceClasses) throws Exception { 154 return createProxyObject(endpoint, true, producer, getClassLoader(interfaceClasses), interfaceClasses, createMethodInfoCache(endpoint)); 155 } 156 157 /** 158 * Creates a Proxy which sends the exchange to the endpoint. 159 */ 160 public static <T> T createProxy(Endpoint endpoint, boolean binding, Producer producer, Class<T>... interfaceClasses) throws Exception { 161 return createProxyObject(endpoint, binding, producer, getClassLoader(interfaceClasses), interfaceClasses, createMethodInfoCache(endpoint)); 162 } 163 164 /** 165 * Returns the class loader of the first interface or throws {@link IllegalArgumentException} if there are no interfaces specified 166 */ 167 protected static ClassLoader getClassLoader(Class<?>... interfaces) { 168 if (interfaces == null || interfaces.length < 1) { 169 throw new IllegalArgumentException("You must provide at least 1 interface class."); 170 } 171 return interfaces[0].getClassLoader(); 172 } 173 174 protected static MethodInfoCache createMethodInfoCache(Endpoint endpoint) { 175 return new MethodInfoCache(endpoint.getCamelContext()); 176 } 177 178 @SuppressWarnings("unchecked") 179 private static <T> Class<T>[] toArray(Class<T> interfaceClass) { 180 // this method and it's usage is introduced to avoid compiler warnings 181 // about the generic Class arrays in the case we've got only one single 182 // Class to build a Proxy for 183 return new Class[] {interfaceClass}; 184 } 185}