001package org.javasimon.jdbcx4; 002 003import java.sql.Connection; 004import java.sql.SQLException; 005import java.sql.SQLFeatureNotSupportedException; 006import java.util.logging.Logger; 007import javax.sql.DataSource; 008 009import org.javasimon.jdbc4.SimonConnection; 010import org.javasimon.jdbc4.WrapperSupport; 011 012/** 013 * WrappingSimonDataSource allows to wrap existing datasource instead of providing 014 * the Driver and URL information. For example - it can be used with Spring easily: 015 * <pre>{@code 016 * <bean id="dataSource" class="org.javasimon.jdbcx.WrappingSimonDataSource"> 017 * <property name="dataSource" ref="pooledDataSource"/> 018 * <property name="prefix" value="sky.batchpricer.skydb"/> 019 * </bean> 020 * 021 * <bean id="pooledDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 022 * <property name="driverClassName" value="my.driver.class.Driver"/> 023 * <property name="url" value="${mydb.url}"/> 024 * <property name="initialSize" value="0"/> 025 * <property name="maxActive" value="5"/> 026 * <property name="maxIdle" value="2"/> 027 * <property name="validationQuery" value="SELECT 1"/> 028 * </bean>}</pre> 029 * 030 * @author Radovan Sninsky 031 * @author <a href="mailto:virgo47@gmail.com">Richard "Virgo" Richter</a> 032 * @since 2.4 033 */ 034public class WrappingSimonDataSource extends AbstractSimonDataSource implements DataSource { 035 private DataSource ds; 036 private WrapperSupport<DataSource> wrapperSupport; 037 038 public DataSource getDataSource() { 039 return ds; 040 } 041 042 public void setDataSource(DataSource ds) { 043 this.ds = ds; 044 this.wrapperSupport = new WrapperSupport<>(ds, DataSource.class); 045 } 046 047 /** 048 * Attempts to establish a connection with the data source that this {@code DataSource} object represents. 049 * 050 * @return a connection to the data source 051 * @throws SQLException if a database access error occurs 052 */ 053 @Override 054 public Connection getConnection() throws SQLException { 055 return new SimonConnection(getDataSource().getConnection(), getPrefix()); 056 } 057 058 /** 059 * Attempts to establish a connection with the data source that this {@code DataSource} object represents. 060 * 061 * @param user the database user on whose behalf the connection is being made 062 * @param password the user's password 063 * @return a connection to the data source 064 * @throws SQLException if a database access error occurs 065 */ 066 @Override 067 public Connection getConnection(String user, String password) throws SQLException { 068 return new SimonConnection(getDataSource().getConnection(user, password), getPrefix()); 069 } 070 071 @Override 072 public <T> T unwrap(Class<T> iface) throws SQLException { 073 return wrapperSupport.unwrap(iface); 074 } 075 076 @Override 077 public boolean isWrapperFor(Class<?> iface) throws SQLException { 078 return wrapperSupport.isWrapperFor(iface); 079 } 080 081 @Override 082 protected String doGetRealDataSourceClassName() { 083 return this.configuration.getRealDataSourceName(); 084 } 085 086 @Override 087 public Logger getParentLogger() throws SQLFeatureNotSupportedException { 088 return ds.getParentLogger(); 089 } 090}