1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.routing.outbound; |
12 | |
|
13 | |
import org.mule.impl.MuleMessage; |
14 | |
import org.mule.impl.endpoint.MuleEndpoint; |
15 | |
import org.mule.umo.UMOException; |
16 | |
import org.mule.umo.UMOMessage; |
17 | |
import org.mule.umo.UMOSession; |
18 | |
import org.mule.umo.endpoint.UMOEndpoint; |
19 | |
import org.mule.umo.routing.CouldNotRouteOutboundMessageException; |
20 | |
import org.mule.umo.routing.RoutingException; |
21 | |
|
22 | |
import java.util.ArrayList; |
23 | |
import java.util.Iterator; |
24 | |
import java.util.List; |
25 | |
|
26 | |
import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; |
27 | |
import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentMap; |
28 | |
import org.apache.commons.logging.Log; |
29 | |
import org.apache.commons.logging.LogFactory; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 4 | public abstract class AbstractRecipientList extends FilteringOutboundRouter |
38 | |
{ |
39 | |
|
40 | |
|
41 | |
|
42 | 4 | protected final Log logger = LogFactory.getLog(getClass()); |
43 | |
|
44 | 4 | private final ConcurrentMap recipientCache = new ConcurrentHashMap(); |
45 | |
|
46 | |
public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous) |
47 | |
throws RoutingException |
48 | |
{ |
49 | 6 | List recipients = this.getRecipients(message); |
50 | 6 | List results = new ArrayList(); |
51 | |
|
52 | 6 | if (enableCorrelation != ENABLE_CORRELATION_NEVER) |
53 | |
{ |
54 | 6 | boolean correlationSet = message.getCorrelationGroupSize() != -1; |
55 | 6 | if (correlationSet && (enableCorrelation == ENABLE_CORRELATION_IF_NOT_SET)) |
56 | |
{ |
57 | 0 | logger.debug("CorrelationId is already set, not setting Correlation group size"); |
58 | |
} |
59 | |
else |
60 | |
{ |
61 | |
|
62 | 6 | message.setCorrelationGroupSize(recipients.size()); |
63 | |
} |
64 | |
} |
65 | |
|
66 | 6 | UMOMessage result = null; |
67 | |
UMOEndpoint endpoint; |
68 | |
UMOMessage request; |
69 | |
|
70 | 6 | for (Iterator iterator = recipients.iterator(); iterator.hasNext();) |
71 | |
{ |
72 | 12 | String recipient = (String) iterator.next(); |
73 | |
|
74 | |
|
75 | |
|
76 | 12 | request = new MuleMessage(message.getPayload(), message); |
77 | 12 | endpoint = this.getRecipientEndpoint(request, recipient); |
78 | |
|
79 | |
try |
80 | |
{ |
81 | 10 | if (synchronous) |
82 | |
{ |
83 | 6 | result = this.send(session, request, endpoint); |
84 | 6 | if (result != null) |
85 | |
{ |
86 | 6 | results.add(result.getPayload()); |
87 | |
} |
88 | |
else |
89 | |
{ |
90 | 0 | if (logger.isDebugEnabled()) |
91 | |
{ |
92 | 0 | logger.debug("No result was returned for sync call to: " |
93 | |
+ endpoint.getEndpointURI()); |
94 | |
} |
95 | |
} |
96 | |
} |
97 | |
else |
98 | |
{ |
99 | 4 | this.dispatch(session, request, endpoint); |
100 | |
} |
101 | |
} |
102 | 0 | catch (UMOException e) |
103 | |
{ |
104 | 0 | throw new CouldNotRouteOutboundMessageException(request, endpoint, e); |
105 | 10 | } |
106 | 10 | } |
107 | |
|
108 | 4 | if (results.size() == 0) |
109 | |
{ |
110 | 2 | return null; |
111 | |
} |
112 | 2 | else if (results.size() == 1) |
113 | |
{ |
114 | 0 | return new MuleMessage(results.get(0), result); |
115 | |
} |
116 | |
else |
117 | |
{ |
118 | 2 | return new MuleMessage(results, result); |
119 | |
} |
120 | |
} |
121 | |
|
122 | |
protected UMOEndpoint getRecipientEndpoint(UMOMessage message, String recipient) throws RoutingException |
123 | |
{ |
124 | 12 | UMOEndpoint endpoint = (UMOEndpoint) recipientCache.get(recipient); |
125 | |
|
126 | 12 | if (endpoint != null) |
127 | |
{ |
128 | 4 | return endpoint; |
129 | |
} |
130 | |
|
131 | |
try |
132 | |
{ |
133 | 8 | endpoint = MuleEndpoint.getOrCreateEndpointForUri(recipient, UMOEndpoint.ENDPOINT_TYPE_SENDER); |
134 | |
} |
135 | 2 | catch (UMOException e) |
136 | |
{ |
137 | 2 | throw new RoutingException(message, endpoint, e); |
138 | 6 | } |
139 | |
|
140 | 6 | UMOEndpoint existingEndpoint = (UMOEndpoint) recipientCache.putIfAbsent(recipient, endpoint); |
141 | 6 | if (existingEndpoint != null) |
142 | |
{ |
143 | 0 | endpoint = existingEndpoint; |
144 | |
} |
145 | |
|
146 | 6 | return endpoint; |
147 | |
} |
148 | |
|
149 | |
protected abstract List getRecipients(UMOMessage message); |
150 | |
|
151 | |
public boolean isDynamicEndpoints() |
152 | |
{ |
153 | 0 | return true; |
154 | |
} |
155 | |
|
156 | |
} |