View Javadoc

1   /*
2    * $Id: MessageProcessorExpressionEvaluator.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.expression;
12  
13  import org.mule.DefaultMuleEvent;
14  import org.mule.DefaultMuleMessage;
15  import org.mule.RequestContext;
16  import org.mule.api.MuleContext;
17  import org.mule.api.MuleException;
18  import org.mule.api.MuleMessage;
19  import org.mule.api.expression.ExpressionEvaluator;
20  import org.mule.api.expression.ExpressionRuntimeException;
21  import org.mule.api.processor.MessageProcessor;
22  import org.mule.config.i18n.CoreMessages;
23  import org.mule.util.StringUtils;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /**
29   * Returns the message result of invoking the specified globally accessible message
30   * processor. If just the message processor name is specified then the current
31   * message is used. If a nested expression is specified then this is first evaluated
32   * and the result of this evaluation used when invoking the message processor
33   * <b>Example:</b>
34   * <ul>
35   * <li><b>"StringToMyObjext"</b> - invokes the 'StringToMyObjext' transformer using
36   * the current message</li>
37   * <li><b>"StringToMyObjext:header:one"</b> - invokes the 'StringToMyObjext'
38   * transformerusing the value of the outbound header 'one'</li>
39   * <li><b>"thatFlow:xpath://my/path"</b> - invokes the 'thatFlow' flow using the
40   * result of the xpath evaluation</li>
41   * </ul>
42   * If no expression is set the MuleMessage itself will be returned.
43   * <p/>
44   * 
45   * @see ExpressionEvaluator
46   * @see DefaultExpressionManager
47   */
48  public class MessageProcessorExpressionEvaluator implements ExpressionEvaluator
49  {
50      protected transient Log logger = LogFactory.getLog(MessageProcessorExpressionEvaluator.class);
51  
52      public static final String NAME = "process";
53  
54      public Object evaluate(String expression, MuleMessage message)
55      {
56          MuleContext muleContext = message.getMuleContext();
57  
58          String processorName;
59          String processorArgExpression = null;
60          MuleMessage messageToProcess = message;
61  
62          if (StringUtils.isBlank(expression))
63          {
64              return message;
65          }
66  
67          boolean isNestedExpression = expression.indexOf(':') > 0;
68          if (!isNestedExpression)
69          {
70              processorName = expression;
71          }
72          else
73          {
74              processorName = expression.substring(0, expression.indexOf(':'));
75              processorArgExpression = expression.substring(expression.indexOf(':') + 1, expression.length());
76          }
77  
78          if (processorArgExpression != null)
79          {
80              messageToProcess = evaluateProcessorArgument(message, processorArgExpression);
81          }
82  
83          MessageProcessor processor = lookupMessageProcessor(processorName, muleContext);
84  
85          try
86          {
87              return processor.process(new DefaultMuleEvent(messageToProcess, RequestContext.getEvent()))
88                  .getMessage();
89          }
90          catch (MuleException e)
91          {
92              throw new ExpressionRuntimeException(
93                  CoreMessages.createStaticMessage("Exception invoking MessageProcessor '" + processorName
94                                                   + "'"), e);
95          }
96      }
97  
98      protected MessageProcessor lookupMessageProcessor(String processorName, MuleContext muleContext)
99      {
100         Object processor = muleContext.getRegistry().lookupObject(processorName);
101 
102         if (!(processor instanceof MessageProcessor))
103         {
104             throw new ExpressionRuntimeException(CoreMessages.createStaticMessage("No MessageProcessor '"
105                                                                                   + processorName
106                                                                                   + "' found."));
107         }
108         return (MessageProcessor) processor;
109     }
110 
111     protected MuleMessage evaluateProcessorArgument(MuleMessage message, String processorArgExpression)
112     {
113         Object result = message.getMuleContext().getExpressionManager().evaluate(processorArgExpression,
114             message);
115         if (result instanceof MuleMessage)
116         {
117             return (MuleMessage) result;
118         }
119         else
120         {
121             return new DefaultMuleMessage(result, message.getMuleContext());
122         }
123     }
124 
125     /**
126      * {@inheritDoc}
127      */
128     public String getName()
129     {
130         return NAME;
131     }
132 
133     /**
134      * {@inheritDoc}
135      */
136     public void setName(String name)
137     {
138         throw new UnsupportedOperationException();
139     }
140 }