001/*
002 * (C) Copyright 2006-2011 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.ecm.directory.sql;
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 driver, url and user/password instead of a JNDI
033 * name.
034 */
035public class SimpleDataSource implements DataSource {
036
037    private final String url;
038
039    private final String user;
040
041    private final String password;
042
043    public SimpleDataSource(String url, String driver, String user, String password) {
044        url = Framework.expandVars(url);
045        driver = Framework.expandVars(driver);
046        user = Framework.expandVars(user);
047        password = Framework.expandVars(password);
048        try {
049            Class.forName(driver);
050        } catch (ClassNotFoundException e) {
051            throw new RuntimeException("driver class not found", e);
052        }
053        this.url = url;
054        this.user = user;
055        this.password = password;
056    }
057
058    @Override
059    public Connection getConnection() throws SQLException {
060        Connection con = JDBCUtils.getConnection(url, user, password);
061        con.setAutoCommit(true);
062        return con;
063    }
064
065    @Override
066    public Connection getConnection(String username, String password) throws SQLException {
067        throw new UnsupportedOperationException();
068    }
069
070    @Override
071    public PrintWriter getLogWriter() throws SQLException {
072        throw new UnsupportedOperationException();
073    }
074
075    @Override
076    public int getLoginTimeout() throws SQLException {
077        throw new UnsupportedOperationException();
078    }
079
080    @Override
081    public void setLogWriter(PrintWriter out) throws SQLException {
082        throw new UnsupportedOperationException();
083    }
084
085    @Override
086    public void setLoginTimeout(int seconds) throws SQLException {
087        throw new UnsupportedOperationException();
088    }
089
090    // @Override in CommonDataSource for Java SE 7 / JDBC 4.1
091    @Override
092    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
093        throw new SQLFeatureNotSupportedException();
094    }
095
096    @Override
097    public boolean isWrapperFor(Class<?> iface) throws SQLException {
098        throw new UnsupportedOperationException();
099    }
100
101    @Override
102    public <T> T unwrap(Class<T> iface) throws SQLException {
103        throw new UnsupportedOperationException();
104    }
105
106}