| 1 | /** |
| 2 | * Copyright (C) 2006 NetMind Consulting Bt. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 3 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | package hu.netmind.beankeeper.db.impl; |
| 20 | |
| 21 | import javax.sql.DataSource; |
| 22 | import java.sql.Connection; |
| 23 | import java.sql.DriverManager; |
| 24 | import java.sql.SQLException; |
| 25 | import java.io.PrintWriter; |
| 26 | import hu.netmind.beankeeper.common.StoreException; |
| 27 | |
| 28 | /** |
| 29 | * This is a data source for backwards compatibility. Older JDBC implementations |
| 30 | * only supported driver based connection establishing, this is a wrapper |
| 31 | * to make data source out of a driver. |
| 32 | * @author Brautigam Robert |
| 33 | * @version Revision: $Revision$ |
| 34 | */ |
| 35 | public class DriverDataSource implements DataSource |
| 36 | { |
| 37 | private String url; |
| 38 | |
| 39 | public DriverDataSource(String driverClass, String url) |
| 40 | { |
| 41 | try |
| 42 | { |
| 43 | // Register driver |
| 44 | Class.forName(driverClass); |
| 45 | // Remember url |
| 46 | this.url=url; |
| 47 | } catch ( Exception e ) { |
| 48 | throw new StoreException("could not allocate driver",e); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get the connection using the url specified in constructor. |
| 54 | * @return A connection from the DriverManager. |
| 55 | */ |
| 56 | public Connection getConnection() |
| 57 | throws SQLException |
| 58 | { |
| 59 | return DriverManager.getConnection(url); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get the connection using the url specified in constructor. |
| 64 | * @param params Additional parameters. |
| 65 | * @return A connection from the DriverManager. |
| 66 | */ |
| 67 | public Connection getConnection(String params) |
| 68 | throws SQLException |
| 69 | { |
| 70 | return DriverManager.getConnection(url+";"+params); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Get the connection using the url specified in constructor, with |
| 75 | * username and password given. |
| 76 | * @param username The username. |
| 77 | * @param password The password. |
| 78 | * @return A connection from the DriverManager. |
| 79 | */ |
| 80 | public Connection getConnection(String username, String password) |
| 81 | throws SQLException |
| 82 | { |
| 83 | return DriverManager.getConnection(url,username,password); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get the login timeout. |
| 88 | */ |
| 89 | public int getLoginTimeout() |
| 90 | { |
| 91 | return DriverManager.getLoginTimeout(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Get PrintWriter. |
| 96 | */ |
| 97 | public PrintWriter getLogWriter() |
| 98 | { |
| 99 | return DriverManager.getLogWriter(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Set the login timeout. |
| 104 | */ |
| 105 | public void setLoginTimeout(int timeout) |
| 106 | { |
| 107 | DriverManager.setLoginTimeout(timeout); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Set PrintWriter. |
| 112 | */ |
| 113 | public void setLogWriter(PrintWriter writer) |
| 114 | { |
| 115 | DriverManager.setLogWriter(writer); |
| 116 | } |
| 117 | |
| 118 | public boolean isWrapperFor(Class c) |
| 119 | { |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | public Object unwrap(Class c) |
| 124 | throws SQLException |
| 125 | { |
| 126 | throw new SQLException("DriverDataSource is not a wrapper for a specific object."); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | |