1 16 17 package org.springframework.jdbc.datasource.lookup; 18 19 import java.util.Collections ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import javax.sql.DataSource ; 24 25 import org.springframework.util.Assert; 26 27 38 public class MapDataSourceLookup implements DataSourceLookup { 39 40 private final Map dataSources = new HashMap (4); 41 42 43 46 public MapDataSourceLookup() { 47 } 48 49 54 public MapDataSourceLookup(Map dataSources) { 55 setDataSources(dataSources); 56 } 57 58 63 public MapDataSourceLookup(String dataSourceName, DataSource dataSource) { 64 addDataSource(dataSourceName, dataSource); 65 } 66 67 68 75 public void setDataSources(Map dataSources) { 76 if (dataSources != null) { 77 this.dataSources.putAll(dataSources); 78 } 79 } 80 81 86 public Map getDataSources() { 87 return Collections.unmodifiableMap(this.dataSources); 88 } 89 90 96 public void addDataSource(String dataSourceName, DataSource dataSource) { 97 Assert.notNull(dataSourceName, "DataSource name must not be null"); 98 Assert.notNull(dataSource, "DataSource must not be null"); 99 this.dataSources.put(dataSourceName, dataSource); 100 } 101 102 public DataSource getDataSource(String dataSourceName) throws DataSourceLookupFailureException { 103 Assert.notNull(dataSourceName, "DataSource name must not be null"); 104 Object value = this.dataSources.get(dataSourceName); 105 if (value == null) { 106 throw new DataSourceLookupFailureException( 107 "No DataSource with name '" + dataSourceName + "' registered"); 108 } 109 if (!(value instanceof DataSource )) { 110 throw new DataSourceLookupFailureException( 111 "The object [" + value + "] with name '" + dataSourceName + 112 "' in the DataSource map is not a [javax.sql.DataSource]"); 113 } 114 return (DataSource ) value; 115 } 116 117 } 118 | Popular Tags |