mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add util
This commit is contained in:
parent
090905ec59
commit
735325e870
@ -41,6 +41,8 @@
|
||||
* 【core 】 ZipUtil增加append方法(pr#441@Gitee)
|
||||
* 【core 】 CollUtil增加重载(issue#I4E9FS@Gitee)
|
||||
* 【core 】 CopyOptions新增setFieldValueEditor(issue#I4E08T@Gitee)
|
||||
* 【core 】 增加SystemPropsUtil(issue#1918@Gitee)
|
||||
* 【core 】 增加`hutool.date.lenient`系统属性(issue#1918@Gitee)
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复CollUtil.isEqualList两个null返回错误问题(issue#1885@Github)
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
@ -24,7 +25,7 @@ import java.util.Set;
|
||||
|
||||
/**
|
||||
* Setting文件加载器
|
||||
*
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
@ -47,7 +48,7 @@ public class SettingLoader {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
*
|
||||
* @param groupedMap GroupedMap
|
||||
*/
|
||||
public SettingLoader(GroupedMap groupedMap) {
|
||||
@ -56,7 +57,7 @@ public class SettingLoader {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
*
|
||||
* @param groupedMap GroupedMap
|
||||
* @param charset 编码
|
||||
* @param isUseVariable 是否使用变量
|
||||
@ -69,7 +70,7 @@ public class SettingLoader {
|
||||
|
||||
/**
|
||||
* 加载设置文件
|
||||
*
|
||||
*
|
||||
* @param resource 配置文件URL
|
||||
* @return 加载是否成功
|
||||
*/
|
||||
@ -93,7 +94,7 @@ public class SettingLoader {
|
||||
|
||||
/**
|
||||
* 加载设置文件。 此方法不会关闭流对象
|
||||
*
|
||||
*
|
||||
* @param settingStream 文件流
|
||||
* @return 加载成功与否
|
||||
* @throws IOException IO异常
|
||||
@ -146,7 +147,7 @@ public class SettingLoader {
|
||||
/**
|
||||
* 设置变量的正则<br>
|
||||
* 正则只能有一个group表示变量本身,剩余为字符 例如 \$\{(name)\}表示${name}变量名为name的一个变量表示
|
||||
*
|
||||
*
|
||||
* @param regex 正则
|
||||
*/
|
||||
public void setVarRegex(String regex) {
|
||||
@ -155,7 +156,7 @@ public class SettingLoader {
|
||||
|
||||
/**
|
||||
* 赋值分隔符(用于分隔键值对)
|
||||
*
|
||||
*
|
||||
* @param assignFlag 正则
|
||||
* @since 4.6.5
|
||||
*/
|
||||
@ -166,7 +167,7 @@ public class SettingLoader {
|
||||
/**
|
||||
* 持久化当前设置,会覆盖掉之前的设置<br>
|
||||
* 持久化会不会保留之前的分组
|
||||
*
|
||||
*
|
||||
* @param absolutePath 设置文件的绝对路径
|
||||
*/
|
||||
public void store(String absolutePath) {
|
||||
@ -194,7 +195,7 @@ public class SettingLoader {
|
||||
|
||||
/**
|
||||
* 存储到Writer
|
||||
*
|
||||
*
|
||||
* @param writer Writer
|
||||
*/
|
||||
synchronized private void store(PrintWriter writer) {
|
||||
@ -209,7 +210,7 @@ public class SettingLoader {
|
||||
// ----------------------------------------------------------------------------------- Private method start
|
||||
/**
|
||||
* 替换给定值中的变量标识
|
||||
*
|
||||
*
|
||||
* @param group 所在分组
|
||||
* @param value 值
|
||||
* @return 替换后的字符串
|
||||
@ -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) {
|
||||
|
@ -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虚拟机类加载系统相关属性
|
||||
|
Loading…
x
Reference in New Issue
Block a user