package com.eactive.eai.common.jdbc;

import java.io.Serializable;

import java.sql.*;

import java.util.*;
import java.util.logging.Logger;

import javax.naming.*;

import javax.sql.*;


public class DataSourceProxy implements DataSource, Serializable {
	private static final long serialVersionUID = 1L;
	final static String WEBLOGIC_INITIAL_CONTEXT_FACTORY = "weblogic.jndi.WLInitialContextFactory";
    private DataSource ds;

    public DataSourceProxy() {
    }

    public DataSourceProxy(String t3URL, String jndiName) {
        try {
            Hashtable<String, String> env = new Hashtable<String, String>();
            env.put(Context.INITIAL_CONTEXT_FACTORY,
                WEBLOGIC_INITIAL_CONTEXT_FACTORY);
            env.put(Context.PROVIDER_URL, t3URL);

            Context ctx = new InitialContext(env);
            ds = (DataSource) ctx.lookup(jndiName);
            /*
            if (!(ds instanceof com.hicall.framework.db.jdbc.DataSourceProxy)) {
                ctx.rebind(jndiName, this);
                System.out.println(
                    "com.hicall.framework.db.jdbc.DataSourceProxy is rebined. - " +
                    jndiName);
            }
            */
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Connection getConnection() throws SQLException {
        return new ConnectionProxy(ds);
    }

    public Connection getConnection(String username, String password)
        throws SQLException {
        return new ConnectionProxy(ds, username, password);
    }

    public java.io.PrintWriter getLogWriter() throws SQLException {
        return ds.getLogWriter();
    }

    public void setLogWriter(java.io.PrintWriter out) throws SQLException {
        ds.setLogWriter(out);
    }

    public void setLoginTimeout(int seconds) throws SQLException {
        ds.setLoginTimeout(seconds);
    }

    public int getLoginTimeout() throws SQLException {
        return ds.getLoginTimeout();
    }

	public <T> T unwrap(Class<T> iface) throws SQLException {
		return null;
	}

	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return false;
	}

	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		return null;
	}

}
