View Javadoc

1   /*
2    * $Id: MessagePayloadExpressionEvaluator.java 20321 2010-11-24 15:21:24Z 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.api.MuleContext;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.MuleRuntimeException;
16  import org.mule.api.context.MuleContextAware;
17  import org.mule.api.expression.ExpressionEvaluator;
18  import org.mule.api.transformer.DataType;
19  import org.mule.api.transformer.TransformerException;
20  import org.mule.config.i18n.CoreMessages;
21  import org.mule.transformer.types.DataTypeFactory;
22  import org.mule.util.ClassUtils;
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 payload.  If the expression is set to a class name then Mule will attempt to transform the payload by
30   * discovering the correct transformer(s) in the registry. This is only suited for simple transformations between common types.
31   * <p/>
32   * <code>
33   * #[payload:byte[]]
34   * </code>
35   * <p/>
36   * or
37   * <p/>
38   * <code>
39   * #[payload:org.mule.api.OutputHandler]
40   * </code>
41   * <p/>
42   * If the object passed in is not a MuleMessage, the same object will be returned.
43   *
44   * @see org.mule.api.expression.ExpressionEvaluator
45   * @see DefaultExpressionManager
46   */
47  public class MessagePayloadExpressionEvaluator implements ExpressionEvaluator, MuleContextAware
48  {
49      public static final String NAME = "payload";
50      public static final String BYTE_ARRAY = "byte[]";
51  
52      /**
53       * logger used by this class
54       */
55      protected transient final Log logger = LogFactory.getLog(MessagePayloadExpressionEvaluator.class);
56  
57      protected MuleContext muleContext;
58  
59      public void setMuleContext(MuleContext context)
60      {
61          this.muleContext = context;
62      }
63  
64      public Object evaluate(String expression, MuleMessage message)
65      {
66          if(message==null) return null;
67          
68          if (StringUtils.isEmpty(expression))
69          {
70              return message.getPayload();
71          }
72          else
73          {
74              try
75              {
76                  if (expression.equals(BYTE_ARRAY))
77                  {
78                      return message.getPayload(DataType.BYTE_ARRAY_DATA_TYPE);
79                  }
80                  else
81                  {
82                      return message.getPayload(DataTypeFactory.create(ClassUtils.loadClass(expression, getClass())));
83                  }
84              }
85              catch (TransformerException e)
86              {
87                  throw new MuleRuntimeException(CoreMessages.failedToProcessExtractorFunction(expression), e);
88              }
89              catch (ClassNotFoundException e)
90              {
91                  throw new MuleRuntimeException(CoreMessages.failedToProcessExtractorFunction(expression), e);
92              }
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();
110     }
111 }