001: /*
002: * Copyright 2002-2007 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: * http://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:
017: package org.springframework.jdbc.datasource;
018:
019: import java.io.PrintWriter;
020: import java.sql.Connection;
021: import java.sql.SQLException;
022:
023: import javax.sql.DataSource;
024:
025: import org.springframework.beans.factory.InitializingBean;
026: import 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: */
040: public class DelegatingDataSource implements DataSource,
041: InitializingBean {
042:
043: private DataSource targetDataSource;
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: * Set the target DataSource that this DataSource should delegate to.
062: */
063: public final void setTargetDataSource(DataSource targetDataSource) {
064: Assert.notNull(targetDataSource,
065: "'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: public void afterPropertiesSet() {
077: if (getTargetDataSource() == null) {
078: throw new IllegalArgumentException(
079: "Property 'targetDataSource' is required");
080: }
081: }
082:
083: public Connection getConnection() throws SQLException {
084: return getTargetDataSource().getConnection();
085: }
086:
087: public Connection getConnection(String username, String password)
088: throws SQLException {
089: return getTargetDataSource().getConnection(username, password);
090: }
091:
092: public PrintWriter getLogWriter() throws SQLException {
093: return getTargetDataSource().getLogWriter();
094: }
095:
096: public void setLogWriter(PrintWriter out) throws SQLException {
097: getTargetDataSource().setLogWriter(out);
098: }
099:
100: public int getLoginTimeout() throws SQLException {
101: return getTargetDataSource().getLoginTimeout();
102: }
103:
104: public void setLoginTimeout(int seconds) throws SQLException {
105: getTargetDataSource().setLoginTimeout(seconds);
106: }
107:
108: //---------------------------------------------------------------------
109: // Implementation of JDBC 4.0's Wrapper interface
110: //---------------------------------------------------------------------
111:
112: public Object unwrap(Class iface) throws SQLException {
113: return getTargetDataSource().unwrap(iface);
114: }
115:
116: public boolean isWrapperFor(Class iface) throws SQLException {
117: return getTargetDataSource().isWrapperFor(iface);
118: }
119:
120: }
|