View Javadoc

1   /*
2    * $Id: AutoTransformer.java 20321 2010-11-24 15:21:24Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.simple;
11  
12  import org.mule.transformer.AbstractMessageTransformer;
13  import org.mule.api.MuleMessage;
14  import org.mule.api.lifecycle.InitialisationException;
15  import org.mule.api.transformer.TransformerException;
16  import org.mule.config.i18n.CoreMessages;
17  import org.mule.transformer.types.DataTypeFactory;
18  
19  /**
20   * A transformer that uses the transform discovery mechanism to convert the message payload. This transformer
21   * works much better when transforming custom object types rather that java types since there is less chance for
22   * ambiguity.
23   * If an exact match cannot be made an execption will be thrown.
24   */
25  public class AutoTransformer extends AbstractMessageTransformer
26  {
27      /**
28       * Template method where deriving classes can do any initialisation after the
29       * properties have been set on this transformer
30       *
31       * @throws org.mule.api.lifecycle.InitialisationException
32       *
33       */
34      @Override
35      public void initialise() throws InitialisationException
36      {
37          super.initialise();
38          if(getReturnClass().equals(Object.class))
39          {
40              throw new InitialisationException(CoreMessages.transformerInvalidReturnType(Object.class, getName()), this);
41          }
42      }
43  
44      @Override
45      public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
46      {
47          return message.getPayload(DataTypeFactory.create(getReturnClass()));
48      }
49  }