All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.taotao.boot.oss.sftp.SftpOssConfiguration Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.taotao.boot.oss.sftp;

import com.jcraft.jsch.ChannelSftp;
import com.taotao.boot.common.constant.StarterName;
import com.taotao.boot.common.utils.log.LogUtils;
import com.taotao.boot.oss.common.condition.ConditionalOnOssEnabled;
import com.taotao.boot.oss.common.propeties.OssProperties;
import com.taotao.boot.oss.common.service.StandardOssClient;
import org.dromara.hutool.extra.spring.SpringUtil;
import org.dromara.hutool.extra.ssh.engine.jsch.JschSftp;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import java.util.Map;

/**
 * sftp oss配置
 *
 * @author shuigedeng
 * @version 2022.04
 * @since 2022-04-27 17:42:59
 */
@AutoConfiguration
@ConditionalOnOssEnabled
@ConditionalOnClass(ChannelSftp.class)
@EnableConfigurationProperties({SftpOssProperties.class})
@ConditionalOnProperty(prefix = OssProperties.PREFIX, name = "type", havingValue = "sftp")
public class SftpOssConfiguration implements InitializingBean {

	public SftpOssConfiguration(SftpOssProperties sftpOssProperties) {
		this.sftpOssProperties = sftpOssProperties;
	}

	@Override
    public void afterPropertiesSet() throws Exception {
        LogUtils.started(SftpOssConfiguration.class, StarterName.OSS_SFTP_STARTER);
    }

    public static final String DEFAULT_BEAN_NAME = "sftpOssClient";

    private final SftpOssProperties sftpOssProperties;

    @Bean
    @ConditionalOnMissingBean
    public StandardOssClient sftpOssClient() {
        Map sftpOssConfigMap = sftpOssProperties.getOssConfig();
        if (sftpOssConfigMap.isEmpty()) {
            SpringUtil.registerBean(DEFAULT_BEAN_NAME, sftpOssClient(sftpOssProperties));
        } else {
            sftpOssConfigMap.forEach(
                    (name, sftpOssConfig) -> SpringUtil.registerBean(name, sftpOssClient(sftpOssConfig)));
        }
        return null;
    }

    public StandardOssClient sftpOssClient(SftpOssConfig sftpOssConfig) {
        return new SftpOssClient(sftp(sftpOssConfig), sftpOssConfig);
    }

    public JschSftp sftp(SftpOssConfig sftpOssConfig) {
        return new JschSftp(sftpOssConfig);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy