1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.routing.outbound; |
12 | |
|
13 | |
import org.mule.DefaultMuleMessage; |
14 | |
import org.mule.api.MessagingException; |
15 | |
import org.mule.api.MuleContext; |
16 | |
import org.mule.api.MuleEvent; |
17 | |
import org.mule.api.MuleMessage; |
18 | |
import org.mule.api.MuleSession; |
19 | |
import org.mule.api.lifecycle.Disposable; |
20 | |
import org.mule.api.lifecycle.Initialisable; |
21 | |
import org.mule.api.lifecycle.InitialisationException; |
22 | |
import org.mule.api.routing.MatchableMessageProcessor; |
23 | |
import org.mule.api.routing.OutboundRouter; |
24 | |
import org.mule.api.routing.OutboundRouterCatchAllStrategy; |
25 | |
import org.mule.api.routing.OutboundRouterCollection; |
26 | |
import org.mule.api.routing.RouterStatisticsRecorder; |
27 | |
import org.mule.api.routing.RoutingException; |
28 | |
import org.mule.api.routing.TransformingMatchable; |
29 | |
import org.mule.config.i18n.CoreMessages; |
30 | |
import org.mule.management.stats.RouterStatistics; |
31 | |
import org.mule.routing.AbstractCatchAllStrategy; |
32 | |
import org.mule.util.ObjectUtils; |
33 | |
|
34 | |
import java.util.Iterator; |
35 | |
import java.util.List; |
36 | |
|
37 | |
import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList; |
38 | |
|
39 | |
import org.apache.commons.logging.Log; |
40 | |
import org.apache.commons.logging.LogFactory; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 0 | public class DefaultOutboundRouterCollection implements OutboundRouterCollection |
50 | |
{ |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | 0 | protected final transient Log logger = LogFactory.getLog(getClass()); |
56 | |
|
57 | 0 | @SuppressWarnings("unchecked") |
58 | |
protected List<MatchableMessageProcessor> routers = new CopyOnWriteArrayList(); |
59 | 0 | protected boolean matchAll = false; |
60 | |
private OutboundRouterCatchAllStrategy catchAllStrategy; |
61 | |
|
62 | 0 | protected RouterStatistics statistics = new RouterStatistics(RouterStatistics.TYPE_OUTBOUND); |
63 | |
protected MuleContext muleContext; |
64 | |
|
65 | |
public MuleEvent process(final MuleEvent event) throws MessagingException |
66 | |
{ |
67 | 0 | MuleMessage message = event.getMessage(); |
68 | 0 | MuleSession session = event.getSession(); |
69 | |
MuleEvent result; |
70 | 0 | boolean matchfound = false; |
71 | |
|
72 | 0 | for (Iterator<MatchableMessageProcessor> iterator = getRoutes().iterator(); iterator.hasNext();) |
73 | |
{ |
74 | 0 | OutboundRouter outboundRouter = (OutboundRouter) iterator.next(); |
75 | |
|
76 | |
final MuleMessage outboundRouterMessage; |
77 | |
|
78 | |
|
79 | |
|
80 | 0 | if (iterator.hasNext() |
81 | |
&& (isMatchAll() || ((outboundRouter instanceof TransformingMatchable) && ((TransformingMatchable) outboundRouter).isTransformBeforeMatch()))) |
82 | |
{ |
83 | 0 | if (((DefaultMuleMessage) message).isConsumable()) |
84 | |
{ |
85 | 0 | throw new MessagingException(CoreMessages.cannotCopyStreamPayload(message.getPayload() |
86 | |
.getClass() |
87 | |
.getName()), event); |
88 | |
} |
89 | 0 | outboundRouterMessage = new DefaultMuleMessage(message.getPayload(), message, muleContext); |
90 | |
} |
91 | |
else |
92 | |
{ |
93 | 0 | outboundRouterMessage = message; |
94 | |
} |
95 | |
|
96 | |
try |
97 | |
{ |
98 | 0 | if (outboundRouter.isMatch(outboundRouterMessage)) |
99 | |
{ |
100 | 0 | matchfound = true; |
101 | |
|
102 | 0 | final OutboundRouter router = outboundRouter; |
103 | |
|
104 | 0 | result = router.process(event); |
105 | |
|
106 | 0 | if (!isMatchAll()) |
107 | |
{ |
108 | 0 | return result; |
109 | |
} |
110 | |
} |
111 | |
} |
112 | 0 | catch (MessagingException e) |
113 | |
{ |
114 | 0 | throw e; |
115 | |
} |
116 | 0 | catch (Exception e) |
117 | |
{ |
118 | 0 | throw new RoutingException(event, outboundRouter, e); |
119 | 0 | } |
120 | 0 | } |
121 | |
|
122 | 0 | if (!matchfound && getCatchAllStrategy() != null) |
123 | |
{ |
124 | 0 | if (logger.isDebugEnabled()) |
125 | |
{ |
126 | 0 | logger.debug("Message did not match any routers on: " + session.getFlowConstruct().getName() |
127 | |
+ " invoking catch all strategy"); |
128 | |
} |
129 | 0 | return catchAll(event); |
130 | |
} |
131 | 0 | else if (!matchfound) |
132 | |
{ |
133 | 0 | logger.warn("Message did not match any routers on: " |
134 | |
+ session.getFlowConstruct().getName() |
135 | |
+ " and there is no catch all strategy configured on this router. Disposing message " |
136 | |
+ message); |
137 | |
} |
138 | 0 | return event; |
139 | |
} |
140 | |
|
141 | |
protected MuleEvent catchAll(MuleEvent event) throws RoutingException |
142 | |
{ |
143 | 0 | if (getRouterStatistics().isEnabled()) |
144 | |
{ |
145 | 0 | getRouterStatistics().incrementCaughtMessage(); |
146 | |
} |
147 | |
|
148 | 0 | return getCatchAllStrategy().process(event); |
149 | |
} |
150 | |
|
151 | |
public void initialise() throws InitialisationException |
152 | |
{ |
153 | 0 | for (MatchableMessageProcessor router : routers) |
154 | |
{ |
155 | 0 | if (router instanceof Initialisable) |
156 | |
{ |
157 | 0 | ((Initialisable) router).initialise(); |
158 | |
} |
159 | |
} |
160 | 0 | } |
161 | |
|
162 | |
public void dispose() |
163 | |
{ |
164 | 0 | for (MatchableMessageProcessor router : routers) |
165 | |
{ |
166 | 0 | if (router instanceof Disposable) |
167 | |
{ |
168 | 0 | ((Disposable) router).dispose(); |
169 | |
} |
170 | |
} |
171 | 0 | } |
172 | |
|
173 | |
|
174 | |
@Deprecated |
175 | |
public void setMessageProcessors(List<MatchableMessageProcessor> routers) |
176 | |
{ |
177 | 0 | for (MatchableMessageProcessor router : routers) |
178 | |
{ |
179 | 0 | addRoute(router); |
180 | |
} |
181 | 0 | } |
182 | |
|
183 | |
public void addRoute(MatchableMessageProcessor router) |
184 | |
{ |
185 | 0 | if (router instanceof RouterStatisticsRecorder) |
186 | |
{ |
187 | 0 | ((RouterStatisticsRecorder) router).setRouterStatistics(getRouterStatistics()); |
188 | |
} |
189 | 0 | routers.add(router); |
190 | 0 | } |
191 | |
|
192 | |
public void removeRoute(MatchableMessageProcessor router) |
193 | |
{ |
194 | 0 | routers.remove(router); |
195 | 0 | } |
196 | |
|
197 | |
public List<MatchableMessageProcessor> getRoutes() |
198 | |
{ |
199 | 0 | return routers; |
200 | |
} |
201 | |
|
202 | |
public OutboundRouterCatchAllStrategy getCatchAllStrategy() |
203 | |
{ |
204 | 0 | return catchAllStrategy; |
205 | |
} |
206 | |
|
207 | |
public void setCatchAllStrategy(OutboundRouterCatchAllStrategy catchAllStrategy) |
208 | |
{ |
209 | 0 | this.catchAllStrategy = catchAllStrategy; |
210 | 0 | if (this.catchAllStrategy != null && catchAllStrategy instanceof AbstractCatchAllStrategy) |
211 | |
{ |
212 | 0 | ((AbstractCatchAllStrategy) this.catchAllStrategy).setRouterStatistics(statistics); |
213 | |
} |
214 | 0 | } |
215 | |
|
216 | |
public boolean isMatchAll() |
217 | |
{ |
218 | 0 | return matchAll; |
219 | |
} |
220 | |
|
221 | |
public void setMatchAll(boolean matchAll) |
222 | |
{ |
223 | 0 | this.matchAll = matchAll; |
224 | 0 | } |
225 | |
|
226 | |
public RouterStatistics getRouterStatistics() |
227 | |
{ |
228 | 0 | return statistics; |
229 | |
} |
230 | |
|
231 | |
public void setRouterStatistics(RouterStatistics stat) |
232 | |
{ |
233 | 0 | this.statistics = stat; |
234 | 0 | } |
235 | |
|
236 | |
public void setMuleContext(MuleContext context) |
237 | |
{ |
238 | 0 | this.muleContext = context; |
239 | 0 | } |
240 | |
|
241 | |
public boolean hasEndpoints() |
242 | |
{ |
243 | 0 | for (Iterator iterator = routers.iterator(); iterator.hasNext();) |
244 | |
{ |
245 | 0 | OutboundRouter router = (OutboundRouter) iterator.next(); |
246 | 0 | if (router.getRoutes().size() > 0 || router.isDynamicRoutes()) |
247 | |
{ |
248 | 0 | return true; |
249 | |
} |
250 | 0 | } |
251 | 0 | return false; |
252 | |
} |
253 | |
|
254 | |
@Override |
255 | |
public String toString() |
256 | |
{ |
257 | 0 | return ObjectUtils.toString(this); |
258 | |
} |
259 | |
} |