AbstractSerialStateHolder.java

  1. /*
  2.  *    Copyright 2009-2023 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.  *       https://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. import java.io.ByteArrayInputStream;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.Externalizable;
  20. import java.io.IOException;
  21. import java.io.InvalidClassException;
  22. import java.io.ObjectInput;
  23. import java.io.ObjectInputStream;
  24. import java.io.ObjectOutput;
  25. import java.io.ObjectOutputStream;
  26. import java.io.ObjectStreamException;
  27. import java.io.StreamCorruptedException;
  28. import java.util.Arrays;
  29. import java.util.HashMap;
  30. import java.util.List;
  31. import java.util.Map;

  32. import org.apache.ibatis.io.SerialFilterChecker;
  33. import org.apache.ibatis.reflection.factory.ObjectFactory;

  34. /**
  35.  * @author Eduardo Macarron
  36.  * @author Franta Mejta
  37.  */
  38. public abstract class AbstractSerialStateHolder implements Externalizable {

  39.   private static final long serialVersionUID = 8940388717901644661L;
  40.   private static final ThreadLocal<ObjectOutputStream> stream = new ThreadLocal<>();
  41.   private byte[] userBeanBytes = {};
  42.   private Object userBean;
  43.   private Map<String, ResultLoaderMap.LoadPair> unloadedProperties;
  44.   private ObjectFactory objectFactory;
  45.   private Class<?>[] constructorArgTypes;
  46.   private Object[] constructorArgs;

  47.   public AbstractSerialStateHolder() {
  48.   }

  49.   public AbstractSerialStateHolder(final Object userBean,
  50.       final Map<String, ResultLoaderMap.LoadPair> unloadedProperties, final ObjectFactory objectFactory,
  51.       List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
  52.     this.userBean = userBean;
  53.     this.unloadedProperties = new HashMap<>(unloadedProperties);
  54.     this.objectFactory = objectFactory;
  55.     this.constructorArgTypes = constructorArgTypes.toArray(new Class<?>[0]);
  56.     this.constructorArgs = constructorArgs.toArray(new Object[0]);
  57.   }

  58.   @Override
  59.   public final void writeExternal(final ObjectOutput out) throws IOException {
  60.     boolean firstRound = false;
  61.     final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  62.     ObjectOutputStream os = stream.get();
  63.     if (os == null) {
  64.       os = new ObjectOutputStream(baos);
  65.       firstRound = true;
  66.       stream.set(os);
  67.     }

  68.     os.writeObject(this.userBean);
  69.     os.writeObject(this.unloadedProperties);
  70.     os.writeObject(this.objectFactory);
  71.     os.writeObject(this.constructorArgTypes);
  72.     os.writeObject(this.constructorArgs);

  73.     final byte[] bytes = baos.toByteArray();
  74.     out.writeObject(bytes);

  75.     if (firstRound) {
  76.       stream.remove();
  77.     }
  78.   }

  79.   @Override
  80.   public final void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
  81.     final Object data = in.readObject();
  82.     if (data.getClass().isArray()) {
  83.       this.userBeanBytes = (byte[]) data;
  84.     } else {
  85.       this.userBean = data;
  86.     }
  87.   }

  88.   @SuppressWarnings("unchecked")
  89.   protected final Object readResolve() throws ObjectStreamException {
  90.     /* Second run */
  91.     if (this.userBean != null && this.userBeanBytes.length == 0) {
  92.       return this.userBean;
  93.     }

  94.     SerialFilterChecker.check();

  95.     /* First run */
  96.     try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(this.userBeanBytes))) {
  97.       this.userBean = in.readObject();
  98.       this.unloadedProperties = (Map<String, ResultLoaderMap.LoadPair>) in.readObject();
  99.       this.objectFactory = (ObjectFactory) in.readObject();
  100.       this.constructorArgTypes = (Class<?>[]) in.readObject();
  101.       this.constructorArgs = (Object[]) in.readObject();
  102.     } catch (final IOException ex) {
  103.       throw (ObjectStreamException) new StreamCorruptedException().initCause(ex);
  104.     } catch (final ClassNotFoundException ex) {
  105.       throw (ObjectStreamException) new InvalidClassException(ex.getLocalizedMessage()).initCause(ex);
  106.     }

  107.     final Map<String, ResultLoaderMap.LoadPair> arrayProps = new HashMap<>(this.unloadedProperties);
  108.     final List<Class<?>> arrayTypes = Arrays.asList(this.constructorArgTypes);
  109.     final List<Object> arrayValues = Arrays.asList(this.constructorArgs);

  110.     return this.createDeserializationProxy(userBean, arrayProps, objectFactory, arrayTypes, arrayValues);
  111.   }

  112.   protected abstract Object createDeserializationProxy(Object target,
  113.       Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory,
  114.       List<Class<?>> constructorArgTypes, List<Object> constructorArgs);
  115. }