mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
bca0f82bac
commit
03b8f57648
@ -19,7 +19,6 @@ import org.dromara.hutool.core.text.CharUtil;
|
|||||||
import org.dromara.hutool.core.text.StrUtil;
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
|
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.WatchEvent;
|
import java.nio.file.WatchEvent;
|
||||||
@ -47,7 +46,7 @@ public class WatchMonitor extends Thread implements Closeable, Serializable {
|
|||||||
/**
|
/**
|
||||||
* 监听的文件,对于单文件监听不为空
|
* 监听的文件,对于单文件监听不为空
|
||||||
*/
|
*/
|
||||||
private Path filePath;
|
private Path file;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 递归目录的最大深度,当小于1时不递归下层目录
|
* 递归目录的最大深度,当小于1时不递归下层目录
|
||||||
@ -61,7 +60,7 @@ public class WatchMonitor extends Thread implements Closeable, Serializable {
|
|||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
*
|
*
|
||||||
* @param dir 字符串路径
|
* @param dir 字符串路径
|
||||||
* @param events 监听事件列表
|
* @param events 监听事件列表
|
||||||
*/
|
*/
|
||||||
public WatchMonitor(final Path dir, final WatchEvent.Kind<?>... events) {
|
public WatchMonitor(final Path dir, final WatchEvent.Kind<?>... events) {
|
||||||
@ -77,7 +76,7 @@ public class WatchMonitor extends Thread implements Closeable, Serializable {
|
|||||||
* maxDepth = 3 表示监听当前目录以及下两层
|
* maxDepth = 3 表示监听当前目录以及下两层
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param dir 字符串路径
|
* @param dir 路径
|
||||||
* @param maxDepth 递归目录的最大深度,当小于2时不递归下层目录
|
* @param maxDepth 递归目录的最大深度,当小于2时不递归下层目录
|
||||||
* @param events 监听事件列表
|
* @param events 监听事件列表
|
||||||
*/
|
*/
|
||||||
@ -150,6 +149,11 @@ public class WatchMonitor extends Thread implements Closeable, Serializable {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
this.watchService.close();
|
||||||
|
}
|
||||||
|
|
||||||
//------------------------------------------------------ private method start
|
//------------------------------------------------------ private method start
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -171,8 +175,8 @@ public class WatchMonitor extends Thread implements Closeable, Serializable {
|
|||||||
final String lastPathEleStr = lastPathEle.toString();
|
final String lastPathEleStr = lastPathEle.toString();
|
||||||
//带有点表示有扩展名,按照未创建的文件对待。Linux下.d的为目录,排除之
|
//带有点表示有扩展名,按照未创建的文件对待。Linux下.d的为目录,排除之
|
||||||
if (StrUtil.contains(lastPathEleStr, CharUtil.DOT) && !StrUtil.endWithIgnoreCase(lastPathEleStr, ".d")) {
|
if (StrUtil.contains(lastPathEleStr, CharUtil.DOT) && !StrUtil.endWithIgnoreCase(lastPathEleStr, ".d")) {
|
||||||
this.filePath = this.dir;
|
this.file = this.dir;
|
||||||
this.dir = this.filePath.getParent();
|
this.dir = this.file.getParent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -180,8 +184,8 @@ public class WatchMonitor extends Thread implements Closeable, Serializable {
|
|||||||
PathUtil.mkdir(this.dir);
|
PathUtil.mkdir(this.dir);
|
||||||
} else if (PathUtil.isFile(this.dir, false)) {
|
} else if (PathUtil.isFile(this.dir, false)) {
|
||||||
// 文件路径
|
// 文件路径
|
||||||
this.filePath = this.dir;
|
this.file = this.dir;
|
||||||
this.dir = this.filePath.getParent();
|
this.dir = this.file.getParent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,19 +195,14 @@ public class WatchMonitor extends Thread implements Closeable, Serializable {
|
|||||||
* @param watcher {@link Watcher}
|
* @param watcher {@link Watcher}
|
||||||
*/
|
*/
|
||||||
private void doTakeAndWatch(final Watcher watcher) {
|
private void doTakeAndWatch(final Watcher watcher) {
|
||||||
this.watchService.watch(watcher, watchEvent -> null == filePath || filePath.endsWith(watchEvent.context().toString()));
|
this.watchService.watch(watcher, watchEvent -> null == file || file.endsWith(watchEvent.context().toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注册监听路径
|
* 注册监听路径
|
||||||
*/
|
*/
|
||||||
private void registerPath() {
|
private void registerPath() {
|
||||||
this.watchService.registerPath(this.dir, (null != this.filePath) ? 0 : this.maxDepth);
|
this.watchService.registerPath(this.dir, (null != this.file) ? 0 : this.maxDepth);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void close() {
|
|
||||||
this.watchService.close();
|
|
||||||
}
|
}
|
||||||
//------------------------------------------------------ private method end
|
//------------------------------------------------------ private method end
|
||||||
}
|
}
|
||||||
|
@ -264,14 +264,13 @@ public class WatchUtil {
|
|||||||
* @param watcher {@link Watcher}
|
* @param watcher {@link Watcher}
|
||||||
* @return {@link WatchMonitor}
|
* @return {@link WatchMonitor}
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("resource")
|
||||||
public static WatchMonitor ofAll(final Path path, final int maxDepth, final Watcher watcher) {
|
public static WatchMonitor ofAll(final Path path, final int maxDepth, final Watcher watcher) {
|
||||||
final WatchMonitor watchMonitor = of(path, maxDepth, WatchKind.ALL);
|
return of(path, maxDepth, WatchKind.ALL).setWatcher(watcher);
|
||||||
watchMonitor.setWatcher(watcher);
|
|
||||||
return watchMonitor;
|
|
||||||
}
|
}
|
||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
// region ----- createModify
|
// region ----- ofModify
|
||||||
/**
|
/**
|
||||||
* 创建并初始化监听,监听修改事件
|
* 创建并初始化监听,监听修改事件
|
||||||
*
|
*
|
||||||
@ -280,8 +279,8 @@ public class WatchUtil {
|
|||||||
* @return {@link WatchMonitor}
|
* @return {@link WatchMonitor}
|
||||||
* @since 4.5.2
|
* @since 4.5.2
|
||||||
*/
|
*/
|
||||||
public static WatchMonitor createModify(final URL url, final Watcher watcher) {
|
public static WatchMonitor ofModify(final URL url, final Watcher watcher) {
|
||||||
return createModify(url, 0, watcher);
|
return ofModify(url, 0, watcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -293,8 +292,8 @@ public class WatchUtil {
|
|||||||
* @return {@link WatchMonitor}
|
* @return {@link WatchMonitor}
|
||||||
* @since 4.5.2
|
* @since 4.5.2
|
||||||
*/
|
*/
|
||||||
public static WatchMonitor createModify(final URL url, final int maxDepth, final Watcher watcher) {
|
public static WatchMonitor ofModify(final URL url, final int maxDepth, final Watcher watcher) {
|
||||||
return createModify(UrlUtil.toURI(url), maxDepth, watcher);
|
return ofModify(UrlUtil.toURI(url), maxDepth, watcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -305,8 +304,8 @@ public class WatchUtil {
|
|||||||
* @return {@link WatchMonitor}
|
* @return {@link WatchMonitor}
|
||||||
* @since 4.5.2
|
* @since 4.5.2
|
||||||
*/
|
*/
|
||||||
public static WatchMonitor createModify(final URI uri, final Watcher watcher) {
|
public static WatchMonitor ofModify(final URI uri, final Watcher watcher) {
|
||||||
return createModify(uri, 0, watcher);
|
return ofModify(uri, 0, watcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -318,8 +317,8 @@ public class WatchUtil {
|
|||||||
* @return {@link WatchMonitor}
|
* @return {@link WatchMonitor}
|
||||||
* @since 4.5.2
|
* @since 4.5.2
|
||||||
*/
|
*/
|
||||||
public static WatchMonitor createModify(final URI uri, final int maxDepth, final Watcher watcher) {
|
public static WatchMonitor ofModify(final URI uri, final int maxDepth, final Watcher watcher) {
|
||||||
return createModify(Paths.get(uri), maxDepth, watcher);
|
return ofModify(Paths.get(uri), maxDepth, watcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -330,8 +329,8 @@ public class WatchUtil {
|
|||||||
* @return {@link WatchMonitor}
|
* @return {@link WatchMonitor}
|
||||||
* @since 4.5.2
|
* @since 4.5.2
|
||||||
*/
|
*/
|
||||||
public static WatchMonitor createModify(final File file, final Watcher watcher) {
|
public static WatchMonitor ofModify(final File file, final Watcher watcher) {
|
||||||
return createModify(file, 0, watcher);
|
return ofModify(file, 0, watcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -343,8 +342,8 @@ public class WatchUtil {
|
|||||||
* @return {@link WatchMonitor}
|
* @return {@link WatchMonitor}
|
||||||
* @since 4.5.2
|
* @since 4.5.2
|
||||||
*/
|
*/
|
||||||
public static WatchMonitor createModify(final File file, final int maxDepth, final Watcher watcher) {
|
public static WatchMonitor ofModify(final File file, final int maxDepth, final Watcher watcher) {
|
||||||
return createModify(file.toPath(), maxDepth, watcher);
|
return ofModify(file.toPath(), maxDepth, watcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -355,8 +354,8 @@ public class WatchUtil {
|
|||||||
* @return {@link WatchMonitor}
|
* @return {@link WatchMonitor}
|
||||||
* @since 4.5.2
|
* @since 4.5.2
|
||||||
*/
|
*/
|
||||||
public static WatchMonitor createModify(final String path, final Watcher watcher) {
|
public static WatchMonitor ofModify(final String path, final Watcher watcher) {
|
||||||
return createModify(path, 0, watcher);
|
return ofModify(path, 0, watcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -368,8 +367,8 @@ public class WatchUtil {
|
|||||||
* @return {@link WatchMonitor}
|
* @return {@link WatchMonitor}
|
||||||
* @since 4.5.2
|
* @since 4.5.2
|
||||||
*/
|
*/
|
||||||
public static WatchMonitor createModify(final String path, final int maxDepth, final Watcher watcher) {
|
public static WatchMonitor ofModify(final String path, final int maxDepth, final Watcher watcher) {
|
||||||
return createModify(Paths.get(path), maxDepth, watcher);
|
return ofModify(Paths.get(path), maxDepth, watcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -380,8 +379,8 @@ public class WatchUtil {
|
|||||||
* @return {@link WatchMonitor}
|
* @return {@link WatchMonitor}
|
||||||
* @since 4.5.2
|
* @since 4.5.2
|
||||||
*/
|
*/
|
||||||
public static WatchMonitor createModify(final Path path, final Watcher watcher) {
|
public static WatchMonitor ofModify(final Path path, final Watcher watcher) {
|
||||||
return createModify(path, 0, watcher);
|
return ofModify(path, 0, watcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -393,7 +392,7 @@ public class WatchUtil {
|
|||||||
* @return {@link WatchMonitor}
|
* @return {@link WatchMonitor}
|
||||||
* @since 4.5.2
|
* @since 4.5.2
|
||||||
*/
|
*/
|
||||||
public static WatchMonitor createModify(final Path path, final int maxDepth, final Watcher watcher) {
|
public static WatchMonitor ofModify(final Path path, final int maxDepth, final Watcher watcher) {
|
||||||
final WatchMonitor watchMonitor = of(path, maxDepth, WatchKind.MODIFY.getValue());
|
final WatchMonitor watchMonitor = of(path, maxDepth, WatchKind.MODIFY.getValue());
|
||||||
watchMonitor.setWatcher(watcher);
|
watchMonitor.setWatcher(watcher);
|
||||||
return watchMonitor;
|
return watchMonitor;
|
||||||
|
@ -25,7 +25,7 @@ public interface Watcher {
|
|||||||
* 文件创建时执行的方法
|
* 文件创建时执行的方法
|
||||||
*
|
*
|
||||||
* @param event 事件
|
* @param event 事件
|
||||||
* @param key 事件发生的{@link WatchKey}
|
* @param key 事件发生的{@link WatchKey},可以通过{@link WatchKey#watchable()}获取监听的Path路径
|
||||||
*/
|
*/
|
||||||
void onCreate(WatchEvent<?> event, WatchKey key);
|
void onCreate(WatchEvent<?> event, WatchKey key);
|
||||||
|
|
||||||
@ -34,7 +34,7 @@ public interface Watcher {
|
|||||||
* 文件修改可能触发多次
|
* 文件修改可能触发多次
|
||||||
*
|
*
|
||||||
* @param event 事件
|
* @param event 事件
|
||||||
* @param key 事件发生的{@link WatchKey}
|
* @param key 事件发生的{@link WatchKey},可以通过{@link WatchKey#watchable()}获取监听的Path路径
|
||||||
*/
|
*/
|
||||||
void onModify(WatchEvent<?> event, WatchKey key);
|
void onModify(WatchEvent<?> event, WatchKey key);
|
||||||
|
|
||||||
@ -42,7 +42,7 @@ public interface Watcher {
|
|||||||
* 文件删除时执行的方法
|
* 文件删除时执行的方法
|
||||||
*
|
*
|
||||||
* @param event 事件
|
* @param event 事件
|
||||||
* @param key 事件发生的{@link WatchKey}
|
* @param key 事件发生的{@link WatchKey},可以通过{@link WatchKey#watchable()}获取监听的Path路径
|
||||||
*/
|
*/
|
||||||
void onDelete(WatchEvent<?> event, WatchKey key);
|
void onDelete(WatchEvent<?> event, WatchKey key);
|
||||||
|
|
||||||
@ -50,7 +50,7 @@ public interface Watcher {
|
|||||||
* 事件丢失或出错时执行的方法
|
* 事件丢失或出错时执行的方法
|
||||||
*
|
*
|
||||||
* @param event 事件
|
* @param event 事件
|
||||||
* @param key 事件发生的{@link WatchKey}
|
* @param key 事件发生的{@link WatchKey},可以通过{@link WatchKey#watchable()}获取监听的Path路径
|
||||||
*/
|
*/
|
||||||
void onOverflow(WatchEvent<?> event, WatchKey key);
|
void onOverflow(WatchEvent<?> event, WatchKey key);
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ import org.dromara.hutool.core.io.watch.watchers.DelayWatcher;
|
|||||||
import org.dromara.hutool.core.io.watch.watchers.SimpleWatcher;
|
import org.dromara.hutool.core.io.watch.watchers.SimpleWatcher;
|
||||||
import org.dromara.hutool.core.lang.Console;
|
import org.dromara.hutool.core.lang.Console;
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.nio.file.WatchEvent;
|
import java.nio.file.WatchEvent;
|
||||||
import java.nio.file.WatchKey;
|
import java.nio.file.WatchKey;
|
||||||
|
|
||||||
@ -35,6 +36,7 @@ public class WatchMonitorTest {
|
|||||||
@Override
|
@Override
|
||||||
public void onCreate(final WatchEvent<?> event, final WatchKey key) {
|
public void onCreate(final WatchEvent<?> event, final WatchKey key) {
|
||||||
final Object obj = event.context();
|
final Object obj = event.context();
|
||||||
|
Console.log(((Path)obj).toAbsolutePath());
|
||||||
Console.log("创建:{}-> {}", key.watchable(), obj);
|
Console.log("创建:{}-> {}", key.watchable(), obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -213,7 +213,7 @@ public class Setting extends AbsSetting implements Map<String, String> {
|
|||||||
Assert.notNull(this.resource, "Setting resource must be not null !");
|
Assert.notNull(this.resource, "Setting resource must be not null !");
|
||||||
// 先关闭之前的监听
|
// 先关闭之前的监听
|
||||||
IoUtil.closeQuietly(this.watchMonitor);
|
IoUtil.closeQuietly(this.watchMonitor);
|
||||||
this.watchMonitor = WatchUtil.createModify(resource.getUrl(), new SimpleWatcher() {
|
this.watchMonitor = WatchUtil.ofModify(resource.getUrl(), new SimpleWatcher() {
|
||||||
@Override
|
@Override
|
||||||
public void onModify(final WatchEvent<?> event, final WatchKey key) {
|
public void onModify(final WatchEvent<?> event, final WatchKey key) {
|
||||||
final boolean success = load();
|
final boolean success = load();
|
||||||
|
@ -228,11 +228,9 @@ public final class Props extends Properties implements TypeGetter<CharSequence>
|
|||||||
public void autoLoad(final boolean autoReload) {
|
public void autoLoad(final boolean autoReload) {
|
||||||
if (autoReload) {
|
if (autoReload) {
|
||||||
Assert.notNull(this.resource, "Properties resource must be not null!");
|
Assert.notNull(this.resource, "Properties resource must be not null!");
|
||||||
if (null != this.watchMonitor) {
|
// 先关闭之前的监听
|
||||||
// 先关闭之前的监听
|
IoUtil.closeQuietly(this.watchMonitor);
|
||||||
this.watchMonitor.close();
|
this.watchMonitor = WatchUtil.ofModify(this.resource.getUrl(), new SimpleWatcher() {
|
||||||
}
|
|
||||||
this.watchMonitor = WatchUtil.createModify(this.resource.getUrl(), new SimpleWatcher() {
|
|
||||||
@Override
|
@Override
|
||||||
public void onModify(final WatchEvent<?> event, final WatchKey key) {
|
public void onModify(final WatchEvent<?> event, final WatchKey key) {
|
||||||
load();
|
load();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user