View Javadoc

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