View Javadoc
1   /*
2    *    Copyright 2009-2021 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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   * @author Eduardo Macarron
39   * @author Franta Mejta
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 = new byte[0];
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(
56            final Object userBean,
57            final Map<String, ResultLoaderMap.LoadPair> unloadedProperties,
58            final ObjectFactory objectFactory,
59            List<Class<?>> constructorArgTypes,
60            List<Object> constructorArgs) {
61      this.userBean = userBean;
62      this.unloadedProperties = new HashMap<>(unloadedProperties);
63      this.objectFactory = objectFactory;
64      this.constructorArgTypes = constructorArgTypes.toArray(new Class<?>[0]);
65      this.constructorArgs = constructorArgs.toArray(new Object[0]);
66    }
67  
68    @Override
69    public final void writeExternal(final ObjectOutput out) throws IOException {
70      boolean firstRound = false;
71      final ByteArrayOutputStream baos = new ByteArrayOutputStream();
72      ObjectOutputStream os = stream.get();
73      if (os == null) {
74        os = new ObjectOutputStream(baos);
75        firstRound = true;
76        stream.set(os);
77      }
78  
79      os.writeObject(this.userBean);
80      os.writeObject(this.unloadedProperties);
81      os.writeObject(this.objectFactory);
82      os.writeObject(this.constructorArgTypes);
83      os.writeObject(this.constructorArgs);
84  
85      final byte[] bytes = baos.toByteArray();
86      out.writeObject(bytes);
87  
88      if (firstRound) {
89        stream.remove();
90      }
91    }
92  
93    @Override
94    public final void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
95      final Object data = in.readObject();
96      if (data.getClass().isArray()) {
97        this.userBeanBytes = (byte[]) data;
98      } else {
99        this.userBean = data;
100     }
101   }
102 
103   @SuppressWarnings("unchecked")
104   protected final Object readResolve() throws ObjectStreamException {
105     /* Second run */
106     if (this.userBean != null && this.userBeanBytes.length == 0) {
107       return this.userBean;
108     }
109 
110     SerialFilterChecker.check();
111 
112     /* First run */
113     try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(this.userBeanBytes))) {
114       this.userBean = in.readObject();
115       this.unloadedProperties = (Map<String, ResultLoaderMap.LoadPair>) in.readObject();
116       this.objectFactory = (ObjectFactory) in.readObject();
117       this.constructorArgTypes = (Class<?>[]) in.readObject();
118       this.constructorArgs = (Object[]) in.readObject();
119     } catch (final IOException ex) {
120       throw (ObjectStreamException) new StreamCorruptedException().initCause(ex);
121     } catch (final ClassNotFoundException ex) {
122       throw (ObjectStreamException) new InvalidClassException(ex.getLocalizedMessage()).initCause(ex);
123     }
124 
125     final Map<String, ResultLoaderMap.LoadPair> arrayProps = new HashMap<>(this.unloadedProperties);
126     final List<Class<?>> arrayTypes = Arrays.asList(this.constructorArgTypes);
127     final List<Object> arrayValues = Arrays.asList(this.constructorArgs);
128 
129     return this.createDeserializationProxy(userBean, arrayProps, objectFactory, arrayTypes, arrayValues);
130   }
131 
132   protected abstract Object createDeserializationProxy(Object target, Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory,
133           List<Class<?>> constructorArgTypes, List<Object> constructorArgs);
134 }