KickJava   Java API By Example, From Geeks To Geeks.

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


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.dao.client.Dao;
19 import com.ibatis.dao.client.DaoException;
20 import com.ibatis.dao.client.DaoManager;
21 import com.ibatis.dao.client.DaoTransaction;
22
23 import java.util.*;
24
25 public class StandardDaoManager implements DaoManager {
26
27   private static final String JavaDoc DAO_EXPLICIT_TX = "__DAO_EXPLICIT_TX";
28
29   private ThreadLocal JavaDoc transactionMode = new ThreadLocal JavaDoc();
30   private ThreadLocal JavaDoc contextInTransactionList = new ThreadLocal JavaDoc();
31
32   private Map idContextMap = new HashMap();
33   private Map typeContextMap = new HashMap();
34   private Map daoImplMap = new HashMap();
35
36   public void addContext(DaoContext context) {
37     // Add context ID mapping
38
if (context.getId() != null && context.getId().length() > 0) {
39       if (idContextMap.containsKey(context.getId())) {
40         throw new DaoException("There is already a DAO Context with the ID '" + context.getId() + "'.");
41       }
42       idContextMap.put(context.getId(), context);
43     }
44     // Add type mappings
45
Iterator i = context.getDaoImpls();
46     while (i.hasNext()) {
47       DaoImpl daoImpl = (DaoImpl) i.next();
48
49       // Don't associate a default DAO when multiple DAO impls are registered.
50
if (typeContextMap.containsKey(daoImpl.getDaoInterface())) {
51         typeContextMap.put(daoImpl.getDaoInterface(), null);
52       } else {
53         typeContextMap.put(daoImpl.getDaoInterface(), context);
54       }
55
56       daoImplMap.put(daoImpl.getProxy(), daoImpl);
57       daoImplMap.put(daoImpl.getDaoInstance(), daoImpl);
58     }
59   }
60
61   public Dao getDao(Class JavaDoc iface) {
62     DaoContext context = (DaoContext) typeContextMap.get(iface);
63     if (context == null) {
64       throw new DaoException("There is no DAO implementation found for " + iface + " in any context. If you've " +
65           "registered multiple implementations of this DAO, you must specify the Context ID for the DAO implementation" +
66           "you're looking for using the getDao(Class iface, String contextId) method.");
67     }
68     return context.getDao(iface);
69   }
70
71   public Dao getDao(Class JavaDoc iface, String JavaDoc contextId) {
72     DaoContext context = (DaoContext) idContextMap.get(contextId);
73     if (context == null) {
74       throw new DaoException("There is no Context found with the ID " + contextId + ".");
75     }
76     return context.getDao(iface);
77   }
78
79   public void startTransaction() {
80     transactionMode.set(DAO_EXPLICIT_TX);
81   }
82
83   public void commitTransaction() {
84     List ctxList = getContextInTransactionList();
85     Iterator i = ctxList.iterator();
86     while (i.hasNext()) {
87       DaoContext context = (DaoContext) i.next();
88       context.commitTransaction();
89     }
90   }
91
92   public void endTransaction() {
93     List ctxList = getContextInTransactionList();
94     try {
95       Iterator i = ctxList.iterator();
96       while (i.hasNext()) {
97         DaoContext context = (DaoContext) i.next();
98         context.endTransaction();
99       }
100     } finally {
101       transactionMode.set(null);
102       ctxList.clear();
103     }
104   }
105
106   public DaoTransaction getTransaction(Dao dao) {
107     DaoImpl impl = (DaoImpl) daoImplMap.get(dao);
108     return impl.getDaoContext().getTransaction();
109   }
110
111   public boolean isExplicitTransaction() {
112     return DAO_EXPLICIT_TX.equals(transactionMode.get());
113   }
114
115   public void addContextInTransaction(DaoContext ctx) {
116     List ctxList = getContextInTransactionList();
117     if (!ctxList.contains(ctx)) {
118       ctxList.add(ctx);
119     }
120   }
121
122   private List getContextInTransactionList() {
123     List list = (List) contextInTransactionList.get();
124     if (list == null) {
125       list = new ArrayList();
126       contextInTransactionList.set(list);
127     }
128     return list;
129   }
130
131 }
132
133
134
Popular Tags