KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > mapping > result > loader > EnhancedLazyResultLoader


1 /*
2  * Copyright 2004 Clinton Begin
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 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 JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.List JavaDoc;
29
30 /**
31  * Class to lazily load results into objects (uses CGLib to improve performance)
32  */

33 public class EnhancedLazyResultLoader {
34
35   private static final Class JavaDoc[] INTERFACES = new Class JavaDoc[]{List JavaDoc.class};
36   private Object JavaDoc loader;
37
38
39   /**
40    * Constructor for an enhanced lazy list loader
41    *
42    * @param client - the client that is creating the lazy list
43    * @param statementName - the statement to be used to build the list
44    * @param parameterObject - the parameter object to be used to build the list
45    * @param targetType - the type we are putting data into
46    */

47   public EnhancedLazyResultLoader(ExtendedSqlMapClient client, String JavaDoc statementName, Object JavaDoc parameterObject, Class JavaDoc targetType) {
48     loader = new EnhancedLazyResultLoaderImpl(client, statementName, parameterObject, targetType);
49   }
50
51   /**
52    * Loads the result
53    *
54    * @return the results - a list or object
55    *
56    * @throws SQLException if there is a problem
57    */

58   public Object JavaDoc loadResult() throws SQLException JavaDoc {
59     return ((EnhancedLazyResultLoaderImpl) loader).loadResult();
60   }
61
62
63   private static class EnhancedLazyResultLoaderImpl implements InvocationHandler {
64
65
66     protected ExtendedSqlMapClient client;
67     protected String JavaDoc statementName;
68     protected Object JavaDoc parameterObject;
69     protected Class JavaDoc targetType;
70
71     protected boolean loaded;
72     protected Object JavaDoc resultObject;
73
74     /**
75      * Constructor for an enhanced lazy list loader implementation
76      *
77      * @param client - the client that is creating the lazy list
78      * @param statementName - the statement to be used to build the list
79      * @param parameterObject - the parameter object to be used to build the list
80      * @param targetType - the type we are putting data into
81      */

82     public EnhancedLazyResultLoaderImpl(ExtendedSqlMapClient client, String JavaDoc statementName, Object JavaDoc parameterObject, Class JavaDoc targetType) {
83       this.client = client;
84       this.statementName = statementName;
85       this.parameterObject = parameterObject;
86       this.targetType = targetType;
87     }
88
89     /**
90      * Loads the result
91      *
92      * @return the results - a list or object
93      *
94      * @throws SQLException if there is a problem
95      */

96     public Object JavaDoc loadResult() throws SQLException JavaDoc {
97       if (DomTypeMarker.class.isAssignableFrom(targetType)) {
98         return ResultLoader.getResult(client, statementName, parameterObject, targetType);
99       } else if (Collection JavaDoc.class.isAssignableFrom(targetType)) {
100         return Enhancer.create(Object JavaDoc.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 JavaDoc invoke(Object JavaDoc o, Method JavaDoc method, Object JavaDoc[] objects) throws Throwable JavaDoc {
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 JavaDoc 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 JavaDoc e) {
132           throw new NestedRuntimeException("Error lazy loading result. Cause: " + e, e);
133         }
134       }
135     }
136   }
137
138
139 }
140
Popular Tags