diff --git a/CHANGELOG.md b/CHANGELOG.md index 2206c4513..22b8f4297 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * 【http】 自动关闭HttpURLConnection的头安全检查(issue#512@Github) * 【setting】 Setting变量替换支持从系统参数中取值 * 【core】 改进NumberUtil.isNumber方法(pr#68@Gitee) +* 【system】 增加Oshi工具封装 ### Bug修复 * 【db】 解决ThreadLocalConnection多数据源被移除问题(pr#66@Gitee) diff --git a/hutool-core/src/main/java/cn/hutool/core/util/NumberUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/NumberUtil.java index dddb047e5..7766d1736 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/NumberUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/NumberUtil.java @@ -1043,7 +1043,15 @@ public class NumberUtil { // ------------------------------------------------------------------------------------------- isXXX /** - * 是否为数字 + * 是否为数字,支持包括: + * + *
+	 * 1、10进制
+	 * 2、16进制数字(0x开头)
+	 * 3、科学计数法形式(1234E3)
+	 * 4、类型标识形式(123D)
+	 * 5、正负数标识形式(+123、-234)
+	 * 
* * @param str 字符串值 * @return 是否为数字 diff --git a/hutool-system/pom.xml b/hutool-system/pom.xml index 4e3ae6e05..28a258f2c 100644 --- a/hutool-system/pom.xml +++ b/hutool-system/pom.xml @@ -1,9 +1,11 @@ - + 4.0.0 jar - + cn.hutool hutool-parent @@ -12,13 +14,20 @@ hutool-system ${project.artifactId} - Hutool 系统调用(Runtime) - + Hutool 系统调用(Runtime)、系统监控封装 + cn.hutool hutool-core ${project.parent.version} + + + com.github.oshi + oshi-core + 3.13.3 + provided + diff --git a/hutool-system/src/main/java/cn/hutool/system/oshi/OshiUtil.java b/hutool-system/src/main/java/cn/hutool/system/oshi/OshiUtil.java new file mode 100644 index 000000000..0a67ebd04 --- /dev/null +++ b/hutool-system/src/main/java/cn/hutool/system/oshi/OshiUtil.java @@ -0,0 +1,99 @@ +package cn.hutool.system.oshi; + +import oshi.SystemInfo; +import oshi.hardware.CentralProcessor; +import oshi.hardware.ComputerSystem; +import oshi.hardware.GlobalMemory; +import oshi.hardware.HWDiskStore; +import oshi.hardware.HardwareAbstractionLayer; +import oshi.hardware.Sensors; +import oshi.software.os.OperatingSystem; + +/** + * Oshi库封装的工具类,通过此工具类,可获取系统、硬件相关信息 + * + *
+ * 1、系统信息
+ * 2、硬件信息
+ * 
+ * + * @author Looly + * @since 4.6.4 + */ +public class OshiUtil { + + private static final SystemInfo systemInfo; + /** 硬件信息 */ + private static final HardwareAbstractionLayer hardware; + /** 系统信息 */ + private static final OperatingSystem os; + + static { + systemInfo = new SystemInfo(); + hardware = systemInfo.getHardware(); + os = systemInfo.getOperatingSystem(); + } + + /** + * 获取操作系统相关信息,包括系统版本、文件系统、进程等 + * + * @return 操作系统相关信息 + */ + public static OperatingSystem getOs() { + return os; + } + + /** + * 获取硬件相关信息,包括内存、硬盘、网络设备、显示器、USB、声卡等 + * + * @return 硬件相关信息 + */ + public static HardwareAbstractionLayer getHardware() { + return hardware; + } + + /** + * 获取BIOS中计算机相关信息,比如序列号、固件版本等 + * + * @return 获取BIOS中计算机相关信息 + */ + public static ComputerSystem getSystem() { + return hardware.getComputerSystem(); + } + + /** + * 获取内存相关信息,比如总内存、可用内存等 + * + * @return 内存相关信息 + */ + public static GlobalMemory getMemory() { + return hardware.getMemory(); + } + + /** + * 获取CPU(处理器)相关信息,比如CPU负载等 + * + * @return CPU(处理器)相关信息 + */ + public static CentralProcessor getProcessor() { + return hardware.getProcessor(); + } + + /** + * 获取传感器相关信息,例如CPU温度、风扇转速等,传感器可能有多个 + * + * @return 传感器相关信息 + */ + public static Sensors getSensors() { + return hardware.getSensors(); + } + + /** + * 获取磁盘相关信息,可能有多个磁盘(包括可移动磁盘等) + * + * @return 磁盘相关信息 + */ + public static HWDiskStore[] getDiskStores() { + return hardware.getDiskStores(); + } +} diff --git a/hutool-system/src/main/java/cn/hutool/system/oshi/package-info.java b/hutool-system/src/main/java/cn/hutool/system/oshi/package-info.java new file mode 100644 index 000000000..a8efd1ef8 --- /dev/null +++ b/hutool-system/src/main/java/cn/hutool/system/oshi/package-info.java @@ -0,0 +1,8 @@ +/** + * Oshi库封装
+ * https://github.com/oshi/oshi + * + * @author Looly + * @since 4.6.4 + */ +package cn.hutool.system.oshi; \ No newline at end of file diff --git a/hutool-system/src/test/java/cn/hutool/system/OshiTest.java b/hutool-system/src/test/java/cn/hutool/system/OshiTest.java new file mode 100644 index 000000000..ebc638156 --- /dev/null +++ b/hutool-system/src/test/java/cn/hutool/system/OshiTest.java @@ -0,0 +1,15 @@ +package cn.hutool.system; + +import org.junit.Assert; +import org.junit.Test; + +import cn.hutool.system.oshi.OshiUtil; + +public class OshiTest { + + @Test + public void getMemeryTest() { + long total = OshiUtil.getMemory().getTotal(); + Assert.assertTrue(total > 0); + } +}