package com.iamberry.wechat.tools;

import java.io.IOException;
import java.util.Properties;


/**
 * description ： 加载jdbc的配置文件 
 * @author 何秀刚
 * create date：2016年3月23日
 */
public class DataSourceManager {
	
	
	private String jdbcDriver; // driver.class

	private String jdbcUserName; // 用户名
	
	private String jdbcPassword; // 密码
	
	private String jdbcUrl; // url
	
	private Integer minPoolSize = 5; // 数据库连接处最小保持连接数,默认为5
	
	private Integer maxPoolSize = 100; // 数据库连接最多保持连接数,默认为15, 保证系统还有活跃链接50
	
	private Integer initialPoolSize = 3; // 初始化连接池数据,默认为3，在minPoolSize-maxPoolSize之间取值
	
	private Integer maxIdleTime = 0; // 最大空闲时间，60秒内未使用则连接被丢弃。若为0则永不丢弃。默认值: 0
	
	private Integer checkoutTimeout = 0;// 当连接池连接耗尽时，客户端调用getConnection()后等待获取新连接的时间，超时后将抛出SQLException，如设为0则无限期等待。单位毫秒。默认: 0
	
	private Integer acquireIncrement = 3; // 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。默认值: 3
	
	private Integer acquireRetryAttempts = 30; // 定义在从数据库获取新连接失败后重复尝试的次数。默认值: 30 ；小于等于0表示无限次
	
	private Integer acquireRetryDelay = 1000; // 重新尝试的时间间隔，默认为：1000毫秒
	
	private Boolean autoCommitOnClose = false; // 关闭连接时，是否提交未提交的事务，默认为false，即关闭连接，回滚未提交的事务
	
	private String automaticTestTable = "tb_ratfw_table_text"; // c3p0将建一张名为Test的空表，并使用其自带的查询语句进行测试。如果定义了这个参数那么属性preferredTestQuery将被忽略。你不能在这张Test表上进行任何操作，它将只供c3p0测试使用。默认值: null
	
	private Boolean breakAfterAcquireFailure = false; // 如果为false，则获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常，但是数据源仍有效保留，并在下次调用getConnection()的时候继续尝试获取连接。如果设为true，那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。默认: false
	
	private Integer idleConnectionTestPeriod = 0; // 每60秒检查所有连接池中的空闲连接。默认值: 0，不检查 
	
	private Integer maxStatements = 0; // c3p0全局的PreparedStatements缓存的大小。如果maxStatements与maxStatementsPerConnection均为0，则缓存不生效，只要有一个不为0，则语句的缓存就能生效。如果默认值: 0
	
	private Integer maxStatementsPerConnection = 0; // maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。默认值: 0
	
	public Integer getMinPoolSize() {
		return minPoolSize;
	}

	public void setMinPoolSize(Integer minPoolSize) {
		if (minPoolSize != null)
			this.minPoolSize = minPoolSize;
	}

	public Integer getMaxPoolSize() {
		return maxPoolSize;
	}

	public void setMaxPoolSize(Integer maxPoolSize) {
		if (maxPoolSize != null)
			this.maxPoolSize = maxPoolSize;
	}

	public Integer getInitialPoolSize() {
		return initialPoolSize;
	}

	public void setInitialPoolSize(Integer initialPoolSize) {
		if (initialPoolSize != null)
			this.initialPoolSize = initialPoolSize;
	}

	public Integer getMaxIdleTime() {
		return maxIdleTime;
	}

	public void setMaxIdleTime(Integer maxIdleTime) {
		if (maxIdleTime != null)
			this.maxIdleTime = maxIdleTime;
	}

	public Integer getCheckoutTimeout() {
		return checkoutTimeout;
	}

	public void setCheckoutTimeout(Integer checkoutTimeout) {
		if (checkoutTimeout != null)
			this.checkoutTimeout = checkoutTimeout;
	}

	public Integer getAcquireIncrement() {
		return acquireIncrement;
	}

