All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nervousync.utils.SystemUtils Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
/*
 * Licensed to the Nervousync Studio (NSYC) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.nervousync.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.*;

import org.nervousync.commons.Globals;

import org.nervousync.beans.network.NetworkInfo;
import org.nervousync.exceptions.beans.network.NetworkInfoException;

/**
 * 

System Utilities

* * Current utilities implements features: *
    Retrieve current system information.
*
    Retrieve current system Java runtime information.
*
    Generate unique identified ID for current system.
*
*

系统工具

* * 此工具集实现以下功能: *
    获取当前系统信息
*
    获取当前系统中的Java运行环境信息
*
    生成当前系统的唯一标识ID
*
* * @author Steven Wee [email protected] * @version $Revision: 1.2.0 $ $Date: Jul 24, 2015 11:43:24 $ */ public final class SystemUtils { /** * Logger instance * 日志实例 */ private static final LoggerUtils.Logger LOGGER = LoggerUtils.getLogger(SystemUtils.class); /** * Default certificate library path of Java runtime * Java运行环境默认证书库地址 */ private static final String JAVA_CERT_PATH = Globals.DEFAULT_PAGE_SEPARATOR + "lib" + Globals.DEFAULT_PAGE_SEPARATOR + "security" + Globals.DEFAULT_PAGE_SEPARATOR + "cacerts"; /** * Current operating system name * 当前操作系统名称 */ public static final String OPERATE_SYSTEM_NAME = System.getProperty("os.name"); /** * Current operating system version * 当前操作系统版本 */ public static final String OPERATE_SYSTEM_VERSION = System.getProperty("os.version"); /** * Current JRE home folder path * 当前JRE主目录地址 */ public static final String JAVA_HOME = System.getProperty("java.home"); /** * Current JDK version * 当前JDK版本信息 */ public static final String JDK_VERSION = System.getProperty("java.version"); /** * Current JDK tmp directory path * 当前JDK临时目录地址 */ public static final String JAVA_TMP_DIR = System.getProperty("java.io.tmpdir"); /** * Current username * 当前用户名 */ public static final String USER_NAME = System.getProperty("user.name"); /** * Current user home directory path * 当前用户主目录地址 */ public static final String USER_HOME = System.getProperty("user.home"); /** * Current user work directory path * 当前用户工作目录地址 */ public static final String USER_DIR = System.getProperty("user.dir"); /** * Major version of current JDK version * 当前JDK的主版本号 */ public static final int MAJOR_VERSION; /** * System identified ID * 系统标识ID */ private static final String IDENTIFIED_KEY = SystemUtils.generateIdentifiedKey(); static { if (JDK_VERSION.startsWith("1.8.")) { MAJOR_VERSION = 8; } else if (JDK_VERSION.startsWith("1.7.")) { MAJOR_VERSION = 7; } else if (JDK_VERSION.startsWith("1.6.")) { MAJOR_VERSION = 6; } else if (JDK_VERSION.startsWith("1.5.")) { MAJOR_VERSION = 5; } else if (JDK_VERSION.startsWith("1.")) { MAJOR_VERSION = Globals.DEFAULT_VALUE_INT; } else { MAJOR_VERSION = JDK_VERSION.indexOf(".") > 0 ? Integer.parseInt(JDK_VERSION.substring(0, JDK_VERSION.indexOf("."))) : Integer.parseInt(JDK_VERSION); } } /** *

Private constructor for SystemUtils

* SystemUtils is running in singleton instance mode *

SystemUtils的私有构造函数

* SystemUtils使用单例模式运行 */ private SystemUtils() { } /** *

Retrieve system identified ID

*

读取系统唯一标识ID

* * @return System identified ID * 系统标识ID */ public static String identifiedKey() { return IDENTIFIED_KEY; } /** *

Check current operating system is Windows

*

判断当前操作系统为Windows

* * @return Check result * 检查结果 */ public static boolean isWindows() { return OPERATE_SYSTEM_NAME.toLowerCase().contains("windows"); } /** *

Check current operating system is Unix

*

判断当前操作系统为Unix

* * @return Check result * 检查结果 */ public static boolean isUnix() { return OPERATE_SYSTEM_NAME.toLowerCase().indexOf("unix") > 0; } /** *

Check current operating system is Linux

*

判断当前操作系统为Linux

* * @return Check result * 检查结果 */ public static boolean isLinux() { return OPERATE_SYSTEM_NAME.toLowerCase().indexOf("linux") > 0; } /** *

Check current operating system is Apple MacOS

*

判断当前操作系统为苹果MacOS

* * @return Check result * 检查结果 */ public static boolean isMacOS() { return OPERATE_SYSTEM_NAME.toLowerCase().indexOf("mac os") > 0; } /** *

Check current operating system is Apple MacOS X

*

判断当前操作系统为苹果MacOS X

* * @return Check result * 检查结果 */ public static boolean isMacOSX() { return OPERATE_SYSTEM_NAME.toLowerCase().indexOf("mac os x") > 0; } /** *

Check current operating system is OS2

*

判断当前操作系统为OS2

* * @return Check result * 检查结果 */ public static boolean isOS2() { return OPERATE_SYSTEM_NAME.toLowerCase().indexOf("os/2") > 0; } /** *

Check current operating system is Solaris

*

判断当前操作系统为Solaris

* * @return Check result * 检查结果 */ public static boolean isSolaris() { return OPERATE_SYSTEM_NAME.toLowerCase().indexOf("solaris") > 0; } /** *

Check current operating system is SunOS

*

判断当前操作系统为SunOS

* * @return Check result * 检查结果 */ public static boolean isSunOS() { return OPERATE_SYSTEM_NAME.toLowerCase().indexOf("sunos") > 0; } /** *

Check current operating system is MPEiX

*

判断当前操作系统为MPEiX

* * @return Check result * 检查结果 */ public static boolean isMPEiX() { return OPERATE_SYSTEM_NAME.toLowerCase().indexOf("mpe/ix") > 0; } /** *

Check current operating system is HP Unix

*

判断当前操作系统为HP Unix

* * @return Check result * 检查结果 */ public static boolean isHPUX() { return OPERATE_SYSTEM_NAME.toLowerCase().indexOf("hp-ux") > 0; } /** *

Check current operating system is AIX

*

判断当前操作系统为AIX

* * @return Check result * 检查结果 */ public static boolean isAIX() { return OPERATE_SYSTEM_NAME.toLowerCase().indexOf("aix") > 0; } /** *

Check current operating system is OS390

*

判断当前操作系统为OS390

* * @return Check result * 检查结果 */ public static boolean isOS390() { return OPERATE_SYSTEM_NAME.toLowerCase().indexOf("os/390") > 0; } /** *

Check current operating system is FreeBSD

*

判断当前操作系统为FreeBSD

* * @return Check result * 检查结果 */ public static boolean isFreeBSD() { return OPERATE_SYSTEM_NAME.toLowerCase().indexOf("freebsd") > 0; } /** *

Check current operating system is Irix

*

判断当前操作系统为Irix

* * @return Check result * 检查结果 */ public static boolean isIrix() { return OPERATE_SYSTEM_NAME.toLowerCase().indexOf("irix") > 0; } /** *

Check current operating system is Digital Unix

*

判断当前操作系统为Digital Unix

* * @return Check result * 检查结果 */ public static boolean isDigitalUnix() { return OPERATE_SYSTEM_NAME.toLowerCase().indexOf("digital unix") > 0; } /** *

Check current operating system is Netware

*

判断当前操作系统为Netware

* * @return Check result * 检查结果 */ public static boolean isNetware() { return OPERATE_SYSTEM_NAME.toLowerCase().indexOf("netware") > 0; } /** *

Check current operating system is OSF1

*

判断当前操作系统为OSF1

* * @return Check result * 检查结果 */ public static boolean isOSF1() { return OPERATE_SYSTEM_NAME.toLowerCase().indexOf("osf1") > 0; } /** *

Check current operating system is OpenVMS

*

判断当前操作系统为OpenVMS

* * @return Check result * 检查结果 */ public static boolean isOpenVMS() { return OPERATE_SYSTEM_NAME.toLowerCase().indexOf("openvms") > 0; } /** *

Retrieve default certificate library path

*

读取当前默认证书库的路径

* * @return Certificate library path * 证书库的路径 */ public static String systemCertPath() { return JAVA_HOME + JAVA_CERT_PATH; } /** *

Retrieve current network interface MAC address

*

读取当前系统网卡的物理地址

* * @return MAC address data bytes * 网卡物理地址的二进制数组 */ public static byte[] localMac() { byte[] macAddress = null; try { final InetAddress localHost = InetAddress.getLocalHost(); try { final NetworkInterface localInterface = NetworkInterface.getByInetAddress(localHost); if (localInterface != null && !localInterface.isLoopback() && localInterface.isUp()) { macAddress = localInterface.getHardwareAddress(); } if (macAddress == null) { final Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements() && macAddress == null) { final NetworkInterface networkInterface = networkInterfaces.nextElement(); if (networkInterface != null && !networkInterface.isLoopback() && networkInterface.isUp()) { macAddress = networkInterface.getHardwareAddress(); } } } } catch (final SocketException e) { LOGGER.error("Retrieve_MAC_System_Error", e); } if (macAddress == null || macAddress.length == 0) { // Take only 6 bytes if the address is an IPv6 otherwise will pad with two zero bytes macAddress = Arrays.copyOf(localHost.getAddress(), 6); } } catch (final UnknownHostException ignored) { // ignored } return macAddress; } /** *

Generate system identified ID

*

生成系统唯一标识ID

* * @return Generated ID string * 生成的ID字符串 */ private static String generateIdentifiedKey() { try { List networkList = retrieveNetworkList(); List macAddressList = new ArrayList<>(); for (NetworkInfo networkInfo : networkList) { if (!networkInfo.isVirtual() && networkInfo.getMacAddress().length() > 0 && !macAddressList.contains(networkInfo.getMacAddress())) { macAddressList.add(networkInfo.getMacAddress()); } } Collections.sort(macAddressList); return ConvertUtils.toHex(SecurityUtils.SHA256(macAddressList)); } catch (Exception e) { LOGGER.error("Generate_Identified_ID_System_Error"); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Stack_Message_Error", e); } } return null; } /** *

Retrieve system local network adapter list

*

获取系统物理网络适配器列表

* * @return Local network adapter info list * 物理网络适配器列表 * * @throws SocketException * Retrieve network interfaces error * 获取网络适配器时发生错误 */ private static List retrieveNetworkList() throws SocketException { List networkList = new ArrayList<>(); Enumeration enumeration = NetworkInterface.getNetworkInterfaces(); while (enumeration.hasMoreElements()) { try { networkList.add(new NetworkInfo(enumeration.nextElement())); } catch (NetworkInfoException e) { LOGGER.error("Retrieve_Network_Interface_System_Error"); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Stack_Message_Error", e); } } } return networkList; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy