KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > datasource > lookup > MapDataSourceLookup


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jdbc.datasource.lookup;
18
19 import java.util.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.sql.DataSource JavaDoc;
24
25 import org.springframework.util.Assert;
26
27 /**
28  * Simple {@link DataSourceLookup} implementation that relies on a map for doing lookups.
29  *
30  * <p>Useful for testing environments or applications that need to match arbitrary
31  * {@link String} names to target {@link DataSource} objects.
32  *
33  * @author Costin Leau
34  * @author Juergen Hoeller
35  * @author Rick Evans
36  * @since 2.0
37  */

38 public class MapDataSourceLookup implements DataSourceLookup {
39
40     private final Map JavaDoc dataSources = new HashMap JavaDoc(4);
41
42
43     /**
44      * Create a new instance of the {@link MapDataSourceLookup} class.
45      */

46     public MapDataSourceLookup() {
47     }
48
49     /**
50      * Create a new instance of the {@link MapDataSourceLookup} class.
51      * @param dataSources the {@link Map} of {@link DataSource DataSources}; the keys
52      * are {@link String Strings}, the values are actual {@link DataSource} instances.
53      */

54     public MapDataSourceLookup(Map JavaDoc dataSources) {
55         setDataSources(dataSources);
56     }
57
58     /**
59      * Create a new instance of the {@link MapDataSourceLookup} class.
60      * @param dataSourceName the name under which the supplied {@link DataSource} is to be added
61      * @param dataSource the {@link DataSource} to be added
62      */

63     public MapDataSourceLookup(String JavaDoc dataSourceName, DataSource JavaDoc dataSource) {
64         addDataSource(dataSourceName, dataSource);
65     }
66
67
68     /**
69      * Set the {@link Map} of {@link DataSource DataSources}; the keys
70      * are {@link String Strings}, the values are actual {@link DataSource} instances.
71      * <p>If the supplied {@link Map} is <code>null</code>, then this method
72      * call effectively has no effect.
73      * @param dataSources said {@link Map} of {@link DataSource DataSources}
74      */

75     public void setDataSources(Map JavaDoc dataSources) {
76         if (dataSources != null) {
77             this.dataSources.putAll(dataSources);
78         }
79     }
80
81     /**
82      * Get the {@link Map} of {@link DataSource DataSources} maintained by this object.
83      * <p>The returned {@link Map} is {@link Collections#unmodifiableMap(java.util.Map) unmodifiable}.
84      * @return said {@link Map} of {@link DataSource DataSources} (never <code>null</code>)
85      */

86     public Map JavaDoc getDataSources() {
87         return Collections.unmodifiableMap(this.dataSources);
88     }
89
90     /**
91      * Add the supplied {@link DataSource} to the map of {@link DataSource DataSources}
92      * maintained by this object.
93      * @param dataSourceName the name under which the supplied {@link DataSource} is to be added
94      * @param dataSource the {@link DataSource} to be so added
95      */

96     public void addDataSource(String JavaDoc dataSourceName, DataSource JavaDoc 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 JavaDoc getDataSource(String JavaDoc dataSourceName) throws DataSourceLookupFailureException {
103         Assert.notNull(dataSourceName, "DataSource name must not be null");
104         Object JavaDoc 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 JavaDoc)) {
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 JavaDoc) value;
115     }
116
117 }
118
Popular Tags