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