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

import com.crunii.micro.common.ftp.ChannelSftpFactory;
import com.crunii.micro.common.ftp.ChannelSftpPoolFactory;
import com.crunii.micro.common.ftp.RemoteFile;
import com.crunii.micro.common.ftp.SftpRemoteFile;
import com.crunii.micro.common.pool.PoolConfig;
import com.jcraft.jsch.ChannelSftp;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 田平 create 2019年5月24日下午2:47:05
 */
@Configuration
@ConditionalOnProperty(name = "spring.sftp.host")
public class SftpRemoteFileConfiguration {

	@Value("${spring.sftp.host}")
	private String sftpHost;
	@Value("${spring.sftp.port}")
	private int sftpPort;
	@Value("${spring.sftp.username}")
	private String sftpUsername;
	@Value("${spring.sftp.password}")
	private String sftpPassword;

	@Bean(name = "remoteFile")
	public RemoteFile regSftpRemoteFile() throws Exception {
		SftpRemoteFile remoteFile = new SftpRemoteFile(initChannelSftpPoolFactory().getObject());
		return remoteFile;
	}

	@Bean(name = "channelSftpPoolFactory")
	public ChannelSftpPoolFactory initChannelSftpPoolFactory() {
		ChannelSftpPoolFactory poolFactory = new ChannelSftpPoolFactory();
		poolFactory.setConfig(initSftpPoolConf());
		poolFactory.setChannelSftpFactory(initChannelSftpFactory());
		return poolFactory;
	}

	@Bean(name = "sftpPoolConf")
	public GenericObjectPoolConfig<ChannelSftp> initSftpPoolConf() {
		GenericObjectPoolConfig<ChannelSftp> config = new GenericObjectPoolConfig<ChannelSftp>();
		// sftp的最大channel数不要设置太大，ssh服务一般有限制
		config.setMaxTotal(20);
		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);
		return config;
	}

	@Bean(name = "channelSftpFactory")
	public ChannelSftpFactory initChannelSftpFactory() {
		ChannelSftpFactory channelSftpFactory = new ChannelSftpFactory();
		channelSftpFactory.setHost(sftpHost);
		channelSftpFactory.setPort(sftpPort);
		channelSftpFactory.setUsername(sftpUsername);
		channelSftpFactory.setPassword(sftpPassword);
		return channelSftpFactory;
	}
}
