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