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.util;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.util.store.DeserializationPostInitialisable;
11  
12  import java.io.ByteArrayInputStream;
13  import java.io.IOException;
14  import java.io.InputStream;
15  import java.io.ObjectInputStream;
16  
17  import org.apache.commons.io.input.ClassLoaderObjectInputStream;
18  import org.apache.commons.lang.SerializationException;
19  
20  public class SerializationUtils extends org.apache.commons.lang.SerializationUtils
21  {
22      public static Object deserialize(InputStream inputStream, MuleContext muleContext)
23      {
24          if (muleContext == null)
25          {
26              throw new IllegalArgumentException("The MuleContext must not be null");
27          }
28          return deserialize(inputStream, muleContext.getExecutionClassLoader(), muleContext);
29      }
30  
31      public static Object deserialize(byte[] objectData, MuleContext muleContext)
32      {
33          if (muleContext == null)
34          {
35              throw new IllegalArgumentException("The MuleContext must not be null");
36          }
37          return deserialize(objectData, muleContext.getExecutionClassLoader(), muleContext);        
38      }
39      
40      /**
41       * <p>Deserializes an <code>Object</code> from the specified stream.</p>
42       * <p/>
43       * <p>The stream will be closed once the object is written. This
44       * avoids the need for a finally clause, and maybe also exception
45       * handling, in the application code.</p>
46       * <p/>
47       * <p>The stream passed in is not buffered internally within this method.
48       * This is the responsibility of your application if desired.</p>
49       *
50       * @param inputStream the serialized object input stream, must not be null
51       * @param cl          classloader which can load custom classes from the stream
52       * @return the deserialized object
53       * @throws IllegalArgumentException if <code>inputStream</code> is <code>null</code>
54       * @throws org.apache.commons.lang.SerializationException
55       *                                  (runtime) if the serialization fails
56       */
57      private static Object deserialize(InputStream inputStream, ClassLoader cl, MuleContext muleContext)
58      {
59          if (inputStream == null)
60          {
61              throw new IllegalArgumentException("The InputStream must not be null");
62          }
63          if (cl == null)
64          {
65              throw new IllegalArgumentException("The ClassLoader must not be null");
66          }
67          ObjectInputStream in = null;
68          try
69          {
70              // stream closed in the finally
71              in = new ClassLoaderObjectInputStream(cl, inputStream);
72              Object obj = in.readObject();
73              if (obj instanceof DeserializationPostInitialisable)
74              {
75                  DeserializationPostInitialisable.Implementation.init(obj, muleContext);
76              }
77              return obj;
78          }
79          catch (ClassNotFoundException ex)
80          {
81              throw new SerializationException(ex);
82          }
83          catch (IOException ex)
84          {
85              throw new SerializationException(ex);
86          }
87          catch (Exception ex)
88          {
89              throw new SerializationException(ex);
90          }
91          finally
92          {
93              try
94              {
95                  if (in != null)
96                  {
97                      in.close();
98                  }
99              }
100             catch (IOException ex)
101             {
102                 // ignore close exception
103             }
104         }
105     }
106 
107     /**
108      * <p>Deserializes a single <code>Object</code> from an array of bytes.</p>
109      *
110      * @param objectData the serialized object, must not be null
111      * @param cl         classloader which can load custom classes from the stream
112      * @return the deserialized object
113      * @throws IllegalArgumentException if <code>objectData</code> is <code>null</code>
114      * @throws SerializationException   (runtime) if the serialization fails
115      */
116     private static Object deserialize(byte[] objectData, ClassLoader cl, MuleContext muleContext)
117     {
118         if (objectData == null)
119         {
120             throw new IllegalArgumentException("The byte[] must not be null");
121         }
122         ByteArrayInputStream bais = new ByteArrayInputStream(objectData);
123         return deserialize(bais, cl, muleContext);
124     }
125     
126     /**
127      * @deprecated Call deserialize(InputStream inputStream, MuleContext muleContext) instead
128      */
129     public static Object deserialize(InputStream inputStream, ClassLoader cl)
130     {
131         return deserialize(inputStream, cl, null);
132     }
133     
134     /**
135      * @deprecated Call deserialize(byte[] objectData, MuleContext muleContext) instead
136      */
137     public static Object deserialize(byte[] objectData, ClassLoader cl)
138     {
139         return deserialize(objectData, cl, null);
140     }    
141 }