1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.routing.outbound; |
8 | |
|
9 | |
import org.mule.DefaultMuleEvent; |
10 | |
import org.mule.api.MuleEvent; |
11 | |
import org.mule.api.MuleException; |
12 | |
import org.mule.api.MuleMessage; |
13 | |
import org.mule.api.lifecycle.InitialisationException; |
14 | |
import org.mule.api.processor.MessageProcessor; |
15 | |
import org.mule.api.routing.CouldNotRouteOutboundMessageException; |
16 | |
import org.mule.api.routing.RoutePathNotFoundException; |
17 | |
import org.mule.api.routing.RoutingException; |
18 | |
import org.mule.config.i18n.CoreMessages; |
19 | |
import org.mule.transport.NullPayload; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | 0 | public class ChainingRouter extends FilteringOutboundRouter |
26 | |
{ |
27 | |
@Override |
28 | |
public void initialise() throws InitialisationException |
29 | |
{ |
30 | 0 | super.initialise(); |
31 | 0 | if (routes == null || routes.size() == 0) |
32 | |
{ |
33 | 0 | throw new InitialisationException(CoreMessages.objectIsNull("targets"), this); |
34 | |
} |
35 | 0 | } |
36 | |
|
37 | |
@Override |
38 | |
public MuleEvent route(MuleEvent event) throws RoutingException |
39 | |
{ |
40 | 0 | MuleEvent resultToReturn = event; |
41 | 0 | if (routes == null || routes.size() == 0) |
42 | |
{ |
43 | 0 | throw new RoutePathNotFoundException(CoreMessages.noEndpointsForRouter(), event, null); |
44 | |
} |
45 | |
|
46 | 0 | final int endpointsCount = routes.size(); |
47 | 0 | if (logger.isDebugEnabled()) |
48 | |
{ |
49 | 0 | logger.debug("About to chain " + endpointsCount + " targets."); |
50 | |
} |
51 | |
|
52 | |
|
53 | 0 | MessageProcessor endpoint = null; |
54 | |
try |
55 | |
{ |
56 | 0 | MuleMessage intermediaryResult = event.getMessage(); |
57 | |
|
58 | 0 | for (int i = 0; i < endpointsCount; i++) |
59 | |
{ |
60 | 0 | endpoint = getRoute(i, resultToReturn); |
61 | |
|
62 | |
|
63 | 0 | boolean lastEndpointInChain = (i == endpointsCount - 1); |
64 | |
|
65 | 0 | if (logger.isDebugEnabled()) |
66 | |
{ |
67 | 0 | logger.debug("Sending Chained message '" + i + "': " |
68 | |
+ (intermediaryResult == null ? "null" : intermediaryResult.toString())); |
69 | |
} |
70 | |
|
71 | 0 | if (!lastEndpointInChain) |
72 | |
{ |
73 | 0 | MuleEvent endpointResult = sendRequest(resultToReturn, intermediaryResult, endpoint, true); |
74 | 0 | resultToReturn = endpointResult != null ? endpointResult : resultToReturn; |
75 | 0 | MuleMessage localResult = endpointResult == null ? null : endpointResult.getMessage(); |
76 | |
|
77 | |
|
78 | |
|
79 | 0 | if (localResult != null && |
80 | |
|
81 | |
localResult.getPayload() != NullPayload.getInstance() && |
82 | |
intermediaryResult != null) |
83 | |
{ |
84 | 0 | processIntermediaryResult(localResult, intermediaryResult); |
85 | |
} |
86 | 0 | intermediaryResult = localResult; |
87 | |
|
88 | 0 | if (logger.isDebugEnabled()) |
89 | |
{ |
90 | 0 | logger.debug("Received Chain result '" + i + "': " |
91 | |
+ (intermediaryResult != null ? intermediaryResult.toString() : "null")); |
92 | |
} |
93 | |
|
94 | 0 | if (intermediaryResult == null || intermediaryResult.getPayload() == NullPayload.getInstance()) |
95 | |
{ |
96 | |
|
97 | |
|
98 | 0 | resultToReturn = intermediaryResult == null ? null : new DefaultMuleEvent(intermediaryResult, resultToReturn); |
99 | 0 | logger.warn("Chaining router cannot process any further targets. " |
100 | |
+ "There was no result returned from endpoint invocation: " + endpoint); |
101 | 0 | break; |
102 | |
} |
103 | 0 | } |
104 | |
else |
105 | |
{ |
106 | |
|
107 | |
|
108 | 0 | resultToReturn = sendRequest(resultToReturn, intermediaryResult, endpoint, true); |
109 | 0 | if (logger.isDebugEnabled()) |
110 | |
{ |
111 | 0 | MuleMessage resultMessage = resultToReturn == null ? null : resultToReturn.getMessage(); |
112 | 0 | logger.debug("Received final Chain result '" + i + "': " |
113 | |
+ (resultMessage == null ? "null" : resultMessage.toString())); |
114 | |
} |
115 | |
} |
116 | |
} |
117 | |
|
118 | |
} |
119 | 0 | catch (MuleException e) |
120 | |
{ |
121 | 0 | throw new CouldNotRouteOutboundMessageException(resultToReturn, endpoint, e); |
122 | 0 | } |
123 | 0 | return resultToReturn; |
124 | |
} |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
protected void processIntermediaryResult(MuleMessage localResult, MuleMessage intermediaryResult) |
147 | |
{ |
148 | 0 | localResult.setCorrelationId(intermediaryResult.getCorrelationId()); |
149 | 0 | localResult.setCorrelationSequence(intermediaryResult.getCorrelationSequence()); |
150 | 0 | localResult.setCorrelationGroupSize(intermediaryResult.getCorrelationGroupSize()); |
151 | 0 | localResult.setReplyTo(intermediaryResult.getReplyTo()); |
152 | 0 | } |
153 | |
} |