Skip to content

Commit d3adf0e

Browse files
committed
refactor: 优化设备会话持久化逻辑
1 parent 7b7b96f commit d3adf0e

File tree

2 files changed

+456
-53
lines changed

2 files changed

+456
-53
lines changed
Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,66 @@
11
package org.jetlinks.community.configure.cluster;
22

3+
import org.apache.commons.collections4.MapUtils;
4+
import org.springframework.core.env.Environment;
5+
6+
import java.util.Collections;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
import java.util.Objects;
10+
311
public class Cluster {
412

5-
static String ID = "default";
13+
public static final String TAG_NAME = "name";
14+
public static final String TAG_ID = "id";
15+
private static String ID = "default";
16+
private static String SAFE_ID = "default";
617

18+
private static String NAME = "default";
19+
private static Map<String, String> TAGS = Collections.emptyMap();
720

821
public static String id() {
922
return ID;
1023
}
24+
25+
public static String name() {
26+
return NAME;
27+
}
28+
29+
public static String safeId() {
30+
return SAFE_ID;
31+
}
32+
33+
public static Map<String, String> tags() {
34+
return TAGS;
35+
}
36+
37+
public static boolean hasTag(Map<String, String> tag) {
38+
if (MapUtils.isEmpty(tag)) {
39+
return false;
40+
}
41+
for (Map.Entry<String, String> entry : tag.entrySet()) {
42+
if (!Objects.equals(TAGS.get(entry.getKey()), entry.getValue())) {
43+
return false;
44+
}
45+
}
46+
return true;
47+
}
48+
49+
public static synchronized void setup(Environment env){
50+
Map<String, String> _tags = new HashMap<>(TAGS);
51+
_tags.put("port",env.getProperty("server.port"));
52+
53+
}
54+
55+
public static synchronized void setup(String id, String name, Map<String, String> tags) {
56+
ID = id;
57+
SAFE_ID = ID.replaceAll("[\\s\\\\/:*?\"<>|]", "_");
58+
59+
NAME = name;
60+
Map<String, String> _tags = new HashMap<>(tags);
61+
_tags.putIfAbsent(TAG_NAME, name);
62+
_tags.putIfAbsent(TAG_ID, id);
63+
TAGS = Collections.unmodifiableMap(_tags);
64+
}
65+
1166
}

0 commit comments

Comments
 (0)