View Javadoc
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      private ConcurrentMap inboundEndpointCache = new ConcurrentHashMap();
29      private ConcurrentMap outboundEndpointCache = new ConcurrentHashMap();
30  
31      public SimpleEndpointCache(MuleContext muleContext)
32      {
33          this.muleContext = muleContext;
34      }
35  
36      public OutboundEndpoint getOutboundEndpoint(String uri,
37                                                     MessageExchangePattern mep,
38                                                     Long responseTimeout) throws MuleException
39      {
40          OutboundEndpoint endpoint = (OutboundEndpoint) outboundEndpointCache.get(uri + ":" + mep.toString()
41                                                                                   + ":" + responseTimeout);
42          if (endpoint == null)
43          {
44              EndpointBuilder endpointBuilder = muleContext.getEndpointFactory()
45                  .getEndpointBuilder(uri);
46              endpointBuilder.setExchangePattern(mep);
47              if (responseTimeout != null && responseTimeout > 0)
48              {
49                  endpointBuilder.setResponseTimeout(responseTimeout.intValue());
50              }
51              endpoint = muleContext.getEndpointFactory().getOutboundEndpoint(endpointBuilder);
52              OutboundEndpoint concurrentlyAddedEndpoint = (OutboundEndpoint) outboundEndpointCache.putIfAbsent(
53                  uri + ":" + mep.toString() + ":" + responseTimeout, endpoint);
54              if (concurrentlyAddedEndpoint != null)
55              {
56                  return concurrentlyAddedEndpoint;
57              }
58          }
59          return endpoint;
60      }
61  
62      public InboundEndpoint getInboundEndpoint(String uri, MessageExchangePattern mep) throws MuleException
63      {
64          InboundEndpoint endpoint = (InboundEndpoint) inboundEndpointCache.get(uri + ":" + mep.toString());
65          if (endpoint == null)
66          {
67              EndpointBuilder endpointBuilder = muleContext.getEndpointFactory()
68                  .getEndpointBuilder(uri);
69              endpointBuilder.setExchangePattern(mep);
70              endpoint = muleContext.getEndpointFactory().getInboundEndpoint(endpointBuilder);
71              InboundEndpoint concurrentlyAddedEndpoint = (InboundEndpoint) inboundEndpointCache.putIfAbsent(
72                  uri + ":" + mep.toString(), endpoint);
73              if (concurrentlyAddedEndpoint != null)
74              {
75                  return concurrentlyAddedEndpoint;
76              }
77          }
78          return endpoint;
79      }
80  }