Coverage Report - org.mule.util.expression.MessagePayloadExpressionEvaluator
 
Classes in this File Line Coverage Branch Coverage Complexity
MessagePayloadExpressionEvaluator
67%
12/18
88%
7/8
5.333
 
 1  
 /*
 2  
  * $Id: MessagePayloadExpressionEvaluator.java 12269 2008-07-10 04:19:03Z dfeist $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.util.expression;
 12  
 
 13  
 import org.mule.api.MuleMessage;
 14  
 import org.mule.api.MuleRuntimeException;
 15  
 import org.mule.api.transformer.TransformerException;
 16  
 import org.mule.api.transport.MessageAdapter;
 17  
 import org.mule.config.i18n.CoreMessages;
 18  
 import org.mule.util.ClassUtils;
 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 payload.  If the expression is set to a class name then Mule will attempt to transform the payload by
 26  
  * discovering the correct transformer(s) in the registry. This is only suited for simple transformations between common types.
 27  
  * <p/>
 28  
  * <code>
 29  
  * ${payload:byte[]}
 30  
  * </code>
 31  
  * <p/>
 32  
  * or
 33  
  * <p/>
 34  
  * <code>
 35  
  * ${payload:org.mule.api.OutputHandler}
 36  
  * </code>
 37  
  * <p/>
 38  
  * If the object passed in is not a MuleMessage, the same object will be returned.
 39  
  *
 40  
  * @see ExpressionEvaluator
 41  
  * @see ExpressionEvaluatorManager
 42  
  */
 43  1152
 public class MessagePayloadExpressionEvaluator implements ExpressionEvaluator
 44  
 {
 45  
     public static final String NAME = "payload";
 46  
     public static final String BYTE_ARRAY = "byte[]";
 47  
 
 48  
     /**
 49  
      * logger used by this class
 50  
      */
 51  1152
     protected transient final Log logger = LogFactory.getLog(MessagePayloadExpressionEvaluator.class);
 52  
 
 53  
     public Object evaluate(String expression, Object message)
 54  
     {
 55  36
         if (message instanceof MuleMessage)
 56  
         {
 57  28
             if (StringUtils.isEmpty(expression))
 58  
             {
 59  22
                 return ((MuleMessage) message).getPayload();
 60  
             }
 61  
             else
 62  
             {
 63  
                 try
 64  
                 {
 65  6
                     if (expression.equals(BYTE_ARRAY))
 66  
                     {
 67  2
                         return ((MuleMessage) message).getPayload(byte[].class);
 68  
                     }
 69  
                     else
 70  
                     {
 71  4
                         return ((MuleMessage) message).getPayload(ClassUtils.loadClass(expression, getClass()));
 72  
                     }
 73  
                 }
 74  0
                 catch (TransformerException e)
 75  
                 {
 76  0
                     throw new MuleRuntimeException(CoreMessages.failedToProcessExtractorFunction(expression), e);
 77  
                 }
 78  0
                 catch (ClassNotFoundException e)
 79  
                 {
 80  0
                     throw new MuleRuntimeException(CoreMessages.failedToProcessExtractorFunction(expression), e);
 81  
                 }
 82  
             }
 83  
         }
 84  
         else
 85  
         {
 86  8
             logger.warn("Message is not of type MuleMessage, the expression will return the object without modification");
 87  8
             if (message instanceof MessageAdapter)
 88  
             {
 89  0
                 return ((MessageAdapter) message).getPayload();
 90  
             }
 91  
         }
 92  8
         return message;
 93  
 
 94  
     }
 95  
 
 96  
     /**
 97  
      * {@inheritDoc}
 98  
      */
 99  
     public String getName()
 100  
     {
 101  1146
         return NAME;
 102  
     }
 103  
 
 104  
     /**
 105  
      * {@inheritDoc}
 106  
      */
 107  
     public void setName(String name)
 108  
     {
 109  0
         throw new UnsupportedOperationException("setName");
 110  
     }
 111  
 }