/* JFox, the OpenSource J2EE Application Server
 *
 * Copyright (C) 2002 huihoo.com
 * Distributable under GNU LGPL license
 * See the GNU Lesser General Public License for more details.
 */

package org.huihoo.jfox.mx;

import java.sql.Connection;
import java.util.Properties;
import javax.naming.Context;

import org.huihoo.jfox.util.InitialContextHelper;
import org.huihoo.jfox.system.State;
import org.jfox.jdbc.xa.TxDataSource;
import org.jfox.jdbc.xa.XADataSourceManager;

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

public class TxDataSourceServiceMX implements ServiceMX {
  private static Context initContext = null;
  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 int transactionIsolation = Connection.TRANSACTION_READ_COMMITTED;

  public TxDataSourceServiceMX() {

  }

  public void mx_start() {
    if(initContext == null) {
      initContext = InitialContextHelper.getInitialConext();
    }
    try {
      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);
      // 在服务端没有必要初始化,由客户端 lookup 到本地初始化
//      txds.init();
      initContext.rebind("datasource/" + dsName,txds);
    }
    catch(Exception e){
      e.printStackTrace();
    }
  }

  public void mx_stop(){
    try {
      initContext.unbind("datasource/" + dsName);
      txds.destroy();
    }
    catch(Exception e){
      e.printStackTrace();
    }
  }

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

  public String mx_getDsName() {
    return dsName;
  }

  public String mx_getDbUrl() {
    return dbUrl;
  }

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

  public String mx_getUser() {
    return user;
  }

  public String mx_getPassword() {
    return password;
  }

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

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

  public int mx_getMaxRest() {
    return maxRest;
  }

  public int mx_getInitNum(){
    return initNum;
  }

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

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

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

  public int mx_getTransactionIsolation() {
    return transactionIsolation;
  }

  public Properties getOperationDescriptions() {
    return null;
  }

  public Object getSource() {
    return txds;
  }

  public String getMxDescription() {
    return "a transactional datasource";
  }


  public State mx_getStat(){
    return txds.getState();
  }

  public static void main(String[] args) {
    
  }
}