1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
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 | |
|
22 | |
|
23 | |
|
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 | |
} |