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

top.jfunc.common.propenv.BaseEnvStream Maven / Gradle / Ivy

There is a newer version: 1.8.5
Show newest version
package top.jfunc.common.propenv;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.jfunc.common.utils.FileUtil;

import java.io.File;
import java.io.InputStream;

/**
 * 根据环境变量env加载文件(-Denv=test),从类路径下或者给定文件路径寻找,首先寻找${fileName}-${env}.ext,没找到就寻找找${env}/${fileName}.ext,再没找到就找找${env}/${fileName}.ext。
 * 没有设置env环境变量的时候,给什么路径就从什么路径找
 * @author 熊诗言
 * @see ClasspathEnvStream
 * @see FileEnvStream
 * @see EnvStreamFactory
 */
public abstract class BaseEnvStream {
    private static final Logger logger = LoggerFactory.getLogger(BaseEnvStream.class);
    private static final String ENV = EnvUtil.env();

    /**
     * @param fileName 可以是 jdbc.properties 或者 config/jdbc.properties fileName必须带有扩展名
     * @return InputStream
     */
    public  InputStream loadEnvInputStream(String fileName){
        InputStream inputStream = null;
        if(ENV == null){
            logger.info("env = null , try to load " + fileName);
            inputStream = loadInputStream(fileName);//给什么路径就从什么路径找
            if(inputStream == null){
                throw new IllegalArgumentException("Properties file not found : " + fileName );
            }
        }else {
            String envFileName = FileUtil.getFileNameNoEx(fileName) + "-" + ENV + "." + FileUtil.getExtensionName(fileName);
            logger.info("env = " + ENV + " , try to load " + envFileName);
            inputStream = loadInputStream(envFileName);//找${fileName}-${env}.ext
            if (inputStream == null) {
                String envDirName = ENV + File.separator + fileName;
                logger.info("env = " + ENV + " , try to load " + envDirName);
                inputStream = loadInputStream(envDirName);//找${env}/${fileName}.ext
                if(inputStream == null){
                    logger.info("env = " + ENV + " , try to load " + fileName);
                    inputStream = loadInputStream(fileName);//给什么路径就从什么路径找
                    if(inputStream == null){
                        throw new IllegalArgumentException("Properties file not found : " + fileName + ", and " + envFileName + ",and " + envDirName);
                    }
                }

            }
        }
        return  inputStream;
    }

    /**
     * 子类具体实现获取inputStream的方法
     * @param fileName 文件名
     * @return 没找到就返回null
     */
    protected abstract InputStream loadInputStream(String fileName);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy