1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.routing.outbound; |
12 | |
|
13 | |
import org.mule.management.stats.RouterStatistics; |
14 | |
import org.mule.routing.AbstractRouterCollection; |
15 | |
import org.mule.transaction.TransactionCallback; |
16 | |
import org.mule.transaction.TransactionTemplate; |
17 | |
import org.mule.umo.MessagingException; |
18 | |
import org.mule.umo.UMOMessage; |
19 | |
import org.mule.umo.UMOSession; |
20 | |
import org.mule.umo.endpoint.UMOEndpoint; |
21 | |
import org.mule.umo.routing.RoutingException; |
22 | |
import org.mule.umo.routing.UMOOutboundRouter; |
23 | |
import org.mule.umo.routing.UMOOutboundRouterCollection; |
24 | |
|
25 | |
import java.util.ArrayList; |
26 | |
import java.util.Iterator; |
27 | |
import java.util.List; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public class OutboundRouterCollection extends AbstractRouterCollection implements UMOOutboundRouterCollection |
37 | |
{ |
38 | |
|
39 | |
public OutboundRouterCollection() |
40 | |
{ |
41 | 434 | super(RouterStatistics.TYPE_OUTBOUND); |
42 | 434 | } |
43 | |
|
44 | |
public UMOMessage route(final UMOMessage message, final UMOSession session, final boolean synchronous) |
45 | |
throws MessagingException |
46 | |
{ |
47 | |
|
48 | |
UMOMessage result; |
49 | 18 | boolean matchfound = false; |
50 | |
|
51 | 18 | for (Iterator iterator = getRouters().iterator(); iterator.hasNext();) |
52 | |
{ |
53 | 32 | UMOOutboundRouter umoOutboundRouter = (UMOOutboundRouter) iterator.next(); |
54 | 32 | if (umoOutboundRouter.isMatch(message)) |
55 | |
{ |
56 | 16 | matchfound = true; |
57 | |
|
58 | 16 | final UMOOutboundRouter router = umoOutboundRouter; |
59 | 16 | TransactionTemplate tt = new TransactionTemplate(umoOutboundRouter.getTransactionConfig(), |
60 | |
session.getComponent().getDescriptor().getExceptionListener()); |
61 | |
|
62 | 16 | TransactionCallback cb = new TransactionCallback() |
63 | |
{ |
64 | 16 | public Object doInTransaction() throws Exception |
65 | |
{ |
66 | 16 | return router.route(message, session, synchronous); |
67 | |
} |
68 | |
}; |
69 | |
try |
70 | |
{ |
71 | 16 | result = (UMOMessage) tt.execute(cb); |
72 | |
} |
73 | 0 | catch (Exception e) |
74 | |
{ |
75 | 0 | throw new RoutingException(message, null, e); |
76 | 16 | } |
77 | |
|
78 | 16 | if (!isMatchAll()) |
79 | |
{ |
80 | 12 | return result; |
81 | |
} |
82 | |
} |
83 | 20 | } |
84 | |
|
85 | 6 | if (!matchfound && getCatchAllStrategy() != null) |
86 | |
{ |
87 | 4 | if (logger.isDebugEnabled()) |
88 | |
{ |
89 | 0 | logger.debug("Message did not match any routers on: " |
90 | |
+ session.getComponent().getDescriptor().getName() |
91 | |
+ " invoking catch all strategy"); |
92 | |
} |
93 | 4 | return catchAll(message, session, synchronous); |
94 | |
} |
95 | 2 | else if (!matchfound) |
96 | |
{ |
97 | 0 | logger.warn("Message did not match any routers on: " |
98 | |
+ session.getComponent().getDescriptor().getName() |
99 | |
+ " and there is no catch all strategy configured on this router. Disposing message."); |
100 | |
} |
101 | 2 | return message; |
102 | |
} |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public UMOEndpoint[] getEndpointsForMessage(UMOMessage message) throws MessagingException |
113 | |
{ |
114 | 0 | List endpoints = new ArrayList(); |
115 | 0 | for (Iterator iterator = getRouters().iterator(); iterator.hasNext();) |
116 | |
{ |
117 | 0 | UMOOutboundRouter umoOutboundRouter = (UMOOutboundRouter) iterator.next(); |
118 | 0 | if (umoOutboundRouter.isMatch(message)) |
119 | |
{ |
120 | 0 | endpoints.addAll(umoOutboundRouter.getEndpoints()); |
121 | 0 | if (!isMatchAll()) |
122 | |
{ |
123 | 0 | break; |
124 | |
} |
125 | |
} |
126 | 0 | } |
127 | |
|
128 | 0 | UMOEndpoint[] result = new UMOEndpoint[endpoints.size()]; |
129 | 0 | return (UMOEndpoint[]) endpoints.toArray(result); |
130 | |
} |
131 | |
|
132 | |
protected UMOMessage catchAll(UMOMessage message, UMOSession session, boolean synchronous) |
133 | |
throws RoutingException |
134 | |
{ |
135 | 4 | if (getStatistics().isEnabled()) |
136 | |
{ |
137 | 0 | getStatistics().incrementCaughtMessage(); |
138 | |
} |
139 | |
|
140 | 4 | return getCatchAllStrategy().catchMessage(message, session, synchronous); |
141 | |
} |
142 | |
|
143 | |
public boolean hasEndpoints() |
144 | |
{ |
145 | 4 | for (Iterator iterator = routers.iterator(); iterator.hasNext();) |
146 | |
{ |
147 | 4 | UMOOutboundRouter router = (UMOOutboundRouter) iterator.next(); |
148 | 4 | if (router.getEndpoints().size() > 0 || router.isDynamicEndpoints()) |
149 | |
{ |
150 | 0 | return true; |
151 | |
} |
152 | 4 | } |
153 | 4 | return false; |
154 | |
} |
155 | |
|
156 | |
} |