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 | 0 | public class SerializationUtils extends org.apache.commons.lang.SerializationUtils |
21 | |
{ |
22 | |
public static Object deserialize(InputStream inputStream, MuleContext muleContext) |
23 | |
{ |
24 | 0 | if (muleContext == null) |
25 | |
{ |
26 | 0 | throw new IllegalArgumentException("The MuleContext must not be null"); |
27 | |
} |
28 | 0 | return deserialize(inputStream, muleContext.getExecutionClassLoader(), muleContext); |
29 | |
} |
30 | |
|
31 | |
public static Object deserialize(byte[] objectData, MuleContext muleContext) |
32 | |
{ |
33 | 0 | if (muleContext == null) |
34 | |
{ |
35 | 0 | throw new IllegalArgumentException("The MuleContext must not be null"); |
36 | |
} |
37 | 0 | 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 | 0 | if (inputStream == null) |
60 | |
{ |
61 | 0 | throw new IllegalArgumentException("The InputStream must not be null"); |
62 | |
} |
63 | 0 | if (cl == null) |
64 | |
{ |
65 | 0 | throw new IllegalArgumentException("The ClassLoader must not be null"); |
66 | |
} |
67 | 0 | ObjectInputStream in = null; |
68 | |
try |
69 | |
{ |
70 | |
|
71 | 0 | in = new ClassLoaderObjectInputStream(cl, inputStream); |
72 | 0 | Object obj = in.readObject(); |
73 | 0 | if (obj instanceof DeserializationPostInitialisable) |
74 | |
{ |
75 | 0 | DeserializationPostInitialisable.Implementation.init(obj, muleContext); |
76 | |
} |
77 | 0 | return obj; |
78 | |
} |
79 | 0 | catch (ClassNotFoundException ex) |
80 | |
{ |
81 | 0 | throw new SerializationException(ex); |
82 | |
} |
83 | 0 | catch (IOException ex) |
84 | |
{ |
85 | 0 | throw new SerializationException(ex); |
86 | |
} |
87 | 0 | catch (Exception ex) |
88 | |
{ |
89 | 0 | throw new SerializationException(ex); |
90 | |
} |
91 | |
finally |
92 | |
{ |
93 | 0 | try |
94 | |
{ |
95 | 0 | if (in != null) |
96 | |
{ |
97 | 0 | in.close(); |
98 | |
} |
99 | |
} |
100 | 0 | catch (IOException ex) |
101 | |
{ |
102 | |
|
103 | 0 | } |
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 | 0 | if (objectData == null) |
119 | |
{ |
120 | 0 | throw new IllegalArgumentException("The byte[] must not be null"); |
121 | |
} |
122 | 0 | ByteArrayInputStream bais = new ByteArrayInputStream(objectData); |
123 | 0 | return deserialize(bais, cl, muleContext); |
124 | |
} |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
public static Object deserialize(InputStream inputStream, ClassLoader cl) |
130 | |
{ |
131 | 0 | return deserialize(inputStream, cl, null); |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public static Object deserialize(byte[] objectData, ClassLoader cl) |
138 | |
{ |
139 | 0 | return deserialize(objectData, cl, null); |
140 | |
} |
141 | |
} |