View Javadoc
1   /**
2    *    Copyright 2009-2019 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.javassist;
17  
18  import java.lang.reflect.Method;
19  import java.util.List;
20  import java.util.Map;
21  import java.util.Set;
22  
23  import javassist.util.proxy.MethodHandler;
24  import javassist.util.proxy.Proxy;
25  import javassist.util.proxy.ProxyFactory;
26  
27  import org.apache.ibatis.executor.ExecutorException;
28  import org.apache.ibatis.executor.loader.AbstractEnhancedDeserializationProxy;
29  import org.apache.ibatis.executor.loader.AbstractSerialStateHolder;
30  import org.apache.ibatis.executor.loader.ResultLoaderMap;
31  import org.apache.ibatis.executor.loader.WriteReplaceInterface;
32  import org.apache.ibatis.io.Resources;
33  import org.apache.ibatis.logging.Log;
34  import org.apache.ibatis.logging.LogFactory;
35  import org.apache.ibatis.reflection.ExceptionUtil;
36  import org.apache.ibatis.reflection.factory.ObjectFactory;
37  import org.apache.ibatis.reflection.property.PropertyCopier;
38  import org.apache.ibatis.reflection.property.PropertyNamer;
39  import org.apache.ibatis.session.Configuration;
40  
41  /**
42   * @author Eduardo Macarron
43   */
44  public class JavassistProxyFactory implements org.apache.ibatis.executor.loader.ProxyFactory {
45  
46    private static final String FINALIZE_METHOD = "finalize";
47    private static final String WRITE_REPLACE_METHOD = "writeReplace";
48  
49    public JavassistProxyFactory() {
50      try {
51        Resources.classForName("javassist.util.proxy.ProxyFactory");
52      } catch (Throwable e) {
53        throw new IllegalStateException("Cannot enable lazy loading because Javassist is not available. Add Javassist to your classpath.", e);
54      }
55    }
56  
57    @Override
58    public Object createProxy(Object target, ResultLoaderMap lazyLoader, Configuration configuration, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
59      return EnhancedResultObjectProxyImpl.createProxy(target, lazyLoader, configuration, objectFactory, constructorArgTypes, constructorArgs);
60    }
61  
62    public Object createDeserializationProxy(Object target, Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
63      return EnhancedDeserializationProxyImpl.createProxy(target, unloadedProperties, objectFactory, constructorArgTypes, constructorArgs);
64    }
65  
66    static Object crateProxy(Class<?> type, MethodHandler callback, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
67  
68      ProxyFactoryder/ProxyFactory.html#ProxyFactory">ProxyFactory enhancer = new ProxyFactory();
69      enhancer.setSuperclass(type);
70  
71      try {
72        type.getDeclaredMethod(WRITE_REPLACE_METHOD);
73        // ObjectOutputStream will call writeReplace of objects returned by writeReplace
74        if (LogHolder.log.isDebugEnabled()) {
75          LogHolder.log.debug(WRITE_REPLACE_METHOD + " method was found on bean " + type + ", make sure it returns this");
76        }
77      } catch (NoSuchMethodException e) {
78        enhancer.setInterfaces(new Class[]{WriteReplaceInterface.class});
79      } catch (SecurityException e) {
80        // nothing to do here
81      }
82  
83      Object enhanced;
84      Class<?>[] typesArray = constructorArgTypes.toArray(new Class[constructorArgTypes.size()]);
85      Object[] valuesArray = constructorArgs.toArray(new Object[constructorArgs.size()]);
86      try {
87        enhanced = enhancer.create(typesArray, valuesArray);
88      } catch (Exception e) {
89        throw new ExecutorException("Error creating lazy proxy.  Cause: " + e, e);
90      }
91      ((Proxy) enhanced).setHandler(callback);
92      return enhanced;
93    }
94  
95    private static class EnhancedResultObjectProxyImpl implements MethodHandler {
96  
97      private final Class<?> type;
98      private final ResultLoaderMap lazyLoader;
99      private final boolean aggressive;
100     private final Set<String> lazyLoadTriggerMethods;
101     private final ObjectFactory objectFactory;
102     private final List<Class<?>> constructorArgTypes;
103     private final List<Object> constructorArgs;
104 
105     private EnhancedResultObjectProxyImpl(Class<?> type, ResultLoaderMap lazyLoader, Configuration configuration, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
106       this.type = type;
107       this.lazyLoader = lazyLoader;
108       this.aggressive = configuration.isAggressiveLazyLoading();
109       this.lazyLoadTriggerMethods = configuration.getLazyLoadTriggerMethods();
110       this.objectFactory = objectFactory;
111       this.constructorArgTypes = constructorArgTypes;
112       this.constructorArgs = constructorArgs;
113     }
114 
115     public static Object createProxy(Object target, ResultLoaderMap lazyLoader, Configuration configuration, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
116       final Class<?> type = target.getClass();
117       EnhancedResultObjectProxyImpl callback = new EnhancedResultObjectProxyImpl(type, lazyLoader, configuration, objectFactory, constructorArgTypes, constructorArgs);
118       Object enhanced = crateProxy(type, callback, constructorArgTypes, constructorArgs);
119       PropertyCopier.copyBeanProperties(type, target, enhanced);
120       return enhanced;
121     }
122 
123     @Override
124     public Object invoke(Object enhanced, Method method, Method methodProxy, Object[] args) throws Throwable {
125       final String methodName = method.getName();
126       try {
127         synchronized (lazyLoader) {
128           if (WRITE_REPLACE_METHOD.equals(methodName)) {
129             Object original;
130             if (constructorArgTypes.isEmpty()) {
131               original = objectFactory.create(type);
132             } else {
133               original = objectFactory.create(type, constructorArgTypes, constructorArgs);
134             }
135             PropertyCopier.copyBeanProperties(type, enhanced, original);
136             if (lazyLoader.size() > 0) {
137               return new JavassistSerialStateHolder(original, lazyLoader.getProperties(), objectFactory, constructorArgTypes, constructorArgs);
138             } else {
139               return original;
140             }
141           } else {
142             if (lazyLoader.size() > 0 && !FINALIZE_METHOD.equals(methodName)) {
143               if (aggressive || lazyLoadTriggerMethods.contains(methodName)) {
144                 lazyLoader.loadAll();
145               } else if (PropertyNamer.isSetter(methodName)) {
146                 final String property = PropertyNamer.methodToProperty(methodName);
147                 lazyLoader.remove(property);
148               } else if (PropertyNamer.isGetter(methodName)) {
149                 final String property = PropertyNamer.methodToProperty(methodName);
150                 if (lazyLoader.hasLoader(property)) {
151                   lazyLoader.load(property);
152                 }
153               }
154             }
155           }
156         }
157         return methodProxy.invoke(enhanced, args);
158       } catch (Throwable t) {
159         throw ExceptionUtil.unwrapThrowable(t);
160       }
161     }
162   }
163 
164   private static class EnhancedDeserializationProxyImpl extends AbstractEnhancedDeserializationProxy implements MethodHandler {
165 
166     private EnhancedDeserializationProxyImpl(Class<?> type, Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory,
167             List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
168       super(type, unloadedProperties, objectFactory, constructorArgTypes, constructorArgs);
169     }
170 
171     public static Object createProxy(Object target, Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory,
172             List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
173       final Class<?> type = target.getClass();
174       EnhancedDeserializationProxyImpl callback = new EnhancedDeserializationProxyImpl(type, unloadedProperties, objectFactory, constructorArgTypes, constructorArgs);
175       Object enhanced = crateProxy(type, callback, constructorArgTypes, constructorArgs);
176       PropertyCopier.copyBeanProperties(type, target, enhanced);
177       return enhanced;
178     }
179 
180     @Override
181     public Object invoke(Object enhanced, Method method, Method methodProxy, Object[] args) throws Throwable {
182       final Object o = super.invoke(enhanced, method, args);
183       return o instanceof AbstractSerialStateHolder ? o : methodProxy.invoke(o, args);
184     }
185 
186     @Override
187     protected AbstractSerialStateHolder newSerialStateHolder(Object userBean, Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory,
188             List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
189       return new JavassistSerialStateHolder(userBean, unloadedProperties, objectFactory, constructorArgTypes, constructorArgs);
190     }
191   }
192 
193   private static class LogHolder {
194     private static final Log log = LogFactory.getLog(JavassistProxyFactory.class);
195   }
196 
197 }