View Javadoc

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