KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > common > jdbc > DbcpConfiguration


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.common.jdbc;
17
18 import com.ibatis.common.beans.Probe;
19 import com.ibatis.common.beans.ProbeFactory;
20 import com.ibatis.common.exception.NestedRuntimeException;
21 import org.apache.commons.dbcp.BasicDataSource;
22
23 import javax.sql.DataSource JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26
27 /**
28  * Wrapper class to simplify use of DBCP
29  */

30 public class DbcpConfiguration {
31
32   private static final Probe PROBE = ProbeFactory.getProbe();
33   private static final String JavaDoc ADD_DRIVER_PROPS_PREFIX = "Driver.";
34   private static final int ADD_DRIVER_PROPS_PREFIX_LENGTH = ADD_DRIVER_PROPS_PREFIX.length();
35   private DataSource dataSource;
36
37   /**
38    * Constructor to supply a map of properties
39    *
40    * @param properties - the map of configuration properties
41    */

42   public DbcpConfiguration(Map JavaDoc properties) {
43     try {
44
45       dataSource = legacyDbcpConfiguration(properties);
46       if (dataSource == null) {
47         dataSource = newDbcpConfiguration(properties);
48       }
49
50     } catch (Exception JavaDoc e) {
51       throw new NestedRuntimeException("Error initializing DbcpDataSourceFactory. Cause: " + e, e);
52     }
53   }
54
55   /**
56    * Getter for DataSource
57    *
58    * @return The DataSource
59    */

60   public DataSource getDataSource() {
61     return dataSource;
62   }
63
64   private BasicDataSource newDbcpConfiguration(Map JavaDoc map) {
65     BasicDataSource basicDataSource = new BasicDataSource();
66     Iterator JavaDoc props = map.keySet().iterator();
67     while (props.hasNext()) {
68       String JavaDoc propertyName = (String JavaDoc) props.next();
69       if (propertyName.startsWith(ADD_DRIVER_PROPS_PREFIX)) {
70         String JavaDoc value = (String JavaDoc) map.get(propertyName);
71         basicDataSource.addConnectionProperty(propertyName.substring(ADD_DRIVER_PROPS_PREFIX_LENGTH), value);
72       } else if (PROBE.hasWritableProperty(basicDataSource, propertyName)) {
73         String JavaDoc value = (String JavaDoc) map.get(propertyName);
74         Object JavaDoc convertedValue = convertValue(basicDataSource, propertyName, value);
75         PROBE.setObject(basicDataSource, propertyName, convertedValue);
76       }
77     }
78     return basicDataSource;
79   }
80
81   private Object JavaDoc convertValue(Object JavaDoc object, String JavaDoc propertyName, String JavaDoc value) {
82     Object JavaDoc convertedValue = value;
83     Class JavaDoc targetType = PROBE.getPropertyTypeForSetter(object, propertyName);
84     if (targetType == Integer JavaDoc.class || targetType == int.class) {
85       convertedValue = Integer.valueOf(value);
86     } else if (targetType == Long JavaDoc.class || targetType == long.class) {
87       convertedValue = Long.valueOf(value);
88     } else if (targetType == Boolean JavaDoc.class || targetType == boolean.class) {
89       convertedValue = Boolean.valueOf(value);
90     }
91     return convertedValue;
92   }
93
94   private BasicDataSource legacyDbcpConfiguration(Map JavaDoc map) {
95     BasicDataSource basicDataSource = null;
96     if (map.containsKey("JDBC.Driver")) {
97       basicDataSource = new BasicDataSource();
98       String JavaDoc driver = (String JavaDoc) map.get("JDBC.Driver");
99       String JavaDoc url = (String JavaDoc) map.get("JDBC.ConnectionURL");
100       String JavaDoc username = (String JavaDoc) map.get("JDBC.Username");
101       String JavaDoc password = (String JavaDoc) map.get("JDBC.Password");
102       String JavaDoc validationQuery = (String JavaDoc) map.get("Pool.ValidationQuery");
103       String JavaDoc maxActive = (String JavaDoc) map.get("Pool.MaximumActiveConnections");
104       String JavaDoc maxIdle = (String JavaDoc) map.get("Pool.MaximumIdleConnections");
105       String JavaDoc maxWait = (String JavaDoc) map.get("Pool.MaximumWait");
106
107       basicDataSource.setUrl(url);
108       basicDataSource.setDriverClassName(driver);
109       basicDataSource.setUsername(username);
110       basicDataSource.setPassword(password);
111
112       if (notEmpty(validationQuery)) {
113         basicDataSource.setValidationQuery(validationQuery);
114       }
115
116       if (notEmpty(maxActive)) {
117         basicDataSource.setMaxActive(Integer.parseInt(maxActive));
118       }
119
120       if (notEmpty(maxIdle)) {
121         basicDataSource.setMaxIdle(Integer.parseInt(maxIdle));
122       }
123
124       if (notEmpty(maxWait)) {
125         basicDataSource.setMaxWait(Integer.parseInt(maxWait));
126       }
127
128     }
129     return basicDataSource;
130   }
131
132   private boolean notEmpty(String JavaDoc s) {
133     return s != null && s.length() > 0;
134   }
135
136
137 }
138
Popular Tags