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.api.transformer.DiscoverableTransformer;
10  import org.mule.api.transformer.TransformerException;
11  import org.mule.config.i18n.CoreMessages;
12  import org.mule.transformer.AbstractTransformer;
13  import org.mule.transformer.types.DataTypeFactory;
14  import org.mule.util.SerializationUtils;
15  
16  import java.io.InputStream;
17  
18  /**
19   * <code>ByteArrayToSerializable</code> converts a serialized object to its object
20   * representation
21   */
22  public class ByteArrayToSerializable extends AbstractTransformer implements DiscoverableTransformer
23  {
24  
25      /**
26       * Give core transformers a slighty higher priority
27       */
28      private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING + 1;
29  
30      public ByteArrayToSerializable()
31      {
32          registerSourceType(DataTypeFactory.BYTE_ARRAY);
33          registerSourceType(DataTypeFactory.INPUT_STREAM);
34      }
35  
36      @Override
37      public Object doTransform(Object src, String encoding) throws TransformerException
38      {
39          try
40          {
41              final Object result;
42              if (src instanceof byte[])
43              {
44                  result = SerializationUtils.deserialize((byte[]) src, muleContext);
45              }
46              else
47              {
48                  result = SerializationUtils.deserialize((InputStream) src, muleContext);
49              }
50              return result;
51          }
52          catch (Exception e)
53          {
54              throw new TransformerException(
55                      CoreMessages.transformFailed("byte[]", "Object"), this, e);
56          }
57      }
58  
59      public int getPriorityWeighting()
60      {
61          return priorityWeighting;
62      }
63  
64      public void setPriorityWeighting(int priorityWeighting)
65      {
66          this.priorityWeighting = priorityWeighting;
67      }
68  }