@@ -38,66 +38,100 @@ public class CoreConfigure {
38
38
39
39
private static final String KEY_UNSAFE_ENABLE = "unsafe.enable" ;
40
40
41
- // 受保护key数组,在保护key范围之内,如果前端已经传递过参数了,只能认前端,后端无法修改
41
+ // 受保护key数组,在保护key范围之内,以用户传递的配置为准,系统配置不允许覆盖
42
42
private static final String [] PROTECT_KEY_ARRAY = {KEY_NAMESPACE , KEY_SANDBOX_HOME , KEY_LAUNCH_MODE , KEY_SERVER_IP , KEY_SERVER_PORT , KEY_SERVER_CHARSET };
43
43
44
44
// 用户配置和系统默认配置都可以,需要进行合并的key,例如user_module
45
45
private static final String [] MULTI_KEY_ARRAY = {KEY_USER_MODULE_LIB_PATH };
46
46
47
47
private static final FeatureCodec codec = new FeatureCodec (';' , '=' );
48
48
49
- private final Map <String , String > featureMap ;
49
+ private final Map <String , String > featureMap = new LinkedHashMap < String , String >() ;
50
50
51
- private CoreConfigure (final String featureString ) {
52
- this .featureMap = codec .toMap (featureString );
51
+ private CoreConfigure (final String featureString ,
52
+ final String propertiesFilePath ) {
53
+ final Map <String , String > featureMap = toFeatureMap (featureString );
54
+ final Map <String , String > propertiesMap = toPropertiesMap (propertiesFilePath );
55
+ this .featureMap .putAll (merge (featureMap , propertiesMap ));
53
56
}
54
57
55
- private static volatile CoreConfigure instance ;
56
-
57
- public static CoreConfigure toConfigure (final String featureString , final String propertiesFilePath ) {
58
- return instance = mergePropertiesFile (new CoreConfigure (featureString ), propertiesFilePath );
58
+ private Map <String , String > toFeatureMap (String featureString ) {
59
+ return codec .toMap (featureString );
59
60
}
60
61
61
- // 从配置文件中合并配置到CoreConfigure中
62
- private static CoreConfigure mergePropertiesFile (final CoreConfigure cfg , final String propertiesFilePath ) {
63
- Map <String , String > propertiesMap = propertiesToStringMap (cfg , fetchProperties (propertiesFilePath ));
64
- for (String key : MULTI_KEY_ARRAY ) {
65
- if (cfg .featureMap .containsKey (key ) && propertiesMap .containsKey (key )) {
66
- propertiesMap .put (key , cfg .featureMap .get (key ) + ";" + propertiesMap .get (key ));
67
- }
62
+ private Map <String , String > toPropertiesMap (String propertiesFilePath ) {
63
+ final Map <String , String > propertiesMap = new LinkedHashMap <String , String >();
64
+
65
+ if (null == propertiesFilePath ) {
66
+ return propertiesMap ;
68
67
}
69
- cfg .featureMap .putAll (propertiesMap );
70
- return cfg ;
71
- }
72
68
73
- // 从指定配置文件路径中获取配置信息
74
- private static Properties fetchProperties (final String propertiesFilePath ) {
69
+ final File propertiesFile = new File (propertiesFilePath );
70
+ if (!propertiesFile .exists ()
71
+ || !propertiesFile .canRead ()) {
72
+ return propertiesMap ;
73
+ }
74
+
75
+
76
+ // 从指定配置文件路径中获取配置信息
75
77
final Properties properties = new Properties ();
76
78
InputStream is = null ;
77
79
try {
78
- is = FileUtils .openInputStream (new File ( propertiesFilePath ) );
80
+ is = FileUtils .openInputStream (propertiesFile );
79
81
properties .load (is );
80
82
} catch (Throwable cause ) {
81
83
// cause.printStackTrace(System.err);
82
84
} finally {
83
85
IOUtils .closeQuietly (is );
84
86
}
85
- return properties ;
86
- }
87
87
88
- // 配置转map
89
- private static Map <String , String > propertiesToStringMap (CoreConfigure cfg , final Properties properties ) {
90
- final Map <String , String > map = new HashMap <String , String >();
88
+ // 转换为Map
91
89
for (String key : properties .stringPropertyNames ()) {
90
+ propertiesMap .put (key , properties .getProperty (key ));
91
+ }
92
+
93
+ return propertiesMap ;
94
+ }
95
+
96
+ private Map <String , String > merge (Map <String , String > featureMap , Map <String , String > propertiesMap ) {
92
97
93
- //如果受保护的key已经由入参指定,则过滤掉受保护的key,防止入参被覆盖
94
- if (cfg .featureMap .containsKey (key ) && ArrayUtils .contains (PROTECT_KEY_ARRAY , key )) {
98
+ // 以featureMap配置为准
99
+ final Map <String , String > mergeMap = new LinkedHashMap <String , String >(featureMap );
100
+
101
+ // 合并propertiesMap
102
+ for (final Map .Entry <String , String > propertiesEntry : propertiesMap .entrySet ()) {
103
+
104
+ // 如果是受保护的KEY,则以featureMap中的非空值为准
105
+ if (mergeMap .containsKey (propertiesEntry .getKey ())
106
+ && ArrayUtils .contains (PROTECT_KEY_ARRAY , propertiesEntry .getKey ())) {
95
107
continue ;
96
108
}
97
109
98
- map .put (key , properties .getProperty (key ));
110
+ // 如果是多值合并的KEY,则不进行覆盖,转为合并
111
+ else if (ArrayUtils .contains (MULTI_KEY_ARRAY , propertiesEntry .getKey ())
112
+ && mergeMap .containsKey (propertiesEntry .getKey ())) {
113
+ mergeMap .put (
114
+ propertiesEntry .getKey (),
115
+ mergeMap .get (propertiesEntry .getKey ()) + ";" + propertiesEntry .getValue ()
116
+ );
117
+ continue ;
118
+ }
119
+
120
+ // 合并K,V
121
+ else {
122
+ mergeMap .put (propertiesEntry .getKey (), propertiesEntry .getValue ());
123
+ }
124
+
99
125
}
100
- return map ;
126
+
127
+ return mergeMap ;
128
+
129
+ }
130
+
131
+ private static volatile CoreConfigure instance ;
132
+
133
+ public static CoreConfigure toConfigure (final String featureString , final String propertiesFilePath ) {
134
+ return instance = new CoreConfigure (featureString , propertiesFilePath );
101
135
}
102
136
103
137
public static CoreConfigure getInstance () {
0 commit comments