View Javadoc

1   /*
2    * $Id: ByteArrayToSerializable.java 7976 2007-08-21 14:26:13Z 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.transformers.simple;
12  
13  import org.mule.config.i18n.CoreMessages;
14  import org.mule.transformers.AbstractTransformer;
15  import org.mule.umo.transformer.TransformerException;
16  
17  import java.io.InputStream;
18  
19  import org.apache.commons.lang.SerializationUtils;
20  
21  /**
22   * <code>ByteArrayToSerializable</code> converts a serialized object to its object
23   * representation
24   */
25  public class ByteArrayToSerializable extends AbstractTransformer
26  {
27  
28      public ByteArrayToSerializable()
29      {
30          registerSourceType(byte[].class);
31          registerSourceType(InputStream.class);
32      }
33  
34      public Object doTransform(Object src, String encoding) throws TransformerException
35      {
36          try
37          {
38              if (src instanceof byte[])
39              {
40                  return SerializationUtils.deserialize((byte[]) src);
41              }
42              else
43              {
44                  return SerializationUtils.deserialize((InputStream) src);
45  
46              }
47          }
48          catch (Exception e)
49          {
50              throw new TransformerException(
51                  CoreMessages.transformFailed("byte[]", "Object"), this, e);
52          }
53      }
54  
55  }