1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.routing.outbound; |
8 | |
|
9 | |
import org.mule.api.MuleEvent; |
10 | |
import org.mule.api.expression.ExpressionManager; |
11 | |
import org.mule.api.routing.CouldNotRouteOutboundMessageException; |
12 | |
import org.mule.config.i18n.CoreMessages; |
13 | |
import org.mule.util.StringUtils; |
14 | |
|
15 | |
import java.text.MessageFormat; |
16 | |
import java.util.ArrayList; |
17 | |
import java.util.Arrays; |
18 | |
import java.util.List; |
19 | |
|
20 | 0 | public class ExpressionRecipientList extends AbstractRecipientList |
21 | |
{ |
22 | |
public static final String DEFAULT_SELECTOR_PROPERTY = "recipients"; |
23 | |
public static final String DEFAULT_SELECTOR_EVALUATOR = "header"; |
24 | |
public static final String DEFAULT_SELECTOR_EXPRESSION = DEFAULT_SELECTOR_PROPERTY; |
25 | |
|
26 | 0 | private String expression = DEFAULT_SELECTOR_EXPRESSION; |
27 | 0 | private String evaluator = DEFAULT_SELECTOR_EVALUATOR; |
28 | |
private String customEvaluator; |
29 | |
private String fullExpression; |
30 | |
|
31 | |
@Override |
32 | |
protected List getRecipients(MuleEvent event) throws CouldNotRouteOutboundMessageException |
33 | |
{ |
34 | 0 | String expr = getFullExpression(); |
35 | 0 | if (!muleContext.getExpressionManager().isValidExpression(expr)) |
36 | |
{ |
37 | 0 | throw new CouldNotRouteOutboundMessageException( |
38 | |
CoreMessages.expressionInvalidForProperty("expression", expr), event, null); |
39 | |
} |
40 | |
|
41 | 0 | Object msgRecipients = muleContext.getExpressionManager().evaluate(expr, event.getMessage()); |
42 | 0 | if (msgRecipients == null) |
43 | |
{ |
44 | 0 | throw new CouldNotRouteOutboundMessageException( |
45 | |
CoreMessages.propertyIsNotSetOnEvent(getFullExpression()), event, null); |
46 | |
} |
47 | 0 | else if (msgRecipients instanceof String) |
48 | |
{ |
49 | 0 | return Arrays.asList(StringUtils.splitAndTrim(msgRecipients.toString(), " ,;:")); |
50 | |
} |
51 | 0 | else if (msgRecipients instanceof List) |
52 | |
{ |
53 | 0 | return new ArrayList((List) msgRecipients); |
54 | |
} |
55 | |
else |
56 | |
{ |
57 | 0 | logger.error("Recipients on message are neither String nor List but: " + msgRecipients.getClass()); |
58 | 0 | throw new CouldNotRouteOutboundMessageException( |
59 | |
CoreMessages.propertyIsNotSupportedType(getFullExpression(), new Class[]{String.class, List.class}, msgRecipients.getClass()), event, null); |
60 | |
} |
61 | |
} |
62 | |
|
63 | |
public String getFullExpression() |
64 | |
{ |
65 | 0 | if (fullExpression == null) |
66 | |
{ |
67 | 0 | if (evaluator.equalsIgnoreCase("custom")) |
68 | |
{ |
69 | 0 | evaluator = customEvaluator; |
70 | |
} |
71 | 0 | fullExpression = MessageFormat.format("{0}{1}:{2}{3}", |
72 | |
ExpressionManager.DEFAULT_EXPRESSION_PREFIX, |
73 | |
evaluator, expression, |
74 | |
ExpressionManager.DEFAULT_EXPRESSION_POSTFIX); |
75 | 0 | logger.debug("Full expression for EndpointSelector is: " + fullExpression); |
76 | |
} |
77 | 0 | return fullExpression; |
78 | |
} |
79 | |
|
80 | |
public String getExpression() |
81 | |
{ |
82 | 0 | return expression; |
83 | |
} |
84 | |
|
85 | |
public void setExpression(String expression) |
86 | |
{ |
87 | 0 | this.expression = expression; |
88 | 0 | } |
89 | |
|
90 | |
public String getCustomEvaluator() |
91 | |
{ |
92 | 0 | return customEvaluator; |
93 | |
} |
94 | |
|
95 | |
public void setCustomEvaluator(String customEvaluator) |
96 | |
{ |
97 | 0 | this.customEvaluator = customEvaluator; |
98 | 0 | } |
99 | |
|
100 | |
public String getEvaluator() |
101 | |
{ |
102 | 0 | return evaluator; |
103 | |
} |
104 | |
|
105 | |
public void setEvaluator(String evaluator) |
106 | |
{ |
107 | 0 | this.evaluator = evaluator; |
108 | 0 | } |
109 | |
} |