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