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 * @deprecated binding will be default true in Camel 3 onwards 073 */ 074 @Deprecated 075 public ProxyBuilder binding(boolean binding) { 076 this.binding = binding; 077 return this; 078 } 079 080 /** 081 * Builds the proxy. 082 * 083 * @param interfaceClass the service interface 084 * @return the proxied bean 085 * @throws Exception is thrown if error creating the proxy 086 */ 087 @SuppressWarnings("unchecked") 088 public <T> T build(Class<T> interfaceClass) throws Exception { 089 // this method is introduced to avoid compiler warnings about the 090 // generic Class arrays in the case we've got only one single Class 091 // to build a Proxy for 092 return build((Class<T>[]) new Class[] {interfaceClass}); 093 } 094 095 /** 096 * Builds the proxy. 097 * 098 * @param interfaceClasses the service interface(s) 099 * @return the proxied bean 100 * @throws Exception is thrown if error creating the proxy 101 */ 102 public <T> T build(Class<T>... interfaceClasses) throws Exception { 103 ObjectHelper.notNull(endpoint, "endpoint"); 104 return ProxyHelper.createProxy(endpoint, binding, interfaceClasses); 105 } 106 107}