View Javadoc

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  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      protected transient final Log logger = LogFactory.getLog(MessagePayloadExpressionEvaluator.class);
52  
53      public Object evaluate(String expression, Object message)
54      {
55          if (message instanceof MuleMessage)
56          {
57              if (StringUtils.isEmpty(expression))
58              {
59                  return ((MuleMessage) message).getPayload();
60              }
61              else
62              {
63                  try
64                  {
65                      if (expression.equals(BYTE_ARRAY))
66                      {
67                          return ((MuleMessage) message).getPayload(byte[].class);
68                      }
69                      else
70                      {
71                          return ((MuleMessage) message).getPayload(ClassUtils.loadClass(expression, getClass()));
72                      }
73                  }
74                  catch (TransformerException e)
75                  {
76                      throw new MuleRuntimeException(CoreMessages.failedToProcessExtractorFunction(expression), e);
77                  }
78                  catch (ClassNotFoundException e)
79                  {
80                      throw new MuleRuntimeException(CoreMessages.failedToProcessExtractorFunction(expression), e);
81                  }
82              }
83          }
84          else
85          {
86              logger.warn("Message is not of type MuleMessage, the expression will return the object without modification");
87              if (message instanceof MessageAdapter)
88              {
89                  return ((MessageAdapter) message).getPayload();
90              }
91          }
92          return message;
93  
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      public String getName()
100     {
101         return NAME;
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     public void setName(String name)
108     {
109         throw new UnsupportedOperationException("setName");
110     }
111 }