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.javassist;
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 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 Log log = LogFactory.getLog(JavassistProxyFactory.class);
48    private static final String FINALIZE_METHOD = "finalize";
49    private static final String WRITE_REPLACE_METHOD = "writeReplace";
50  
51    public JavassistProxyFactory() {
52      try {
53        Resources.classForName("javassist.util.proxy.ProxyFactory");
54      } catch (Throwable e) {
55        throw new IllegalStateException("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, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
61      return EnhancedResultObjectProxyImpl.createProxy(target, lazyLoader, configuration, objectFactory, constructorArgTypes, constructorArgs);
62    }
63  
64    public Object createDeserializationProxy(Object target, Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
65      return EnhancedDeserializationProxyImpl.createProxy(target, unloadedProperties, objectFactory, constructorArgTypes, constructorArgs);
66    }
67  
68    @Override
69    public void setProperties(Properties properties) {
70        // Not Implemented
71    }
72  
73    static Object crateProxy(Class<?> type, MethodHandler callback, List<Class<?>> constructorArgTypes, 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 (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  
90      Object enhanced = null;
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 
104     private Class<?> type;
105     private ResultLoaderMap lazyLoader;
106     private boolean aggressive;
107     private Set<String> lazyLoadTriggerMethods;
108     private ObjectFactory objectFactory;
109     private List<Class<?>> constructorArgTypes;
110     private List<Object> constructorArgs;
111 
112     private EnhancedResultObjectProxyImpl(Class<?> type, ResultLoaderMap lazyLoader, Configuration configuration, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
113       this.type = type;
114       this.lazyLoader = lazyLoader;
115       this.aggressive = configuration.isAggressiveLazyLoading();
116       this.lazyLoadTriggerMethods = configuration.getLazyLoadTriggerMethods();
117       this.objectFactory = objectFactory;
118       this.constructorArgTypes = constructorArgTypes;
119       this.constructorArgs = constructorArgs;
120     }
121 
122     public static Object createProxy(Object target, ResultLoaderMap lazyLoader, Configuration configuration, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
123       final Class<?> type = target.getClass();
124       EnhancedResultObjectProxyImpl callback = new EnhancedResultObjectProxyImpl(type, lazyLoader, configuration, objectFactory, constructorArgTypes, constructorArgs);
125       Object enhanced = crateProxy(type, callback, constructorArgTypes, constructorArgs);
126       PropertyCopier.copyBeanProperties(type, target, enhanced);
127       return enhanced;
128     }
129 
130     @Override
131     public Object invoke(Object enhanced, Method method, Method methodProxy, Object[] args) throws Throwable {
132       final String methodName = method.getName();
133       try {
134         synchronized (lazyLoader) {
135           if (WRITE_REPLACE_METHOD.equals(methodName)) {
136             Object original = null;
137             if (constructorArgTypes.isEmpty()) {
138               original = objectFactory.create(type);
139             } else {
140               original = objectFactory.create(type, constructorArgTypes, constructorArgs);
141             }
142             PropertyCopier.copyBeanProperties(type, enhanced, original);
143             if (lazyLoader.size() > 0) {
144               return new JavassistSerialStateHolder(original, lazyLoader.getProperties(), objectFactory, constructorArgTypes, constructorArgs);
145             } else {
146               return original;
147             }
148           } else {
149             if (lazyLoader.size() > 0 && !FINALIZE_METHOD.equals(methodName)) {
150               if (aggressive || lazyLoadTriggerMethods.contains(methodName)) {
151                 lazyLoader.loadAll();
152               } else if (PropertyNamer.isProperty(methodName)) {
153                 final String property = PropertyNamer.methodToProperty(methodName);
154                 if (lazyLoader.hasLoader(property)) {
155                   lazyLoader.load(property);
156                 }
157               }
158             }
159           }
160         }
161         return methodProxy.invoke(enhanced, args);
162       } catch (Throwable t) {
163         throw ExceptionUtil.unwrapThrowable(t);
164       }
165     }
166   }
167 
168   private static class EnhancedDeserializationProxyImpl extends AbstractEnhancedDeserializationProxy implements MethodHandler {
169 
170     private EnhancedDeserializationProxyImpl(Class<?> type, Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory,
171             List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
172       super(type, unloadedProperties, objectFactory, constructorArgTypes, constructorArgs);
173     }
174 
175     public static Object createProxy(Object target, Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory,
176             List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
177       final Class<?> type = target.getClass();
178       EnhancedDeserializationProxyImpl callback = new EnhancedDeserializationProxyImpl(type, unloadedProperties, objectFactory, constructorArgTypes, constructorArgs);
179       Object enhanced = crateProxy(type, callback, constructorArgTypes, constructorArgs);
180       PropertyCopier.copyBeanProperties(type, target, enhanced);
181       return enhanced;
182     }
183 
184     @Override
185     public Object invoke(Object enhanced, Method method, Method methodProxy, Object[] args) throws Throwable {
186       final Object o = super.invoke(enhanced, method, args);
187       return (o instanceof AbstractSerialStateHolder) ? o : methodProxy.invoke(o, args);
188     }
189 
190     @Override
191     protected AbstractSerialStateHolder newSerialStateHolder(Object userBean, Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory,
192             List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
193       return new JavassistSerialStateHolder(userBean, unloadedProperties, objectFactory, constructorArgTypes, constructorArgs);
194     }
195   }
196 }