View Javadoc

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