001 /* 002 * JBoss DNA (http://www.jboss.org/dna) 003 * See the COPYRIGHT.txt file distributed with this work for information 004 * regarding copyright ownership. Some portions may be licensed 005 * to Red Hat, Inc. under one or more contributor license agreements. 006 * See the AUTHORS.txt file in the distribution for a full listing of 007 * individual contributors. 008 * 009 * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA 010 * is licensed to you under the terms of the GNU Lesser General Public License as 011 * published by the Free Software Foundation; either version 2.1 of 012 * the License, or (at your option) any later version. 013 * 014 * JBoss DNA is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 017 * Lesser General Public License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this software; if not, write to the Free 021 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 022 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 023 */ 024 package org.jboss.dna.common.jdbc.provider; 025 026 import java.sql.Connection; 027 import javax.naming.InitialContext; 028 import javax.sql.DataSource; 029 030 /** 031 * Default DataSource based DatabaseMetadataProvider 032 * 033 * @author <a href="mailto:litsenko_sergey@yahoo.com">Sergiy Litsenko</a> 034 */ 035 public class DefaultDataSourceDatabaseMetadataProvider extends DefaultDatabaseMetadataProvider 036 implements DataSourceDatabaseMetadataProvider { 037 // ~ Instance fields ------------------------------------------------------------------ 038 039 private static final long serialVersionUID = -405855619877872933L; 040 private String dataSourceName; 041 private DataSource dataSource; 042 043 // ~ Constructors --------------------------------------------------------------------- 044 045 /** 046 * Default Constructor 047 */ 048 public DefaultDataSourceDatabaseMetadataProvider() { 049 } 050 051 /** 052 * Constructor 053 * 054 * @param name the DatabaseMetadataProvider logical name 055 */ 056 public DefaultDataSourceDatabaseMetadataProvider( String name ) { 057 super(name); 058 } 059 060 // ~ Methods -------------------------------------------------------------------------- 061 062 /** 063 * Opens new database connection based on suppied parameters 064 * 065 * @return new database connection based on suppied parameters 066 * @throws Exception 067 */ 068 @Override 069 protected Connection openConnection() throws Exception { 070 // log debug info 071 if (log.isDebugEnabled()) { 072 log.debug("Opening database connection from data source " + getDataSourceName()); 073 } 074 075 // loads connection from data source 076 return getDataSource().getConnection(); 077 } 078 079 /** 080 * Returns DataSource 081 * 082 * @return DataSource 083 * @throws Exception 084 */ 085 public DataSource getDataSource() throws Exception { 086 // lazy load data source 087 if (dataSource == null) { 088 // log debug info 089 if (log.isDebugEnabled()) { 090 log.debug("Creating JNDI initial context with following properties: " + getProperties()); 091 } 092 093 // create initial context using properties if any 094 InitialContext initialContext = new InitialContext(getProperties()); 095 096 try { 097 // log debug info 098 if (log.isDebugEnabled()) { 099 log.debug("Performing JNDI lookup for data source " + getDataSourceName()); 100 } 101 102 // lookups datasource 103 dataSource = (DataSource)initialContext.lookup(getDataSourceName()); 104 } finally { 105 // close context 106 initialContext.close(); 107 } 108 } 109 110 // return 111 return dataSource; 112 } 113 114 /** 115 * Sets data source JNDI name 116 * 117 * @return data source JNDI name 118 */ 119 public String getDataSourceName() { 120 // return 121 return dataSourceName; 122 } 123 124 /** 125 * Sets data source JNDI name 126 * 127 * @param dataSourceName the data source JNDI name 128 */ 129 public void setDataSourceName( String dataSourceName ) { 130 this.dataSourceName = dataSourceName; 131 } 132 }