1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.routing.outbound; |
12 | |
|
13 | |
import org.mule.config.i18n.CoreMessages; |
14 | |
import org.mule.providers.NullPayload; |
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.RoutePathNotFoundException; |
21 | |
import org.mule.umo.routing.RoutingException; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | 8 | public class ChainingRouter extends FilteringOutboundRouter |
29 | |
{ |
30 | |
|
31 | |
public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous) |
32 | |
throws RoutingException |
33 | |
{ |
34 | 8 | UMOMessage resultToReturn = null; |
35 | 8 | if (endpoints == null || endpoints.size() == 0) |
36 | |
{ |
37 | 0 | throw new RoutePathNotFoundException(CoreMessages.noEndpointsForRouter(), message, null); |
38 | |
} |
39 | |
|
40 | 8 | final int endpointsCount = endpoints.size(); |
41 | 8 | if (logger.isDebugEnabled()) |
42 | |
{ |
43 | 0 | logger.debug("About to chain " + endpointsCount + " endpoints."); |
44 | |
} |
45 | |
|
46 | |
|
47 | 8 | UMOEndpoint endpoint = null; |
48 | |
try |
49 | |
{ |
50 | 8 | UMOMessage intermediaryResult = message; |
51 | |
|
52 | 22 | for (int i = 0; i < endpointsCount; i++) |
53 | |
{ |
54 | 16 | endpoint = getEndpoint(i, intermediaryResult); |
55 | |
|
56 | |
|
57 | 16 | boolean lastEndpointInChain = (i == endpointsCount - 1); |
58 | |
|
59 | 16 | if (logger.isDebugEnabled()) |
60 | |
{ |
61 | 0 | logger.debug("Sending Chained message '" + i + "': " |
62 | |
+ (intermediaryResult == null ? "null" : intermediaryResult.toString())); |
63 | |
} |
64 | |
|
65 | 16 | if (!lastEndpointInChain) |
66 | |
{ |
67 | 10 | UMOMessage localResult = send(session, intermediaryResult, endpoint); |
68 | |
|
69 | |
|
70 | |
|
71 | 10 | if (localResult != null && |
72 | |
|
73 | |
localResult.getPayload() != NullPayload.getInstance() && |
74 | |
intermediaryResult != null) |
75 | |
{ |
76 | 8 | processIntermediaryResult(localResult, intermediaryResult); |
77 | |
} |
78 | 10 | intermediaryResult = localResult; |
79 | |
|
80 | 10 | if (logger.isDebugEnabled()) |
81 | |
{ |
82 | 0 | logger.debug("Received Chain result '" + i + "': " |
83 | |
+ (intermediaryResult != null ? intermediaryResult.toString() : "null")); |
84 | |
} |
85 | |
|
86 | 10 | if (intermediaryResult == null || intermediaryResult.getPayload() == NullPayload.getInstance()) |
87 | |
{ |
88 | |
|
89 | |
|
90 | 2 | resultToReturn = intermediaryResult; |
91 | 2 | logger.warn("Chaining router cannot process any further endpoints. " |
92 | |
+ "There was no result returned from endpoint invocation: " + endpoint); |
93 | 2 | break; |
94 | |
} |
95 | 8 | } |
96 | |
else |
97 | |
{ |
98 | |
|
99 | |
|
100 | 6 | if (synchronous) |
101 | |
{ |
102 | 4 | resultToReturn = send(session, intermediaryResult, endpoint); |
103 | 4 | if (logger.isDebugEnabled()) |
104 | |
{ |
105 | 0 | logger.debug("Received final Chain result '" + i + "': " |
106 | |
+ (resultToReturn == null ? "null" : resultToReturn.toString())); |
107 | |
} |
108 | |
} |
109 | |
else |
110 | |
{ |
111 | |
|
112 | 2 | resultToReturn = null; |
113 | 2 | dispatch(session, intermediaryResult, endpoint); |
114 | |
} |
115 | |
} |
116 | |
} |
117 | |
|
118 | |
} |
119 | 0 | catch (UMOException e) |
120 | |
{ |
121 | 0 | throw new CouldNotRouteOutboundMessageException(message, endpoint, e); |
122 | 8 | } |
123 | 8 | return resultToReturn; |
124 | |
} |
125 | |
|
126 | |
public void addEndpoint(UMOEndpoint endpoint) |
127 | |
{ |
128 | 18 | if (!endpoint.isRemoteSync()) |
129 | |
{ |
130 | 18 | logger.debug("Endpoint: " |
131 | |
+ endpoint.getEndpointURI() |
132 | |
+ " registered on chaining router needs to be RemoteSync enabled. Setting this property now"); |
133 | 18 | endpoint.setRemoteSync(true); |
134 | |
} |
135 | 18 | super.addEndpoint(endpoint); |
136 | 18 | } |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
protected void processIntermediaryResult(UMOMessage localResult, UMOMessage intermediaryResult) |
159 | |
{ |
160 | 8 | localResult.setCorrelationId(intermediaryResult.getCorrelationId()); |
161 | 8 | localResult.setCorrelationSequence(intermediaryResult.getCorrelationSequence()); |
162 | 8 | localResult.setCorrelationGroupSize(intermediaryResult.getCorrelationGroupSize()); |
163 | 8 | localResult.setReplyTo(intermediaryResult.getReplyTo()); |
164 | 8 | } |
165 | |
} |