/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.cqcis.com</a>
 */
package com.crunii.micro.common.ftp;

import com.crunii.micro.common.pool.PoolConfig;
import com.jcraft.jsch.ChannelSftp;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

/**
 * @author 田平 create 2016-6-14下午4:40:58
 */
public class ChannelSftpPoolFactory implements FactoryBean<GenericObjectPool<ChannelSftp>>, DisposableBean, InitializingBean {
	private ChannelSftpFactory channelSftpFactory;

	private GenericObjectPool<ChannelSftp> pool;

	private GenericObjectPoolConfig<ChannelSftp> config = null;

	/**
	 * @see FactoryBean#getObject()
	 */
	@Override
	public GenericObjectPool<ChannelSftp> getObject() throws Exception {
		return pool;
	}

	/**
	 * @see FactoryBean#getObjectType()
	 */
	@Override
	public Class<?> getObjectType() {
		return GenericObjectPool.class;
	}

	/**
	 * @see FactoryBean#isSingleton()
	 */
	@Override
	public boolean isSingleton() {
		return false;
	}

	/**
	 * @see DisposableBean#destroy()
	 */
	@Override
	public void destroy() throws Exception {
		if (pool != null) {
			pool.close();
		}

	}

	/**
	 * @see InitializingBean#afterPropertiesSet()
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		if (config == null) {
			config = new GenericObjectPoolConfig<ChannelSftp>();
			config.setMaxTotal(PoolConfig.DEFAULT_MAX_TOTAL);
			config.setMaxIdle(PoolConfig.DEFAULT_MAX_IDLE);
			config.setMinIdle(PoolConfig.DEFAULT_MIN_IDLE);
			config.setMaxWaitMillis(PoolConfig.DEFAULT_MAX_WAIT_MILLIS);
			config.setMinEvictableIdleTimeMillis(PoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
			config.setSoftMinEvictableIdleTimeMillis(PoolConfig.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
			config.setTimeBetweenEvictionRunsMillis(PoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS);
			config.setTestOnBorrow(PoolConfig.DEFAULT_TEST_ON_BORROW);
			config.setTestOnReturn(PoolConfig.DEFAULT_TEST_ON_RETURN);
			config.setTestOnCreate(PoolConfig.DEFAULT_TEST_ON_CREATE);
		}
		pool = new GenericObjectPool<ChannelSftp>(channelSftpFactory, config);
	}

	/**
	 * @return the pool
	 */
	public GenericObjectPool<ChannelSftp> getPool() {
		return pool;
	}

	/**
	 * @param pool the pool to set
	 */
	public void setPool(GenericObjectPool<ChannelSftp> pool) {
		this.pool = pool;
	}

	/**
	 * @return the config
	 */
	public GenericObjectPoolConfig<ChannelSftp> getConfig() {
		return config;
	}

	/**
	 * @param config the config to set
	 */
	public void setConfig(GenericObjectPoolConfig<ChannelSftp> config) {
		this.config = config;
	}

	/**
	 * @return the channelSftpFactory
	 */
	public ChannelSftpFactory getChannelSftpFactory() {
		return channelSftpFactory;
	}

	/**
	 * @param channelSftpFactory the channelSftpFactory to set
	 */
	public void setChannelSftpFactory(ChannelSftpFactory channelSftpFactory) {
		this.channelSftpFactory = channelSftpFactory;
	}

}
