package com.zy.system.entity.license; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import java.util.ArrayList; import java.util.Collections; import java.util.List; public final class LicenseBindingSupport { private LicenseBindingSupport() { } public static boolean isV2Extra(Object extra) { if (extra == null) { return false; } if (extra instanceof LicenseBindModel) { return true; } if (extra instanceof JSONObject) { return isV2Json((JSONObject) extra); } if (extra instanceof String) { String text = ((String) extra).trim(); if (isBlank(text) || !text.startsWith("{")) { return false; } return isV2Json(JSON.parseObject(text)); } return false; } public static LicenseBindModel parseBindModel(Object extra) { if (extra == null) { return null; } if (extra instanceof LicenseBindModel) { return normalizeBindModel((LicenseBindModel) extra); } if (extra instanceof JSONObject) { return normalizeBindModel(((JSONObject) extra).toJavaObject(LicenseBindModel.class)); } if (extra instanceof String) { String text = ((String) extra).trim(); if (isBlank(text)) { return null; } return normalizeBindModel(JSON.parseObject(text, LicenseBindModel.class)); } return normalizeBindModel(JSON.parseObject(JSON.toJSONString(extra), LicenseBindModel.class)); } public static LicenseCheck parseLegacyLicenseCheck(Object extra) { if (extra == null) { return null; } if (extra instanceof LicenseCheck) { return (LicenseCheck) extra; } if (extra instanceof JSONObject) { return ((JSONObject) extra).toJavaObject(LicenseCheck.class); } if (extra instanceof String) { String text = ((String) extra).trim(); if (isBlank(text)) { return null; } return JSON.parseObject(text, LicenseCheck.class); } return JSON.parseObject(JSON.toJSONString(extra), LicenseCheck.class); } public static boolean matches(LicenseBindModel licenseBind, LicenseCheck serverCheck) { if (licenseBind == null) { return false; } if ("UNLIMITED".equalsIgnoreCase(trimToEmpty(licenseBind.getBindMode()))) { return true; } List nodes = licenseBind.getNodes(); if (nodes == null || nodes.isEmpty()) { return true; } for (LicenseNodeCheck node : nodes) { if (matchesNode(node, serverCheck)) { return true; } } return false; } private static boolean isV2Json(JSONObject jsonObject) { if (jsonObject == null) { return false; } return jsonObject.containsKey("nodes") || jsonObject.containsKey("bindMode") || jsonObject.containsKey("matchMode") || Integer.valueOf(2).equals(jsonObject.getInteger("version")); } private static LicenseBindModel normalizeBindModel(LicenseBindModel source) { if (source == null) { return null; } LicenseBindModel target = new LicenseBindModel(); target.setVersion(source.getVersion() == null ? 2 : source.getVersion()); target.setBindMode(isBlank(source.getBindMode()) ? "MULTI_NODE" : source.getBindMode().trim()); target.setMatchMode(isBlank(source.getMatchMode()) ? "ANY" : source.getMatchMode().trim()); List normalizedNodes = new ArrayList<>(); if (source.getNodes() != null) { for (LicenseNodeCheck node : source.getNodes()) { if (node == null) { continue; } LicenseNodeCheck normalizedNode = new LicenseNodeCheck(); normalizedNode.setNodeId(trimToEmpty(node.getNodeId())); normalizedNode.setIpAddress(normalizeList(node.getIpAddress(), false)); normalizedNode.setMacAddress(normalizeList(node.getMacAddress(), true)); normalizedNode.setCpuSerial(trimToEmpty(node.getCpuSerial())); normalizedNode.setMainBoardSerial(trimToEmpty(node.getMainBoardSerial())); normalizedNodes.add(normalizedNode); } } target.setNodes(normalizedNodes); return target; } private static boolean matchesNode(LicenseNodeCheck node, LicenseCheck serverCheck) { if (node == null || serverCheck == null) { return false; } if (!checkList(node.getIpAddress(), serverCheck.getIpAddress())) { return false; } if (!checkList(node.getMacAddress(), serverCheck.getMacAddress())) { return false; } if (!checkSerial(node.getMainBoardSerial(), serverCheck.getMainBoardSerial())) { return false; } return checkSerial(node.getCpuSerial(), serverCheck.getCpuSerial()); } private static boolean checkList(List expectedList, List actualList) { if (expectedList == null || expectedList.isEmpty()) { return true; } if (actualList == null || actualList.isEmpty()) { return false; } for (String expected : expectedList) { if (!isBlank(expected) && actualList.contains(expected.trim())) { return true; } } return false; } private static boolean checkSerial(String expectedSerial, String actualSerial) { if (isBlank(expectedSerial)) { return true; } return !isBlank(actualSerial) && expectedSerial.equals(actualSerial); } private static List normalizeList(List values, boolean upperCase) { List 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 boolean isBlank(String value) { return value == null || value.trim().isEmpty(); } private static String trimToEmpty(String value) { return value == null ? "" : value.trim(); } }