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 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 private String expression = DEFAULT_SELECTOR_EXPRESSION;
27 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 String expr = getFullExpression();
35 if (!muleContext.getExpressionManager().isValidExpression(expr))
36 {
37 throw new CouldNotRouteOutboundMessageException(
38 CoreMessages.expressionInvalidForProperty("expression", expr), event, null);
39 }
40
41 Object msgRecipients = muleContext.getExpressionManager().evaluate(expr, event.getMessage());
42 if (msgRecipients == null)
43 {
44 throw new CouldNotRouteOutboundMessageException(
45 CoreMessages.propertyIsNotSetOnEvent(getFullExpression()), event, null);
46 }
47 else if (msgRecipients instanceof String)
48 {
49 return Arrays.asList(StringUtils.splitAndTrim(msgRecipients.toString(), " ,;:"));
50 }
51 else if (msgRecipients instanceof List)
52 {
53 return new ArrayList((List) msgRecipients);
54 }
55 else
56 {
57 logger.error("Recipients on message are neither String nor List but: " + msgRecipients.getClass());
58 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 if (fullExpression == null)
66 {
67 if (evaluator.equalsIgnoreCase("custom"))
68 {
69 evaluator = customEvaluator;
70 }
71 fullExpression = MessageFormat.format("{0}{1}:{2}{3}",
72 ExpressionManager.DEFAULT_EXPRESSION_PREFIX,
73 evaluator, expression,
74 ExpressionManager.DEFAULT_EXPRESSION_POSTFIX);
75 logger.debug("Full expression for EndpointSelector is: " + fullExpression);
76 }
77 return fullExpression;
78 }
79
80 public String getExpression()
81 {
82 return expression;
83 }
84
85 public void setExpression(String expression)
86 {
87 this.expression = expression;
88 }
89
90 public String getCustomEvaluator()
91 {
92 return customEvaluator;
93 }
94
95 public void setCustomEvaluator(String customEvaluator)
96 {
97 this.customEvaluator = customEvaluator;
98 }
99
100 public String getEvaluator()
101 {
102 return evaluator;
103 }
104
105 public void setEvaluator(String evaluator)
106 {
107 this.evaluator = evaluator;
108 }
109 }