Coverage Report - org.mule.endpoint.SimpleEndpointCache
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleEndpointCache
0%
0/25
0%
0/12
3.333
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.endpoint;
 8  
 
 9  
 import org.mule.MessageExchangePattern;
 10  
 import org.mule.api.MuleContext;
 11  
 import org.mule.api.MuleException;
 12  
 import org.mule.api.endpoint.EndpointBuilder;
 13  
 import org.mule.api.endpoint.EndpointCache;
 14  
 import org.mule.api.endpoint.InboundEndpoint;
 15  
 import org.mule.api.endpoint.OutboundEndpoint;
 16  
 
 17  
 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
 18  
 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentMap;
 19  
 
 20  
 /**
 21  
  * Cache endpoints in order to prevent memory leaks.
 22  
  *
 23  
  * @see MULE-5422
 24  
  */
 25  
 public class SimpleEndpointCache implements EndpointCache
 26  
 {
 27  
     protected MuleContext muleContext;
 28  0
     private ConcurrentMap inboundEndpointCache = new ConcurrentHashMap();
 29  0
     private ConcurrentMap outboundEndpointCache = new ConcurrentHashMap();
 30  
 
 31  
     public SimpleEndpointCache(MuleContext muleContext)
 32  0
     {
 33  0
         this.muleContext = muleContext;
 34  0
     }
 35  
 
 36  
     public OutboundEndpoint getOutboundEndpoint(String uri,
 37  
                                                    MessageExchangePattern mep,
 38  
                                                    Long responseTimeout) throws MuleException
 39  
     {
 40  0
         OutboundEndpoint endpoint = (OutboundEndpoint) outboundEndpointCache.get(uri + ":" + mep.toString()
 41  
                                                                                  + ":" + responseTimeout);
 42  0
         if (endpoint == null)
 43  
         {
 44  0
             EndpointBuilder endpointBuilder = muleContext.getEndpointFactory()
 45  
                 .getEndpointBuilder(uri);
 46  0
             endpointBuilder.setExchangePattern(mep);
 47  0
             if (responseTimeout != null && responseTimeout > 0)
 48  
             {
 49  0
                 endpointBuilder.setResponseTimeout(responseTimeout.intValue());
 50  
             }
 51  0
             endpoint = muleContext.getEndpointFactory().getOutboundEndpoint(endpointBuilder);
 52  0
             OutboundEndpoint concurrentlyAddedEndpoint = (OutboundEndpoint) outboundEndpointCache.putIfAbsent(
 53  
                 uri + ":" + mep.toString() + ":" + responseTimeout, endpoint);
 54  0
             if (concurrentlyAddedEndpoint != null)
 55  
             {
 56  0
                 return concurrentlyAddedEndpoint;
 57  
             }
 58  
         }
 59  0
         return endpoint;
 60  
     }
 61  
 
 62  
     public InboundEndpoint getInboundEndpoint(String uri, MessageExchangePattern mep) throws MuleException
 63  
     {
 64  0
         InboundEndpoint endpoint = (InboundEndpoint) inboundEndpointCache.get(uri + ":" + mep.toString());
 65  0
         if (endpoint == null)
 66  
         {
 67  0
             EndpointBuilder endpointBuilder = muleContext.getEndpointFactory()
 68  
                 .getEndpointBuilder(uri);
 69  0
             endpointBuilder.setExchangePattern(mep);
 70  0
             endpoint = muleContext.getEndpointFactory().getInboundEndpoint(endpointBuilder);
 71  0
             InboundEndpoint concurrentlyAddedEndpoint = (InboundEndpoint) inboundEndpointCache.putIfAbsent(
 72  
                 uri + ":" + mep.toString(), endpoint);
 73  0
             if (concurrentlyAddedEndpoint != null)
 74  
             {
 75  0
                 return concurrentlyAddedEndpoint;
 76  
             }
 77  
         }
 78  0
         return endpoint;
 79  
     }
 80  
 }