|
1 | 1 | package org.jetlinks.community.configure.cluster; |
2 | 2 |
|
| 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 | + |
3 | 11 | public class Cluster { |
4 | 12 |
|
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"; |
6 | 17 |
|
| 18 | + private static String NAME = "default"; |
| 19 | + private static Map<String, String> TAGS = Collections.emptyMap(); |
7 | 20 |
|
8 | 21 | public static String id() { |
9 | 22 | return ID; |
10 | 23 | } |
| 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 | + |
11 | 66 | } |
0 commit comments