package com.dxhy.oss.utils;

import lombok.Data;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.pool2.impl.GenericObjectPool;

@Data
public class SftpPool {

    private GenericObjectPool<FTPClient> pool;

    public SftpPool(SftpFactory factory) {
        this.pool = new GenericObjectPool<>(factory, factory.getOssConfig().getPool());
    }

    /**
     * 获取一个sftp连接对象
     *
     * @return sftp连接对象
     */
    public FTPClient borrowObject() throws SftpPoolException {
        try {
            return pool.borrowObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new SftpPoolException("获取ftp连接失败", e);
        }
    }

    /**
     * 归还一个sftp连接对象
     *
     * @param ftpClient sftp连接对象
     */
    public void returnObject(FTPClient ftpClient) {
        if (ftpClient != null) {
            pool.returnObject(ftpClient);
        }
    }

}