1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.routing.outbound; |
12 | |
|
13 | |
import org.mule.api.MuleException; |
14 | |
import org.mule.api.MuleMessage; |
15 | |
import org.mule.api.MuleSession; |
16 | |
import org.mule.api.endpoint.OutboundEndpoint; |
17 | |
import org.mule.api.routing.CouldNotRouteOutboundMessageException; |
18 | |
import org.mule.api.routing.RoutingException; |
19 | |
import org.mule.config.i18n.CoreMessages; |
20 | |
import org.mule.util.StringUtils; |
21 | |
import org.mule.util.expression.ExpressionEvaluatorManager; |
22 | |
|
23 | |
import java.util.ArrayList; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.List; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 8 | public class EndpointSelector extends FilteringOutboundRouter |
45 | |
{ |
46 | |
public static final String DEFAULT_SELECTOR_EVALUATOR = "header"; |
47 | |
public static final String DEFAULT_SELECTOR_EXPRESSION = "endpoint"; |
48 | |
|
49 | 8 | private String expression = DEFAULT_SELECTOR_EXPRESSION; |
50 | 8 | private String evaluator = DEFAULT_SELECTOR_EVALUATOR; |
51 | |
private String customEvaluator; |
52 | |
private String fullExpression; |
53 | |
|
54 | |
public MuleMessage route(MuleMessage message, MuleSession session, boolean synchronous) |
55 | |
throws RoutingException |
56 | |
{ |
57 | |
List endpoints; |
58 | |
String endpointName; |
59 | |
|
60 | 8 | String prop = getFullExpression(); |
61 | 8 | if(!ExpressionEvaluatorManager.isValidExpression(prop)) |
62 | |
{ |
63 | 0 | throw new CouldNotRouteOutboundMessageException( |
64 | |
CoreMessages.expressionInvalidForProperty("expression", prop), message, null); |
65 | |
} |
66 | |
|
67 | 8 | Object property = ExpressionEvaluatorManager.evaluate(prop, message); |
68 | 8 | if(property ==null) |
69 | |
{ |
70 | 2 | throw new CouldNotRouteOutboundMessageException( |
71 | |
CoreMessages.propertyIsNotSetOnEvent(getFullExpression()), message, null); |
72 | |
} |
73 | |
|
74 | 6 | if (property instanceof String) |
75 | |
{ |
76 | 6 | endpoints = new ArrayList(1); |
77 | 6 | endpoints.add(property); |
78 | |
} |
79 | 0 | else if(property instanceof List) |
80 | |
{ |
81 | 0 | endpoints = (List)property; |
82 | |
} |
83 | |
else |
84 | |
{ |
85 | 0 | throw new CouldNotRouteOutboundMessageException(CoreMessages.propertyIsNotSupportedType( |
86 | |
getFullExpression(), new Class[]{String.class, List.class}, property.getClass()), message, null); |
87 | |
} |
88 | |
|
89 | 6 | MuleMessage result = null; |
90 | 6 | for (Iterator iterator = endpoints.iterator(); iterator.hasNext();) |
91 | |
{ |
92 | 6 | endpointName = iterator.next().toString(); |
93 | |
|
94 | 6 | if(StringUtils.isEmpty(endpointName)) |
95 | |
{ |
96 | 0 | throw new CouldNotRouteOutboundMessageException( |
97 | |
CoreMessages.objectIsNull("Endpoint Name: " + getFullExpression()), message, null); |
98 | |
} |
99 | 6 | OutboundEndpoint ep = null; |
100 | |
try |
101 | |
{ |
102 | 6 | ep = lookupEndpoint(endpointName); |
103 | 4 | if (ep == null) |
104 | |
{ |
105 | 0 | throw new CouldNotRouteOutboundMessageException(CoreMessages.objectNotFound("Endpoint", |
106 | |
endpointName), message, ep); |
107 | |
} |
108 | 4 | if (synchronous) |
109 | |
{ |
110 | |
|
111 | 0 | result = send(session, message, ep); |
112 | |
} |
113 | |
else |
114 | |
{ |
115 | 4 | dispatch(session, message, ep); |
116 | |
} |
117 | |
} |
118 | 0 | catch (MuleException e) |
119 | |
{ |
120 | 0 | throw new CouldNotRouteOutboundMessageException(message, ep, e); |
121 | 4 | } |
122 | 4 | } |
123 | 4 | return result; |
124 | |
} |
125 | |
|
126 | |
protected OutboundEndpoint lookupEndpoint(String endpointName) throws MuleException |
127 | |
{ |
128 | |
OutboundEndpoint ep; |
129 | 6 | Iterator iterator = endpoints.iterator(); |
130 | 18 | while (iterator.hasNext()) |
131 | |
{ |
132 | 16 | ep = (OutboundEndpoint) iterator.next(); |
133 | |
|
134 | 16 | if (endpointName.equals(ep.getEndpointURI().getEndpointName())) |
135 | |
{ |
136 | 0 | return ep; |
137 | |
} |
138 | |
|
139 | 16 | else if (endpointName.equals(ep.getName())) |
140 | |
{ |
141 | 4 | return ep; |
142 | |
} |
143 | 12 | else if (endpointName.equals(ep.getEndpointURI().getUri().toString())) |
144 | |
{ |
145 | 0 | return ep; |
146 | |
} |
147 | |
} |
148 | 2 | return getMuleContext().getRegistry().lookupEndpointFactory().getOutboundEndpoint(endpointName); |
149 | |
} |
150 | |
|
151 | |
public String getFullExpression() |
152 | |
{ |
153 | 10 | if(fullExpression==null) |
154 | |
{ |
155 | 8 | if(evaluator.equalsIgnoreCase("custom")) |
156 | |
{ |
157 | 0 | evaluator = customEvaluator; |
158 | |
} |
159 | 8 | fullExpression = evaluator + ":" + expression; |
160 | 8 | logger.debug("Full expression for EndpointSelector is: " + fullExpression); |
161 | |
} |
162 | 10 | return fullExpression; |
163 | |
} |
164 | |
|
165 | |
public String getExpression() |
166 | |
{ |
167 | 0 | return expression; |
168 | |
} |
169 | |
|
170 | |
public void setExpression(String expression) |
171 | |
{ |
172 | 2 | this.expression = expression; |
173 | 2 | } |
174 | |
|
175 | |
public String getCustomEvaluator() |
176 | |
{ |
177 | 0 | return customEvaluator; |
178 | |
} |
179 | |
|
180 | |
public void setCustomEvaluator(String customEvaluator) |
181 | |
{ |
182 | 0 | this.customEvaluator = customEvaluator; |
183 | 0 | } |
184 | |
|
185 | |
public String getEvaluator() |
186 | |
{ |
187 | 0 | return evaluator; |
188 | |
} |
189 | |
|
190 | |
public void setEvaluator(String evaluator) |
191 | |
{ |
192 | 2 | this.evaluator = evaluator; |
193 | 2 | } |
194 | |
} |