1
2
3
4
5
6
7
8
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
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
107 }
108 }
109 }
110
111
112
113
114
115
116
117
118
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
132
133 public static Object deserialize(InputStream inputStream, ClassLoader cl)
134 {
135 return deserialize(inputStream, cl, null);
136 }
137
138
139
140
141 public static Object deserialize(byte[] objectData, ClassLoader cl)
142 {
143 return deserialize(objectData, cl, null);
144 }
145 }