01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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 javax.sql.DataSource;
20:
21: import org.springframework.beans.BeansException;
22: import org.springframework.beans.factory.BeanFactory;
23: import org.springframework.beans.factory.BeanFactoryAware;
24: import org.springframework.util.Assert;
25:
26: /**
27: * {@link DataSourceLookup} implementation based on a Spring {@link BeanFactory}.
28: *
29: * <p>Will lookup Spring managed beans identified by bean name,
30: * expecting them to be of type <code>javax.sql.DataSource</code>.
31: *
32: * @author Costin Leau
33: * @author Juergen Hoeller
34: * @since 2.0
35: * @see org.springframework.beans.factory.BeanFactory
36: */
37: public class BeanFactoryDataSourceLookup implements DataSourceLookup,
38: BeanFactoryAware {
39:
40: private BeanFactory beanFactory;
41:
42: /**
43: * Create a new instance of the {@link BeanFactoryDataSourceLookup} class.
44: * <p>The BeanFactory to access must be set via <code>setBeanFactory</code>.
45: * @see #setBeanFactory
46: */
47: public BeanFactoryDataSourceLookup() {
48: }
49:
50: /**
51: * Create a new instance of the {@link BeanFactoryDataSourceLookup} class.
52: * <p>Use of this constructor is redundant if this object is being created
53: * by a Spring IoC container, as the supplied {@link BeanFactory} will be
54: * replaced by the {@link BeanFactory} that creates it (c.f. the
55: * {@link BeanFactoryAware} contract). So only use this constructor if you
56: * are using this class outside the context of a Spring IoC container.
57: * @param beanFactory the bean factory to be used to lookup {@link DataSource DataSources}
58: */
59: public BeanFactoryDataSourceLookup(BeanFactory beanFactory) {
60: Assert.notNull(beanFactory, "BeanFactory is required");
61: this .beanFactory = beanFactory;
62: }
63:
64: public void setBeanFactory(BeanFactory beanFactory) {
65: this .beanFactory = beanFactory;
66: }
67:
68: public DataSource getDataSource(String dataSourceName)
69: throws DataSourceLookupFailureException {
70: Assert.notNull(this .beanFactory, "BeanFactory is required");
71: try {
72: return (DataSource) this .beanFactory.getBean(
73: dataSourceName, DataSource.class);
74: } catch (BeansException ex) {
75: throw new DataSourceLookupFailureException(
76: "Failed to look up DataSource bean with name '"
77: + dataSourceName + "'", ex);
78: }
79: }
80:
81: }
|