change setting

This commit is contained in:
Looly 2021-09-06 09:30:17 +08:00
parent 186eeb8054
commit 87b0a0df97
3 changed files with 26 additions and 28 deletions

View File

@ -10,6 +10,7 @@
* 【extra 】 SpringUtil增加getApplicationName、publishEvent方法issue#I485NZ@Gitee * 【extra 】 SpringUtil增加getApplicationName、publishEvent方法issue#I485NZ@Gitee
* 【core 】 BeanUtil.getProperty增加判空issue#I488HA@Gitee * 【core 】 BeanUtil.getProperty增加判空issue#I488HA@Gitee
* 【core 】 OptionalBean弃用pr#1182@Github * 【core 】 OptionalBean弃用pr#1182@Github
* 【setting】 Setting、Props持有URL改为持有Resourcepr#1182@Github
### 🐞Bug修复 ### 🐞Bug修复

View File

@ -81,9 +81,9 @@ public class Setting extends AbsSetting implements Map<String, String> {
*/ */
protected boolean isUseVariable; protected boolean isUseVariable;
/** /**
* 设定文件的URL * 设定文件的资源
*/ */
protected URL settingUrl; protected Resource resource;
private SettingLoader settingLoader; private SettingLoader settingLoader;
private WatchMonitor watchMonitor; private WatchMonitor watchMonitor;
@ -187,10 +187,8 @@ public class Setting extends AbsSetting implements Map<String, String> {
* @return 成功初始化与否 * @return 成功初始化与否
*/ */
public boolean init(Resource resource, Charset charset, boolean isUseVariable) { public boolean init(Resource resource, Charset charset, boolean isUseVariable) {
if (resource == null) { Assert.notNull(resource, "Setting resource must be not null!");
throw new NullPointerException("Null setting resource define!"); this.resource = resource;
}
this.settingUrl = resource.getUrl();
this.charset = charset; this.charset = charset;
this.isUseVariable = isUseVariable; this.isUseVariable = isUseVariable;
@ -206,7 +204,7 @@ public class Setting extends AbsSetting implements Map<String, String> {
if (null == this.settingLoader) { if (null == this.settingLoader) {
settingLoader = new SettingLoader(this.groupedMap, this.charset, this.isUseVariable); settingLoader = new SettingLoader(this.groupedMap, this.charset, this.isUseVariable);
} }
return settingLoader.load(new UrlResource(this.settingUrl)); return settingLoader.load(this.resource);
} }
/** /**
@ -226,12 +224,12 @@ public class Setting extends AbsSetting implements Map<String, String> {
*/ */
public void autoLoad(boolean autoReload, Consumer<Boolean> callback) { public void autoLoad(boolean autoReload, Consumer<Boolean> callback) {
if (autoReload) { if (autoReload) {
Assert.notNull(this.settingUrl, "Setting URL is null !"); Assert.notNull(this.resource, "Setting resource must be not null !");
if (null != this.watchMonitor) { if (null != this.watchMonitor) {
// 先关闭之前的监听 // 先关闭之前的监听
this.watchMonitor.close(); this.watchMonitor.close();
} }
this.watchMonitor = WatchUtil.createModify(this.settingUrl, new SimpleWatcher() { this.watchMonitor = WatchUtil.createModify(resource.getUrl(), new SimpleWatcher() {
@Override @Override
public void onModify(WatchEvent<?> event, Path currentPath) { public void onModify(WatchEvent<?> event, Path currentPath) {
boolean success = load(); boolean success = load();
@ -242,7 +240,7 @@ public class Setting extends AbsSetting implements Map<String, String> {
} }
}); });
this.watchMonitor.start(); this.watchMonitor.start();
StaticLog.debug("Auto load for [{}] listenning...", this.settingUrl); StaticLog.debug("Auto load for [{}] listenning...", this.resource.getUrl());
} else { } else {
IoUtil.close(this.watchMonitor); IoUtil.close(this.watchMonitor);
this.watchMonitor = null; this.watchMonitor = null;
@ -256,7 +254,7 @@ public class Setting extends AbsSetting implements Map<String, String> {
* @since 5.4.3 * @since 5.4.3
*/ */
public URL getSettingUrl() { public URL getSettingUrl() {
return this.settingUrl; return (null == this.resource) ? null : this.resource.getUrl();
} }
/** /**
@ -265,7 +263,8 @@ public class Setting extends AbsSetting implements Map<String, String> {
* @return 获得设定文件的路径 * @return 获得设定文件的路径
*/ */
public String getSettingPath() { public String getSettingPath() {
return (null == this.settingUrl) ? null : this.settingUrl.getPath(); final URL settingUrl = getSettingUrl();
return (null == settingUrl) ? null : settingUrl.getPath();
} }
/** /**
@ -376,8 +375,9 @@ public class Setting extends AbsSetting implements Map<String, String> {
* @since 5.4.3 * @since 5.4.3
*/ */
public void store() { public void store() {
Assert.notNull(this.settingUrl, "Setting path must be not null !"); final URL resourceUrl = getSettingUrl();
store(FileUtil.file(this.settingUrl)); Assert.notNull(resourceUrl, "Setting path must be not null !");
store(FileUtil.file(resourceUrl));
} }
/** /**
@ -751,7 +751,7 @@ public class Setting extends AbsSetting implements Map<String, String> {
result = prime * result + ((charset == null) ? 0 : charset.hashCode()); result = prime * result + ((charset == null) ? 0 : charset.hashCode());
result = prime * result + groupedMap.hashCode(); result = prime * result + groupedMap.hashCode();
result = prime * result + (isUseVariable ? 1231 : 1237); result = prime * result + (isUseVariable ? 1231 : 1237);
result = prime * result + ((settingUrl == null) ? 0 : settingUrl.hashCode()); result = prime * result + ((this.resource == null) ? 0 : this.resource.hashCode());
return result; return result;
} }
@ -780,10 +780,10 @@ public class Setting extends AbsSetting implements Map<String, String> {
if (isUseVariable != other.isUseVariable) { if (isUseVariable != other.isUseVariable) {
return false; return false;
} }
if (settingUrl == null) { if (this.resource == null) {
return other.settingUrl == null; return other.resource == null;
} else { } else {
return settingUrl.equals(other.settingUrl); return resource.equals(other.resource);
} }
} }

View File

@ -21,7 +21,6 @@ import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.log.StaticLog; import cn.hutool.log.StaticLog;
import cn.hutool.setting.SettingRuntimeException;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
@ -62,9 +61,9 @@ public final class Props extends Properties implements BasicTypeGetter<String>,
// ----------------------------------------------------------------------- 私有属性 start // ----------------------------------------------------------------------- 私有属性 start
/** /**
* 属性文件的URL * 属性文件的Resource
*/ */
private URL propertiesFileUrl; private Resource resource;
private WatchMonitor watchMonitor; private WatchMonitor watchMonitor;
/** /**
* properties文件编码<br> * properties文件编码<br>
@ -275,10 +274,8 @@ public final class Props extends Properties implements BasicTypeGetter<String>,
* @param resource {@link Resource} * @param resource {@link Resource}
*/ */
public void load(Resource resource) { public void load(Resource resource) {
this.propertiesFileUrl = resource.getUrl(); Assert.notNull(resource, "Props resource must be not null!");
if (null == this.propertiesFileUrl) { this.resource = resource;
throw new SettingRuntimeException("Can not find properties file: [{}]", resource);
}
try (final BufferedReader reader = resource.getReader(charset)) { try (final BufferedReader reader = resource.getReader(charset)) {
super.load(reader); super.load(reader);
@ -291,7 +288,7 @@ public final class Props extends Properties implements BasicTypeGetter<String>,
* 重新加载配置文件 * 重新加载配置文件
*/ */
public void load() { public void load() {
this.load(this.propertiesFileUrl); this.load(this.resource);
} }
/** /**
@ -301,12 +298,12 @@ public final class Props extends Properties implements BasicTypeGetter<String>,
*/ */
public void autoLoad(boolean autoReload) { public void autoLoad(boolean autoReload) {
if (autoReload) { if (autoReload) {
Assert.notNull(this.propertiesFileUrl, "Properties URL is null !"); Assert.notNull(this.resource, "Properties resource must be not null!");
if (null != this.watchMonitor) { if (null != this.watchMonitor) {
// 先关闭之前的监听 // 先关闭之前的监听
this.watchMonitor.close(); this.watchMonitor.close();
} }
this.watchMonitor = WatchUtil.createModify(this.propertiesFileUrl, new SimpleWatcher() { this.watchMonitor = WatchUtil.createModify(this.resource.getUrl(), new SimpleWatcher() {
@Override @Override
public void onModify(WatchEvent<?> event, Path currentPath) { public void onModify(WatchEvent<?> event, Path currentPath) {
load(); load();