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