自动化立体仓库 - WMS系统
zwl
昨天 780ff45fdc167cadf4724c6c94530929b7445aab
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
package com.zy.system.entity.license;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
 
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
 
public class LicenseUtils {
 
    public static LicenseCheck getServerInfos() {
        String osName = System.getProperty("os.name").toLowerCase();
        AbstractServerInfos abstractServerInfos;
 
        if (osName.startsWith("windows")) {
            abstractServerInfos = new WindowsServerInfos();
        } else if (osName.startsWith("linux")) {
            abstractServerInfos = new LinuxServerInfos();
        } else {
            abstractServerInfos = new LinuxServerInfos();
        }
 
        return abstractServerInfos.getServerInfos();
    }
 
    public static String buildRequestCode(String subject) {
        return buildRequestCode(subject, getServerInfos());
    }
 
    public static String buildRequestCode(String subject, LicenseCheck licenseCheck) {
        if (isBlank(subject)) {
            throw new IllegalArgumentException("许可证名称不能为空");
        }
 
        LicenseCheck normalized = normalizeLicenseCheck(licenseCheck == null ? new LicenseCheck() : licenseCheck);
        JSONObject payload = new JSONObject(true);
        payload.put("version", 2);
        payload.put("subject", subject);
        payload.put("licenseBind", buildBindModel(normalized));
        return Base64.getEncoder().encodeToString(JSON.toJSONString(payload).getBytes(StandardCharsets.UTF_8));
    }
 
    private static JSONObject buildBindModel(LicenseCheck licenseCheck) {
        JSONObject bindModel = new JSONObject(true);
        bindModel.put("version", 2);
        bindModel.put("bindMode", "MULTI_NODE");
        bindModel.put("matchMode", "ANY");
 
        JSONObject node = new JSONObject(true);
        node.put("nodeId", getNodeId());
        node.put("ipAddress", toJsonArray(licenseCheck.getIpAddress()));
        node.put("macAddress", toJsonArray(licenseCheck.getMacAddress()));
        node.put("cpuSerial", trimToEmpty(licenseCheck.getCpuSerial()));
        node.put("mainBoardSerial", trimToEmpty(licenseCheck.getMainBoardSerial()));
 
        JSONArray nodes = new JSONArray();
        nodes.add(node);
        bindModel.put("nodes", nodes);
        return bindModel;
    }
 
    private static LicenseCheck normalizeLicenseCheck(LicenseCheck source) {
        LicenseCheck target = new LicenseCheck();
        target.setIpAddress(normalizeList(source.getIpAddress(), false));
        target.setMacAddress(normalizeList(source.getMacAddress(), true));
        target.setCpuSerial(trimToEmpty(source.getCpuSerial()));
        target.setMainBoardSerial(trimToEmpty(source.getMainBoardSerial()));
        return target;
    }
 
    private static List<String> normalizeList(List<String> values, boolean upperCase) {
        List<String> result = new ArrayList<>();
        if (values == null || values.isEmpty()) {
            return result;
        }
        for (String value : values) {
            if (isBlank(value)) {
                continue;
            }
            String normalized = value.trim();
            normalized = upperCase ? normalized.toUpperCase() : normalized.toLowerCase();
            if (!result.contains(normalized)) {
                result.add(normalized);
            }
        }
        Collections.sort(result);
        return result;
    }
 
    private static JSONArray toJsonArray(List<String> values) {
        JSONArray array = new JSONArray();
        if (values != null && !values.isEmpty()) {
            array.addAll(values);
        }
        return array;
    }
 
    private static String getNodeId() {
        try {
            String hostName = InetAddress.getLocalHost().getHostName();
            if (!isBlank(hostName)) {
                return hostName.trim();
            }
        } catch (Exception ignored) {
        }
        return "node-1";
    }
 
    private static boolean isBlank(String value) {
        return value == null || value.trim().isEmpty();
    }
 
    private static String trimToEmpty(String value) {
        return value == null ? "" : value.trim();
    }
}