1 16 package com.ibatis.sqlmap.engine.mapping.result.loader; 17 18 import com.ibatis.common.beans.ClassInfo; 19 import com.ibatis.common.exception.NestedRuntimeException; 20 import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient; 21 import com.ibatis.sqlmap.engine.type.DomTypeMarker; 22 import net.sf.cglib.proxy.Enhancer; 23 import net.sf.cglib.proxy.InvocationHandler; 24 25 import java.lang.reflect.Method ; 26 import java.sql.SQLException ; 27 import java.util.Collection ; 28 import java.util.List ; 29 30 33 public class EnhancedLazyResultLoader { 34 35 private static final Class [] INTERFACES = new Class []{List .class}; 36 private Object loader; 37 38 39 47 public EnhancedLazyResultLoader(ExtendedSqlMapClient client, String statementName, Object parameterObject, Class targetType) { 48 loader = new EnhancedLazyResultLoaderImpl(client, statementName, parameterObject, targetType); 49 } 50 51 58 public Object loadResult() throws SQLException { 59 return ((EnhancedLazyResultLoaderImpl) loader).loadResult(); 60 } 61 62 63 private static class EnhancedLazyResultLoaderImpl implements InvocationHandler { 64 65 66 protected ExtendedSqlMapClient client; 67 protected String statementName; 68 protected Object parameterObject; 69 protected Class targetType; 70 71 protected boolean loaded; 72 protected Object resultObject; 73 74 82 public EnhancedLazyResultLoaderImpl(ExtendedSqlMapClient client, String statementName, Object parameterObject, Class targetType) { 83 this.client = client; 84 this.statementName = statementName; 85 this.parameterObject = parameterObject; 86 this.targetType = targetType; 87 } 88 89 96 public Object loadResult() throws SQLException { 97 if (DomTypeMarker.class.isAssignableFrom(targetType)) { 98 return ResultLoader.getResult(client, statementName, parameterObject, targetType); 99 } else if (Collection .class.isAssignableFrom(targetType)) { 100 return Enhancer.create(Object .class, INTERFACES, this); 101 } else if (targetType.isArray() || ClassInfo.isKnownType(targetType)) { 102 return ResultLoader.getResult(client, statementName, parameterObject, targetType); 103 } else { 104 return Enhancer.create(targetType, this); 105 } 106 } 107 108 public Object invoke(Object o, Method method, Object [] objects) throws Throwable { 109 if ("finalize".hashCode() == method.getName().hashCode() 110 && "finalize".equals(method.getName())) { 111 return null; 112 } else { 113 loadObject(); 114 if (resultObject != null) { 115 try { 116 return method.invoke(resultObject, objects); 117 } catch (Throwable t) { 118 throw ClassInfo.unwrapThrowable(t); 119 } 120 } else { 121 return null; 122 } 123 } 124 } 125 126 private synchronized void loadObject() { 127 if (!loaded) { 128 try { 129 loaded = true; 130 resultObject = ResultLoader.getResult(client, statementName, parameterObject, targetType); 131 } catch (SQLException e) { 132 throw new NestedRuntimeException("Error lazy loading result. Cause: " + e, e); 133 } 134 } 135 } 136 } 137 138 139 } 140 | Popular Tags |