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