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.builder;
018
019import org.apache.camel.CamelContext;
020import org.apache.camel.Endpoint;
021import org.apache.camel.component.bean.ProxyHelper;
022import org.apache.camel.util.ObjectHelper;
023
024/**
025 * A build to create Camel proxies.
026 *
027 * @version 
028 */
029public final class ProxyBuilder {
030
031    private final CamelContext camelContext;
032    private Endpoint endpoint;
033
034    public ProxyBuilder(CamelContext camelContext) {
035        this.camelContext = camelContext;
036    }
037
038    /**
039     * Send the proxied message to this endpoint
040     *
041     * @param url  uri of endpoint
042     * @return the builder
043     */
044    public ProxyBuilder endpoint(String url) {
045        this.endpoint = camelContext.getEndpoint(url);
046        return this;
047    }
048
049    /**
050     * Send the proxied message to this endpoint
051     *
052     * @param endpoint  the endpoint
053     * @return the builder
054     */
055    public ProxyBuilder endpoint(Endpoint endpoint) {
056        this.endpoint = endpoint;
057        return this;
058    }
059
060    /**
061     * Builds the proxy.
062     *
063     * @param interfaceClass  the service interface
064     * @return the proxied bean
065     * @throws Exception is thrown if error creating the proxy
066     */
067    @SuppressWarnings("unchecked")
068    public <T> T build(Class<T> interfaceClass) throws Exception {
069        // this method is introduced to avoid compiler warnings about the
070        // generic Class arrays in the case we've got only one single Class
071        // to build a Proxy for
072        return build((Class<T>[]) new Class[] {interfaceClass});
073    }
074
075    /**
076     * Builds the proxy.
077     *
078     * @param interfaceClasses  the service interface(s)
079     * @return the proxied bean
080     * @throws Exception is thrown if error creating the proxy
081     */
082    public <T> T build(Class<T>... interfaceClasses) throws Exception {
083        ObjectHelper.notNull(endpoint, "endpoint");
084        return ProxyHelper.createProxy(endpoint, interfaceClasses);
085    }
086
087}