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