1 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 DAO_EXPLICIT_TX = "__DAO_EXPLICIT_TX"; 28 29 private ThreadLocal transactionMode = new ThreadLocal (); 30 private ThreadLocal contextInTransactionList = new ThreadLocal (); 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 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 Iterator i = context.getDaoImpls(); 46 while (i.hasNext()) { 47 DaoImpl daoImpl = (DaoImpl) i.next(); 48 49 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 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 iface, String 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 |