001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     George Lefter
018 *     Florent Guillaume
019 */
020package org.nuxeo.runtime.datasource;
021
022import java.io.PrintWriter;
023import java.sql.Connection;
024import java.sql.SQLException;
025import java.sql.SQLFeatureNotSupportedException;
026import java.util.logging.Logger;
027
028import javax.sql.DataSource;
029
030import org.nuxeo.common.utils.JDBCUtils;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * This class is used for tests, where datasources are set up from a jdbc: url and user/password instead of a JNDI name.
035 */
036public class DataSourceFromUrl implements DataSource {
037
038    private final String url;
039
040    private final String user;
041
042    private final String password;
043
044    public DataSourceFromUrl(String url, String user, String password) {
045        this.url = Framework.expandVars(url);
046        this.user = Framework.expandVars(user);
047        this.password = Framework.expandVars(password);
048    }
049
050    public DataSourceFromUrl(String url, String user, String password, String driver) {
051        this(url, user, password);
052        if (driver != null) {
053            driver = Framework.expandVars(driver);
054            try {
055                // Driver registration is automatic since Java 6, provided
056                // the JDBC library is correctly written. If it's not, then
057                // this can still be useful.
058                Class.forName(driver);
059            } catch (ClassNotFoundException e) {
060                throw new RuntimeException("Driver class not found: " + driver, e);
061            }
062        }
063    }
064
065    @Override
066    public Connection getConnection() throws SQLException {
067        Connection con = JDBCUtils.getConnection(url, user, password);
068        con.setAutoCommit(true); // default
069        return con;
070    }
071
072    @Override
073    public Connection getConnection(String username, String password) throws SQLException {
074        throw new UnsupportedOperationException();
075    }
076
077    @Override
078    public PrintWriter getLogWriter() throws SQLException {
079        throw new UnsupportedOperationException();
080    }
081
082    @Override
083    public int getLoginTimeout() throws SQLException {
084        throw new UnsupportedOperationException();
085    }
086
087    @Override
088    public void setLogWriter(PrintWriter out) throws SQLException {
089        throw new UnsupportedOperationException();
090    }
091
092    @Override
093    public void setLoginTimeout(int seconds) throws SQLException {
094        throw new UnsupportedOperationException();
095    }
096
097    // @Override in CommonDataSource for Java SE 7 / JDBC 4.1
098    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
099        throw new SQLFeatureNotSupportedException();
100    }
101
102    @Override
103    public boolean isWrapperFor(Class<?> iface) throws SQLException {
104        throw new UnsupportedOperationException();
105    }
106
107    @Override
108    public <T> T unwrap(Class<T> iface) throws SQLException {
109        throw new UnsupportedOperationException();
110    }
111
112}