1 16 17 package org.springframework.jdbc.datasource.lookup; 18 19 import java.sql.Connection ; 20 import java.sql.SQLException ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import javax.sql.DataSource ; 26 27 import org.springframework.beans.factory.InitializingBean; 28 import org.springframework.jdbc.datasource.AbstractDataSource; 29 import org.springframework.util.Assert; 30 31 42 public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean { 43 44 private Map targetDataSources; 45 46 private Object defaultTargetDataSource; 47 48 private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup(); 49 50 private Map resolvedDataSources; 51 52 private DataSource resolvedDefaultDataSource; 53 54 55 65 public void setTargetDataSources(Map targetDataSources) { 66 this.targetDataSources = targetDataSources; 67 } 68 69 78 public void setDefaultTargetDataSource(Object defaultTargetDataSource) { 79 this.defaultTargetDataSource = defaultTargetDataSource; 80 } 81 82 88 public void setDataSourceLookup(DataSourceLookup dataSourceLookup) { 89 this.dataSourceLookup = (dataSourceLookup != null ? dataSourceLookup : new JndiDataSourceLookup()); 90 } 91 92 93 public void afterPropertiesSet() { 94 if (this.targetDataSources == null) { 95 throw new IllegalArgumentException ("targetDataSources is required"); 96 } 97 this.resolvedDataSources = new HashMap (this.targetDataSources.size()); 98 for (Iterator it = this.targetDataSources.entrySet().iterator(); it.hasNext();) { 99 Map.Entry entry = (Map.Entry ) it.next(); 100 Object lookupKey = resolveSpecifiedLookupKey(entry.getKey()); 101 DataSource dataSource = resolveSpecifiedDataSource(entry.getValue()); 102 this.resolvedDataSources.put(lookupKey, dataSource); 103 } 104 if (this.defaultTargetDataSource != null) { 105 this.resolvedDefaultDataSource = resolveSpecifiedDataSource(this.defaultTargetDataSource); 106 } 107 } 108 109 118 protected DataSource resolveSpecifiedDataSource(Object dataSource) throws IllegalArgumentException { 119 if (dataSource instanceof DataSource ) { 120 return (DataSource ) dataSource; 121 } 122 else if (dataSource instanceof String ) { 123 return this.dataSourceLookup.getDataSource((String ) dataSource); 124 } 125 else { 126 throw new IllegalArgumentException ( 127 "Illegal data source value - only [javax.sql.DataSource] and String supported: " + dataSource); 128 } 129 } 130 131 132 public Connection getConnection() throws SQLException { 133 return determineTargetDataSource().getConnection(); 134 } 135 136 public Connection getConnection(String username, String password) throws SQLException { 137 return determineTargetDataSource().getConnection(username, password); 138 } 139 140 148 protected DataSource determineTargetDataSource() { 149 Assert.notNull(this.resolvedDataSources, "DataSource router not initialized"); 150 Object lookupKey = determineCurrentLookupKey(); 151 DataSource dataSource = (DataSource ) this.resolvedDataSources.get(lookupKey); 152 if (dataSource == null) { 153 dataSource = this.resolvedDefaultDataSource; 154 } 155 if (dataSource == null) { 156 throw new IllegalStateException ("Cannot determine target DataSource for lookup key [" + lookupKey + "]"); 157 } 158 return dataSource; 159 } 160 161 162 171 protected Object resolveSpecifiedLookupKey(Object lookupKey) { 172 return lookupKey; 173 } 174 175 182 protected abstract Object determineCurrentLookupKey(); 183 184 } 185 | Popular Tags |