WangHan
2024-09-12 d5855a4926926698b740bc6c7ba489de47adb68b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package tech.powerjob.common.utils;
 
import org.apache.commons.lang3.StringUtils;
 
/**
 * PropertyUtils
 *
 * @author tjq
 * @since 2023/7/15
 */
public class PropertyUtils {
 
    public static String readProperty(String key, String defaultValue) {
        // 从启动参数读取
        String property = System.getProperty(key);
        if (StringUtils.isNotEmpty(property)) {
            return property;
        }
 
        // 从 ENV 读取
        property= System.getenv(key);
        if (StringUtils.isNotEmpty(property)) {
            return property;
        }
        // 部分操作系统不兼容 a.b.c 的环境变量,转换为 a_b_c 再取一次,即 PowerJob 支持 2 种类型的环境变量 key
        property = System.getenv(key.replaceAll("\\.", "_"));
        if (StringUtils.isNotEmpty(property)) {
            return property;
        }
        return defaultValue;
    }
}