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