View Javadoc

1   /*
2    * $Id: AbstractEventAwareTransformer.java 7963 2007-08-21 08:53:15Z dirk.olmes $
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.transformers;
12  
13  import org.mule.config.i18n.CoreMessages;
14  import org.mule.impl.RequestContext;
15  import org.mule.umo.UMOEventContext;
16  import org.mule.umo.transformer.TransformerException;
17  
18  /**
19   * <code>AbstractEventAwareTransformer</code> is a transformer that has a reference
20   * to the current message. This message can be used obtains properties associated
21   * with the current message useful to the transform. Note that when part of a
22   * transform chain, the Message payload reflects the pre-transform message state,
23   * unless there is no current event for this thread, then the message will be a new
24   * MuleMessage with the src as its payload. Transformers should always work on the
25   * src object not the message payload.
26   * 
27   * @see org.mule.umo.UMOMessage
28   * @see org.mule.impl.MuleMessage
29   */
30  
31  public abstract class AbstractEventAwareTransformer extends AbstractTransformer
32  {
33      public final Object doTransform(Object src, String encoding) throws TransformerException
34      {
35          UMOEventContext event = RequestContext.getEventContext();
36          if (event == null && requiresCurrentEvent())
37          {
38              throw new TransformerException(CoreMessages.noCurrentEventForTransformer(), this);
39          }
40          return transform(src, encoding, event);
41      }
42  
43      public abstract Object transform(Object src, String encoding, UMOEventContext context)
44          throws TransformerException;
45  
46      protected boolean requiresCurrentEvent()
47      {
48          return true;
49      }
50  }