KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > datasource > DelegatingDataSource


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jdbc.datasource;
18
19 import java.io.PrintWriter JavaDoc;
20 import java.sql.Connection JavaDoc;
21 import java.sql.SQLException JavaDoc;
22
23 import javax.sql.DataSource JavaDoc;
24
25 import org.springframework.beans.factory.InitializingBean;
26 import org.springframework.util.Assert;
27
28 /**
29  * JDBC {@link javax.sql.DataSource} implementation that delegates all calls
30  * to a given target {@link javax.sql.DataSource}.
31  *
32  * <p>This class is meant to be subclassed, with subclasses overriding only
33  * those methods (such as {@link #getConnection()}) that should not simply
34  * delegate to the target DataSource.
35  *
36  * @author Juergen Hoeller
37  * @since 1.1
38  * @see #getConnection
39  */

40 public class DelegatingDataSource implements DataSource JavaDoc, InitializingBean {
41
42     private DataSource JavaDoc targetDataSource;
43
44
45     /**
46      * Create a new DelegatingDataSource.
47      * @see #setTargetDataSource
48      */

49     public DelegatingDataSource() {
50     }
51
52     /**
53      * Create a new DelegatingDataSource.
54      * @param targetDataSource the target DataSource
55      */

56     public DelegatingDataSource(DataSource JavaDoc targetDataSource) {
57         setTargetDataSource(targetDataSource);
58     }
59
60
61     /**
62      * Set the target DataSource that this DataSource should delegate to.
63      */

64     public final void setTargetDataSource(DataSource JavaDoc targetDataSource) {
65         Assert.notNull(targetDataSource, "'targetDataSource' must not be null");
66         this.targetDataSource = targetDataSource;
67     }
68
69     /**
70      * Return the target DataSource that this DataSource should delegate to.
71      */

72     public DataSource JavaDoc getTargetDataSource() {
73         return this.targetDataSource;
74     }
75
76     public void afterPropertiesSet() {
77         if (getTargetDataSource() == null) {
78             throw new IllegalArgumentException JavaDoc("Property 'targetDataSource' is required");
79         }
80     }
81
82
83     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
84         return getTargetDataSource().getConnection();
85     }
86
87     public Connection JavaDoc getConnection(String JavaDoc username, String JavaDoc password) throws SQLException JavaDoc {
88         return getTargetDataSource().getConnection(username, password);
89     }
90
91     public PrintWriter JavaDoc getLogWriter() throws SQLException JavaDoc {
92         return getTargetDataSource().getLogWriter();
93     }
94
95     public void setLogWriter(PrintWriter JavaDoc out) throws SQLException JavaDoc {
96         getTargetDataSource().setLogWriter(out);
97     }
98
99     public int getLoginTimeout() throws SQLException JavaDoc {
100         return getTargetDataSource().getLoginTimeout();
101     }
102
103     public void setLoginTimeout(int seconds) throws SQLException JavaDoc {
104         getTargetDataSource().setLoginTimeout(seconds);
105     }
106
107 }
108
Popular Tags