View Javadoc

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