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.cglib;
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 net.sf.cglib.proxy.Callback;
24  import net.sf.cglib.proxy.Enhancer;
25  import net.sf.cglib.proxy.MethodInterceptor;
26  import net.sf.cglib.proxy.MethodProxy;
27  
28  import org.apache.ibatis.executor.loader.AbstractEnhancedDeserializationProxy;
29  import org.apache.ibatis.executor.loader.AbstractSerialStateHolder;
30  import org.apache.ibatis.executor.loader.ProxyFactory;
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 Clinton Begin
44   */
45  public class CglibProxyFactory implements ProxyFactory {
46  
47    private static final String FINALIZE_METHOD = "finalize";
48    private static final String WRITE_REPLACE_METHOD = "writeReplace";
49  
50    public CglibProxyFactory() {
51      try {
52        Resources.classForName("net.sf.cglib.proxy.Enhancer");
53      } catch (Throwable e) {
54        throw new IllegalStateException("Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath.", e);
55      }
56    }
57  
58    @Override
59    public Object createProxy(Object target, ResultLoaderMap lazyLoader, Configuration configuration, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
60      return EnhancedResultObjectProxyImpl.createProxy(target, lazyLoader, configuration, objectFactory, constructorArgTypes, constructorArgs);
61    }
62  
63    public Object createDeserializationProxy(Object target, Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
64      return EnhancedDeserializationProxyImpl.createProxy(target, unloadedProperties, objectFactory, constructorArgTypes, constructorArgs);
65    }
66  
67    static Object crateProxy(Class<?> type, Callback callback, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
68      Enhancer enhancer = new Enhancer();
69      enhancer.setCallback(callback);
70      enhancer.setSuperclass(type);
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      Object enhanced;
83      if (constructorArgTypes.isEmpty()) {
84        enhanced = enhancer.create();
85      } else {
86        Class<?>[] typesArray = constructorArgTypes.toArray(new Class[constructorArgTypes.size()]);
87        Object[] valuesArray = constructorArgs.toArray(new Object[constructorArgs.size()]);
88        enhanced = enhancer.create(typesArray, valuesArray);
89      }
90      return enhanced;
91    }
92  
93    private static class EnhancedResultObjectProxyImpl implements MethodInterceptor {
94  
95      private final Class<?> type;
96      private final ResultLoaderMap lazyLoader;
97      private final boolean aggressive;
98      private final Set<String> lazyLoadTriggerMethods;
99      private final ObjectFactory objectFactory;
100     private final List<Class<?>> constructorArgTypes;
101     private final List<Object> constructorArgs;
102 
103     private EnhancedResultObjectProxyImpl(Class<?> type, ResultLoaderMap lazyLoader, Configuration configuration, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
104       this.type = type;
105       this.lazyLoader = lazyLoader;
106       this.aggressive = configuration.isAggressiveLazyLoading();
107       this.lazyLoadTriggerMethods = configuration.getLazyLoadTriggerMethods();
108       this.objectFactory = objectFactory;
109       this.constructorArgTypes = constructorArgTypes;
110       this.constructorArgs = constructorArgs;
111     }
112 
113     public static Object createProxy(Object target, ResultLoaderMap lazyLoader, Configuration configuration, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
114       final Class<?> type = target.getClass();
115       EnhancedResultObjectProxyImpl callback = new EnhancedResultObjectProxyImpl(type, lazyLoader, configuration, objectFactory, constructorArgTypes, constructorArgs);
116       Object enhanced = crateProxy(type, callback, constructorArgTypes, constructorArgs);
117       PropertyCopier.copyBeanProperties(type, target, enhanced);
118       return enhanced;
119     }
120 
121     @Override
122     public Object intercept(Object enhanced, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
123       final String methodName = method.getName();
124       try {
125         synchronized (lazyLoader) {
126           if (WRITE_REPLACE_METHOD.equals(methodName)) {
127             Object original;
128             if (constructorArgTypes.isEmpty()) {
129               original = objectFactory.create(type);
130             } else {
131               original = objectFactory.create(type, constructorArgTypes, constructorArgs);
132             }
133             PropertyCopier.copyBeanProperties(type, enhanced, original);
134             if (lazyLoader.size() > 0) {
135               return new CglibSerialStateHolder(original, lazyLoader.getProperties(), objectFactory, constructorArgTypes, constructorArgs);
136             } else {
137               return original;
138             }
139           } else {
140             if (lazyLoader.size() > 0 && !FINALIZE_METHOD.equals(methodName)) {
141               if (aggressive || lazyLoadTriggerMethods.contains(methodName)) {
142                 lazyLoader.loadAll();
143               } else if (PropertyNamer.isSetter(methodName)) {
144                 final String property = PropertyNamer.methodToProperty(methodName);
145                 lazyLoader.remove(property);
146               } else if (PropertyNamer.isGetter(methodName)) {
147                 final String property = PropertyNamer.methodToProperty(methodName);
148                 if (lazyLoader.hasLoader(property)) {
149                   lazyLoader.load(property);
150                 }
151               }
152             }
153           }
154         }
155         return methodProxy.invokeSuper(enhanced, args);
156       } catch (Throwable t) {
157         throw ExceptionUtil.unwrapThrowable(t);
158       }
159     }
160   }
161 
162   private static class EnhancedDeserializationProxyImpl extends AbstractEnhancedDeserializationProxy implements MethodInterceptor {
163 
164     private EnhancedDeserializationProxyImpl(Class<?> type, Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory,
165             List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
166       super(type, unloadedProperties, objectFactory, constructorArgTypes, constructorArgs);
167     }
168 
169     public static Object createProxy(Object target, Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory,
170             List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
171       final Class<?> type = target.getClass();
172       EnhancedDeserializationProxyImpl callback = new EnhancedDeserializationProxyImpl(type, unloadedProperties, objectFactory, constructorArgTypes, constructorArgs);
173       Object enhanced = crateProxy(type, callback, constructorArgTypes, constructorArgs);
174       PropertyCopier.copyBeanProperties(type, target, enhanced);
175       return enhanced;
176     }
177 
178     @Override
179     public Object intercept(Object enhanced, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
180       final Object o = super.invoke(enhanced, method, args);
181       return o instanceof AbstractSerialStateHolder ? o : methodProxy.invokeSuper(o, args);
182     }
183 
184     @Override
185     protected AbstractSerialStateHolder newSerialStateHolder(Object userBean, Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory,
186             List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
187       return new CglibSerialStateHolder(userBean, unloadedProperties, objectFactory, constructorArgTypes, constructorArgs);
188     }
189   }
190 
191   private static class LogHolder {
192     private static final Log log = LogFactory.getLog(CglibProxyFactory.class);
193   }
194 
195 }