KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > dao > engine > impl > DaoProxy


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.dao.engine.impl;
17
18 import com.ibatis.common.beans.ClassInfo;
19 import com.ibatis.dao.client.Dao;
20
21 import java.lang.reflect.InvocationHandler JavaDoc;
22 import java.lang.reflect.Method JavaDoc;
23 import java.lang.reflect.Proxy JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Set JavaDoc;
26
27 public class DaoProxy implements InvocationHandler JavaDoc {
28
29   private static final Set JavaDoc PASSTHROUGH_METHODS = new HashSet JavaDoc();
30
31   private DaoImpl daoImpl;
32
33   static {
34     PASSTHROUGH_METHODS.add("equals");
35     PASSTHROUGH_METHODS.add("getClass");
36     PASSTHROUGH_METHODS.add("hashCode");
37     PASSTHROUGH_METHODS.add("notify");
38     PASSTHROUGH_METHODS.add("notifyAll");
39     PASSTHROUGH_METHODS.add("toString");
40     PASSTHROUGH_METHODS.add("wait");
41   }
42
43   public DaoProxy(DaoImpl daoImpl) {
44     this.daoImpl = daoImpl;
45   }
46
47   public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
48       throws Throwable JavaDoc {
49     Object JavaDoc result = null;
50     if (PASSTHROUGH_METHODS.contains(method.getName())) {
51       try {
52         result = method.invoke(daoImpl.getDaoInstance(), args);
53       } catch (Throwable JavaDoc t) {
54         throw ClassInfo.unwrapThrowable(t);
55       }
56     } else {
57       StandardDaoManager daoManager = daoImpl.getDaoManager();
58       DaoContext context = daoImpl.getDaoContext();
59
60       if (daoManager.isExplicitTransaction()) {
61         // Just start the transaction (explicit)
62
try {
63           context.startTransaction();
64           result = method.invoke(daoImpl.getDaoInstance(), args);
65         } catch (Throwable JavaDoc t) {
66           throw ClassInfo.unwrapThrowable(t);
67         }
68       } else {
69         // Start, commit and end the transaction (autocommit)
70
try {
71           context.startTransaction();
72           result = method.invoke(daoImpl.getDaoInstance(), args);
73           context.commitTransaction();
74         } catch (Throwable JavaDoc t) {
75           throw ClassInfo.unwrapThrowable(t);
76         } finally {
77           context.endTransaction();
78         }
79       }
80
81     }
82     return result;
83   }
84
85   public static Dao newInstance(DaoImpl daoImpl) {
86     return (Dao) Proxy.newProxyInstance(daoImpl.getDaoInterface().getClassLoader(),
87         new Class JavaDoc[]{Dao.class, daoImpl.getDaoInterface()},
88         new DaoProxy(daoImpl));
89   }
90
91 }
92
Popular Tags