1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.routing.outbound; |
12 | |
|
13 | |
import org.mule.config.i18n.MessageFactory; |
14 | |
import org.mule.umo.UMOException; |
15 | |
import org.mule.umo.UMOMessage; |
16 | |
import org.mule.umo.UMOSession; |
17 | |
import org.mule.umo.endpoint.UMOEndpoint; |
18 | |
import org.mule.umo.routing.CouldNotRouteOutboundMessageException; |
19 | |
import org.mule.umo.routing.RoutingException; |
20 | |
|
21 | |
import java.util.Iterator; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | public class EndpointSelector extends FilteringOutboundRouter |
43 | |
{ |
44 | 0 | private String selectorProperty = "endpoint"; |
45 | |
|
46 | |
public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous) |
47 | |
throws RoutingException |
48 | |
{ |
49 | 0 | String endpointName = message.getStringProperty(getSelectorProperty(), null); |
50 | 0 | if (endpointName == null) |
51 | |
{ |
52 | 0 | throw new IllegalArgumentException("selectorProperty '" + getSelectorProperty() |
53 | |
+ "' must be set on message in order to route it."); |
54 | |
} |
55 | |
|
56 | 0 | UMOEndpoint ep = lookupEndpoint(endpointName); |
57 | 0 | if (ep == null) |
58 | |
{ |
59 | 0 | throw new CouldNotRouteOutboundMessageException( |
60 | |
MessageFactory.createStaticMessage("No endpoint found with the name " + endpointName), message, ep); |
61 | |
} |
62 | |
|
63 | |
try |
64 | |
{ |
65 | 0 | if (synchronous) |
66 | |
{ |
67 | 0 | return send(session, message, ep); |
68 | |
} |
69 | |
else |
70 | |
{ |
71 | 0 | dispatch(session, message, ep); |
72 | 0 | return null; |
73 | |
} |
74 | |
} |
75 | 0 | catch (UMOException e) |
76 | |
{ |
77 | 0 | throw new CouldNotRouteOutboundMessageException(message, ep, e); |
78 | |
} |
79 | |
} |
80 | |
|
81 | |
protected UMOEndpoint lookupEndpoint(String endpointName) |
82 | |
{ |
83 | |
UMOEndpoint ep; |
84 | 0 | Iterator iterator = endpoints.iterator(); |
85 | 0 | while (iterator.hasNext()) |
86 | |
{ |
87 | 0 | ep = (UMOEndpoint) iterator.next(); |
88 | |
|
89 | 0 | if (endpointName.equals(ep.getEndpointURI().getEndpointName())) |
90 | |
{ |
91 | 0 | return ep; |
92 | |
} |
93 | |
|
94 | 0 | else if (endpointName.equals(ep.getName())) |
95 | |
{ |
96 | 0 | return ep; |
97 | |
} |
98 | 0 | else if (endpointName.equals(ep.getEndpointURI().getUri().toString())) |
99 | |
{ |
100 | 0 | return ep; |
101 | |
} |
102 | |
} |
103 | 0 | return null; |
104 | |
} |
105 | |
|
106 | |
public String getSelectorProperty() |
107 | |
{ |
108 | 0 | return selectorProperty; |
109 | |
} |
110 | |
|
111 | |
public void setSelectorProperty(String selectorProperty) |
112 | |
{ |
113 | 0 | this.selectorProperty = selectorProperty; |
114 | 0 | } |
115 | |
} |