add OshiUtil

This commit is contained in:
Looly 2019-08-28 18:50:50 +08:00
parent f4a5b8b9b7
commit f8d3e3fb14
6 changed files with 145 additions and 5 deletions

View File

@ -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

View File

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

View File

@ -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>

View File

@ -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();
}
}

View File

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

View File

@ -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);
}
}