KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > ojb > support > LocalDataSourceConnectionFactory


1 /*
2  * Copyright 2002-2005 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.orm.ojb.support;
18
19 import java.sql.Connection JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.sql.DataSource JavaDoc;
24
25 import org.apache.ojb.broker.accesslayer.ConnectionFactoryManagedImpl;
26 import org.apache.ojb.broker.accesslayer.LookupException;
27 import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
28
29 import org.springframework.beans.factory.BeanFactory;
30
31 /**
32  * OJB connection factory that delegates to Spring-managed DataSource beans.
33  *
34  * <p>Define the following entry in your OJB.properties to use this connection factory:
35  *
36  * <pre>
37  * ConnectionFactoryClass=org.springframework.orm.ojb.support.LocalDataSourceConnectionFactory</pre>
38  *
39  * Interprets JCD aliases in OJB's JDBC connection descriptors as Spring bean names.
40  * For example, the following will delegate to the Spring bean named "myDataSource":
41  *
42  * <pre>
43  * &lt;jdbc-connection-descriptor jcd-alias="myDataSource" default-connection="true" useAutoCommit="1"/&gt;</pre>
44  *
45  * Depends on LocalOjbConfigurer being defined as Spring bean, which will expose
46  * the Spring BeanFactory to the corresponding static field of this connection factory.
47  *
48  * <p>Consider using the subclass TransactionAwareDataSourceConnectionFactory instead,
49  * to let Spring's OJB access to participate in JDBC-based transactions managed outside
50  * of OJB (for example, by Spring's DataSourceTransactionManager), or to automatically
51  * apply transaction timeouts managed by PersistenceBrokerTransactionManager.
52  *
53  * @author Juergen Hoeller
54  * @since 1.1
55  * @see LocalOjbConfigurer
56  * @see TransactionAwareDataSourceConnectionFactory
57  */

58 public class LocalDataSourceConnectionFactory extends ConnectionFactoryManagedImpl {
59
60     /**
61      * This will hold the BeanFactory to retrieve DataSource beans from.
62      */

63     protected static BeanFactory beanFactory;
64
65     /**
66      * Map that holds already retrieved DataSources,
67      * with JCD alias Strings as keys and DataSources as values.
68      */

69     private Map JavaDoc dataSources = new HashMap JavaDoc();
70
71     public LocalDataSourceConnectionFactory() {
72         if (beanFactory == null) {
73             throw new IllegalStateException JavaDoc("No BeanFactory found for configuration - " +
74                     "LocalOjbConfigurer must be defined as Spring bean");
75         }
76     }
77
78     public Connection JavaDoc lookupConnection(JdbcConnectionDescriptor jcd) throws LookupException {
79         try {
80             DataSource JavaDoc dataSource = null;
81             synchronized (this.dataSources) {
82                 dataSource = (DataSource JavaDoc) this.dataSources.get(jcd.getJcdAlias());
83                 if (dataSource == null) {
84                     dataSource = getDataSource(jcd.getJcdAlias());
85                     this.dataSources.put(jcd.getJcdAlias(), dataSource);
86                 }
87             }
88             return dataSource.getConnection();
89         }
90         catch (Exception JavaDoc ex) {
91             throw new LookupException("Could not obtain connection from data source", ex);
92         }
93     }
94
95     /**
96      * Return the DataSource to use for the given JCD alias.
97      * <p>This implementation fetches looks for a bean with the
98      * JCD alias name in the provided Spring BeanFactory.
99      * @param jcdAlias the JCD alias to retrieve a DataSource for
100      * @return the DataSource to use
101      */

102     protected DataSource JavaDoc getDataSource(String JavaDoc jcdAlias) {
103         return (DataSource JavaDoc) beanFactory.getBean(jcdAlias, DataSource JavaDoc.class);
104     }
105
106 }
107
Popular Tags