package org.jfox.jdbc.xa;

import java.sql.Connection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.huihoo.jfox.logging.Logger;

/**
 * 负责管理 DataSource
 *
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class XADataSourceManager {

  private static Map dataSources = Collections.synchronizedMap(new HashMap());
  private static Logger logger = Logger.getLogger(XADataSourceManager.class.getName());

  public static void registerDataSource(String name, TxDataSource txds){
    logger.debug("registerDataSource " + name + " => " + txds);
    dataSources.put(name,txds);
  }

  public static void unregisterDataSource(String name){
    logger.debug("unregisterDataSource " + name);
    dataSources.remove(name);
  }

  public static TxDataSource getDataSource(String name) {
    logger.debug("getDataSource " + name);
    if(!existsDataSource(name)) {
      logger.warn("XADataSource " + name + " is not exists!");
      return null;
    }
    return (TxDataSource)dataSources.get(name);
  }

  public static TxDataSource createDataSource(String dsName, String dbUrl, String user, String password){
    return createDataSource(dsName,dbUrl,user,password,Connection.TRANSACTION_READ_COMMITTED);
  }
  /**
   * 如果存在该 dsName,直接返回,不存在,则生成一个
   * DB2_URL: jdbc:db2://localhost;databaseName=SAMPLE
   * ORACLE_URL: jdbc:oracle:thin:@localhost:1521:yang
   */
  public static TxDataSource createDataSource(String dsName, String dbUrl, String user, String password, int transactionIsolate){
    logger.debug("createDataSource [DataSourceName=" + dsName + ", URL=" + dbUrl + ", user=" + user +", password=<hidden>, TransactionIsolate=" + transactionIsolate +"]");
    if(existsDataSource(dsName)){
      TxDataSource txds = getDataSource(dsName);
      if(!txds.getDbUrl().equals(dbUrl)
              || !txds.getUser().equals(user)
              || !txds.getPassword().equals(password)
              || txds.getTransactionIsolation() != transactionIsolate) {
        logger.warn("can not create DataSource, " + dsName + " is exists, but not with same configuration");
        return null;
      }
      txds.setTransactionIsolation(transactionIsolate);
      return txds;
    }
    else {
      TxDataSource tds = newDataSource(dbUrl,user,password);
      tds.setTransactionIsolation(transactionIsolate);
      registerDataSource(dsName,tds);
      return tds;
    }
  }

  public static TxDataSource newDataSource(String dbUrl, String user, String password){
    return newDataSource(dbUrl,user,password,Connection.TRANSACTION_READ_COMMITTED);
  }

  /**
   * 生成一个新的 TxDataSource,不注册
   * DB2_URL: jdbc:db2://localhost;databaseName=SAMPLE
   * ORACLE_URL: jdbc:oracle:thin:@localhost:1521:yang
   */
  public static TxDataSource newDataSource(String dbUrl, String user, String password, int transactionIsolate){
    logger.debug("createDataSource [URL=" + dbUrl + ", user=" + user +", password=<hidden>, TransactionIsolate=" + transactionIsolate +"]");
    TxDataSource txds = new TxDataSource(dbUrl,user,password);
    txds.setTransactionIsolation(transactionIsolate);
    return txds;
  }

  public static boolean existsDataSource(String name){
    if(name == null){
      return false;
    }
    return dataSources.containsKey(name);
  }

  public static int getDataSourceCount(){
    return dataSources.size();
  }

  public static void main(String[] args) {

  }
}