1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.executor.loader;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.Externalizable;
21 import java.io.IOException;
22 import java.io.InvalidClassException;
23 import java.io.ObjectInput;
24 import java.io.ObjectInputStream;
25 import java.io.ObjectOutput;
26 import java.io.ObjectOutputStream;
27 import java.io.ObjectStreamException;
28 import java.io.StreamCorruptedException;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33
34 import org.apache.ibatis.reflection.factory.ObjectFactory;
35
36
37
38
39
40 public abstract class AbstractSerialStateHolder implements Externalizable {
41
42 private static final long serialVersionUID = 8940388717901644661L;
43 private static final ThreadLocal<ObjectOutputStream> stream = new ThreadLocal<ObjectOutputStream>();
44 private byte[] userBeanBytes = new byte[0];
45 private Object userBean;
46 private Map<String, ResultLoaderMap.LoadPair> unloadedProperties;
47 private ObjectFactory objectFactory;
48 private Class<?>[] constructorArgTypes;
49 private Object[] constructorArgs;
50
51 public AbstractSerialStateHolder() {
52 }
53
54 public AbstractSerialStateHolder(
55 final Object userBean,
56 final Map<String, ResultLoaderMap.LoadPair> unloadedProperties,
57 final ObjectFactory objectFactory,
58 List<Class<?>> constructorArgTypes,
59 List<Object> constructorArgs) {
60 this.userBean = userBean;
61 this.unloadedProperties = new HashMap<String, ResultLoaderMap.LoadPair>(unloadedProperties);
62 this.objectFactory = objectFactory;
63 this.constructorArgTypes = constructorArgTypes.toArray(new Class<?>[constructorArgTypes.size()]);
64 this.constructorArgs = constructorArgs.toArray(new Object[constructorArgs.size()]);
65 }
66
67 @Override
68 public final void writeExternal(final ObjectOutput out) throws IOException {
69 boolean firstRound = false;
70 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
71 ObjectOutputStream os = stream.get();
72 if (os == null) {
73 os = new ObjectOutputStream(baos);
74 firstRound = true;
75 stream.set(os);
76 }
77
78 os.writeObject(this.userBean);
79 os.writeObject(this.unloadedProperties);
80 os.writeObject(this.objectFactory);
81 os.writeObject(this.constructorArgTypes);
82 os.writeObject(this.constructorArgs);
83
84 final byte[] bytes = baos.toByteArray();
85 out.writeObject(bytes);
86
87 if (firstRound) {
88 stream.remove();
89 }
90 }
91
92 @Override
93 public final void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
94 final Object data = in.readObject();
95 if (data.getClass().isArray()) {
96 this.userBeanBytes = (byte[]) data;
97 } else {
98 this.userBean = data;
99 }
100 }
101
102 @SuppressWarnings("unchecked")
103 protected final Object readResolve() throws ObjectStreamException {
104
105 if (this.userBean != null && this.userBeanBytes.length == 0) {
106 return this.userBean;
107 }
108
109
110 try {
111 final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(this.userBeanBytes));
112 this.userBean = in.readObject();
113 this.unloadedProperties = (Map<String, ResultLoaderMap.LoadPair>) in.readObject();
114 this.objectFactory = (ObjectFactory) in.readObject();
115 this.constructorArgTypes = (Class<?>[]) in.readObject();
116 this.constructorArgs = (Object[]) in.readObject();
117 } catch (final IOException ex) {
118 throw (ObjectStreamException) new StreamCorruptedException().initCause(ex);
119 } catch (final ClassNotFoundException ex) {
120 throw (ObjectStreamException) new InvalidClassException(ex.getLocalizedMessage()).initCause(ex);
121 }
122
123 final Map<String, ResultLoaderMap.LoadPair> arrayProps = new HashMap<String, ResultLoaderMap.LoadPair>(this.unloadedProperties);
124 final List<Class<?>> arrayTypes = Arrays.asList(this.constructorArgTypes);
125 final List<Object> arrayValues = Arrays.asList(this.constructorArgs);
126
127 return this.createDeserializationProxy(userBean, arrayProps, objectFactory, arrayTypes, arrayValues);
128 }
129
130 protected abstract Object createDeserializationProxy(Object target, Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory,
131 List<Class<?>> constructorArgTypes, List<Object> constructorArgs);
132 }