-
Notifications
You must be signed in to change notification settings - Fork 288
/
Configuration.java
1139 lines (1017 loc) · 34.6 KB
/
Configuration.java
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.wgzhao.addax.common.util;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.wgzhao.addax.common.exception.AddaxException;
import com.wgzhao.addax.common.exception.CommonErrorCode;
import com.wgzhao.addax.common.spi.ErrorCode;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Configuration 提供多级JSON配置信息无损存储 <br>
* 实例代码:<br>
* 获取job的配置信息<br>
* Configuration configuration = Configuration.from(new File("Config.json")); <br>
* String jobContainerClass =
* configuration.getString("core.container.job.class"); <br>
* 设置多级List <br>
* configuration.set("job.reader.parameter.jdbcUrl", Arrays.asList(new String[]
* {"jdbc", "jdbc"}));
* 合并Configuration: <br>
* configuration.merge(another);
* Configuration 存在两种较好地实现方式<br>
* 第一种是将JSON配置信息中所有的Key全部打平,用a.b.c的级联方式作为Map的Key,内部使用一个Map保存信息 <br>
* 第二种是将JSON的对象直接使用结构化树形结构保存<br>
* 目前使用的第二种实现方式,使用第一种的问题在于: <br>
* 1. 插入新对象,比较难处理,例如a.b.c="foo",此时如果需要插入a="foo",也即是根目录下第一层所有类型全部要废弃
* ,使用"foo"作为value,第一种方式使用字符串表示key,难以处理这类问题。 <br>
* 2. 返回树形结构,例如 a.b.c.d = "foo",如果返回"a"下的所有元素,实际上是一个Map,需要合并处理 <br>
* 3. 输出JSON,将上述对象转为JSON,要把上述Map的多级key转为树形结构,并输出为JSON <br>
*/
public class Configuration
{
/**
* 对于加密的keyPath,需要记录下来
* 为的是后面分布式情况下将该值加密后抛到 AddaxServer中
*/
private Set<String> secretKeyPathSet =
new HashSet<>();
private Object root;
private Configuration(String json)
{
try {
this.root = JSON.parse(json);
}
catch (Exception e) {
throw AddaxException.asAddaxException(CommonErrorCode.CONFIG_ERROR,
String.format("The configuration is incorrect. The configuration you provided is " +
"not in valid JSON format: %s.", e.getMessage()));
}
}
/*
* 初始化空白的Configuration
*/
public static Configuration newDefault()
{
return Configuration.from("{}");
}
/*
* 从JSON字符串加载Configuration
*/
public static Configuration from(String json)
{
json = StrUtil.replaceVariable(json);
checkJSON(json);
try {
return new Configuration(json);
}
catch (Exception e) {
throw AddaxException.asAddaxException(CommonErrorCode.CONFIG_ERROR,
e);
}
}
/*
* 从包括json的File对象加载Configuration
*/
public static Configuration from(File file)
{
try {
return Configuration.from(IOUtils
.toString(new FileInputStream(file), StandardCharsets.UTF_8));
}
catch (FileNotFoundException e) {
throw AddaxException.asAddaxException(CommonErrorCode.CONFIG_ERROR,
String.format("No such file: %s", file.getAbsolutePath()));
}
catch (IOException e) {
throw AddaxException.asAddaxException(
CommonErrorCode.CONFIG_ERROR,
String.format("Failed to read the configuration [%s], reason: %s.",
file.getAbsolutePath(), e));
}
}
/*
* 从包括json的InputStream对象加载Configuration
*/
public static Configuration from(InputStream is)
{
try {
return Configuration.from(IOUtils.toString(is, StandardCharsets.UTF_8));
}
catch (IOException e) {
throw AddaxException.asAddaxException(CommonErrorCode.CONFIG_ERROR,
String.format("Failed to read the configuration reason: %s.", e));
}
}
/*
* 从Map对象加载Configuration
*/
public static Configuration from(final Map<String, Object> object)
{
return Configuration.from(Configuration.toJSONString(object));
}
/*
* 从List对象加载Configuration
*/
public static Configuration from(final List<Object> object)
{
return Configuration.from(Configuration.toJSONString(object));
}
private static void checkJSON(String json)
{
if (StringUtils.isBlank(json)) {
throw AddaxException.asAddaxException(CommonErrorCode.CONFIG_ERROR,
"The configure file is empty.");
}
}
private static String toJSONString(Object object)
{
return JSON.toJSONString(object);
}
public String getNecessaryValue(String key, ErrorCode errorCode)
{
String value = this.getString(key, null);
if (StringUtils.isBlank(value)) {
throw AddaxException.asAddaxException(errorCode,
String.format("Illegal configuration, the item [%s] is required.", key));
}
return value;
}
public String getUnnecessaryValue(String key, String defaultValue)
{
String value = this.getString(key, defaultValue);
if (StringUtils.isBlank(value)) {
value = defaultValue;
}
return value;
}
/**
* 根据用户提供的json path,寻址具体的对象。
*
* NOTE: 目前仅支持Map以及List下标寻址, 例如:
*
* 对于如下JSON
* <p>
* {"a": {"b": {"c": [0,1,2,3]}}}
* </p>
* config.get("") 返回整个Map <br>
* config.get("a") 返回a下属整个Map <br>
* config.get("a.b.c") 返回c对应的数组List <br>
* config.get("a.b.c[0]") 返回数字0
*
* @param path String 要查的json路径
* @return Java表示的JSON对象,如果path不存在或者对象不存在,均返回null。
*/
public Object get(String path)
{
this.checkPath(path);
try {
return this.findObject(path);
}
catch (Exception e) {
return null;
}
}
/**
* 用户指定部分path,获取Configuration的子集
*
* @param path String json 路径
* @return Configuration 配置对象, 如果path获取的路径或者对象不存在,返回null
*/
public Configuration getConfiguration(String path)
{
Object object = this.get(path);
if (null == object) {
return null;
}
return Configuration.from(Configuration.toJSONString(object));
}
/**
* 根据用户提供的json path,寻址String对象
*
* @param path String json 路径
*
* @return String 对象,如果path不存在或者String不存在,返回null
*/
public String getString(String path)
{
Object string = this.get(path);
if (null == string) {
return null;
}
return String.valueOf(string);
}
/**
* 根据用户提供的json path,寻址String对象,如果对象不存在,返回默认字符串
*
* @param path String json 路径
* @param defaultValue String 如果path不存在,则返回该值
*
* @return String对象,如果path不存在或者String不存在,返回默认字符串
*/
public String getString(String path, String defaultValue)
{
String result = this.getString(path);
if (null == result) {
return defaultValue;
}
return result;
}
/**
* 根据用户提供的json path,寻址Character对象
*
* @param path String json 路径
* @return Character对象,如果path不存在或者Character不存在,返回null
*/
public Character getChar(String path)
{
String result = this.getString(path);
if (null == result) {
return null;
}
try {
return CharUtils.toChar(result);
}
catch (Exception e) {
throw AddaxException.asAddaxException(
CommonErrorCode.CONFIG_ERROR,
String.format("Illegal configuration, cannot be converted to string for [%s], reason: %s.", path,
e.getMessage()));
}
}
/**
* 根据用户提供的json path,寻址Boolean对象,如果对象不存在,返回默认Character对象
*
* @param path String json 路径
* @param defaultValue String 如果path不存在,则返回该值
*
* @return Character对象,如果path不存在或者Character不存在,返回默认Character对象
*/
public Character getChar(String path, char defaultValue)
{
Character result = this.getChar(path);
if (null == result) {
return defaultValue;
}
return result;
}
/**
* 根据用户提供的json path,寻址Boolean对象
*
* @param path String json 路径
*
* @return Boolean对象,如果path值非true,false ,将报错.特别注意:当 path 不存在时,会返回:null.
*/
public Boolean getBool(String path)
{
String result = this.getString(path);
if (null == result) {
return null; //NOSONAR
}
else if ("true".equalsIgnoreCase(result)) {
return Boolean.TRUE;
}
else if ("false".equalsIgnoreCase(result)) {
return Boolean.FALSE;
}
else {
throw AddaxException.asAddaxException(CommonErrorCode.CONFIG_ERROR,
String.format("Illegal configuration, the value [%s] of [%s] cannot be converted to bool type.",
path, result));
}
}
/**
* 根据用户提供的json path,寻址Boolean对象,如果对象不存在,返回默认Boolean对象
*
*
* @param path String json 路径
* @param defaultValue String 如果path不存在,则返回该值
*
* @return Boolean对象,如果path不存在或者Boolean不存在,返回默认Boolean对象
*/
public Boolean getBool(String path, boolean defaultValue)
{
Boolean result = this.getBool(path);
if (null == result) {
return defaultValue;
}
return result;
}
/**
* 根据用户提供的json path,寻址Integer对象
*
* @param path String json 路径
*
* @return Integer对象,如果path不存在或者Integer不存在,返回null
*/
public Integer getInt(String path)
{
String result = this.getString(path);
if (null == result) {
return null;
}
try {
return Integer.valueOf(result);
}
catch (Exception e) {
throw AddaxException.asAddaxException(
CommonErrorCode.CONFIG_ERROR,
String.format("Illegal configuration, the value [%s] of [%s] is expected integer type.", path,
e.getMessage()));
}
}
/**
* 根据用户提供的json path,寻址Integer对象,如果对象不存在,返回默认Integer对象
*
* @param path String json 路径
* @param defaultValue String 如果path不存在,则返回该值
*
* @return Integer对象,如果path不存在或者Integer不存在,返回默认Integer对象
*/
public Integer getInt(String path, int defaultValue)
{
Integer object = this.getInt(path);
if (null == object) {
return defaultValue;
}
return object;
}
/**
* 根据用户提供的json path,寻址Long对象
*
* @param path String json 路径
*
* @return Long对象,如果path不存在或者Long不存在,返回null
*/
public Long getLong(String path)
{
String result = this.getString(path);
if (StringUtils.isBlank(result)) {
return null;
}
try {
return Long.valueOf(result);
}
catch (Exception e) {
throw AddaxException.asAddaxException(
CommonErrorCode.CONFIG_ERROR,
String.format("Illegal configuration, the value [%s] of [%s] is expected integer/long type.", path,
e.getMessage()));
}
}
/**
* 根据用户提供的json path,寻址Long对象,如果对象不存在,返回默认Long对象
*
* @param path String json 路径
* @param defaultValue String 如果path不存在,则返回该值
*
* @return Long对象,如果path不存在或者Integer不存在,返回默认Long对象
*/
public Long getLong(String path, long defaultValue)
{
Long result = this.getLong(path);
if (null == result) {
return defaultValue;
}
return result;
}
/**
* 根据用户提供的json path,寻址Double对象
*
* @param path String json 路径
*
* @return Double对象,如果path不存在或者Double不存在,返回null
*/
public Double getDouble(String path)
{
String result = this.getString(path);
if (StringUtils.isBlank(result)) {
return null;
}
try {
return Double.valueOf(result);
}
catch (Exception e) {
throw AddaxException.asAddaxException(
CommonErrorCode.CONFIG_ERROR,
String.format("Illegal configuration, the value [%s] of [%s] is expected float type.", path,
e.getMessage()));
}
}
/**
* 根据用户提供的json path,寻址Double对象,如果对象不存在,返回默认Double对象
*
* @param path String json 路径
* @param defaultValue String 如果path不存在,则返回该值
*
* @return Double对象,如果path不存在或者Double不存在,返回默认Double对象
*/
public Double getDouble(String path, double defaultValue)
{
Double result = this.getDouble(path);
if (null == result) {
return defaultValue;
}
return result;
}
/**
* 根据用户提供的json path,寻址List对象,如果对象不存在,返回null
*
* @param path String json 路径
*
* @return 对象列表
*/
@SuppressWarnings("unchecked")
public List<Object> getList(String path)
{
return this.get(path, List.class);
}
/**
* 根据用户提供的json path,寻址List对象,如果对象不存在,返回null
*
* @param path json 路径
* @param t 要转换的类型
* @param <T> 类型
* @return 列表
*/
@SuppressWarnings("unchecked")
public <T> List<T> getList(String path, Class<T> t)
{
List<Object> object = this.get(path, List.class);
if (null == object) {
return new ArrayList<>();
}
List<T> result = new ArrayList<>();
List<Object> origin;
try {
origin = object;
}
catch (ClassCastException e) {
// .warn("{} 转为 List 时发生了异常,默认将此值添加到 List 中", String.valueOf(object))
origin = new ArrayList<>();
origin.add(String.valueOf(object));
}
for (Object each : origin) {
result.add((T) each);
}
return result;
}
/**
* 根据用户提供的json path,寻址List对象,如果对象不存在,返回默认List
*
* @param path json 路径
* @param defaultList 默认返回的值
*
* @return 对象列表
*/
public List<Object> getList(String path, List<Object> defaultList)
{
List<Object> object = this.getList(path);
if (null == object) {
return defaultList;
}
return object;
}
/**
* 根据用户提供的json path,寻址List对象,如果对象不存在,返回默认List
*
* @param path String json 路径
* @param defaultList 默认返回的值
* @param t 要转换的类型
* @param <T> 类型
*
* @return 列表
*/
public <T> List<T> getList(String path, List<T> defaultList,
Class<T> t)
{
List<T> list = this.getList(path, t);
if (null == list) {
return defaultList;
}
return list;
}
/**
* 根据用户提供的json path,寻址包含Configuration的List,如果对象不存在,返回默认null
*
* @param path String json 路径
*
* @return 列表
*/
public List<Configuration> getListConfiguration(String path)
{
List<Object> lists = getList(path);
if (lists == null) {
return new ArrayList<>();
}
List<Configuration> result = new ArrayList<>();
for (Object object : lists) {
result.add(Configuration.from(Configuration.toJSONString(object)));
}
return result;
}
/**
* 根据用户提供的json path,寻址Map对象,如果对象不存在,返回null
*
* @param path String json 路径
*
* @return map 对象
*/
@SuppressWarnings("unchecked")
public Map<String, Object> getMap(String path)
{
return this.get(path, Map.class);
}
/**
* 根据用户提供的json path,寻址Map对象,如果对象不存在,返回默认map
*
* @param path String json 路径
* @param defaultMap 默认返回值
*
* @return map对象
*/
public Map<String, Object> getMap(String path,
Map<String, Object> defaultMap)
{
Map<String, Object> object = this.getMap(path);
if (null == object) {
return defaultMap;
}
return object;
}
/**
* 根据用户提供的json path,寻址具体的对象,并转为用户提供的类型
* NOTE: 目前仅支持Map以及List下标寻址, 例如:
* 对于如下JSON
* <p>
* {"a": {"b": {"c": [0,1,2,3]}}}
* </p>
* config.get("") 返回整个Map <br>
* config.get("a") 返回a下属整个Map <br>
* config.get("a.b.c") 返回c对应的数组List <br>
* config.get("a.b.c[0]") 返回数字0
*
* @param path String json 路径
* @param clazz Class 要转换的类型
* @param <T> 类型
*
* @return Java表示的JSON对象,如果转型失败,将抛出异常
*/
@SuppressWarnings("unchecked")
public <T> T get(String path, Class<T> clazz)
{
this.checkPath(path);
return (T) this.get(path);
}
/**
* 格式化Configuration输出
*
* @return string
*/
public String beautify()
{
return JSON.toJSONString(this.getInternal(), JSONWriter.Feature.PrettyFormat);
}
/**
* 根据用户提供的json path,插入指定对象,并返回之前存在的对象(如果存在)
*
* 目前仅支持.以及数组下标寻址, 例如:
* <p>
* config.set("a.b.c[3]", object);
* </p>
* 对于插入对象,Configuration 不做任何限制,但是请务必保证该对象是简单对象,不要使用自定义对象,否则后续对于JSON序列化等情况会出现未定义行为。
*
* @param path JSON path对象
* @param object 需要插入的对象
*
* @return Java表示的JSON对象
*/
public Object set(String path, Object object)
{
checkPath(path);
Object result = this.get(path);
setObject(path, extractConfiguration(object));
return result;
}
/**
* 获取Configuration下所有叶子节点的key
* 对于
* <p>
* {"a": {"b": {"c": [0,1,2,3]}}, "x": "y"}
* </p>
* 下属的key包括: a.b.c[0],a.b.c[1],a.b.c[2],a.b.c[3],x
*
* @return set of string
*/
public Set<String> getKeys()
{
Set<String> collect = new HashSet<>();
this.getKeysRecursive(this.getInternal(), "", collect);
return collect;
}
/**
* 删除path对应的值,如果path不存在,将抛出异常。
*
* @param path String json 路径
*
* @return Object 返回查到的对象,若不存在则返回为空
*/
public Object remove(String path)
{
Object result = this.get(path);
if (null == result) {
throw AddaxException.asAddaxException(
CommonErrorCode.RUNTIME_ERROR,
String.format("Illegal configuration, the key [%s] does not exists.", path));
}
this.set(path, null);
return result;
}
/**
* 合并其他Configuration,并修改两者冲突的KV配置
*
* @param another Configuration 合并加入的第三方Configuration
* @param updateWhenConflict boolean 当合并双方出现KV冲突时候,选择更新当前KV,或者忽略该KV
*
* @return Configuration
*/
public Configuration merge(Configuration another,
boolean updateWhenConflict)
{
Set<String> keys = another.getKeys();
// 如果使用更新策略,凡是another存在的key,均需要更新
// 使用忽略策略,只有another Configuration存在但是当前Configuration不存在的key,才需要更新
keys.forEach(key -> {
if (updateWhenConflict) {
this.set(key, another.get(key));
return;
}
boolean isCurrentExists = this.get(key) != null;
if (isCurrentExists) {
return;
}
this.set(key, another.get(key));
});
return this;
}
@Override
public String toString()
{
return this.toJSON();
}
/**
* 将Configuration作为JSON输出
*
* @return String json string
*/
public String toJSON()
{
return Configuration.toJSONString(this.getInternal());
}
/**
* 拷贝当前Configuration,注意,这里使用了深拷贝,避免冲突
*/
@Override
public Configuration clone()
{
Configuration config = Configuration
.from(Configuration.toJSONString(this.getInternal()));
config.addSecretKeyPath(this.secretKeyPathSet);
return config;
}
/**
* 按照configuration要求格式的path
* 比如:
* a.b.c
* a.b[2].c
*
* @param path 路径
*
*/
public void addSecretKeyPath(String path)
{
if (StringUtils.isNotBlank(path)) {
this.secretKeyPathSet.add(path);
}
}
public void addSecretKeyPath(Set<String> pathSet)
{
if (pathSet != null) {
this.secretKeyPathSet.addAll(pathSet);
}
}
@SuppressWarnings("unchecked")
void getKeysRecursive(Object current, String path, Set<String> collect)
{
boolean isRegularElement = !(current instanceof Map || current instanceof List);
if (isRegularElement) {
collect.add(path);
return;
}
boolean isMap = current instanceof Map;
if (isMap) {
Map<String, Object> mapping = ((Map<String, Object>) current);
mapping.keySet().forEach(key -> {
if (StringUtils.isBlank(path)) {
getKeysRecursive(mapping.get(key), key.trim(), collect);
}
else {
getKeysRecursive(mapping.get(key), path + "." + key.trim(),
collect);
}
});
return;
}
List<Object> lists = (List<Object>) current;
for (int i = 0; i < lists.size(); i++) {
getKeysRecursive(lists.get(i), path + String.format("[%d]", i),
collect);
}
}
public Object getInternal()
{
return this.root;
}
private void setObject(String path, Object object)
{
Object newRoot = setObjectRecursive(this.root, split2List(path), 0,
object);
if (isSuitForRoot(newRoot)) {
this.root = newRoot;
return;
}
throw AddaxException.asAddaxException(CommonErrorCode.RUNTIME_ERROR,
String.format("Illegal value, cannot set value [%s] for key [%s]",
ToStringBuilder.reflectionToString(object), path));
}
@SuppressWarnings("unchecked")
private Object extractConfiguration(Object object)
{
if (object instanceof Configuration) {
return extractFromConfiguration(object);
}
if (object instanceof List) {
List<Object> result = new ArrayList<>();
for (Object each : (List<Object>) object) {
result.add(extractFromConfiguration(each));
}
return result;
}
if (object instanceof Map) {
Map<String, Object> result = new HashMap<>();
for (String key : ((Map<String, Object>) object).keySet()) {
result.put(key,
extractFromConfiguration(((Map<String, Object>) object)
.get(key)));
}
return result;
}
return object;
}
private Object extractFromConfiguration(Object object)
{
if (object instanceof Configuration) {
return ((Configuration) object).getInternal();
}
return object;
}
Object buildObject(List<String> paths, Object object)
{
if (null == paths) {
throw AddaxException.asAddaxException(
CommonErrorCode.RUNTIME_ERROR, "The Path cannot be null");
}
if (1 == paths.size() && StringUtils.isBlank(paths.get(0))) {
return object;
}
Object child = object;
for (int i = paths.size() - 1; i >= 0; i--) {
String path = paths.get(i);
if (isPathMap(path)) {
Map<String, Object> mapping = new HashMap<>();
mapping.put(path, child);
child = mapping;
continue;
}
if (isPathList(path)) {
List<Object> lists = new ArrayList<>(
this.getIndex(path) + 1);
expand(lists, this.getIndex(path) + 1);
lists.set(this.getIndex(path), child);
child = lists;
continue;
}
throw AddaxException.asAddaxException(
CommonErrorCode.RUNTIME_ERROR, String.format("the value [%s] of [%s] is illegal.",
StringUtils.join(paths, "."), path));
}
return child;
}
@SuppressWarnings("unchecked")
Object setObjectRecursive(Object current, List<String> paths,
int index, Object value)
{
// 如果是已经超出path,我们就返回value即可,作为最底层叶子节点
boolean isLastIndex = index == paths.size();
if (isLastIndex) {
return value;
}
String path = paths.get(index).trim();
boolean isNeedMap = isPathMap(path);
if (isNeedMap) {
Map<String, Object> mapping;
// 当前不是map,因此全部替换为map,并返回新建的map对象
boolean isCurrentMap = current instanceof Map;
if (!isCurrentMap) {
mapping = new HashMap<>();
mapping.put(
path,
buildObject(paths.subList(index + 1, paths.size()),
value));
return mapping;
}
// 当前是map,但是没有对应的key,也就是我们需要新建对象插入该map,并返回该map
mapping = ((Map<String, Object>) current);
boolean hasSameKey = mapping.containsKey(path);
if (!hasSameKey) {
mapping.put(
path,
buildObject(paths.subList(index + 1, paths.size()),
value));
return mapping;
}
// 当前是map,而且还竟然存在这个值,好吧,继续递归遍历
current = mapping.get(path);
mapping.put(path,
setObjectRecursive(current, paths, index + 1, value));
return mapping;
}
boolean isNeedList = isPathList(path);
if (isNeedList) {
List<Object> lists;
int listIndexer = getIndex(path);
// 当前是list,直接新建并返回即可
boolean isCurrentList = current instanceof List;
if (!isCurrentList) {
lists = expand(new ArrayList<>(), listIndexer + 1);
lists.set(
listIndexer,
buildObject(paths.subList(index + 1, paths.size()),
value));
return lists;
}
// 当前是list,但是对应的indexer是没有具体的值,也就是我们新建对象然后插入到该list,并返回该List
lists = expand((List<Object>) current, listIndexer + 1);
boolean hasSameIndex = lists.get(listIndexer) != null;
if (!hasSameIndex) {
lists.set(
listIndexer,
buildObject(paths.subList(index + 1, paths.size()),