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 public class EndpointSelector extends FilteringOutboundRouter
43 {
44 private String selectorProperty = "endpoint";
45
46 public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous)
47 throws RoutingException
48 {
49 String endpointName = message.getStringProperty(getSelectorProperty(), null);
50 if (endpointName == null)
51 {
52 throw new IllegalArgumentException("selectorProperty '" + getSelectorProperty()
53 + "' must be set on message in order to route it.");
54 }
55
56 UMOEndpoint ep = lookupEndpoint(endpointName);
57 if (ep == null)
58 {
59 throw new CouldNotRouteOutboundMessageException(
60 MessageFactory.createStaticMessage("No endpoint found with the name " + endpointName), message, ep);
61 }
62
63 try
64 {
65 if (synchronous)
66 {
67 return send(session, message, ep);
68 }
69 else
70 {
71 dispatch(session, message, ep);
72 return null;
73 }
74 }
75 catch (UMOException e)
76 {
77 throw new CouldNotRouteOutboundMessageException(message, ep, e);
78 }
79 }
80
81 protected UMOEndpoint lookupEndpoint(String endpointName)
82 {
83 UMOEndpoint ep;
84 Iterator iterator = endpoints.iterator();
85 while (iterator.hasNext())
86 {
87 ep = (UMOEndpoint) iterator.next();
88
89 if (endpointName.equals(ep.getEndpointURI().getEndpointName()))
90 {
91 return ep;
92 }
93
94 else if (endpointName.equals(ep.getName()))
95 {
96 return ep;
97 }
98 else if (endpointName.equals(ep.getEndpointURI().getUri().toString()))
99 {
100 return ep;
101 }
102 }
103 return null;
104 }
105
106 public String getSelectorProperty()
107 {
108 return selectorProperty;
109 }
110
111 public void setSelectorProperty(String selectorProperty)
112 {
113 this.selectorProperty = selectorProperty;
114 }
115 }