001: /*
002: * $Id: MessagePayloadExpressionEvaluator.java 11294 2008-03-09 14:16:21Z rossmason $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.util.expression;
012:
013: import org.mule.api.MuleMessage;
014: import org.mule.api.MuleRuntimeException;
015: import org.mule.api.transformer.TransformerException;
016: import org.mule.api.transport.MessageAdapter;
017: import org.mule.config.i18n.CoreMessages;
018: import org.mule.util.ClassUtils;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022:
023: /**
024: * Returns the message payload. If the expression is set to a class name then Mule will attempt to transform the payload by
025: * discovering the correct transformer(s) in the registry. This is only suited for simple transformations between common types.
026: * <p/>
027: * <code>
028: * ${payload:byte[]}
029: * </code>
030: * <p/>
031: * or
032: * <p/>
033: * <code>
034: * ${payload:org.mule.api.OutputHandler}
035: * </code>
036: * <p/>
037: * If the object passed in is not a MuleMessage, the same object will be returned.
038: *
039: * @see ExpressionEvaluator
040: * @see ExpressionEvaluatorManager
041: */
042: public class MessagePayloadExpressionEvaluator implements
043: ExpressionEvaluator {
044: public static final String NAME = "payload";
045: public static final String BYTE_ARRAY = "byte[]";
046:
047: /**
048: * logger used by this class
049: */
050: protected transient final Log logger = LogFactory
051: .getLog(MessagePayloadExpressionEvaluator.class);
052:
053: public Object evaluate(String expression, Object message) {
054: if (message instanceof MuleMessage) {
055: if (expression == null) {
056: return ((MuleMessage) message).getPayload();
057: } else {
058: try {
059: if (expression.equals(BYTE_ARRAY)) {
060: return ((MuleMessage) message)
061: .getPayload(byte[].class);
062: } else {
063: return ((MuleMessage) message)
064: .getPayload(ClassUtils.loadClass(
065: expression, getClass()));
066: }
067: } catch (TransformerException e) {
068: throw new MuleRuntimeException(
069: CoreMessages
070: .failedToProcessExtractorFunction(expression),
071: e);
072: } catch (ClassNotFoundException e) {
073: e.printStackTrace();
074: }
075: }
076: } else {
077: logger
078: .warn("Message is not of type MuleMessage, the expression will return the object without modification");
079: if (message instanceof MessageAdapter) {
080: return ((MessageAdapter) message).getPayload();
081: }
082: }
083: return message;
084:
085: }
086:
087: /**
088: * {@inheritDoc}
089: */
090: public String getName() {
091: return NAME;
092: }
093:
094: /**
095: * {@inheritDoc}
096: */
097: public void setName(String name) {
098: throw new UnsupportedOperationException("setName");
099: }
100: }
|