1
2
3
4
5
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
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
103 }
104 }
105 }
106
107
108
109
110
111
112
113
114
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
128
129 public static Object deserialize(InputStream inputStream, ClassLoader cl)
130 {
131 return deserialize(inputStream, cl, null);
132 }
133
134
135
136
137 public static Object deserialize(byte[] objectData, ClassLoader cl)
138 {
139 return deserialize(objectData, cl, null);
140 }
141 }