001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     George Lefter
016 *     Florent Guillaume
017 */
018package org.nuxeo.runtime.datasource;
019
020import java.io.PrintWriter;
021import java.sql.Connection;
022import java.sql.SQLException;
023import java.sql.SQLFeatureNotSupportedException;
024import java.util.logging.Logger;
025
026import javax.sql.DataSource;
027
028import org.nuxeo.common.utils.JDBCUtils;
029import org.nuxeo.runtime.api.Framework;
030
031/**
032 * This class is used for tests, where datasources are set up from a jdbc: url and user/password instead of a JNDI name.
033 */
034public class DataSourceFromUrl implements DataSource {
035
036    private final String url;
037
038    private final String user;
039
040    private final String password;
041
042    public DataSourceFromUrl(String url, String user, String password) {
043        this.url = Framework.expandVars(url);
044        this.user = Framework.expandVars(user);
045        this.password = Framework.expandVars(password);
046    }
047
048    public DataSourceFromUrl(String url, String user, String password, String driver) {
049        this(url, user, password);
050        if (driver != null) {
051            driver = Framework.expandVars(driver);
052            try {
053                // Driver registration is automatic since Java 6, provided
054                // the JDBC library is correctly written. If it's not, then
055                // this can still be useful.
056                Class.forName(driver);
057            } catch (ClassNotFoundException e) {
058                throw new RuntimeException("Driver class not found: " + driver, e);
059            }
060        }
061    }
062
063    @Override
064    public Connection getConnection() throws SQLException {
065        Connection con = JDBCUtils.getConnection(url, user, password);
066        con.setAutoCommit(true); // default
067        return con;
068    }
069
070    @Override
071    public Connection getConnection(String username, String password) throws SQLException {
072        throw new UnsupportedOperationException();
073    }
074
075    @Override
076    public PrintWriter getLogWriter() throws SQLException {
077        throw new UnsupportedOperationException();
078    }
079
080    @Override
081    public int getLoginTimeout() throws SQLException {
082        throw new UnsupportedOperationException();
083    }
084
085    @Override
086    public void setLogWriter(PrintWriter out) throws SQLException {
087        throw new UnsupportedOperationException();
088    }
089
090    @Override
091    public void setLoginTimeout(int seconds) throws SQLException {
092        throw new UnsupportedOperationException();
093    }
094
095    // @Override in CommonDataSource for Java SE 7 / JDBC 4.1
096    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
097        throw new SQLFeatureNotSupportedException();
098    }
099
100    @Override
101    public boolean isWrapperFor(Class<?> iface) throws SQLException {
102        throw new UnsupportedOperationException();
103    }
104
105    @Override
106    public <T> T unwrap(Class<T> iface) throws SQLException {
107        throw new UnsupportedOperationException();
108    }
109
110}