/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */

package org.jfox.jdbc.datasource;

import java.sql.Connection;

import org.jfox.framework.extra.ActiveComponent;
import org.jfox.framework.extra.Managable;
import org.jfox.framework.extra.ManagementInfo;
import org.jfox.jndi.InitialContextHelper;
import org.jfox.system.AbstractService;

/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class PoolDataSourceService extends AbstractService implements Managable, ActiveComponent {
    private PoolDataSource pds = null;

    private String dbDriver; // database driver class name.
    private String dbURL; // database url
    private String user; // database user
    private String password; //database password
    private String dsName = null;
    private int initNum = 2;
    private int maxRest = 10;

    private String jndiPrefix = "";

    public PoolDataSourceService(String dsName, String dbDriver, String dbURL, String user, String password) {
        this.dsName = dsName;
        this.dbDriver = dbDriver;
        this.dbURL = dbURL;
        this.user = user;
        this.password = password;
    }

    public String getDbDriver() {
        return dbDriver;
    }

    public void setDbDriver(String dbDriver) {
        this.dbDriver = dbDriver;
    }

    public String getDbUrl() {
        return dbURL;
    }

    public void setDbUrl(String dbURL) {
        this.dbURL = dbURL;
    }

    public void setDsName(String dsName) {
        this.dsName = dsName;
    }

    public String getDsName() {
        return dsName;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getMaxRest() {
        return maxRest;
    }

    public int getInitNum() {
        return initNum;
    }

    public void setInitNum(int initNum) {
        this.initNum = initNum;
    }

    public void setMaxRest(int maxRest) {
        this.maxRest = maxRest;
    }

    protected void doInit() throws Exception {
        pds = new PoolDataSource(dsName, dbDriver, dbURL, user, password);
        pds.setInitNum(initNum);
        pds.setMaxRest(maxRest);
//    pds.init();
    }

    protected void doStart() throws Exception {
        InitialContextHelper.getInitialContext().bind(getJndiName(), pds);
    }

    protected void doStop() throws Exception {
        InitialContextHelper.getInitialContext().unbind(getJndiName());
    }

    protected void doDestroy() throws Exception {
//    pds.destroy();
    }

    public String getJndiPrefix() {
        return jndiPrefix;
    }

    public void setJndiPrefix(String jndiPrefix) {
        this.jndiPrefix = jndiPrefix.replace('/', '_');
    }

    public void run() {
    }

    public boolean testConnection() {
        try {
            PoolDataSource pds = (PoolDataSource) javax.rmi.PortableRemoteObject.narrow(InitialContextHelper.getInitialContext().lookup(getJndiName()), PoolDataSource.class);
            Connection conn = pds.getConnection();
            // close will restore the connection to connection pool
            conn.close();
            pds.destroy();
            return true;
        }
        catch(Exception e) {
            logger.warn("get connection error", e);
            return false;
        }
    }

    public ManagementInfo getManagementInfo() {
        return ManagementInfo.exposePublic(this.getClass());
    }

    public String getJndiName() {
        return getJndiPrefix() + "/" + getDsName();
    }

    public static void main(String[] args) {

    }

}
