yangys
2024-03-31 153d165114fb17722853629dfdc9c1d59b73e439
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.qianwen.core.launch.props;
 
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import com.qianwen.core.launch.constant.NacosConstant;
import com.qianwen.core.launch.constant.TokenConstant;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.core.env.EnvironmentCapable;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
 
@ConfigurationProperties(NacosConstant.NACOS_CONFIG_PREFIX)
public class BladeProperties implements EnvironmentAware, EnvironmentCapable {
    @Nullable
    private Environment environment;
    private final Map<String, String> prop = new HashMap<>();
 
    public Map<String, String> getProp() {
        return this.prop;
    }
 
    @Nullable
    public String get(String key) {
        return get(key, null);
    }
 
    @Nullable
    public String get(String key, @Nullable String defaultValue) {
        String value = this.prop.get(key);
        if (value == null) {
            return defaultValue;
        }
        return value;
    }
 
    @Nullable
    public Integer getInt(String key) {
        return getInt(key, null);
    }
 
    @Nullable
    public Integer getInt(String key, @Nullable Integer defaultValue) {
        String value = this.prop.get(key);
        if (value != null) {
            return Integer.valueOf(value.trim());
        }
        return defaultValue;
    }
 
    @Nullable
    public Long getLong(String key) {
        return getLong(key, null);
    }
 
    @Nullable
    public Long getLong(String key, @Nullable Long defaultValue) {
        String value = this.prop.get(key);
        if (value != null) {
            return Long.valueOf(value.trim());
        }
        return defaultValue;
    }
 
    @Nullable
    public Boolean getBoolean(String key) {
        return getBoolean(key, null);
    }
 
    @Nullable
    public Boolean getBoolean(String key, @Nullable Boolean defaultValue) {
        String value = this.prop.get(key);
        if (value != null) {
            return Boolean.valueOf(Boolean.parseBoolean(value.toLowerCase().trim()));
        }
        return defaultValue;
    }
 
    @Nullable
    public Double getDouble(String key) {
        return getDouble(key, null);
    }
 
    @Nullable
    public Double getDouble(String key, @Nullable Double defaultValue) {
        String value = this.prop.get(key);
        if (value != null) {
            return Double.valueOf(Double.parseDouble(value.trim()));
        }
        return defaultValue;
    }
 
    public boolean containsKey(String key) {
        return this.prop.containsKey(key);
    }
 
    public String getEnv() {
        Objects.requireNonNull(this.environment, "Spring boot 环境下 Environment 不可能为null");
        String env = this.environment.getProperty("blade.env");
        Assert.notNull(env, "请使用 BladeApplication 启动...");
        return env;
    }
 
    public String getName() {
        Objects.requireNonNull(this.environment, "Spring boot 环境下 Environment 不可能为null");
        return this.environment.getProperty("spring.application.name", this.environment.getProperty("blade.name", TokenConstant.DEFAULT_AVATAR));
    }
 
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }
 
    public Environment getEnvironment() {
        Objects.requireNonNull(this.environment, "Spring boot 环境下 Environment 不可能为null");
        return this.environment;
    }
}