View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transformer;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.transformer.TransformerException;
11  
12  /** TODO */
13  public class TransformerTemplate extends AbstractMessageTransformer
14  {
15      private TransformerCallback callback;
16  
17      public TransformerTemplate(TransformerCallback callback)
18      {
19          this.callback = callback;
20      }
21  
22      @Override
23      public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
24      {
25          try
26          {
27              return callback.doTransform(message);
28          }
29          catch (TransformerException e)
30          {
31              throw new TransformerException(e.getI18nMessage(),this, e);
32          }
33          catch (Exception e)
34          {
35              throw new TransformerException(this, e);
36          }
37      }
38  
39      public interface TransformerCallback
40      {
41          public Object doTransform(MuleMessage message) throws Exception;
42      }
43  
44      public static class OverwitePayloadCallback implements TransformerCallback
45      {
46          private Object payload;
47  
48          public OverwitePayloadCallback(Object payload)
49          {
50              this.payload = payload;
51          }
52  
53          public Object doTransform(MuleMessage message) throws Exception
54          {
55              return payload;
56          }
57      }
58  }