KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 import java.lang.reflect.InvocationHandler JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.lang.reflect.Proxy JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.List JavaDoc;
28
29 /**
30  * Class to lazily load results into objects
31  */

32 public class LazyResultLoader implements InvocationHandler JavaDoc {
33
34   private static final Class JavaDoc[] LIST_INTERFACES = new Class JavaDoc[]{List JavaDoc.class};
35
36   protected ExtendedSqlMapClient client;
37   protected String JavaDoc statementName;
38   protected Object JavaDoc parameterObject;
39   protected Class JavaDoc targetType;
40
41   protected boolean loaded;
42   protected Object JavaDoc resultObject;
43
44   /**
45    * Constructor for a lazy list loader
46    *
47    * @param client - the client that is creating the lazy list
48    * @param statementName - the statement to be used to build the list
49    * @param parameterObject - the parameter object to be used to build the list
50    * @param targetType - the type we are putting data into
51    */

52   public LazyResultLoader(ExtendedSqlMapClient client, String JavaDoc statementName, Object JavaDoc parameterObject, Class JavaDoc targetType) {
53     this.client = client;
54     this.statementName = statementName;
55     this.parameterObject = parameterObject;
56     this.targetType = targetType;
57   }
58
59   /**
60    * Loads the result
61    *
62    * @return the results - a list or object
63    *
64    * @throws SQLException if there is a problem
65    */

66   public Object JavaDoc loadResult() throws SQLException JavaDoc {
67     if (Collection JavaDoc.class.isAssignableFrom(targetType)) {
68       InvocationHandler JavaDoc handler = new LazyResultLoader(client, statementName, parameterObject, targetType);
69       ClassLoader JavaDoc cl = targetType.getClassLoader();
70       return Proxy.newProxyInstance(cl, LIST_INTERFACES, handler);
71     } else {
72       return ResultLoader.getResult(client, statementName, parameterObject, targetType);
73     }
74   }
75
76   public Object JavaDoc invoke(Object JavaDoc o, Method JavaDoc method, Object JavaDoc[] objects) throws Throwable JavaDoc {
77     if ("finalize".hashCode() == method.getName().hashCode()
78         && "finalize".equals(method.getName())) {
79       return null;
80     } else {
81       loadObject();
82       if (resultObject != null) {
83         try {
84           return method.invoke(resultObject, objects);
85         } catch (Throwable JavaDoc t) {
86           throw ClassInfo.unwrapThrowable(t);
87         }
88       } else {
89         return null;
90       }
91     }
92   }
93
94   private synchronized void loadObject() {
95     if (!loaded) {
96       try {
97         loaded = true;
98         resultObject = ResultLoader.getResult(client, statementName, parameterObject, targetType);
99       } catch (SQLException JavaDoc e) {
100         throw new NestedRuntimeException("Error lazy loading result. Cause: " + e, e);
101       }
102     }
103   }
104
105 }
106
Popular Tags