This commit is contained in:
Looly 2021-10-29 02:37:27 +08:00
parent 090905ec59
commit 735325e870
5 changed files with 165 additions and 132 deletions

View File

@ -41,6 +41,8 @@
* 【core 】 ZipUtil增加append方法pr#441@Gitee
* 【core 】 CollUtil增加重载issue#I4E9FS@Gitee
* 【core 】 CopyOptions新增setFieldValueEditorissue#I4E08T@Gitee
* 【core 】 增加SystemPropsUtilissue#1918@Gitee
* 【core 】 增加`hutool.date.lenient`系统属性issue#1918@Gitee
### 🐞Bug修复
* 【core 】 修复CollUtil.isEqualList两个null返回错误问题issue#1885@Github

View File

@ -7,6 +7,7 @@ import cn.hutool.core.date.format.GlobalCustomFormat;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.SystemPropsUtil;
import java.sql.Timestamp;
import java.text.DateFormat;
@ -288,7 +289,7 @@ public class DateTime extends Date {
* @see DatePattern
*/
public DateTime(CharSequence dateStr, DateParser dateParser) {
this(dateStr, dateParser, true);
this(dateStr, dateParser, SystemPropsUtil.getBoolean(SystemPropsUtil.HUTOOL_DATE_LENIENT, true));
}
/**

View File

@ -0,0 +1,146 @@
package cn.hutool.core.util;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Console;
import java.util.Properties;
/**
* 系统属性工具<br>
* 此工具用于读取系统属性或环境变量信息封装包括
* <ul>
* <li>{@link System#getProperty(String)}</li>
* <li>{@link System#getenv(String)}</li>
* </ul>
*
* @author looly
* @since 5.7.16
*/
public class SystemPropsUtil {
/** Hutool自定义系统属性是否解析日期字符串采用严格模式 */
public static String HUTOOL_DATE_LENIENT = "hutool.date.lenient";
/**
* 取得系统属性如果因为Java安全的限制而失败则将错误打在Log中然后返回 defaultValue
*
* @param name 属性名
* @param defaultValue 默认值
* @return 属性值或defaultValue
* @see System#getProperty(String)
* @see System#getenv(String)
*/
public static String get(String name, String defaultValue) {
return StrUtil.nullToDefault(get(name, false), defaultValue);
}
/**
* 取得系统属性如果因为Java安全的限制而失败则将错误打在Log中然后返回 {@code null}
*
* @param name 属性名
* @param quiet 安静模式不将出错信息打在{@code System.err}
* @return 属性值或{@code null}
* @see System#getProperty(String)
* @see System#getenv(String)
*/
public static String get(String name, boolean quiet) {
String value = null;
try {
value = System.getProperty(name);
} catch (SecurityException e) {
if (false == quiet) {
Console.error("Caught a SecurityException reading the system property '{}'; " +
"the SystemUtil property value will default to null.", name);
}
}
if (null == value) {
try {
value = System.getenv(name);
} catch (SecurityException e) {
if (false == quiet) {
Console.error("Caught a SecurityException reading the system env '{}'; " +
"the SystemUtil env value will default to null.", name);
}
}
}
return value;
}
/**
* 获得System属性
*
* @param key
* @return 属性值
* @see System#getProperty(String)
* @see System#getenv(String)
*/
public static String get(String key) {
return get(key, null);
}
/**
* 获得boolean类型值
*
* @param key
* @param defaultValue 默认值
* @return
*/
public static boolean getBoolean(String key, boolean defaultValue) {
String value = get(key);
if (value == null) {
return defaultValue;
}
value = value.trim().toLowerCase();
if (value.isEmpty()) {
return true;
}
return Convert.toBool(value, defaultValue);
}
/**
* 获得int类型值
*
* @param key
* @param defaultValue 默认值
* @return
*/
public static long getInt(String key, int defaultValue) {
return Convert.toInt(get(key), defaultValue);
}
/**
* 获得long类型值
*
* @param key
* @param defaultValue 默认值
* @return
*/
public static long getLong(String key, long defaultValue) {
return Convert.toLong(get(key), defaultValue);
}
/**
* @return 属性列表
*/
public static Properties getProps() {
return System.getProperties();
}
/**
* 设置系统属性value为{@code null}表示移除此属性
*
* @param key 属性名
* @param value 属性值{@code null}表示移除此属性
*/
public static void set(String key, String value) {
if (null == value) {
System.clearProperty(key);
} else {
System.setProperty(key, value);
}
}
}

View File

@ -8,6 +8,7 @@ import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.SystemPropsUtil;
import cn.hutool.log.Log;
import java.io.BufferedReader;
@ -230,13 +231,9 @@ public class SettingLoader {
varValue = this.groupedMap.get(groupAndKey.get(0), groupAndKey.get(1));
}
}
// 系统参数中查找
// 系统参数和环境变量中查找
if (null == varValue) {
varValue = System.getProperty(key);
}
// 环境变量中查找
if (null == varValue) {
varValue = System.getenv(key);
varValue = SystemPropsUtil.get(key);
}
if (null != varValue) {

View File

@ -1,9 +1,9 @@
package cn.hutool.system;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.Singleton;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.SystemPropsUtil;
import java.io.PrintWriter;
import java.lang.management.ClassLoadingMXBean;
@ -17,7 +17,6 @@ import java.lang.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadMXBean;
import java.util.List;
import java.util.Properties;
/**
* Java的System类封装工具类<br>
@ -25,7 +24,7 @@ import java.util.Properties;
*
* @author Looly
*/
public class SystemUtil {
public class SystemUtil extends SystemPropsUtil {
// ----- Java运行时环境信息 -----/
/**
@ -149,117 +148,6 @@ public class SystemUtil {
*/
public final static String USER_DIR = SystemPropsKeys.USER_DIR;
// ----------------------------------------------------------------------- Basic start
/**
* 取得系统属性如果因为Java安全的限制而失败则将错误打在Log中然后返回 defaultValue
*
* @param name 属性名
* @param defaultValue 默认值
* @return 属性值或defaultValue
* @see System#getProperty(String)
* @see System#getenv(String)
*/
public static String get(String name, String defaultValue) {
return StrUtil.nullToDefault(get(name, false), defaultValue);
}
/**
* 取得系统属性如果因为Java安全的限制而失败则将错误打在Log中然后返回 {@code null}
*
* @param name 属性名
* @param quiet 安静模式不将出错信息打在{@code System.err}
* @return 属性值或{@code null}
* @see System#getProperty(String)
* @see System#getenv(String)
*/
public static String get(String name, boolean quiet) {
String value = null;
try {
value = System.getProperty(name);
} catch (SecurityException e) {
if (false == quiet) {
Console.error("Caught a SecurityException reading the system property '{}'; " +
"the SystemUtil property value will default to null.", name);
}
}
if (null == value) {
try {
value = System.getenv(name);
} catch (SecurityException e) {
if (false == quiet) {
Console.error("Caught a SecurityException reading the system env '{}'; " +
"the SystemUtil env value will default to null.", name);
}
}
}
return value;
}
/**
* 获得System属性
*
* @param key
* @return 属性值
* @see System#getProperty(String)
* @see System#getenv(String)
*/
public static String get(String key) {
return get(key, null);
}
/**
* 获得boolean类型值
*
* @param key
* @param defaultValue 默认值
* @return
*/
public static boolean getBoolean(String key, boolean defaultValue) {
String value = get(key);
if (value == null) {
return defaultValue;
}
value = value.trim().toLowerCase();
if (value.isEmpty()) {
return true;
}
return Convert.toBool(value, defaultValue);
}
/**
* 获得int类型值
*
* @param key
* @param defaultValue 默认值
* @return
*/
public static long getInt(String key, int defaultValue) {
return Convert.toInt(get(key), defaultValue);
}
/**
* 获得long类型值
*
* @param key
* @param defaultValue 默认值
* @return
*/
public static long getLong(String key, long defaultValue) {
return Convert.toLong(get(key), defaultValue);
}
/**
* @return 属性列表
*/
public static Properties props() {
return System.getProperties();
}
/**
* 获取当前进程 PID
*
@ -268,7 +156,6 @@ public class SystemUtil {
public static long getCurrentPID() {
return Long.parseLong(getRuntimeMXBean().getName().split("@")[0]);
}
// ----------------------------------------------------------------------- Basic end
/**
* 返回Java虚拟机类加载系统相关属性