View Javadoc

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