001/* 002 * Copyright 2002-2012 the original author or authors. 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 * https://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 017package org.springframework.jdbc.datasource.lookup; 018 019import java.util.Collections; 020import java.util.HashMap; 021import java.util.Map; 022import javax.sql.DataSource; 023 024import org.springframework.util.Assert; 025 026/** 027 * Simple {@link DataSourceLookup} implementation that relies on a map for doing lookups. 028 * 029 * <p>Useful for testing environments or applications that need to match arbitrary 030 * {@link String} names to target {@link DataSource} objects. 031 * 032 * @author Costin Leau 033 * @author Juergen Hoeller 034 * @author Rick Evans 035 * @since 2.0 036 */ 037public class MapDataSourceLookup implements DataSourceLookup { 038 039 private final Map<String, DataSource> dataSources = new HashMap<String, DataSource>(4); 040 041 042 /** 043 * Create a new instance of the {@link MapDataSourceLookup} class. 044 */ 045 public MapDataSourceLookup() { 046 } 047 048 /** 049 * Create a new instance of the {@link MapDataSourceLookup} class. 050 * @param dataSources the {@link Map} of {@link DataSource DataSources}; the keys 051 * are {@link String Strings}, the values are actual {@link DataSource} instances. 052 */ 053 public MapDataSourceLookup(Map<String, DataSource> dataSources) { 054 setDataSources(dataSources); 055 } 056 057 /** 058 * Create a new instance of the {@link MapDataSourceLookup} class. 059 * @param dataSourceName the name under which the supplied {@link DataSource} is to be added 060 * @param dataSource the {@link DataSource} to be added 061 */ 062 public MapDataSourceLookup(String dataSourceName, DataSource dataSource) { 063 addDataSource(dataSourceName, dataSource); 064 } 065 066 067 /** 068 * Set the {@link Map} of {@link DataSource DataSources}; the keys 069 * are {@link String Strings}, the values are actual {@link DataSource} instances. 070 * <p>If the supplied {@link Map} is {@code null}, then this method 071 * call effectively has no effect. 072 * @param dataSources said {@link Map} of {@link DataSource DataSources} 073 */ 074 public void setDataSources(Map<String, DataSource> dataSources) { 075 if (dataSources != null) { 076 this.dataSources.putAll(dataSources); 077 } 078 } 079 080 /** 081 * Get the {@link Map} of {@link DataSource DataSources} maintained by this object. 082 * <p>The returned {@link Map} is {@link Collections#unmodifiableMap(java.util.Map) unmodifiable}. 083 * @return said {@link Map} of {@link DataSource DataSources} (never {@code null}) 084 */ 085 public Map<String, DataSource> getDataSources() { 086 return Collections.unmodifiableMap(this.dataSources); 087 } 088 089 /** 090 * Add the supplied {@link DataSource} to the map of {@link DataSource DataSources} 091 * maintained by this object. 092 * @param dataSourceName the name under which the supplied {@link DataSource} is to be added 093 * @param dataSource the {@link DataSource} to be so added 094 */ 095 public void addDataSource(String dataSourceName, DataSource dataSource) { 096 Assert.notNull(dataSourceName, "DataSource name must not be null"); 097 Assert.notNull(dataSource, "DataSource must not be null"); 098 this.dataSources.put(dataSourceName, dataSource); 099 } 100 101 @Override 102 public DataSource getDataSource(String dataSourceName) throws DataSourceLookupFailureException { 103 Assert.notNull(dataSourceName, "DataSource name must not be null"); 104 DataSource dataSource = this.dataSources.get(dataSourceName); 105 if (dataSource == null) { 106 throw new DataSourceLookupFailureException( 107 "No DataSource with name '" + dataSourceName + "' registered"); 108 } 109 return dataSource; 110 } 111 112}