Home | History | Annotate | Download | only in db
      1  125  alanbur /*
      2  125  alanbur  * CDDL HEADER START
      3  125  alanbur  * The contents of this file are subject to the terms of the Common
      4  125  alanbur  * Development and Distribution License (the "License").  You can find a copy
      5  125  alanbur  * of the License in the file CDDL.LICENSE at the root of the source archive
      6  125  alanbur  * containing this file or at http://www.sun.com/cddl/cddl.html.
      7  125  alanbur  * When distributing Covered Code, include this CDDL header in each file and
      8  125  alanbur  * include the License file at the root of your source archive.
      9  125  alanbur  * CDDL HEADER END
     10  125  alanbur  */
     11  125  alanbur 
     12  125  alanbur /*
     13  125  alanbur  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
     14  125  alanbur  */
     15  125  alanbur 
     16  125  alanbur package org.opensolaris.auth.db;
     17  125  alanbur 
     18  125  alanbur import java.io.PrintWriter;
     19  125  alanbur import java.sql.Connection;
     20  125  alanbur import java.sql.SQLException;
     21  125  alanbur import javax.sql.ConnectionPoolDataSource;
     22  125  alanbur import javax.sql.DataSource;
     23  125  alanbur 
     24  125  alanbur /**
     25  125  alanbur  * Implement a DataSource that uses {@link MiniConnectionPoolManager} to
     26  125  alanbur  * cache the database connections.
     27  125  alanbur  */
     28  125  alanbur class DbDataSource implements DataSource {
     29  125  alanbur 
     30  125  alanbur     /**
     31  125  alanbur      * Create a new data source.
     32  125  alanbur      * @param source the Derby data source to use for the pooled connections.
     33  125  alanbur      * @param maxConn the maximum number of connections in the pool.
     34  125  alanbur      * @param timeout the maximum time, in seconds, to wait for a connection to
     35  125  alanbur      * become available.
     36  125  alanbur      */
     37  125  alanbur     DbDataSource(ConnectionPoolDataSource source, int maxConn, int timeout) {
     38  125  alanbur         this.source = source;
     39  125  alanbur         this.pool = new MiniConnectionPoolManager(source, maxConn, timeout);
     40  125  alanbur     }
     41  125  alanbur 
     42  125  alanbur     /**
     43  125  alanbur      * {@inheritDoc}
     44  125  alanbur      * @return {@inheritDoc}
     45  125  alanbur      * @throws SQLException {@inheritDoc}
     46  125  alanbur      */
     47  338     Alan     @Override
     48  125  alanbur     public Connection getConnection()
     49  125  alanbur       throws SQLException {
     50  338     Alan         Connection c = pool.getConnection();
     51  338     Alan         c.setAutoCommit(false);
     52  338     Alan         return c;
     53  125  alanbur     }
     54  125  alanbur 
     55  125  alanbur     /**
     56  125  alanbur      * {@inheritDoc}
     57  125  alanbur      * The underlying pool manager does not support this functionality, so if
     58  125  alanbur      * this method is called a {@link SQLException} will be thrown.
     59  297  alanbur      * @param username connection username.
     60  125  alanbur      * @param password connection password.
     61  125  alanbur      * @return never returns, always throws a {@link SQLException}.
     62  125  alanbur      * @throws SQLException always.
     63  125  alanbur      */
     64  338     Alan     @Override
     65  297  alanbur     public Connection getConnection(String username, String password)
     66  125  alanbur       throws SQLException {
     67  125  alanbur         throw new UnsupportedOperationException(
     68  297  alanbur           "Cannot specify username/password for connections");
     69  125  alanbur     }
     70  125  alanbur 
     71  125  alanbur     /**
     72  125  alanbur      * {@inheritDoc}
     73  125  alanbur      * @param out {@inheritDoc}
     74  125  alanbur      * @throws SQLException {@inheritDoc}
     75  125  alanbur      */
     76  338     Alan     @Override
     77  125  alanbur     public void setLogWriter(PrintWriter out)
     78  125  alanbur       throws SQLException {
     79  125  alanbur         source.setLogWriter(out);
     80  125  alanbur     }
     81  125  alanbur 
     82  125  alanbur     /**
     83  125  alanbur      * {@inheritDoc}
     84  125  alanbur      * @return {@inheritDoc}
     85  125  alanbur      * @throws SQLException {@inheritDoc}
     86  125  alanbur      */
     87  338     Alan     @Override
     88  125  alanbur     public PrintWriter getLogWriter()
     89  125  alanbur       throws SQLException {
     90  125  alanbur         return source.getLogWriter();
     91  125  alanbur     }
     92  125  alanbur 
     93  125  alanbur     /**
     94  125  alanbur      * {@inheritDoc}
     95  125  alanbur      * @param seconds {@inheritDoc}
     96  125  alanbur      * @throws SQLException {@inheritDoc}
     97  125  alanbur      */
     98  338     Alan     @Override
     99  125  alanbur     public void setLoginTimeout(int seconds)
    100  125  alanbur       throws SQLException {
    101  125  alanbur         source.setLoginTimeout(seconds);
    102  125  alanbur     }
    103  125  alanbur 
    104  125  alanbur     /**
    105  125  alanbur      * {@inheritDoc}
    106  125  alanbur      * @return {@inheritDoc}
    107  125  alanbur      * @throws SQLException {@inheritDoc}
    108  125  alanbur      */
    109  338     Alan     @Override
    110  125  alanbur     public int getLoginTimeout()
    111  125  alanbur       throws SQLException {
    112  125  alanbur         return source.getLoginTimeout();
    113  125  alanbur     }
    114  125  alanbur 
    115  125  alanbur     /**
    116  125  alanbur      * {@inheritDoc}
    117  125  alanbur      * @param iface {@inheritDoc}
    118  125  alanbur      * @return {@inheritDoc}
    119  125  alanbur      * @throws SQLException {@inheritDoc}
    120  125  alanbur      */
    121  338     Alan     @Override
    122  125  alanbur     public <T> T unwrap(Class<T> iface)
    123  125  alanbur       throws SQLException {
    124  125  alanbur         try {
    125  125  alanbur             return iface.cast(this);
    126  125  alanbur         } catch (ClassCastException e) {
    127  125  alanbur             throw new SQLException("Unable to unwrap " + this.toString() +
    128  125  alanbur               " to " + iface.toString());
    129  125  alanbur         }
    130  125  alanbur     }
    131  125  alanbur 
    132  125  alanbur     /**
    133  125  alanbur      * {@inheritDoc}
    134  125  alanbur      * @param iface {@inheritDoc}
    135  125  alanbur      * @return {@inheritDoc}
    136  125  alanbur      * @throws SQLException {@inheritDoc}
    137  125  alanbur      */
    138  338     Alan     @Override
    139  125  alanbur     public boolean isWrapperFor(Class<?> iface)
    140  125  alanbur       throws SQLException {
    141  125  alanbur         return iface.isInstance(this);
    142  125  alanbur     }
    143  125  alanbur 
    144  125  alanbur     /**
    145  125  alanbur      * Dispose of all the currently unused connections in the pool.
    146  125  alanbur      * @throws SQLException if there is an error.
    147  125  alanbur      */
    148  125  alanbur     public void dispose()
    149  125  alanbur       throws SQLException {
    150  125  alanbur         pool.dispose();
    151  125  alanbur     }
    152  125  alanbur 
    153  125  alanbur     /** The Derby connection pool data source. */
    154  125  alanbur     private final ConnectionPoolDataSource source;
    155  125  alanbur     /** The connection pool manager. */
    156  125  alanbur     private final MiniConnectionPoolManager pool;
    157  125  alanbur }
    158