View Javadoc

1   /*
2    * $Id: ByteArrayToSerializable.java 11336 2008-03-12 17:53:18Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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  
18  import java.io.InputStream;
19  
20  import org.apache.commons.lang.SerializationUtils;
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      /** Give core transformers a slighty higher priority */
30      private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING + 1;
31  
32      public ByteArrayToSerializable()
33      {
34          registerSourceType(byte[].class);
35          registerSourceType(InputStream.class);
36      }
37  
38      public Object doTransform(Object src, String encoding) throws TransformerException
39      {
40          try
41          {
42              if (src instanceof byte[])
43              {
44                  return SerializationUtils.deserialize((byte[]) src);
45              }
46              else
47              {
48                  return SerializationUtils.deserialize((InputStream) src);
49              }
50          }
51          catch (Exception e)
52          {
53              throw new TransformerException(
54                      CoreMessages.transformFailed("byte[]", "Object"), this, e);
55          }
56      }
57  
58      public int getPriorityWeighting()
59      {
60          return priorityWeighting;
61      }
62  
63      public void setPriorityWeighting(int priorityWeighting)
64      {
65          this.priorityWeighting = priorityWeighting;
66      }
67  }