Looly 6 年 前
コミット
f8d3e3fb14

+ 1 - 0
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)

+ 9 - 1
hutool-core/src/main/java/cn/hutool/core/util/NumberUtil.java

@@ -1043,7 +1043,15 @@ public class NumberUtil {
 
 	// ------------------------------------------------------------------------------------------- isXXX
 	/**
-	 * 是否为数字
+	 * 是否为数字,支持包括:
+	 * 
+	 * <pre>
+	 * 1、10进制
+	 * 2、16进制数字(0x开头)
+	 * 3、科学计数法形式(1234E3)
+	 * 4、类型标识形式(123D)
+	 * 5、正负数标识形式(+123、-234)
+	 * </pre>
 	 * 
 	 * @param str 字符串值
 	 * @return 是否为数字

+ 13 - 4
hutool-system/pom.xml

@@ -1,9 +1,11 @@
 <?xml version='1.0' encoding='utf-8'?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 	<modelVersion>4.0.0</modelVersion>
 
 	<packaging>jar</packaging>
-	
+
 	<parent>
 		<groupId>cn.hutool</groupId>
 		<artifactId>hutool-parent</artifactId>
@@ -12,13 +14,20 @@
 
 	<artifactId>hutool-system</artifactId>
 	<name>${project.artifactId}</name>
-	<description>Hutool 系统调用(Runtime)</description>
-	
+	<description>Hutool 系统调用(Runtime)、系统监控封装</description>
+
 	<dependencies>
 		<dependency>
 			<groupId>cn.hutool</groupId>
 			<artifactId>hutool-core</artifactId>
 			<version>${project.parent.version}</version>
 		</dependency>
+		<!-- 跨平台的系统及硬件信息库 -->
+		<dependency>
+			<groupId>com.github.oshi</groupId>
+			<artifactId>oshi-core</artifactId>
+			<version>3.13.3</version>
+			<scope>provided</scope>
+		</dependency>
 	</dependencies>
 </project>

+ 99 - 0
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库封装的工具类,通过此工具类,可获取系统、硬件相关信息
+ * 
+ * <pre>
+ * 1、系统信息
+ * 2、硬件信息
+ * </pre>
+ * 
+ * @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();
+	}
+}

+ 8 - 0
hutool-system/src/main/java/cn/hutool/system/oshi/package-info.java

@@ -0,0 +1,8 @@
+/**
+ * Oshi库封装<br>
+ * https://github.com/oshi/oshi
+ * 
+ * @author Looly
+ * @since 4.6.4
+ */
+package cn.hutool.system.oshi;

+ 15 - 0
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);
+	}
+}