package com.se.system.service.inte; import com.se.system.domain.vo.*; import com.se.system.service.impl.LinuxServerInfoService; import com.se.system.service.impl.WindowsServerInfoService; import com.se.system.utils.*; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.stream.Collectors; @SuppressWarnings("ALL") public abstract class IAServerInfoService { public IAServerInfoService() { } private static class GxServerInfosContainer { private static List ipAddress = null; private static List macAddress = null; private static String cpuSerial = null; private static String mainBoardSerial = null; private static boolean isIpCheck = true; private static boolean isMacCheck = true; private static boolean isCpuCheck = true; private static boolean isBoardCheck = true; } /** *

组装需要额外校验的License参数

* * @return LicenseExtraParam 自定义校验参数 */ public LicenseExtraParamVo getServerInfos() { LicenseExtraParamVo result = new LicenseExtraParamVo(); try { initServerInfos(); result.setIpAddress(GxServerInfosContainer.ipAddress); result.setMacAddress(GxServerInfosContainer.macAddress); result.setCpuSerial(GxServerInfosContainer.cpuSerial); result.setMainBoardSerial(GxServerInfosContainer.mainBoardSerial); result.setIpCheck(GxServerInfosContainer.isIpCheck); result.setMacCheck(GxServerInfosContainer.isMacCheck); result.setCpuCheck(GxServerInfosContainer.isCpuCheck); result.setBoardCheck(GxServerInfosContainer.isBoardCheck); } catch (Exception e) { System.out.println("获取服务器硬件信息失败 " + e); } return result; } /** *

初始化服务器硬件信息,并将信息缓存到内存

* * @throws Exception 默认异常 */ private void initServerInfos() throws Exception { if (GxServerInfosContainer.ipAddress == null) { GxServerInfosContainer.ipAddress = this.getIpAddress(); } if (GxServerInfosContainer.macAddress == null) { GxServerInfosContainer.macAddress = this.getMacAddress(); } if (GxServerInfosContainer.cpuSerial == null) { GxServerInfosContainer.cpuSerial = this.getCPUSerial(); } if (GxServerInfosContainer.mainBoardSerial == null) { GxServerInfosContainer.mainBoardSerial = this.getMainBoardSerial(); } } /** *

获取IP地址

* * @return List IP地址 * @throws Exception 默认异常 */ public List getIpAddress() throws Exception { /** 获取所有网络接口 */ List inetAddresses = getLocalAllInetAddress(); if (CommonUtils.isNotEmpty(inetAddresses)) { return inetAddresses.stream().map(InetAddress::getHostAddress).distinct().map(String::toLowerCase).collect(Collectors.toList()); } return null; } /** *

获取Mac地址

* * @return List Mac地址 * @throws Exception 默认异常 */ public List getMacAddress() throws Exception { /** 获取所有网络接口 */ List inetAddresses = getLocalAllInetAddress(); if (CommonUtils.isNotEmpty(inetAddresses)) { return inetAddresses.stream().map(this::getMacByInetAddress).distinct().collect(Collectors.toList()); } return null; } /** *

获取服务器信息

* * @param osName 系统类型 * @return AGxServerInfos 服务信息 */ public static IAServerInfoService getServer(String osName) { if ("".equals(osName) || osName == null) { osName = System.getProperty("os.name").toLowerCase(); } IAServerInfoService abstractServerInfos; // 根据不同操作系统类型选择不同的数据获取方法 if (osName.startsWith("windows")) { abstractServerInfos = new WindowsServerInfoService(); } else if (osName.startsWith("linux")) { abstractServerInfos = new LinuxServerInfoService(); } else {// 其他服务器类型 abstractServerInfos = new LinuxServerInfoService(); } return abstractServerInfos; } /** *

获取服务器临时磁盘位置

*/ public static String getServerTempPath() { String property = System.getProperty("user.dir"); return property; // String osName = System.getProperty("os.name").toLowerCase(); ////根据不同操作系统类型选择不同的数据获取方法 // if (osName.startsWith("windows")) { // return property.substring(0,property.indexOf(":")+1); //} else if (osName.startsWith("linux")) { // return "/home"; //}else{//其他服务器类型 // return "/home"; //} } /** *

获取CPU序列号

* * @return String 主板序列号 * @throws Exception 默认异常 */ protected abstract String getCPUSerial() throws Exception; /** *

获取主板序列号

* * @return String 主板序列号 * @throws Exception 默认异常 */ protected abstract String getMainBoardSerial() throws Exception; /** *

获取当前服务器所有符合条件的网络地址

* * @return List 网络地址列表 * @throws Exception 默认异常 */ private List getLocalAllInetAddress() throws Exception { List result = new ArrayList<>(4); // 遍历所有的网络接口 for (Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces(); networkInterfaces.hasMoreElements(); ) { NetworkInterface ni = (NetworkInterface) networkInterfaces.nextElement(); // 在所有的接口下再遍历IP for (Enumeration addresses = ni.getInetAddresses(); addresses.hasMoreElements(); ) { InetAddress address = (InetAddress) addresses.nextElement(); // 排除LoopbackAddress、SiteLocalAddress、LinkLocalAddress、MulticastAddress类型的IP地址 if (!address.isLoopbackAddress() && !address.isLinkLocalAddress() && !address.isMulticastAddress()) { result.add(address); } } } return result; } /** *

获取某个网络地址对应的Mac地址

* * @param inetAddr 网络地址 * @return String Mac地址 */ private String getMacByInetAddress(InetAddress inetAddr) { try { byte[] mac = NetworkInterface.getByInetAddress(inetAddr).getHardwareAddress(); StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < mac.length; i++) { if (i != 0) { stringBuilder.append("-"); } /** 将十六进制byte转化为字符串 */ String temp = Integer.toHexString(mac[i] & 0xff); if (temp.length() == 1) { stringBuilder.append("0").append(temp); } else { stringBuilder.append(temp); } } return stringBuilder.toString().toUpperCase(); } catch (SocketException e) { e.printStackTrace(); } return null; } }