/*
* RHQ Management Platform
* Copyright (C) 2005-2013 Red Hat, Inc.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package org.rhq.plugins.database;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;
import java.util.logging.Logger;
import javax.sql.DataSource;
/**
* Works with BoneCP to provide connections, because DriverManager won't load the class correctly through BoneCP
* (the actual driver class will be loaded by concrete database plugins).
*/
class DriverDataSource implements DataSource {
private final Properties properties;
private final Driver driver;
private final String url;
private PrintWriter out = new PrintWriter(System.out);
DriverDataSource(Driver driver, String url, Properties properties) {
this.driver = driver;
this.url = url;
if (properties == null) {
properties = new Properties();
}
this.properties = properties;
}
@Override
public PrintWriter getLogWriter() throws SQLException {
return out;
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
this.out = out;
}
@Override
public void setLoginTimeout(int seconds) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public int getLoginTimeout() throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public Connection getConnection() throws SQLException {
return driver.connect(url, properties);
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
Properties p = new Properties(properties);
if (username != null) {
p.put("user", username);
}
if (password != null) {
p.put("password", password);
}
return driver.connect(url, p);
}
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
throw new SQLFeatureNotSupportedException();
}
}