View Javadoc

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