1 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 ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 27 30 public class DbcpConfiguration { 31 32 private static final Probe PROBE = ProbeFactory.getProbe(); 33 private static final String 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 42 public DbcpConfiguration(Map properties) { 43 try { 44 45 dataSource = legacyDbcpConfiguration(properties); 46 if (dataSource == null) { 47 dataSource = newDbcpConfiguration(properties); 48 } 49 50 } catch (Exception e) { 51 throw new NestedRuntimeException("Error initializing DbcpDataSourceFactory. Cause: " + e, e); 52 } 53 } 54 55 60 public DataSource getDataSource() { 61 return dataSource; 62 } 63 64 private BasicDataSource newDbcpConfiguration(Map map) { 65 BasicDataSource basicDataSource = new BasicDataSource(); 66 Iterator props = map.keySet().iterator(); 67 while (props.hasNext()) { 68 String propertyName = (String ) props.next(); 69 if (propertyName.startsWith(ADD_DRIVER_PROPS_PREFIX)) { 70 String value = (String ) map.get(propertyName); 71 basicDataSource.addConnectionProperty(propertyName.substring(ADD_DRIVER_PROPS_PREFIX_LENGTH), value); 72 } else if (PROBE.hasWritableProperty(basicDataSource, propertyName)) { 73 String value = (String ) map.get(propertyName); 74 Object convertedValue = convertValue(basicDataSource, propertyName, value); 75 PROBE.setObject(basicDataSource, propertyName, convertedValue); 76 } 77 } 78 return basicDataSource; 79 } 80 81 private Object convertValue(Object object, String propertyName, String value) { 82 Object convertedValue = value; 83 Class targetType = PROBE.getPropertyTypeForSetter(object, propertyName); 84 if (targetType == Integer .class || targetType == int.class) { 85 convertedValue = Integer.valueOf(value); 86 } else if (targetType == Long .class || targetType == long.class) { 87 convertedValue = Long.valueOf(value); 88 } else if (targetType == Boolean .class || targetType == boolean.class) { 89 convertedValue = Boolean.valueOf(value); 90 } 91 return convertedValue; 92 } 93 94 private BasicDataSource legacyDbcpConfiguration(Map map) { 95 BasicDataSource basicDataSource = null; 96 if (map.containsKey("JDBC.Driver")) { 97 basicDataSource = new BasicDataSource(); 98 String driver = (String ) map.get("JDBC.Driver"); 99 String url = (String ) map.get("JDBC.ConnectionURL"); 100 String username = (String ) map.get("JDBC.Username"); 101 String password = (String ) map.get("JDBC.Password"); 102 String validationQuery = (String ) map.get("Pool.ValidationQuery"); 103 String maxActive = (String ) map.get("Pool.MaximumActiveConnections"); 104 String maxIdle = (String ) map.get("Pool.MaximumIdleConnections"); 105 String maxWait = (String ) 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 s) { 133 return s != null && s.length() > 0; 134 } 135 136 137 } 138 | Popular Tags |