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 private boolean binding = true; 034 035 public ProxyBuilder(CamelContext camelContext) { 036 this.camelContext = camelContext; 037 } 038 039 /** 040 * Send the proxied message to this endpoint 041 * 042 * @param url uri of endpoint 043 * @return the builder 044 */ 045 public ProxyBuilder endpoint(String url) { 046 this.endpoint = camelContext.getEndpoint(url); 047 return this; 048 } 049 050 /** 051 * Send the proxied message to this endpoint 052 * 053 * @param endpoint the endpoint 054 * @return the builder 055 */ 056 public ProxyBuilder endpoint(Endpoint endpoint) { 057 this.endpoint = endpoint; 058 return this; 059 } 060 061 /** 062 * Whether to use binding or not. 063 * <p/> 064 * Binding is enabled by default. Set this to <tt>false</tt> to use old behavior without binding. 065 * <p/> 066 * If binding is enabled then Camel will bind the method parameters to the input {@link org.apache.camel.Message} 067 * on the {@link org.apache.camel.Exchange} when invoking the proxy. 068 * 069 * @param binding <tt>true</tt> to use binding, <tt>false</tt> to use the old behavior with using a {@link org.apache.camel.component.bean.BeanInvocation} 070 * as a provisional message body 071 * @return the builder 072 */ 073 public ProxyBuilder binding(boolean binding) { 074 this.binding = binding; 075 return this; 076 } 077 078 /** 079 * Builds the proxy. 080 * 081 * @param interfaceClass the service interface 082 * @return the proxied bean 083 * @throws Exception is thrown if error creating the proxy 084 */ 085 @SuppressWarnings("unchecked") 086 public <T> T build(Class<T> interfaceClass) throws Exception { 087 // this method is introduced to avoid compiler warnings about the 088 // generic Class arrays in the case we've got only one single Class 089 // to build a Proxy for 090 return build((Class<T>[]) new Class[] {interfaceClass}); 091 } 092 093 /** 094 * Builds the proxy. 095 * 096 * @param interfaceClasses the service interface(s) 097 * @return the proxied bean 098 * @throws Exception is thrown if error creating the proxy 099 */ 100 public <T> T build(Class<T>... interfaceClasses) throws Exception { 101 ObjectHelper.notNull(endpoint, "endpoint"); 102 return ProxyHelper.createProxy(endpoint, binding, interfaceClasses); 103 } 104 105}