View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.routing.outbound;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.lifecycle.InitialisationException;
11  import org.mule.expression.ExpressionConfig;
12  
13  import java.util.ArrayList;
14  import java.util.List;
15  
16  /**
17   * Evaluates a single expression and adds the results of the expression as individual message parts.
18   */
19  public class ExpressionMessageSplitter extends AbstractRoundRobinMessageSplitter
20  {
21      protected ExpressionConfig config = new ExpressionConfig();
22  
23      public ExpressionMessageSplitter()
24      {
25          super();
26      }
27  
28      public ExpressionMessageSplitter(ExpressionConfig config)
29      {
30          this.config = config;
31          setEvaluator(config.getEvaluator());
32      }
33  
34      public String getCustomEvaluator()
35      {
36          return config.getCustomEvaluator();
37      }
38  
39      public void setCustomEvaluator(String customEvaluator)
40      {
41          config.setCustomEvaluator(customEvaluator);
42      }
43  
44      public String getEvaluator()
45      {
46          return config.getEvaluator();
47      }
48  
49      public void setEvaluator(String evaluator)
50      {
51          //Switch to XPath node since we want the Dom nodes not the value of the node
52          if (evaluator.equals("xpath"))
53          {
54              evaluator = "xpath-node";
55          }
56          config.setEvaluator(evaluator);
57      }
58  
59      public String getExpression()
60      {
61          return config.getExpression();
62      }
63  
64      public void setExpression(String expression)
65      {
66          this.config.setExpression(expression);
67      }
68  
69      @Override
70      public void initialise() throws InitialisationException
71      {
72          super.initialise();
73          config.validate(expressionManager);
74      }
75  
76      @Override
77      protected List splitMessage(MuleMessage message)
78      {
79          List results = new ArrayList(4);
80          Object result = muleContext.getExpressionManager().evaluate(config.getFullExpression(expressionManager), message);
81          if (result instanceof List)
82          {
83              results.addAll((List)result);
84          }
85          else
86          {
87              results.add(result);
88              logger.warn("Splitter only returned a single result. If this is not expected, please check your split expression");
89          }
90          return results;
91      }
92  }