-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
/
PropertyResolver.java
218 lines (173 loc) · 7.11 KB
/
PropertyResolver.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
package com.ctrip.apollo.portal.service.txtresolver;
import com.ctrip.apollo.core.dto.ItemChangeSets;
import com.ctrip.apollo.core.dto.ItemDTO;
import com.ctrip.apollo.core.utils.StringUtils;
import com.ctrip.apollo.portal.util.BeanUtils;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* normal property file resolver.
* update comment and blank item implement by create new item and delete old item.
* update normal key/value item implement by update.
*/
@Component
public class PropertyResolver implements ConfigTextResolver {
private static final String KV_SEPARATOR = "=";
private static final String ITEM_SEPARATOR = "\n";
@Override
public TextResolverResult resolve(long namespaceId, String configText, List<ItemDTO> baseItems) {
TextResolverResult result = new TextResolverResult();
if (StringUtils.isEmpty(configText)){
result.setResolveSuccess(false);
result.setMsg("config text can not be empty");
return result;
}
Map<Integer, ItemDTO> oldLineNumMapItem = BeanUtils.mapByKey("lineNum", baseItems);
Map<String, ItemDTO> oldKeyMapItem = BeanUtils.mapByKey("key", baseItems);
//remove comment and blank item map.
oldKeyMapItem.remove("");
String[] newItems = configText.split(ITEM_SEPARATOR);
if (isHasRepeatKey(newItems)){
result.setResolveSuccess(false);
result.setMsg("config text has repeat key please check.");
return result;
}
ItemChangeSets changeSets = new ItemChangeSets();
result.setChangeSets(changeSets);
Map<Integer, String> newLineNumMapItem = new HashMap();//use for delete blank and comment item
int lineCounter = 1;
for (String newItem : newItems) {
newItem = newItem.trim();
newLineNumMapItem.put(lineCounter, newItem);
ItemDTO oldItemByLine = oldLineNumMapItem.get(lineCounter);
//comment item
if (isCommentItem(newItem)) {
handleCommentLine(namespaceId, oldItemByLine, newItem, lineCounter, changeSets);
//blank item
} else if (isBlankItem(newItem)) {
handleBlankLine(namespaceId, oldItemByLine, lineCounter, changeSets);
//normal item
} else {
if (!handleNormalLine(namespaceId, oldKeyMapItem, newItem, lineCounter, result)) {
return result;
}
}
lineCounter++;
}
deleteCommentAndBlankItem(oldLineNumMapItem, newLineNumMapItem, changeSets);
deleteNormalKVItem(oldKeyMapItem, changeSets);
result.setResolveSuccess(true);
return result;
}
private boolean isHasRepeatKey(String[] newItems){
Set<String> keys = new HashSet<>();
int keyCount = 0;
for (String item: newItems){
if (!isCommentItem(item) && !isBlankItem(item)){
keyCount++;
String[] kv = parseKeyValueFromItem(item);
if (kv != null) {
keys.add(kv[0]);
}
}
}
return keyCount > keys.size();
}
private String[] parseKeyValueFromItem(String item){
int kvSeparator = item.indexOf(KV_SEPARATOR);
if (kvSeparator == -1) {
return null;
}
String[] kv = new String[2];
kv[0] = item.substring(0, kvSeparator).trim();
kv[1] = item.substring(kvSeparator + 1, item.length()).trim();
return kv;
}
private void handleCommentLine(Long namespaceId, ItemDTO oldItemByLine, String newItem, int lineCounter, ItemChangeSets changeSets) {
String oldComment = oldItemByLine == null ? "" : oldItemByLine.getComment();
//create comment. implement update comment by delete old comment and create new comment
if (!(isCommentItem(oldItemByLine) && newItem.equals(oldComment))) {
changeSets.addCreateItem(buildCommentItem(0l, namespaceId, newItem, lineCounter));
}
}
private void handleBlankLine(Long namespaceId, ItemDTO oldItem, int lineCounter, ItemChangeSets changeSets) {
if (!isBlankItem(oldItem)) {
changeSets.addCreateItem(buildBlankItem(0l, namespaceId, lineCounter));
}
}
private boolean handleNormalLine(Long namespaceId, Map<String, ItemDTO> keyMapOldItem, String newItem,
int lineCounter, TextResolverResult result) {
ItemChangeSets changeSets = result.getChangeSets();
String[] kv = parseKeyValueFromItem(newItem);
if (kv == null) {
result.setResolveSuccess(false);
result.setMsg(" line:" + lineCounter + " key value must separate by '='");
return false;
}
String newKey = kv[0];
String newValue = kv[1];
ItemDTO oldItem = keyMapOldItem.get(newKey);
if (oldItem == null) {//new item
changeSets.addCreateItem(buildNormalItem(0l, namespaceId, newKey, newValue, "", lineCounter));
} else if (!newValue.equals(oldItem.getValue()) || lineCounter != oldItem.getLineNum()){//update item
changeSets.addUpdateItem(
buildNormalItem(oldItem.getId(), namespaceId, newKey, newValue, oldItem.getComment(),
lineCounter));
}
keyMapOldItem.remove(newKey);
return true;
}
private boolean isCommentItem(ItemDTO item) {
return item != null && "".equals(item.getKey()) && item.getComment().startsWith("#");
}
private boolean isCommentItem(String line) {
return line != null && line.startsWith("#");
}
private boolean isBlankItem(ItemDTO item) {
return item != null && "".equals(item.getKey()) && "".equals(item.getComment());
}
private boolean isBlankItem(String line) {
return "".equals(line);
}
private void deleteNormalKVItem(Map<String, ItemDTO> baseKeyMapItem, ItemChangeSets changeSets) {
//surplus item is to be deleted
for (Map.Entry<String, ItemDTO> entry : baseKeyMapItem.entrySet()) {
changeSets.addDeleteItem(entry.getValue());
}
}
private void deleteCommentAndBlankItem(Map<Integer, ItemDTO> oldLineNumMapItem,
Map<Integer, String> newLineNumMapItem,
ItemChangeSets changeSets) {
for (Map.Entry<Integer, ItemDTO> entry : oldLineNumMapItem.entrySet()) {
int lineNum = entry.getKey();
ItemDTO oldItem = entry.getValue();
String newItem = newLineNumMapItem.get(lineNum);
//1. old is blank by now is not
//2.old is comment by now is not exist or modified
if ((isBlankItem(oldItem) && !isBlankItem(newItem))
|| isCommentItem(oldItem) && (newItem == null || !newItem.equals(oldItem.getComment()))) {
changeSets.addDeleteItem(oldItem);
}
}
}
private ItemDTO buildCommentItem(Long id, Long namespaceId, String comment, int lineNum) {
return buildNormalItem(id, namespaceId, "", "", comment, lineNum);
}
private ItemDTO buildBlankItem(Long id, Long namespaceId, int lineNum) {
return buildNormalItem(id, namespaceId, "", "", "", lineNum);
}
private ItemDTO buildNormalItem(Long id, Long namespaceId, String key, String value, String comment, int lineNum) {
ItemDTO item = new ItemDTO();
item.setId(id);
item.setNamespaceId(namespaceId);
item.setKey(key);
item.setValue(value);
item.setComment(comment);
item.setLineNum(lineNum);
return item;
}
}