View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.dbcp2.datasources;
19  
20  import java.io.PrintWriter;
21  import java.sql.SQLException;
22  import java.sql.SQLFeatureNotSupportedException;
23  import java.util.logging.Logger;
24  
25  import javax.sql.ConnectionPoolDataSource;
26  import javax.sql.PooledConnection;
27  
28  import org.apache.commons.dbcp2.Jdbc41Bridge;
29  
30  /**
31   * ConnectionPoolDataSource implementation that proxies another ConnectionPoolDataSource.
32   */
33  public class ConnectionPoolDataSourceProxy implements ConnectionPoolDataSource {
34  
35      protected ConnectionPoolDataSource delegate;
36  
37      public ConnectionPoolDataSourceProxy(final ConnectionPoolDataSource cpds) {
38          this.delegate = cpds;
39      }
40  
41      public ConnectionPoolDataSource getDelegate() {
42          return delegate;
43      }
44  
45      @Override
46      public int getLoginTimeout() throws SQLException {
47          return delegate.getLoginTimeout();
48      }
49  
50      @Override
51      public PrintWriter getLogWriter() throws SQLException {
52          return delegate.getLogWriter();
53      }
54  
55      @Override
56      public Logger getParentLogger() throws SQLFeatureNotSupportedException {
57          return Jdbc41Bridge.getParentLogger(delegate);
58      }
59  
60      /**
61       * Gets a TesterPooledConnection with notifyOnClose turned on
62       */
63      @Override
64      public PooledConnection getPooledConnection() throws SQLException {
65          return wrapPooledConnection(delegate.getPooledConnection());
66      }
67  
68      /**
69       * Gets a TesterPooledConnection with notifyOnClose turned on
70       */
71      @Override
72      public PooledConnection getPooledConnection(final String user, final String password)
73              throws SQLException {
74          return wrapPooledConnection(delegate.getPooledConnection(user, password));
75      }
76  
77      @Override
78      public void setLoginTimeout(final int seconds) throws SQLException {
79          delegate.setLoginTimeout(seconds);
80      }
81  
82      @Override
83      public void setLogWriter(final PrintWriter out) throws SQLException {
84          delegate.setLogWriter(out);
85      }
86  
87      /**
88       * Creates a TesterPooledConnection with notifyOnClose turned on
89       */
90      protected PooledConnection wrapPooledConnection(final PooledConnection pc) {
91          final PooledConnectionProxy tpc = new PooledConnectionProxy(pc);
92          tpc.setNotifyOnClose(true);
93          return tpc;
94      }
95  }