View Javadoc

1   /*
2    * $Id: ExpressionMessageSplitter.java 20321 2010-11-24 15:21:24Z 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.MuleMessage;
14  import org.mule.api.lifecycle.InitialisationException;
15  import org.mule.expression.ExpressionConfig;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  /**
21   * Evaluates a single expression and adds the results of the expression as individual message parts.
22   */
23  public class ExpressionMessageSplitter extends AbstractRoundRobinMessageSplitter
24  {
25      protected ExpressionConfig config = new ExpressionConfig();
26  
27      public ExpressionMessageSplitter()
28      {
29          super();
30      }
31  
32      public ExpressionMessageSplitter(ExpressionConfig config)
33      {
34          this.config = config;
35          setEvaluator(config.getEvaluator());
36      }
37  
38      public String getCustomEvaluator()
39      {
40          return config.getCustomEvaluator();
41      }
42  
43      public void setCustomEvaluator(String customEvaluator)
44      {
45          config.setCustomEvaluator(customEvaluator);
46      }
47  
48      public String getEvaluator()
49      {
50          return config.getEvaluator();
51      }
52  
53      public void setEvaluator(String evaluator)
54      {
55          //Switch to XPath node since we want the Dom nodes not the value of the node
56          if (evaluator.equals("xpath"))
57          {
58              evaluator = "xpath-node";
59          }
60          config.setEvaluator(evaluator);
61      }
62  
63      public String getExpression()
64      {
65          return config.getExpression();
66      }
67  
68      public void setExpression(String expression)
69      {
70          this.config.setExpression(expression);
71      }
72  
73      @Override
74      public void initialise() throws InitialisationException
75      {
76          super.initialise();
77          config.validate(expressionManager);
78      }
79  
80      @Override
81      protected List splitMessage(MuleMessage message)
82      {
83          List results = new ArrayList(4);
84          Object result = muleContext.getExpressionManager().evaluate(config.getFullExpression(expressionManager), message);
85          if (result instanceof List)
86          {
87              results.addAll((List)result);
88          }
89          else
90          {
91              results.add(result);
92              logger.warn("Splitter only returned a single result. If this is not expected, please check your split expression");
93          }
94          return results;
95      }
96  }