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