1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.routing.outbound; |
8 | |
|
9 | |
import org.mule.api.MuleEvent; |
10 | |
import org.mule.api.MuleException; |
11 | |
import org.mule.api.MuleMessage; |
12 | |
import org.mule.api.endpoint.EndpointNotFoundException; |
13 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
14 | |
import org.mule.api.endpoint.MalformedEndpointException; |
15 | |
import org.mule.api.expression.ExpressionRuntimeException; |
16 | |
import org.mule.api.processor.MessageProcessor; |
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.expression.ExpressionConfig; |
21 | |
import org.mule.util.StringUtils; |
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 | 0 | 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 | |
private String defaultEndpointName; |
50 | |
|
51 | 0 | private ExpressionConfig expressionConfig = new ExpressionConfig(DEFAULT_SELECTOR_EXPRESSION, DEFAULT_SELECTOR_EVALUATOR, null); |
52 | |
|
53 | |
@Override |
54 | |
public MuleEvent route(MuleEvent event) throws RoutingException |
55 | |
{ |
56 | 0 | MuleMessage message = event.getMessage(); |
57 | |
|
58 | |
List<Object> endpoints; |
59 | |
String endpointName; |
60 | |
|
61 | 0 | String prop = expressionConfig.getFullExpression(expressionManager); |
62 | 0 | if (!expressionManager.isValidExpression(prop)) |
63 | |
{ |
64 | 0 | throw new CouldNotRouteOutboundMessageException( |
65 | |
CoreMessages.expressionInvalidForProperty("expression", prop), event, null); |
66 | |
} |
67 | |
|
68 | 0 | Object property = null; |
69 | |
try |
70 | |
{ |
71 | 0 | property = expressionManager.evaluate(prop, message); |
72 | |
} |
73 | 0 | catch (ExpressionRuntimeException e) |
74 | |
{ |
75 | 0 | logger.error(e.getMessage()); |
76 | 0 | } |
77 | |
|
78 | 0 | if (property == null && getDefaultEndpointName() == null) |
79 | |
{ |
80 | 0 | throw new CouldNotRouteOutboundMessageException( |
81 | |
CoreMessages.expressionResultWasNull( |
82 | |
expressionConfig.getFullExpression(expressionManager)), event, null); |
83 | |
} |
84 | 0 | else if (property == null) |
85 | |
{ |
86 | 0 | logger.info("Expression: " + prop + " returned null, using default endpoint: " + getDefaultEndpointName()); |
87 | 0 | property = getDefaultEndpointName(); |
88 | |
} |
89 | |
|
90 | 0 | if (property instanceof String) |
91 | |
{ |
92 | 0 | endpoints = new ArrayList<Object>(1); |
93 | 0 | endpoints.add(property); |
94 | |
} |
95 | 0 | else if (property instanceof List) |
96 | |
{ |
97 | 0 | endpoints = (List<Object>) property; |
98 | |
} |
99 | |
else |
100 | |
{ |
101 | 0 | throw new CouldNotRouteOutboundMessageException(CoreMessages.propertyIsNotSupportedType( |
102 | |
expressionConfig.getFullExpression(expressionManager), |
103 | |
new Class[]{String.class, List.class}, property.getClass()), event, null); |
104 | |
} |
105 | |
|
106 | 0 | List<MuleEvent> results = new ArrayList<MuleEvent>(endpoints.size()); |
107 | |
|
108 | 0 | for (Iterator<Object> iterator = endpoints.iterator(); iterator.hasNext();) |
109 | |
{ |
110 | 0 | endpointName = iterator.next().toString(); |
111 | |
|
112 | 0 | if (StringUtils.isEmpty(endpointName)) |
113 | |
{ |
114 | 0 | throw new CouldNotRouteOutboundMessageException( |
115 | |
CoreMessages.objectIsNull("Endpoint Name: " + expressionConfig.getFullExpression(expressionManager)), event, null); |
116 | |
} |
117 | 0 | MessageProcessor ep = null; |
118 | |
try |
119 | |
{ |
120 | 0 | ep = lookupEndpoint(endpointName); |
121 | 0 | if (ep == null) |
122 | |
{ |
123 | 0 | throw new CouldNotRouteOutboundMessageException(CoreMessages.objectNotFound("Endpoint", |
124 | |
endpointName), event, null); |
125 | |
} |
126 | 0 | MuleEvent result = sendRequest(event, message, ep, true); |
127 | 0 | if (result != null) |
128 | |
{ |
129 | 0 | results.add(result); |
130 | |
} |
131 | |
} |
132 | 0 | catch (MuleException e) |
133 | |
{ |
134 | 0 | throw new CouldNotRouteOutboundMessageException(event, ep, e); |
135 | 0 | } |
136 | 0 | } |
137 | 0 | return resultsHandler.aggregateResults(results, event, muleContext); |
138 | |
} |
139 | |
|
140 | |
protected MessageProcessor lookupEndpoint(String endpointName) throws MuleException |
141 | |
{ |
142 | 0 | for (MessageProcessor target : routes) |
143 | |
{ |
144 | 0 | if (target instanceof ImmutableEndpoint) |
145 | |
{ |
146 | 0 | ImmutableEndpoint ep = (ImmutableEndpoint) target; |
147 | |
|
148 | 0 | if (endpointName.equals(ep.getName())) |
149 | |
{ |
150 | 0 | return target; |
151 | |
} |
152 | |
|
153 | 0 | else if (endpointName.equals(ep.getName())) |
154 | |
{ |
155 | 0 | return target; |
156 | |
} |
157 | 0 | else if (endpointName.equals(ep.getEndpointURI().getUri().toString())) |
158 | |
{ |
159 | 0 | return target; |
160 | |
} |
161 | 0 | } |
162 | |
} |
163 | |
try |
164 | |
{ |
165 | 0 | return getMuleContext().getEndpointFactory().getOutboundEndpoint(endpointName); |
166 | |
} |
167 | 0 | catch (MalformedEndpointException e) |
168 | |
{ |
169 | 0 | throw new EndpointNotFoundException(CoreMessages.endpointNotFound(endpointName), e); |
170 | |
} |
171 | |
} |
172 | |
|
173 | |
public String getExpression() |
174 | |
{ |
175 | 0 | return expressionConfig.getExpression(); |
176 | |
} |
177 | |
|
178 | |
public void setExpression(String expression) |
179 | |
{ |
180 | 0 | expressionConfig.setExpression(expression); |
181 | 0 | } |
182 | |
|
183 | |
public String getCustomEvaluator() |
184 | |
{ |
185 | 0 | return expressionConfig.getCustomEvaluator(); |
186 | |
} |
187 | |
|
188 | |
public void setCustomEvaluator(String customEvaluator) |
189 | |
{ |
190 | 0 | expressionConfig.setCustomEvaluator(customEvaluator); |
191 | 0 | } |
192 | |
|
193 | |
public String getEvaluator() |
194 | |
{ |
195 | 0 | return expressionConfig.getEvaluator(); |
196 | |
} |
197 | |
|
198 | |
public void setEvaluator(String evaluator) |
199 | |
{ |
200 | 0 | expressionConfig.setEvaluator(evaluator); |
201 | 0 | } |
202 | |
|
203 | |
public String getDefaultEndpointName() |
204 | |
{ |
205 | 0 | return defaultEndpointName; |
206 | |
} |
207 | |
|
208 | |
public void setDefaultEndpointName(String defaultEndpointName) |
209 | |
{ |
210 | 0 | this.defaultEndpointName = defaultEndpointName; |
211 | 0 | } |
212 | |
} |