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