package ws.prova;

/*
 * Copyright (C) 1999-2005 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
 * Copyright (C) 2002-2007 <a href="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * A relational data source used via the standard Prova rule base utils.prova.
 *
 * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
 *         Prova complete re-write
 * @author <A HREF="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</A>
 * @author <A HREF="mailto:simon.beaumont@mac.com">Simon Beaumont</A>
 *         Rewrite to make this a transparent bean like object with no logic regarding
 *         connection url semantics.
 * @version 2.0 <25 March 2007>
 */
public class DataSource implements javax.sql.DataSource {
	private String uri;
	private int loginTimeout = 0;
	private PrintWriter logWriter;

	/**
	 * Constructor.
	 */
	public DataSource(String uri) {
		this.uri = uri;
	}

	/**
	 * Get a database connection.
	 *
	 * @return a database connection
	 */
	public Connection getConnection() throws SQLException {
		return DriverManager.getConnection(uri);
	}

	/**
	 * Get a database connection.
	 *
	 * @param userName a user name
	 * @param password a password
	 * @return a database connection
	 */
	public Connection getConnection(String userName, String password) throws SQLException {
		return DriverManager.getConnection(uri, userName, password);
	}

	/**
	 * Get the login timeout.
	 */
	public int getLoginTimeout() throws SQLException {
		return loginTimeout;
	}

	/**
	 * Get the log writer.
	 */
	public PrintWriter getLogWriter() throws SQLException {
		return logWriter;
	}

	/**
	 * Set the login timeout.
	 *
	 * @param millis the time in milli seconds
	 */
	public void setLoginTimeout(int millis) throws SQLException {
		loginTimeout = millis;
	}

	/**
	 * Set the log writer.
	 *
	 * @param writer a writer
	 */
	public void setLogWriter(PrintWriter writer) throws SQLException {
		logWriter = writer;
	}

	/**
	 * Compares objects.
	 *
	 * @param obj another object
	 * @return a boolean
	 */
	public boolean equals(Object obj) {
		if (obj instanceof DataSource) {
			DataSource ds = (DataSource) obj;
			return (ds.uri.equals(uri));
		} else
			return false;
	}

	public int hashCode() {
		return uri.hashCode();
	}

    public Object unwrap(Class iface) throws SQLException {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public boolean isWrapperFor(Class iface) throws SQLException {
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
