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
* 【core 】 BeanUtil.getProperty增加判空issue#I488HA@Gitee
* 【core 】 OptionalBean弃用pr#1182@Github
* 【setting】 Setting、Props持有URL改为持有Resourcepr#1182@Github
### 🐞Bug修复

View File

@ -81,9 +81,9 @@ public class Setting extends AbsSetting implements Map<String, String> {
*/
protected boolean isUseVariable;
/**
* 设定文件的URL
* 设定文件的资源
*/
protected URL settingUrl;
protected Resource resource;
private SettingLoader settingLoader;
private WatchMonitor watchMonitor;
@ -187,10 +187,8 @@ public class Setting extends AbsSetting implements Map<String, String> {
* @return 成功初始化与否
*/
public boolean init(Resource resource, Charset charset, boolean isUseVariable) {
if (resource == null) {
throw new NullPointerException("Null setting resource define!");
}
this.settingUrl = resource.getUrl();
Assert.notNull(resource, "Setting resource must be not null!");
this.resource = resource;
this.charset = charset;
this.isUseVariable = isUseVariable;
@ -206,7 +204,7 @@ public class Setting extends AbsSetting implements Map<String, String> {
if (null == this.settingLoader) {
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) {
if (autoReload) {
Assert.notNull(this.settingUrl, "Setting URL is null !");
Assert.notNull(this.resource, "Setting resource must be not null !");
if (null != this.watchMonitor) {
// 先关闭之前的监听
this.watchMonitor.close();
}
this.watchMonitor = WatchUtil.createModify(this.settingUrl, new SimpleWatcher() {
this.watchMonitor = WatchUtil.createModify(resource.getUrl(), new SimpleWatcher() {
@Override
public void onModify(WatchEvent<?> event, Path currentPath) {
boolean success = load();
@ -242,7 +240,7 @@ public class Setting extends AbsSetting implements Map<String, String> {
}
});
this.watchMonitor.start();
StaticLog.debug("Auto load for [{}] listenning...", this.settingUrl);
StaticLog.debug("Auto load for [{}] listenning...", this.resource.getUrl());
} else {
IoUtil.close(this.watchMonitor);
this.watchMonitor = null;
@ -256,7 +254,7 @@ public class Setting extends AbsSetting implements Map<String, String> {
* @since 5.4.3
*/
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 获得设定文件的路径
*/
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
*/
public void store() {
Assert.notNull(this.settingUrl, "Setting path must be not null !");
store(FileUtil.file(this.settingUrl));
final URL resourceUrl = getSettingUrl();
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 + groupedMap.hashCode();
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;
}
@ -780,10 +780,10 @@ public class Setting extends AbsSetting implements Map<String, String> {
if (isUseVariable != other.isUseVariable) {
return false;
}
if (settingUrl == null) {
return other.settingUrl == null;
if (this.resource == null) {
return other.resource == null;
} 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.StrUtil;
import cn.hutool.log.StaticLog;
import cn.hutool.setting.SettingRuntimeException;
import java.io.BufferedReader;
import java.io.File;
@ -62,9 +61,9 @@ public final class Props extends Properties implements BasicTypeGetter<String>,
// ----------------------------------------------------------------------- 私有属性 start
/**
* 属性文件的URL
* 属性文件的Resource
*/
private URL propertiesFileUrl;
private Resource resource;
private WatchMonitor watchMonitor;
/**
* properties文件编码<br>
@ -275,10 +274,8 @@ public final class Props extends Properties implements BasicTypeGetter<String>,
* @param resource {@link Resource}
*/
public void load(Resource resource) {
this.propertiesFileUrl = resource.getUrl();
if (null == this.propertiesFileUrl) {
throw new SettingRuntimeException("Can not find properties file: [{}]", resource);
}
Assert.notNull(resource, "Props resource must be not null!");
this.resource = resource;
try (final BufferedReader reader = resource.getReader(charset)) {
super.load(reader);
@ -291,7 +288,7 @@ public final class Props extends Properties implements BasicTypeGetter<String>,
* 重新加载配置文件
*/
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) {
if (autoReload) {
Assert.notNull(this.propertiesFileUrl, "Properties URL is null !");
Assert.notNull(this.resource, "Properties resource must be not null!");
if (null != this.watchMonitor) {
// 先关闭之前的监听
this.watchMonitor.close();
}
this.watchMonitor = WatchUtil.createModify(this.propertiesFileUrl, new SimpleWatcher() {
this.watchMonitor = WatchUtil.createModify(this.resource.getUrl(), new SimpleWatcher() {
@Override
public void onModify(WatchEvent<?> event, Path currentPath) {
load();