View Javadoc

1   /*
2    * $Id: TransformerTemplate.java 10489 2008-01-23 17:53:38Z 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  package org.mule.transformer;
11  
12  import org.mule.api.MuleMessage;
13  import org.mule.api.transformer.TransformerException;
14  
15  /** TODO */
16  public class TransformerTemplate extends AbstractMessageAwareTransformer
17  {
18      private TransformerCallback callback;
19  
20      public TransformerTemplate(TransformerCallback callback)
21      {
22          this.callback = callback;
23      }
24  
25      public Object transform(MuleMessage message, String outputEncoding) throws TransformerException
26      {
27          try
28          {
29              return callback.doTransform(message);
30          }
31          catch (TransformerException e)
32          {
33              throw e;
34          }
35          catch (Exception e)
36          {
37              throw new TransformerException(this, e);
38          }
39      }
40  
41      public interface TransformerCallback
42      {
43          public Object doTransform(MuleMessage message) throws Exception;
44      }
45  
46      public static class OverwitePayloadCallback implements TransformerCallback
47      {
48          private Object payload;
49  
50          public OverwitePayloadCallback(Object payload)
51          {
52              this.payload = payload;
53          }
54  
55          public Object doTransform(MuleMessage message) throws Exception
56          {
57              return payload;
58          }
59      }
60  }