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