View Javadoc

1   /*
2    * $Id: AbstractMessageAwareTransformer.java 11343 2008-03-13 10:58:26Z tcarlson $
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.transformer;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.MuleServer;
15  import org.mule.RequestContext;
16  import org.mule.api.MuleEventContext;
17  import org.mule.api.MuleMessage;
18  import org.mule.api.transformer.TransformerException;
19  import org.mule.config.i18n.CoreMessages;
20  
21  /**
22   * <code>AbstractMessageAwareTransformer</code> is a transformer that has a reference
23   * to the current message. This message can be used obtains properties associated
24   * with the current message useful to the transform. Note that when part of a
25   * transform chain, the Message payload reflects the pre-transform message state,
26   * unless there is no current event for this thread, then the message will be a new
27   * DefaultMuleMessage with the src as its payload. Transformers should always work on the
28   * src object not the message payload.
29   *
30   * @see org.mule.api.MuleMessage
31   * @see org.mule.DefaultMuleMessage
32   */
33  
34  public abstract class AbstractMessageAwareTransformer extends AbstractTransformer
35  {
36  
37      public boolean isSourceTypeSupported(Class aClass, boolean exactMatch)
38      {
39          //TODO RM* This is a bit of hack since we could just register MuleMessage as a supportedType, but this has some
40          //funny behaviour in certain ObjectToXml transformers
41          return (super.isSourceTypeSupported(aClass, exactMatch) || MuleMessage.class.isAssignableFrom(aClass)); 
42      }
43  
44      public final Object doTransform(Object src, String encoding) throws TransformerException
45      {
46          MuleMessage message;
47          if(src instanceof MuleMessage)
48          {
49              message = (MuleMessage)src;
50          }
51          else if (MuleServer.getMuleContext().getConfiguration().isAutoWrapMessageAwareTransform())
52          {
53              message = new DefaultMuleMessage(src);
54          }
55          else
56          {
57              MuleEventContext event = RequestContext.getEventContext();
58              if (event == null)
59              {
60                  throw new TransformerException(CoreMessages.noCurrentEventForTransformer(), this);
61              }
62              message = event.getMessage();
63              if(!message.getPayload().equals(src))
64              {
65                  throw new IllegalStateException("Transform payload does not match current MuleEventContext payload");
66              }
67          }
68          return transform(message, encoding);
69      }
70  
71      public abstract Object transform(MuleMessage message, String outputEncoding)
72          throws TransformerException;
73  
74  }