KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.Connection JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.sql.DataSource JavaDoc;
26
27 import org.springframework.beans.factory.InitializingBean;
28 import org.springframework.jdbc.datasource.AbstractDataSource;
29 import org.springframework.util.Assert;
30
31 /**
32  * Abstract DataSource implementation that routes {@link #getConnection()} calls
33  * to one of various target DataSources based on a lookup key. The latter is usually
34  * (but not necessarily) determined through some thread-bound transaction context.
35  *
36  * @author Juergen Hoeller
37  * @since 2.0.1
38  * @see #setTargetDataSources
39  * @see #setDefaultTargetDataSource
40  * @see #determineCurrentLookupKey()
41  */

42 public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean {
43
44     private Map JavaDoc targetDataSources;
45
46     private Object JavaDoc defaultTargetDataSource;
47
48     private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
49
50     private Map JavaDoc resolvedDataSources;
51
52     private DataSource JavaDoc resolvedDefaultDataSource;
53
54
55     /**
56      * Specify the map of target DataSources, with the lookup key as key.
57      * The mapped value can either be a corresponding {@link javax.sql.DataSource}
58      * instance or a data source name String (to be resolved via a
59      * {@link #setDataSourceLookup DataSourceLookup}.
60      * <p>The key can be of arbitrary type; this class implements the
61      * generic lookup process only. The concrete key representation will
62      * be handled by {@link #resolveSpecifiedLookupKey(Object)} and
63      * {@link #determineCurrentLookupKey()}.
64      */

65     public void setTargetDataSources(Map JavaDoc targetDataSources) {
66         this.targetDataSources = targetDataSources;
67     }
68
69     /**
70      * Specify the default target DataSource, if any.
71      * The mapped value can either be a corresponding {@link javax.sql.DataSource}
72      * instance or a data source name String (to be resolved via a
73      * {@link #setDataSourceLookup DataSourceLookup}.
74      * <p>This DataSource will be used as target if none of the keyed
75      * {@link #setTargetDataSources targetDataSources} match the
76      * {@link #determineCurrentLookupKey()} current lookup key}.
77      */

78     public void setDefaultTargetDataSource(Object JavaDoc defaultTargetDataSource) {
79         this.defaultTargetDataSource = defaultTargetDataSource;
80     }
81
82     /**
83      * Set the DataSourceLookup implementation to use for resolving data source
84      * name Strings in the {@link #setTargetDataSources targetDataSources} map.
85      * <p>Default is a {@link JndiDataSourceLookup}, allowing the JNDI names
86      * of application server DataSources to be specified directly.
87      */

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 JavaDoc("targetDataSources is required");
96         }
97         this.resolvedDataSources = new HashMap JavaDoc(this.targetDataSources.size());
98         for (Iterator JavaDoc it = this.targetDataSources.entrySet().iterator(); it.hasNext();) {
99             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
100             Object JavaDoc lookupKey = resolveSpecifiedLookupKey(entry.getKey());
101             DataSource JavaDoc 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     /**
110      * Resolve the specified data source object into a DataSource instance.
111      * <p>The default implementation handles DataSource instances and data source
112      * names (to be resolved via a {@link #setDataSourceLookup DataSourceLookup}).
113      * @param dataSource the data source value object as specified in the
114      * {@link #setTargetDataSources targetDataSources} map
115      * @return the resolved DataSource (never <code>null</code>)
116      * @throws IllegalArgumentException in case of an unsupported value type
117      */

118     protected DataSource JavaDoc resolveSpecifiedDataSource(Object JavaDoc dataSource) throws IllegalArgumentException JavaDoc {
119         if (dataSource instanceof DataSource JavaDoc) {
120             return (DataSource JavaDoc) dataSource;
121         }
122         else if (dataSource instanceof String JavaDoc) {
123             return this.dataSourceLookup.getDataSource((String JavaDoc) dataSource);
124         }
125         else {
126             throw new IllegalArgumentException JavaDoc(
127                     "Illegal data source value - only [javax.sql.DataSource] and String supported: " + dataSource);
128         }
129     }
130
131
132     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
133         return determineTargetDataSource().getConnection();
134     }
135
136     public Connection JavaDoc getConnection(String JavaDoc username, String JavaDoc password) throws SQLException JavaDoc {
137         return determineTargetDataSource().getConnection(username, password);
138     }
139
140     /**
141      * Retrieve the current target DataSource. Determines the
142      * {@link #determineCurrentLookupKey() current lookup key}, performs
143      * a lookup in the {@link #setTargetDataSources targetDataSources} map,
144      * falls back to the specified
145      * {@link #setDefaultTargetDataSource default target DataSource} if necessary.
146      * @see #determineCurrentLookupKey()
147      */

148     protected DataSource JavaDoc determineTargetDataSource() {
149         Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
150         Object JavaDoc lookupKey = determineCurrentLookupKey();
151         DataSource JavaDoc dataSource = (DataSource JavaDoc) this.resolvedDataSources.get(lookupKey);
152         if (dataSource == null) {
153             dataSource = this.resolvedDefaultDataSource;
154         }
155         if (dataSource == null) {
156             throw new IllegalStateException JavaDoc("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
157         }
158         return dataSource;
159     }
160
161
162     /**
163      * Resolve the given lookup key object, as specified in the
164      * {@link #setTargetDataSources targetDataSources} map, into
165      * the actual lookup key to be used for matching with the
166      * {@link #determineCurrentLookupKey() current lookup key}.
167      * <p>The default implementation simply returns the given key as-is.
168      * @param lookupKey the lookup key object as specified by the user
169      * @return the lookup key as needed for matching
170      */

171     protected Object JavaDoc resolveSpecifiedLookupKey(Object JavaDoc lookupKey) {
172         return lookupKey;
173     }
174
175     /**
176      * Determine the current lookup key. This will typically be
177      * implemented to check a thread-bound transaction context.
178      * <p>Allows for arbitrary keys. The returned key needs
179      * to match the stored lookup key type, as resolved by the
180      * {@link #resolveSpecifiedLookupKey} method.
181      */

182     protected abstract Object JavaDoc determineCurrentLookupKey();
183
184 }
185
Popular Tags