001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache 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 * https://www.apache.org/licenses/LICENSE-2.0 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 */ 016 017package org.springframework.jdbc.datasource; 018 019import java.io.PrintWriter; 020import java.sql.Connection; 021import java.sql.SQLException; 022import java.util.logging.Logger; 023import javax.sql.DataSource; 024 025import org.springframework.beans.factory.InitializingBean; 026import org.springframework.util.Assert; 027 028/** 029 * JDBC {@link javax.sql.DataSource} implementation that delegates all calls 030 * to a given target {@link javax.sql.DataSource}. 031 * 032 * <p>This class is meant to be subclassed, with subclasses overriding only 033 * those methods (such as {@link #getConnection()}) that should not simply 034 * delegate to the target DataSource. 035 * 036 * @author Juergen Hoeller 037 * @since 1.1 038 * @see #getConnection 039 */ 040public class DelegatingDataSource implements DataSource, InitializingBean { 041 042 private DataSource targetDataSource; 043 044 045 /** 046 * Create a new DelegatingDataSource. 047 * @see #setTargetDataSource 048 */ 049 public DelegatingDataSource() { 050 } 051 052 /** 053 * Create a new DelegatingDataSource. 054 * @param targetDataSource the target DataSource 055 */ 056 public DelegatingDataSource(DataSource targetDataSource) { 057 setTargetDataSource(targetDataSource); 058 } 059 060 061 /** 062 * Set the target DataSource that this DataSource should delegate to. 063 */ 064 public void setTargetDataSource(DataSource targetDataSource) { 065 Assert.notNull(targetDataSource, "'targetDataSource' must not be null"); 066 this.targetDataSource = targetDataSource; 067 } 068 069 /** 070 * Return the target DataSource that this DataSource should delegate to. 071 */ 072 public DataSource getTargetDataSource() { 073 return this.targetDataSource; 074 } 075 076 @Override 077 public void afterPropertiesSet() { 078 if (getTargetDataSource() == null) { 079 throw new IllegalArgumentException("Property 'targetDataSource' is required"); 080 } 081 } 082 083 084 @Override 085 public Connection getConnection() throws SQLException { 086 return getTargetDataSource().getConnection(); 087 } 088 089 @Override 090 public Connection getConnection(String username, String password) throws SQLException { 091 return getTargetDataSource().getConnection(username, password); 092 } 093 094 @Override 095 public PrintWriter getLogWriter() throws SQLException { 096 return getTargetDataSource().getLogWriter(); 097 } 098 099 @Override 100 public void setLogWriter(PrintWriter out) throws SQLException { 101 getTargetDataSource().setLogWriter(out); 102 } 103 104 @Override 105 public int getLoginTimeout() throws SQLException { 106 return getTargetDataSource().getLoginTimeout(); 107 } 108 109 @Override 110 public void setLoginTimeout(int seconds) throws SQLException { 111 getTargetDataSource().setLoginTimeout(seconds); 112 } 113 114 115 //--------------------------------------------------------------------- 116 // Implementation of JDBC 4.0's Wrapper interface 117 //--------------------------------------------------------------------- 118 119 @Override 120 @SuppressWarnings("unchecked") 121 public <T> T unwrap(Class<T> iface) throws SQLException { 122 if (iface.isInstance(this)) { 123 return (T) this; 124 } 125 return getTargetDataSource().unwrap(iface); 126 } 127 128 @Override 129 public boolean isWrapperFor(Class<?> iface) throws SQLException { 130 return (iface.isInstance(this) || getTargetDataSource().isWrapperFor(iface)); 131 } 132 133 134 //--------------------------------------------------------------------- 135 // Implementation of JDBC 4.1's getParentLogger method 136 //--------------------------------------------------------------------- 137 138 @Override 139 public Logger getParentLogger() { 140 return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); 141 } 142 143}