欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PropertyUtils 文件解读

程序员文章站 2022-03-04 10:21:02
...
单例模式 ,静态代码块
/**
 * property utils
 * single instance
 */
public class PropertyUtils {

    /**
     * logger
     */
private static final Logger logger = LoggerFactory.getLogger(PropertyUtils.class);

    private static final Properties properties = new Properties();

    private PropertyUtils() {
        throw new UnsupportedOperationException("Construct PropertyUtils");
    }

    static {
        String[] propertyFiles = new String[]{"/common.properties"};
        for (String fileName : propertyFiles) {
            InputStream fis = null;
            try {
                fis = PropertyUtils.class.getResourceAsStream(fileName);
                properties.load(fis);

            } catch (IOException e) {
                logger.error(e.getMessage(), e);
                if (fis != null) {
                    IOUtils.closeQuietly(fis);
                }
                System.exit(1);
            } finally {
                IOUtils.closeQuietly(fis);
            }
        }
    }

    /**
     *
     * @return  judge whether resource upload startup
     */
public static Boolean getResUploadStartupState(){
        // HDFS 或者S3 资源是否启用
String resUploadStartupType = PropertyUtils.getUpperCaseString("resource.storage.type");
        ResUploadType resUploadType = ResUploadType.valueOf(resUploadStartupType);
        return resUploadType == ResUploadType.HDFS || resUploadType == ResUploadType.S3;
    }
   // 获取指定key或者获取不到赋予默认值省略
}


相关存储枚举如下:

/**
 * data base types
 */
public enum ResUploadType {
  /**
   * 0 hdfs
   * 1 s3
   * 2 none
   */
HDFS,S3,NONE
}
代码来源于dolphinscheduler 的1.36版本