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: 36321 $
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         * @throws Exception
041         */
042        public static Object createProxy(final Endpoint endpoint, ClassLoader cl, Class interfaces[])
043            throws Exception {
044            final Producer producer = endpoint.createProducer();
045            return Proxy.newProxyInstance(cl, interfaces, new CamelInvocationHandler(endpoint, producer));
046        }
047    
048        /**
049         * Creates a Proxy which sends PojoExchange to the endpoint.
050         *
051         * @throws Exception
052         */
053        public static Object createProxy(Endpoint endpoint, Class interfaces[]) throws Exception {
054            if (interfaces.length < 1) {
055                throw new IllegalArgumentException("You must provide at least 1 interface class.");
056            }
057            return createProxy(endpoint, interfaces[0].getClassLoader(), interfaces);
058        }
059    
060        /**
061         * Creates a Proxy which sends PojoExchange to the endpoint.
062         *
063         * @throws Exception
064         */
065        @SuppressWarnings("unchecked")
066        public static <T> T createProxy(Endpoint endpoint, ClassLoader cl, Class<T> interfaceClass)
067            throws Exception {
068            return (T)createProxy(endpoint, cl, new Class[] {interfaceClass});
069        }
070    
071        /**
072         * Creates a Proxy which sends PojoExchange to the endpoint.
073         *
074         * @throws Exception
075         */
076        @SuppressWarnings("unchecked")
077        public static <T> T createProxy(Endpoint endpoint, Class<T> interfaceClass) throws Exception {
078            return (T)createProxy(endpoint, new Class[] {interfaceClass});
079        }
080    
081    }