1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.routing.inbound; |
12 | |
|
13 | |
import org.mule.api.MessagingException; |
14 | |
import org.mule.api.MuleEvent; |
15 | |
import org.mule.api.MuleException; |
16 | |
import org.mule.api.MuleMessage; |
17 | |
import org.mule.api.config.MuleProperties; |
18 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
19 | |
import org.mule.api.endpoint.InboundEndpoint; |
20 | |
import org.mule.api.endpoint.InvalidEndpointTypeException; |
21 | |
import org.mule.api.routing.InboundRouter; |
22 | |
import org.mule.api.routing.InboundRouterCollection; |
23 | |
import org.mule.api.routing.RoutingException; |
24 | |
import org.mule.config.i18n.CoreMessages; |
25 | |
import org.mule.management.stats.RouterStatistics; |
26 | |
import org.mule.routing.AbstractRouterCollection; |
27 | |
import org.mule.util.StringMessageUtils; |
28 | |
import org.mule.util.StringUtils; |
29 | |
|
30 | |
import java.util.Iterator; |
31 | |
import java.util.List; |
32 | |
|
33 | |
import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList; |
34 | |
import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; |
35 | |
import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentMap; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public class DefaultInboundRouterCollection extends AbstractRouterCollection implements InboundRouterCollection |
45 | |
{ |
46 | 428 | private final List endpoints = new CopyOnWriteArrayList(); |
47 | |
|
48 | |
public DefaultInboundRouterCollection() |
49 | |
{ |
50 | 428 | super(RouterStatistics.TYPE_INBOUND); |
51 | |
|
52 | 428 | setMatchAll(true); |
53 | 428 | } |
54 | |
|
55 | |
public MuleMessage route(MuleEvent event) throws MessagingException |
56 | |
{ |
57 | |
|
58 | 18 | String inboundEndpoint = |
59 | |
|
60 | |
event.getEndpoint().getEndpointURI().getEndpointName(); |
61 | |
|
62 | 18 | if (StringUtils.isBlank(inboundEndpoint)) |
63 | |
{ |
64 | |
|
65 | 18 | inboundEndpoint = event.getEndpoint().getName(); |
66 | |
} |
67 | 18 | if (StringUtils.isBlank(inboundEndpoint)) |
68 | |
{ |
69 | |
|
70 | 0 | inboundEndpoint = event.getEndpoint().getEndpointURI().getUri().toString(); |
71 | |
} |
72 | 18 | event.getMessage().setProperty(MuleProperties.MULE_ORIGINATING_ENDPOINT_PROPERTY, inboundEndpoint); |
73 | |
|
74 | 18 | if (endpoints.size() > 0 && routers.size() == 0) |
75 | |
{ |
76 | 0 | addRouter(new InboundPassThroughRouter()); |
77 | |
} |
78 | |
|
79 | 18 | String componentName = event.getSession().getService().getName(); |
80 | |
|
81 | 18 | ConcurrentMap eventsToRoute = new ConcurrentHashMap(2); |
82 | 18 | boolean noRoute = true; |
83 | 18 | boolean match = false; |
84 | |
InboundRouter umoInboundRouter; |
85 | 18 | MuleEvent lastEvent= null; |
86 | |
|
87 | 18 | for (Iterator iterator = getRouters().iterator(); iterator.hasNext();) |
88 | |
{ |
89 | 18 | umoInboundRouter = (InboundRouter) iterator.next(); |
90 | |
|
91 | 18 | if (umoInboundRouter.isMatch(event)) |
92 | |
{ |
93 | 12 | match = true; |
94 | 12 | MuleEvent[] events = umoInboundRouter.process(event); |
95 | 12 | if(events!=null) |
96 | |
{ |
97 | 24 | for (int i = 0; i < events.length; i++) |
98 | |
{ |
99 | 12 | lastEvent = events[i]; |
100 | |
|
101 | 12 | eventsToRoute.putIfAbsent(lastEvent.getId(), lastEvent); |
102 | |
} |
103 | |
} |
104 | |
|
105 | 12 | noRoute = (events == null); |
106 | 12 | if (!isMatchAll()) |
107 | |
{ |
108 | 0 | break; |
109 | |
} |
110 | 12 | } |
111 | |
} |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | 18 | if (!event.isStopFurtherProcessing()) |
118 | |
{ |
119 | 18 | if (noRoute) |
120 | |
{ |
121 | |
|
122 | 6 | if (getStatistics().isEnabled()) |
123 | |
{ |
124 | 0 | getStatistics().incrementNoRoutedMessage(); |
125 | |
} |
126 | 6 | if (!match) |
127 | |
{ |
128 | 6 | if (getCatchAllStrategy() != null) |
129 | |
{ |
130 | 6 | if (logger.isDebugEnabled()) |
131 | |
{ |
132 | 0 | logger.debug("Message did not match any routers on: " + componentName |
133 | |
+ " - invoking catch all strategy"); |
134 | |
} |
135 | 6 | if (getStatistics().isEnabled()) |
136 | |
{ |
137 | 0 | getStatistics().incrementCaughtMessage(); |
138 | |
} |
139 | 6 | return getCatchAllStrategy().catchMessage(event.getMessage(), event.getSession(), |
140 | |
event.isSynchronous()); |
141 | |
|
142 | |
} |
143 | |
else |
144 | |
{ |
145 | 0 | logger.warn("Message did not match any routers on: " |
146 | |
+ componentName |
147 | |
+ " and there is no catch all strategy configured on this router. Disposing message: " + event); |
148 | 0 | if (logger.isDebugEnabled()) |
149 | |
{ |
150 | |
try |
151 | |
{ |
152 | 0 | logger.warn("Message fragment is: " |
153 | |
+ StringMessageUtils.truncate(event.getMessageAsString(), 100, |
154 | |
true)); |
155 | |
} |
156 | 0 | catch (MuleException e) |
157 | |
{ |
158 | |
|
159 | 0 | } |
160 | |
} |
161 | |
} |
162 | |
} |
163 | |
} |
164 | |
else |
165 | |
{ |
166 | |
try |
167 | |
{ |
168 | 12 | MuleMessage messageResult = null; |
169 | 12 | for (Iterator iterator = eventsToRoute.values().iterator(); iterator.hasNext();) |
170 | |
{ |
171 | 12 | MuleEvent eventToRoute = (MuleEvent) iterator.next(); |
172 | |
|
173 | |
|
174 | 12 | if (event.getMessage().getProperty(MuleProperties.MULE_ORIGINATING_ENDPOINT_PROPERTY) == null) |
175 | |
{ |
176 | 0 | event.getMessage().setProperty(MuleProperties.MULE_ORIGINATING_ENDPOINT_PROPERTY, inboundEndpoint); |
177 | |
} |
178 | |
|
179 | 12 | if (event.isSynchronous()) |
180 | |
{ |
181 | 6 | messageResult = send(eventToRoute); |
182 | |
} |
183 | |
else |
184 | |
{ |
185 | 6 | dispatch(eventToRoute); |
186 | |
} |
187 | |
|
188 | 12 | if (getStatistics().isEnabled()) |
189 | |
{ |
190 | 0 | getStatistics().incrementRoutedMessage(eventToRoute.getEndpoint()); |
191 | |
} |
192 | 12 | } |
193 | 12 | return messageResult; |
194 | |
} |
195 | 0 | catch (MuleException e) |
196 | |
{ |
197 | 0 | throw new RoutingException(event.getMessage(), event.getEndpoint(), e); |
198 | |
} |
199 | |
} |
200 | |
} |
201 | 0 | if(event.isSynchronous()) |
202 | |
{ |
203 | |
|
204 | |
|
205 | 0 | return (lastEvent == null ? null : lastEvent.getMessage()); |
206 | |
} |
207 | |
else |
208 | |
{ |
209 | 0 | return null; |
210 | |
} |
211 | |
|
212 | |
} |
213 | |
|
214 | |
public void dispatch(MuleEvent event) throws MuleException |
215 | |
{ |
216 | 6 | event.getSession().dispatchEvent(event); |
217 | 6 | } |
218 | |
|
219 | |
public MuleMessage send(MuleEvent event) throws MuleException |
220 | |
{ |
221 | 6 | return event.getSession().sendEvent(event); |
222 | |
} |
223 | |
|
224 | |
public void addRouter(InboundRouter router) |
225 | |
{ |
226 | 10 | routers.add(router); |
227 | 10 | } |
228 | |
|
229 | |
public InboundRouter removeRouter(InboundRouter router) |
230 | |
{ |
231 | 0 | if (routers.remove(router)) |
232 | |
{ |
233 | 0 | return router; |
234 | |
} |
235 | |
else |
236 | |
{ |
237 | 0 | return null; |
238 | |
} |
239 | |
} |
240 | |
|
241 | |
public void addEndpoint(InboundEndpoint endpoint) |
242 | |
{ |
243 | 14 | endpoints.add(endpoint); |
244 | 14 | } |
245 | |
|
246 | |
public boolean removeEndpoint(InboundEndpoint endpoint) |
247 | |
{ |
248 | 0 | return endpoints.remove(endpoint); |
249 | |
} |
250 | |
|
251 | |
public List getEndpoints() |
252 | |
{ |
253 | 94 | return endpoints; |
254 | |
} |
255 | |
|
256 | |
public void setEndpoints(List endpoints) |
257 | |
{ |
258 | 6 | if (endpoints != null) |
259 | |
{ |
260 | 6 | this.endpoints.clear(); |
261 | |
|
262 | |
|
263 | 6 | for (Iterator it = endpoints.iterator(); it.hasNext();) |
264 | |
{ |
265 | 12 | ImmutableEndpoint endpoint = (ImmutableEndpoint) it.next(); |
266 | 12 | if (!(endpoint instanceof InboundEndpoint)) |
267 | |
{ |
268 | 4 | throw new InvalidEndpointTypeException(CoreMessages.inboundRouterMustUseInboundEndpoints(this, |
269 | |
endpoint)); |
270 | |
} |
271 | 8 | } |
272 | 2 | this.endpoints.addAll(endpoints); |
273 | |
} |
274 | |
else |
275 | |
{ |
276 | 0 | throw new IllegalArgumentException("List of endpoints = null"); |
277 | |
} |
278 | 2 | } |
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
public InboundEndpoint getEndpoint(String name) |
286 | |
{ |
287 | |
InboundEndpoint endpointDescriptor; |
288 | 0 | for (Iterator iterator = endpoints.iterator(); iterator.hasNext();) |
289 | |
{ |
290 | 0 | endpointDescriptor = (InboundEndpoint) iterator.next(); |
291 | 0 | if (endpointDescriptor.getName().equals(name)) |
292 | |
{ |
293 | 0 | return endpointDescriptor; |
294 | |
} |
295 | |
} |
296 | 0 | return null; |
297 | |
} |
298 | |
} |