KickJava   Java API By Example, From Geeks To Geeks.

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


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.DaoTransaction;
21 import com.ibatis.dao.engine.transaction.DaoTransactionManager;
22
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26
27 public class DaoContext {
28
29   private String JavaDoc id;
30   private StandardDaoManager daoManager;
31   private DaoTransactionManager transactionManager;
32   private ThreadLocal JavaDoc transaction = new ThreadLocal JavaDoc();
33   private ThreadLocal JavaDoc state = new ThreadLocal JavaDoc();
34
35   private Map JavaDoc typeDaoImplMap = new HashMap JavaDoc();
36
37   public DaoContext() {
38   }
39
40   public String JavaDoc getId() {
41     return id;
42   }
43
44   public void setId(String JavaDoc id) {
45     this.id = id;
46   }
47
48   public StandardDaoManager getDaoManager() {
49     return daoManager;
50   }
51
52   public void setDaoManager(StandardDaoManager daoManager) {
53     this.daoManager = daoManager;
54   }
55
56   public DaoTransactionManager getTransactionManager() {
57     return transactionManager;
58   }
59
60   public void setTransactionManager(DaoTransactionManager transactionManager) {
61     this.transactionManager = transactionManager;
62   }
63
64   public void addDao(DaoImpl daoImpl) {
65     if (typeDaoImplMap.containsKey(daoImpl.getDaoInterface())) {
66       throw new DaoException("More than one implementation for '" + daoImpl.getDaoInterface() + "' was configured. " +
67           "Only one implementation per context is allowed.");
68     }
69     typeDaoImplMap.put(daoImpl.getDaoInterface(), daoImpl);
70   }
71
72   public Dao getDao(Class JavaDoc iface) {
73     DaoImpl impl = (DaoImpl) typeDaoImplMap.get(iface);
74     if (impl == null) {
75       throw new DaoException("There is no DAO implementation found for " + iface + " in this context.");
76     }
77     return impl.getProxy();
78   }
79
80   public Iterator JavaDoc getDaoImpls() {
81     return typeDaoImplMap.values().iterator();
82   }
83
84   public DaoTransaction getTransaction() {
85     startTransaction();
86     return (DaoTransaction) transaction.get();
87   }
88
89   public void startTransaction() {
90     if (state.get() != DaoTransactionState.ACTIVE) {
91       DaoTransaction trans = transactionManager.startTransaction();
92       transaction.set(trans);
93       state.set(DaoTransactionState.ACTIVE);
94       daoManager.addContextInTransaction(this);
95     }
96   }
97
98   public void commitTransaction() {
99     DaoTransaction trans = (DaoTransaction) transaction.get();
100     if (state.get() == DaoTransactionState.ACTIVE) {
101       transactionManager.commitTransaction(trans);
102       state.set(DaoTransactionState.COMMITTED);
103     } else {
104       state.set(DaoTransactionState.INACTIVE);
105     }
106   }
107
108   public void endTransaction() {
109     DaoTransaction trans = (DaoTransaction) transaction.get();
110     if (state.get() == DaoTransactionState.ACTIVE) {
111       try {
112         transactionManager.rollbackTransaction(trans);
113       } finally {
114         state.set(DaoTransactionState.ROLLEDBACK);
115         transaction.set(null);
116       }
117     } else {
118       state.set(DaoTransactionState.INACTIVE);
119       transaction.set(null);
120     }
121   }
122
123 }
124
Popular Tags