1 16 package com.ibatis.dao.engine.transaction.jdbc; 17 18 import com.ibatis.common.jdbc.DbcpConfiguration; 19 import com.ibatis.common.jdbc.SimpleDataSource; 20 import com.ibatis.dao.client.DaoException; 21 import com.ibatis.dao.client.DaoTransaction; 22 import com.ibatis.dao.engine.transaction.DaoTransactionManager; 23 24 import javax.naming.InitialContext ; 25 import javax.naming.NamingException ; 26 import javax.sql.DataSource ; 27 import java.util.Hashtable ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.Properties ; 31 32 35 public class JdbcDaoTransactionManager implements DaoTransactionManager { 36 37 private DataSource dataSource; 38 39 public void configure(Properties properties) { 40 if (properties.containsKey("DataSource")) { 41 String type = (String ) properties.get("DataSource"); 42 if ("SIMPLE".equals(type)) { 43 configureSimpleDataSource(properties); 44 } else if ("DBCP".equals(type)) { 45 configureDbcp(properties); 46 } else if ("JNDI".equals(type)) { 47 configureJndi(properties); 48 } else { 49 throw new DaoException("DAO Transaction Manager properties must include a value for 'DataSource' of SIMPLE, DBCP or JNDI."); 50 } 51 } else { 52 throw new DaoException("DAO Transaction Manager properties must include a value for 'DataSource' of SIMPLE, DBCP or JNDI."); 53 } 54 } 55 56 public DaoTransaction startTransaction() { 57 return new JdbcDaoTransaction(dataSource); 58 } 59 60 private void configureSimpleDataSource(Map properties) { 61 dataSource = new SimpleDataSource(properties); 62 } 63 64 private void configureDbcp(Map properties) { 65 DbcpConfiguration dbcp = new DbcpConfiguration(properties); 66 dataSource = dbcp.getDataSource(); 67 } 68 69 private void configureJndi(Map properties) { 70 try { 71 Hashtable contextProps = getContextProperties(properties); 72 InitialContext initCtx = null; 73 if (contextProps == null) { 74 initCtx = new InitialContext (); 75 } else { 76 initCtx = new InitialContext (contextProps); 77 } 78 dataSource = (DataSource) initCtx.lookup((String ) properties.get("DBJndiContext")); 79 } catch (NamingException e) { 80 throw new DaoException("There was an error configuring the DataSource from JNDI. Cause: " + e, e); 81 } 82 } 83 84 public void commitTransaction(DaoTransaction trans) { 85 ((JdbcDaoTransaction) trans).commit(); 86 } 87 88 public void rollbackTransaction(DaoTransaction trans) { 89 ((JdbcDaoTransaction) trans).rollback(); 90 } 91 92 private static Hashtable getContextProperties(Map allProps) { 93 final String PREFIX = "context."; 94 Hashtable contextProperties = null; 95 Iterator keys = allProps.keySet().iterator(); 96 while (keys.hasNext()) { 97 String key = (String ) keys.next(); 98 String value = (String ) allProps.get(key); 99 if (key.startsWith(PREFIX)) { 100 if (contextProperties == null) { 101 contextProperties = new Properties (); 102 } 103 contextProperties.put(key.substring(PREFIX.length()), value); 104 } 105 } 106 return contextProperties; 107 } 108 } 109 | Popular Tags |