/* 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.xa;

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 TxDataSourceService extends AbstractService implements Managable, ActiveComponent {
    private TxDataSource txds = null;

    private String dsName = null;
    private String dbUrl = null;
    private String user = null;
    private String password = null;
    private int initNum = 5;
    private int maxRest = 10;
    private long timeout = 10 * 60 * 1000;
    private int transactionIsolation = Connection.TRANSACTION_READ_COMMITTED;

    private String jndiPrefix = "";

    public TxDataSourceService(String dsName, String dbUrl, String user, String password) {
        this.dsName = dsName;
        this.dbUrl = dbUrl;
        this.user = user;
        this.password = password;
    }

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

    public String getDsName() {
        return dsName;
    }

    public String getDbUrl() {
        return dbUrl;
    }

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

    public String getUser() {
        return user;
    }

    public String getPassword() {
        return password;
    }

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

    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;
    }

    public void setTransactionIsolation(int level) {
        transactionIsolation = level;
    }

    public int getTransactionIsolation() {
        return transactionIsolation;
    }

    public long getTimeout() {
        return timeout;
    }

    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }

    protected void doInit() throws Exception {
        if(dsName == null) {
            throw new Exception("dsName can not be null");
        }
        if(dbUrl == null) {
            throw new Exception("dbUrl can not be null");
        }
        if(user == null) {
            throw new Exception("user can not be null");
        }
        if(password == null) {
            throw new Exception("password can not be null");
        }
        txds = XADataSourceManager.createDataSource(dsName, dbUrl, user, password, transactionIsolation);
        txds.setInitNum(initNum);
        txds.setMaxRest(maxRest);
        txds.setTimeout(timeout);
        // 在服务端没有必要初始化，由客户端 lookup 到本地初始化
//      txds.init();

    }

    protected void doStart() throws Exception {
        InitialContextHelper.getInitialContext().rebind(getJndiName(), txds);
    }

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

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

    public void run() {
    }

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

    public String getJndiPrefix() {
        return jndiPrefix;
    }

    public void setJndiPrefix(String jndiPrefix) {
        this.jndiPrefix = jndiPrefix;
    }

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

    public static void main(String[] args) {

    }
}
