KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > dao > engine > transaction > jdbc > JdbcDaoTransactionManager


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.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 JavaDoc;
25 import javax.naming.NamingException JavaDoc;
26 import javax.sql.DataSource JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 /**
33  * DaoTransactionManager implementation for JDBC
34  */

35 public class JdbcDaoTransactionManager implements DaoTransactionManager {
36
37   private DataSource dataSource;
38
39   public void configure(Properties JavaDoc properties) {
40     if (properties.containsKey("DataSource")) {
41       String JavaDoc type = (String JavaDoc) 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 JavaDoc properties) {
61     dataSource = new SimpleDataSource(properties);
62   }
63
64   private void configureDbcp(Map JavaDoc properties) {
65     DbcpConfiguration dbcp = new DbcpConfiguration(properties);
66     dataSource = dbcp.getDataSource();
67   }
68
69   private void configureJndi(Map JavaDoc properties) {
70     try {
71       Hashtable JavaDoc contextProps = getContextProperties(properties);
72       InitialContext JavaDoc initCtx = null;
73       if (contextProps == null) {
74         initCtx = new InitialContext JavaDoc();
75       } else {
76         initCtx = new InitialContext JavaDoc(contextProps);
77       }
78       dataSource = (DataSource) initCtx.lookup((String JavaDoc) properties.get("DBJndiContext"));
79     } catch (NamingException JavaDoc 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 JavaDoc getContextProperties(Map JavaDoc allProps) {
93     final String JavaDoc PREFIX = "context.";
94     Hashtable JavaDoc contextProperties = null;
95     Iterator JavaDoc keys = allProps.keySet().iterator();
96     while (keys.hasNext()) {
97       String JavaDoc key = (String JavaDoc) keys.next();
98       String JavaDoc value = (String JavaDoc) allProps.get(key);
99       if (key.startsWith(PREFIX)) {
100         if (contextProperties == null) {
101           contextProperties = new Properties JavaDoc();
102         }
103         contextProperties.put(key.substring(PREFIX.length()), value);
104       }
105     }
106     return contextProperties;
107   }
108 }
109
Popular Tags