/**
 * 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 org.apache.commons.net.ftp.FTPClient;
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 FTPClientPoolFactory implements FactoryBean<GenericObjectPool<FTPClient>>, DisposableBean, InitializingBean {
	private FTPClientFactory clientFactory;

	private GenericObjectPool<FTPClient> pool;

	private GenericObjectPoolConfig<FTPClient> config = null;

	/**
	 * @see FactoryBean#getObject()
	 */
	@Override
	public GenericObjectPool<FTPClient> getObject() throws Exception {

		return pool;
	}

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

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

	/**
	 * @return the clientFactory
	 */
	public FTPClientFactory getClientFactory() {
		return clientFactory;
	}

	/**
	 * @param clientFactory the clientFactory to set
	 */
	public void setClientFactory(FTPClientFactory clientFactory) {
		this.clientFactory = clientFactory;
	}

	/**
	 * @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<FTPClient>();
			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<FTPClient>(clientFactory, config);
	}

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

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

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

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

}