	public void setAcquireIncrement(Integer acquireIncrement) {
		if (acquireIncrement != null)
			this.acquireIncrement = acquireIncrement;
	}

	public Integer getAcquireRetryAttempts() {
		return acquireRetryAttempts;
	}

	public void setAcquireRetryAttempts(Integer acquireRetryAttempts) {
		if (acquireRetryAttempts != null)
			this.acquireRetryAttempts = acquireRetryAttempts;
	}

	public Integer getAcquireRetryDelay() {
		return acquireRetryDelay;
	}

	public void setAcquireRetryDelay(Integer acquireRetryDelay) {
		if (acquireRetryDelay != null)
			this.acquireRetryDelay = acquireRetryDelay;
	}

	public boolean isAutoCommitOnClose() {
		return autoCommitOnClose;
	}

	public void setAutoCommitOnClose(Boolean autoCommitOnClose) {
		if (autoCommitOnClose != null)
			this.autoCommitOnClose = autoCommitOnClose;
	}

	public String getAutomaticTestTable() {
		return automaticTestTable;
	}

	public void setAutomaticTestTable(String automaticTestTable) {
		if (automaticTestTable != null && !automaticTestTable.isEmpty())
			this.automaticTestTable = automaticTestTable;
	}

	public boolean isBreakAfterAcquireFailure() {
		return breakAfterAcquireFailure;
	}

	public void setBreakAfterAcquireFailure(Boolean breakAfterAcquireFailure) {
		if (breakAfterAcquireFailure != null)
			this.breakAfterAcquireFailure = breakAfterAcquireFailure;
	}

	public Integer getIdleConnectionTestPeriod() {
		return idleConnectionTestPeriod;
	}

	public void setIdleConnectionTestPeriod(Integer idleConnectionTestPeriod) {
		if(idleConnectionTestPeriod != null)
			this.idleConnectionTestPeriod = idleConnectionTestPeriod;
	}

	public Integer getMaxStatements() {
		return maxStatements;
	}

	public void setMaxStatements(Integer maxStatements) {
		if (maxStatements != null)
			this.maxStatements = maxStatements;
	}

	public Integer getMaxStatementsPerConnection() {
		return maxStatementsPerConnection;
	}

	public void setMaxStatementsPerConnection(Integer maxStatementsPerConnection) {
		if (maxStatementsPerConnection != null)
			this.maxStatementsPerConnection = maxStatementsPerConnection;
	}

	public String getJdbcDriver() {
		return jdbcDriver;
	}

	
	

	public void setJdbcDriver(String jdbcDriver) {
		this.jdbcDriver = jdbcDriver;
	}

	public String getJdbcUserName() {
		return jdbcUserName;
	}

	public void setJdbcUserName(String jdbcUserName) {
		this.jdbcUserName = jdbcUserName;
	}

	public String getJdbcPassword() {
		return jdbcPassword;
	}

	public void setJdbcPassword(String jdbcPassword) {
		this.jdbcPassword = jdbcPassword;
	}

	public String getJdbcUrl() {
		return jdbcUrl;
	}

	public void setJdbcUrl(String jdbcUrl) {
		this.jdbcUrl = jdbcUrl;
	}


	public DataSourceManager () {
		Properties prop = new Properties();
		try {
			prop.load(this.getClass().getClassLoader().getResourceAsStream("jdbc.properties"));
			jdbcDriver = prop.getProperty("jdbc.driver");
			jdbcPassword = prop.getProperty("jdbc.password");
			jdbcUserName = prop.getProperty("jdbc.username");
			jdbcUrl = prop.getProperty("jdbc.url");
		} catch (IOException e) {
			// TODO Auto-generated catch blockr
			jdbcDriver = "com.mysql.jdbc.Driver";
			jdbcPassword = "acoolrat";
			jdbcUserName = "root";
			jdbcUrl = "jdbc:mysql://120.25.79.106:3306/ratfw?useUnicode=true&amp;characterEncoding=UTF-8&amp;allowMultiQueries=true";
		}
	}
}
