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;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.MuleMessage;
12  import org.mule.api.expression.ExpressionManager;
13  import org.mule.api.lifecycle.Initialisable;
14  import org.mule.api.lifecycle.InitialisationException;
15  import org.mule.expression.ExpressionConfig;
16  
17  import java.util.ArrayList;
18  import java.util.Collections;
19  import java.util.List;
20  
21  /**
22   * Splits a message using the expression provided invoking the next message processor
23   * one for each split part.
24   * <p>
25   * <b>EIP Reference:</b> <a href="http://www.eaipatterns.com/Sequencer.html">http://www.eaipatterns.com/Sequencer.html</a>
26   */
27  public class ExpressionSplitter extends AbstractSplitter
28      implements Initialisable
29  {
30  
31      protected ExpressionManager expressionManager;
32      protected ExpressionConfig config = new ExpressionConfig();
33  
34      public ExpressionSplitter()
35      {
36          // Used by spring
37      }
38  
39      public ExpressionSplitter(ExpressionConfig config)
40      {
41          this.config = config;
42          setEvaluator(config.getEvaluator());
43      }
44  
45      public void initialise() throws InitialisationException
46      {
47          expressionManager = muleContext.getExpressionManager();
48          config.validate(expressionManager);
49      }
50  
51      protected List<MuleMessage> splitMessage(MuleEvent event)
52      {
53          Object result = event.getMuleContext().getExpressionManager().evaluate(
54              config.getFullExpression(expressionManager), event.getMessage());
55          if (result instanceof List<?>)
56          {
57              List<MuleMessage> messages = new ArrayList<MuleMessage>();
58              for (Object object : (List<?>) result)
59              {
60                  messages.add(new DefaultMuleMessage(object, muleContext));
61              }
62              return messages;
63          }
64          else if (result instanceof MuleMessage)
65          {
66              return Collections.singletonList((MuleMessage) result);
67          }
68          else
69          {
70              return Collections.<MuleMessage> singletonList(new DefaultMuleMessage(result, muleContext));
71          }
72      }
73  
74      public String getCustomEvaluator()
75      {
76          return config.getCustomEvaluator();
77      }
78  
79      public void setCustomEvaluator(String customEvaluator)
80      {
81          config.setCustomEvaluator(customEvaluator);
82      }
83  
84      public String getEvaluator()
85      {
86          return config.getEvaluator();
87      }
88  
89      public void setEvaluator(String evaluator)
90      {
91          // Switch to XPath node since we want the Dom nodes not the value of the node
92          if (evaluator.equals("xpath"))
93          {
94              evaluator = "xpath-node";
95          }
96          config.setEvaluator(evaluator);
97      }
98  
99      public String getExpression()
100     {
101         return config.getExpression();
102     }
103 
104     public void setExpression(String expression)
105     {
106         this.config.setExpression(expression);
107     }
108 
109 }