001: /*
002: * Copyright 2004 Clinton Begin
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.ibatis.common.jdbc;
017:
018: import com.ibatis.common.beans.Probe;
019: import com.ibatis.common.beans.ProbeFactory;
020:
021: import org.apache.commons.dbcp.BasicDataSource;
022:
023: import javax.sql.DataSource;
024: import java.util.Iterator;
025: import java.util.Map;
026:
027: /**
028: * Wrapper class to simplify use of DBCP
029: */
030: public class DbcpConfiguration {
031:
032: private static final Probe PROBE = ProbeFactory.getProbe();
033: private static final String ADD_DRIVER_PROPS_PREFIX = "Driver.";
034: private static final int ADD_DRIVER_PROPS_PREFIX_LENGTH = ADD_DRIVER_PROPS_PREFIX
035: .length();
036: private DataSource dataSource;
037:
038: /**
039: * Constructor to supply a map of properties
040: *
041: * @param properties - the map of configuration properties
042: */
043: public DbcpConfiguration(Map properties) {
044: try {
045:
046: dataSource = legacyDbcpConfiguration(properties);
047: if (dataSource == null) {
048: dataSource = newDbcpConfiguration(properties);
049: }
050:
051: } catch (Exception e) {
052: throw new RuntimeException(
053: "Error initializing DbcpDataSourceFactory. Cause: "
054: + e, e);
055: }
056: }
057:
058: /**
059: * Getter for DataSource
060: *
061: * @return The DataSource
062: */
063: public DataSource getDataSource() {
064: return dataSource;
065: }
066:
067: private BasicDataSource newDbcpConfiguration(Map map) {
068: BasicDataSource basicDataSource = new BasicDataSource();
069: Iterator props = map.keySet().iterator();
070: while (props.hasNext()) {
071: String propertyName = (String) props.next();
072: if (propertyName.startsWith(ADD_DRIVER_PROPS_PREFIX)) {
073: String value = (String) map.get(propertyName);
074: basicDataSource.addConnectionProperty(propertyName
075: .substring(ADD_DRIVER_PROPS_PREFIX_LENGTH),
076: value);
077: } else if (PROBE.hasWritableProperty(basicDataSource,
078: propertyName)) {
079: String value = (String) map.get(propertyName);
080: Object convertedValue = convertValue(basicDataSource,
081: propertyName, value);
082: PROBE.setObject(basicDataSource, propertyName,
083: convertedValue);
084: }
085: }
086: return basicDataSource;
087: }
088:
089: private Object convertValue(Object object, String propertyName,
090: String value) {
091: Object convertedValue = value;
092: Class targetType = PROBE.getPropertyTypeForSetter(object,
093: propertyName);
094: if (targetType == Integer.class || targetType == int.class) {
095: convertedValue = Integer.valueOf(value);
096: } else if (targetType == Long.class || targetType == long.class) {
097: convertedValue = Long.valueOf(value);
098: } else if (targetType == Boolean.class
099: || targetType == boolean.class) {
100: convertedValue = Boolean.valueOf(value);
101: }
102: return convertedValue;
103: }
104:
105: private BasicDataSource legacyDbcpConfiguration(Map map) {
106: BasicDataSource basicDataSource = null;
107: if (map.containsKey("JDBC.Driver")) {
108: basicDataSource = new BasicDataSource();
109: String driver = (String) map.get("JDBC.Driver");
110: String url = (String) map.get("JDBC.ConnectionURL");
111: String username = (String) map.get("JDBC.Username");
112: String password = (String) map.get("JDBC.Password");
113: String validationQuery = (String) map
114: .get("Pool.ValidationQuery");
115: String maxActive = (String) map
116: .get("Pool.MaximumActiveConnections");
117: String maxIdle = (String) map
118: .get("Pool.MaximumIdleConnections");
119: String maxWait = (String) map.get("Pool.MaximumWait");
120:
121: basicDataSource.setUrl(url);
122: basicDataSource.setDriverClassName(driver);
123: basicDataSource.setUsername(username);
124: basicDataSource.setPassword(password);
125:
126: if (notEmpty(validationQuery)) {
127: basicDataSource.setValidationQuery(validationQuery);
128: }
129:
130: if (notEmpty(maxActive)) {
131: basicDataSource.setMaxActive(Integer
132: .parseInt(maxActive));
133: }
134:
135: if (notEmpty(maxIdle)) {
136: basicDataSource.setMaxIdle(Integer.parseInt(maxIdle));
137: }
138:
139: if (notEmpty(maxWait)) {
140: basicDataSource.setMaxWait(Integer.parseInt(maxWait));
141: }
142:
143: Iterator props = map.keySet().iterator();
144: while (props.hasNext()) {
145: String propertyName = (String) props.next();
146: if (propertyName.startsWith(ADD_DRIVER_PROPS_PREFIX)) {
147: String value = (String) map.get(propertyName);
148: basicDataSource.addConnectionProperty(propertyName
149: .substring(ADD_DRIVER_PROPS_PREFIX_LENGTH),
150: value);
151: }
152: }
153: }
154: return basicDataSource;
155: }
156:
157: private boolean notEmpty(String s) {
158: return s != null && s.length() > 0;
159: }
160:
161: }
|