001/** 002 * Copyright 2005-2018 The Kuali Foundation 003 * 004 * Licensed under the Educational Community 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 * http://www.opensource.org/licenses/ecl2.php 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 */ 016package org.kuali.rice.core.framework.persistence.ojb; 017 018import java.sql.Connection; 019import java.util.ArrayList; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Map; 023 024import javax.sql.DataSource; 025 026import org.apache.ojb.broker.accesslayer.ConnectionFactoryNotPooledImpl; 027import org.apache.ojb.broker.accesslayer.LookupException; 028import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor; 029import org.springframework.beans.factory.BeanFactory; 030import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; 031 032@Deprecated 033public class RiceDataSourceConnectionFactory extends ConnectionFactoryNotPooledImpl { 034 035 /** 036 * BeanFactories to retrieve DataSource beans from. 037 */ 038 private static List<BeanFactory> beanFactories = new ArrayList<BeanFactory>(); 039 040 public static void addBeanFactory(BeanFactory beanFactory) { 041 beanFactories.add(beanFactory); 042 } 043 044 /** 045 * Map that holds already retrieved DataSources, 046 * with JCD alias Strings as keys and DataSources as values. 047 */ 048 private Map<String, DataSource> dataSources = new HashMap<String, DataSource>(); 049 050 public RiceDataSourceConnectionFactory() { 051 if (beanFactories.isEmpty()) { 052 throw new IllegalStateException("No BeanFactories found for configuration - must specify RiceOjbConfigurer as a Spring bean."); 053 } 054 } 055 056 public Connection lookupConnection(JdbcConnectionDescriptor jcd) throws LookupException { 057 try { 058 DataSource dataSource = null; 059 synchronized (this.dataSources) { 060 dataSource = this.dataSources.get(jcd.getJcdAlias()); 061 if (dataSource == null) { 062 dataSource = getDataSource(jcd.getJcdAlias()); 063 this.dataSources.put(jcd.getJcdAlias(), dataSource); 064 } 065 } 066 return dataSource.getConnection(); 067 } 068 catch (Exception ex) { 069 throw new LookupException("Could not obtain connection from data source", ex); 070 } 071 } 072 073 /** 074 * Return the DataSource to use for the given JCD alias. 075 * <p>This implementation fetches looks for a bean with the 076 * JCD alias name in the provided Spring BeanFactory. 077 * @param jcdAlias the JCD alias to retrieve a DataSource for 078 * @return the DataSource to use 079 */ 080 081 protected DataSource getDataSource(String jcdAlias) throws LookupException { 082 DataSource dataSource = null; 083 for (BeanFactory beanFactory : beanFactories) { 084 if (beanFactory.containsBean(jcdAlias)) { 085 dataSource = (DataSource) beanFactory.getBean(jcdAlias, DataSource.class); 086 break; 087 } 088 } 089 if (dataSource == null) { 090 throw new LookupException("Could not lookup datasource with alias " + jcdAlias); 091 } else if (dataSource instanceof TransactionAwareDataSourceProxy) { 092 return dataSource; 093 } else { 094 return new TransactionAwareDataSourceProxy(dataSource); 095 } 096 } 097 098}