Author: wtam Date: Tue Dec 7 22:52:02 2010 New Revision: 1043223 URL: http://svn.apache.org/viewvc?rev=1043223&view=rev Log: [CAMEL-3386] removed client cache in CxfProvider based on Willem's feedback
Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfEndpoint.java camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfProducer.java camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfSpringEndpoint.java camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfEndpoint.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfEndpoint.java?rev=1043223&r1=1043222&r2=1043223&view=diff ============================================================================== --- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfEndpoint.java (original) +++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfEndpoint.java Tue Dec 7 22:52:02 2010 @@ -98,8 +98,7 @@ public class CxfEndpoint extends Default private boolean loggingFeatureEnabled; private String address; private boolean mtomEnabled; - private int maxClientCacheSize = 10; - + public CxfEndpoint(String remaining, CxfComponent cxfComponent) { super(remaining, cxfComponent); setAddress(remaining); @@ -247,12 +246,12 @@ public class CxfEndpoint extends Default /** * Populate a client factory bean */ - protected void setupClientFactoryBean(ClientProxyFactoryBean factoryBean, Class<?> cls, String serviceAddress) { + protected void setupClientFactoryBean(ClientProxyFactoryBean factoryBean, Class<?> cls) { // service class factoryBean.setServiceClass(cls); // address - factoryBean.setAddress(serviceAddress); + factoryBean.setAddress(getAddress()); // wsdl url if (getWsdlURL() != null) { @@ -290,9 +289,9 @@ public class CxfEndpoint extends Default } - protected void setupClientFactoryBean(ClientFactoryBean factoryBean, String serviceAddress) { + protected void setupClientFactoryBean(ClientFactoryBean factoryBean) { // address - factoryBean.setAddress(serviceAddress); + factoryBean.setAddress(getAddress()); // wsdl url if (getWsdlURL() != null) { @@ -336,13 +335,6 @@ public class CxfEndpoint extends Default * Create a CXF client object */ Client createClient() throws Exception { - return createClient(getAddress()); - } - - /** - * Create a CXF client object - */ - Client createClient(String serviceAddress) throws Exception { // get service class if (getDataFormat().equals(DataFormat.POJO)) { @@ -355,14 +347,14 @@ public class CxfEndpoint extends Default // create client factory bean ClientProxyFactoryBean factoryBean = createClientFactoryBean(cls); // setup client factory bean - setupClientFactoryBean(factoryBean, cls, serviceAddress); + setupClientFactoryBean(factoryBean, cls); return ((ClientProxy)Proxy.getInvocationHandler(factoryBean.create())).getClient(); } else { checkName(portName, "endpoint/port name"); checkName(serviceName, "service name"); ClientFactoryBean factoryBean = createClientFactoryBean(); // setup client factory bean - setupClientFactoryBean(factoryBean, serviceAddress); + setupClientFactoryBean(factoryBean); return factoryBean.create(); } @@ -591,20 +583,6 @@ public class CxfEndpoint extends Default } /** - * @param maxClientCacheSize the maxClientCacheSize to set - */ - public void setMaxClientCacheSize(int maxClientCacheSize) { - this.maxClientCacheSize = maxClientCacheSize; - } - - /** - * @return the maxClientCacheSize - */ - public int getMaxClientCacheSize() { - return maxClientCacheSize; - } - - /** * We need to override the {...@link ClientImpl#setParameters} method * to insert parameters into CXF Message for {...@link DataFormat#PAYLOAD} mode. */ Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfProducer.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfProducer.java?rev=1043223&r1=1043222&r2=1043223&view=diff ============================================================================== --- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfProducer.java (original) +++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfProducer.java Tue Dec 7 22:52:02 2010 @@ -17,7 +17,6 @@ package org.apache.camel.component.cxf; import java.io.InputStream; -import java.lang.ref.SoftReference; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; @@ -33,9 +32,7 @@ import org.apache.camel.AsyncProcessor; import org.apache.camel.Exchange; import org.apache.camel.InvalidPayloadException; import org.apache.camel.RuntimeCamelException; -import org.apache.camel.component.cxf.util.CxfEndpointUtils; import org.apache.camel.impl.DefaultProducer; -import org.apache.camel.util.LRUCache; import org.apache.camel.util.ObjectHelper; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -56,9 +53,9 @@ import org.apache.cxf.service.model.Bind */ public class CxfProducer extends DefaultProducer implements AsyncProcessor { private static final Log LOG = LogFactory.getLog(CxfProducer.class); + private Client client; private CxfEndpoint endpoint; - private ClientCache clientCache; - + /** * Constructor to create a CxfProducer. It will create a CXF client * object. @@ -70,7 +67,7 @@ public class CxfProducer extends Default public CxfProducer(CxfEndpoint endpoint) throws Exception { super(endpoint); this.endpoint = endpoint; - clientCache = new ClientCache(endpoint.getMaxClientCacheSize()); + client = endpoint.createClient(); } // As the cxf client async and sync api is implement different, @@ -84,10 +81,8 @@ public class CxfProducer extends Default // create CXF exchange ExchangeImpl cxfExchange = new ExchangeImpl(); - Client client = clientCache.get(CxfEndpointUtils.getEffectiveAddress(camelExchange, endpoint.getAddress())); - // prepare binding operation info - BindingOperationInfo boi = prepareBindingOperation(camelExchange, cxfExchange, client); + BindingOperationInfo boi = prepareBindingOperation(camelExchange, cxfExchange); Map<String, Object> invocationContext = new HashMap<String, Object>(); Map<String, Object> responseContext = new HashMap<String, Object>(); @@ -96,7 +91,7 @@ public class CxfProducer extends Default CxfClientCallback cxfClientCallback = new CxfClientCallback(callback, camelExchange, cxfExchange, boi, endpoint); // send the CXF async request - client.invoke(cxfClientCallback, boi, getParams(endpoint, camelExchange, client), + client.invoke(cxfClientCallback, boi, getParams(endpoint, camelExchange), invocationContext, cxfExchange); } catch (Throwable ex) { // error occurred before we had a chance to go async @@ -121,20 +116,17 @@ public class CxfProducer extends Default // create CXF exchange ExchangeImpl cxfExchange = new ExchangeImpl(); - Client client = clientCache.get(CxfEndpointUtils.getEffectiveAddress(camelExchange, endpoint.getAddress())); - // prepare binding operation info - BindingOperationInfo boi = prepareBindingOperation(camelExchange, cxfExchange, client); + BindingOperationInfo boi = prepareBindingOperation(camelExchange, cxfExchange); Map<String, Object> invocationContext = new HashMap<String, Object>(); Map<String, Object> responseContext = new HashMap<String, Object>(); invocationContext.put(Client.RESPONSE_CONTEXT, responseContext); invocationContext.put(Client.REQUEST_CONTEXT, prepareRequest(camelExchange, cxfExchange)); - try { // send the CXF request - client.invoke(boi, getParams(endpoint, camelExchange, client), + client.invoke(boi, getParams(endpoint, camelExchange), invocationContext, cxfExchange); } finally { // bind the CXF response to Camel exchange @@ -187,9 +179,9 @@ public class CxfProducer extends Default return requestContext.getWrappedMap(); } - private BindingOperationInfo prepareBindingOperation(Exchange camelExchange, org.apache.cxf.message.Exchange cxfExchange, Client client) { + private BindingOperationInfo prepareBindingOperation(Exchange camelExchange, org.apache.cxf.message.Exchange cxfExchange) { // get binding operation info - BindingOperationInfo boi = getBindingOperationInfo(camelExchange, client); + BindingOperationInfo boi = getBindingOperationInfo(camelExchange); ObjectHelper.notNull(boi, "BindingOperationInfo"); // keep the message wrapper in PAYLOAD mode @@ -217,8 +209,8 @@ public class CxfProducer extends Default return boi; } - private void checkParameterSize(CxfEndpoint endpoint, Exchange exchange, Object[] parameters, Client client) { - BindingOperationInfo boi = getBindingOperationInfo(exchange, client); + private void checkParameterSize(CxfEndpoint endpoint, Exchange exchange, Object[] parameters) { + BindingOperationInfo boi = getBindingOperationInfo(exchange); if (boi == null) { throw new RuntimeCamelException("Can't find the binding operation information from camel exchange"); } @@ -262,7 +254,7 @@ public class CxfProducer extends Default /** * Get the parameters for the web service operation */ - private Object[] getParams(CxfEndpoint endpoint, Exchange exchange, Client client) throws InvalidPayloadException { + private Object[] getParams(CxfEndpoint endpoint, Exchange exchange) throws InvalidPayloadException { Object[] params = null; if (endpoint.getDataFormat() == DataFormat.POJO) { @@ -286,7 +278,7 @@ public class CxfProducer extends Default } } // make sure we have the right number of parameters - checkParameterSize(endpoint, exchange, params, client); + checkParameterSize(endpoint, exchange, params); } else if (endpoint.getDataFormat() == DataFormat.PAYLOAD) { params = new Object[1]; @@ -311,7 +303,7 @@ public class CxfProducer extends Default * Get operation name from header and use it to lookup and return a * {...@link BindingOperationInfo}. */ - private BindingOperationInfo getBindingOperationInfo(Exchange ex, Client client) { + private BindingOperationInfo getBindingOperationInfo(Exchange ex) { CxfEndpoint endpoint = (CxfEndpoint)this.getEndpoint(); BindingOperationInfo answer = null; String lp = ex.getIn().getHeader(CxfConstants.OPERATION_NAME, String.class); @@ -353,46 +345,9 @@ public class CxfProducer extends Default } return answer; } - - // only invoked by unit test - public Client getClient() throws Exception { - return clientCache.get(endpoint.getAddress()); + + public Client getClient() { + return client; } - /** - * Cache contains {...@link org.apache.cxf.endpoint.Client} - */ - private class ClientCache { - private LRUCache<String, SoftReference<Client>> cache; - - public ClientCache(final int maxCacheSize) { - this.cache = new LRUCache<String, SoftReference<Client>>(maxCacheSize); - } - - public Client get(String address) throws Exception { - Client retval = null; - synchronized (cache) { - SoftReference<Client> ref = cache.get(address); - - if (ref != null) { - retval = ref.get(); - } - - if (retval == null) { - retval = ((CxfEndpoint)getEndpoint()).createClient(address); - cache.put(address, new SoftReference<Client>(retval)); - - if (LOG.isTraceEnabled()) { - LOG.trace("Created CXF client and add to cache for address '" + address + "'"); - } - - } else { - if (LOG.isTraceEnabled()) { - LOG.trace("Retrieved CXF client from cache for address '" + address + "'"); - } - } - } - return retval; - } - } } Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfSpringEndpoint.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfSpringEndpoint.java?rev=1043223&r1=1043222&r2=1043223&view=diff ============================================================================== --- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfSpringEndpoint.java (original) +++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfSpringEndpoint.java Tue Dec 7 22:52:02 2010 @@ -103,7 +103,7 @@ public class CxfSpringEndpoint extends C * Create a CXF Client */ @Override - Client createClient(String serviceAddress) throws Exception { + Client createClient() throws Exception { // get service class Class<?> cls = getSEIClass(); @@ -120,7 +120,7 @@ public class CxfSpringEndpoint extends C configure(factoryBean); // setup client factory bean - setupClientFactoryBean(factoryBean, cls, serviceAddress); + setupClientFactoryBean(factoryBean, cls); // fill in values that have not been filled. QName serviceQName = null; @@ -145,7 +145,7 @@ public class CxfSpringEndpoint extends C configure(factoryBean); // setup client factory bean - setupClientFactoryBean(factoryBean, serviceAddress); + setupClientFactoryBean(factoryBean); // fill in values that have not been filled. QName serviceQName = null; Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java?rev=1043223&r1=1043222&r2=1043223&view=diff ============================================================================== --- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java (original) +++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java Tue Dec 7 22:52:02 2010 @@ -92,6 +92,16 @@ public class DefaultCxfBinding implement propagateHeadersFromCamelToCxf(camelExchange, camelHeaders, cxfExchange, requestContext); + String overrideAddress = camelExchange.getIn().getHeader(Exchange.DESTINATION_OVERRIDE_URL, String.class); + + if (overrideAddress != null) { + if (LOG.isTraceEnabled()) { + LOG.trace("Client address is overridden by header '" + Exchange.DESTINATION_OVERRIDE_URL + + "' to value '" + overrideAddress + "'"); + } + requestContext.put(Message.ENDPOINT_ADDRESS, overrideAddress); + } + // propagate attachments Set<Attachment> attachments = null; boolean isXop = Boolean.valueOf(camelExchange.getProperty(Message.MTOM_ENABLED, String.class));