From 03b8f57648a946d4d4fb6b131638d8e7de630af7 Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 29 Dec 2023 12:38:14 +0800 Subject: [PATCH] fix code --- .../hutool/core/io/watch/WatchMonitor.java | 29 ++++++------ .../hutool/core/io/watch/WatchUtil.java | 45 +++++++++---------- .../dromara/hutool/core/io/watch/Watcher.java | 8 ++-- .../hutool/core/io/WatchMonitorTest.java | 2 + .../org/dromara/hutool/setting/Setting.java | 2 +- .../dromara/hutool/setting/props/Props.java | 8 ++-- 6 files changed, 46 insertions(+), 48 deletions(-) diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/WatchMonitor.java b/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/WatchMonitor.java index 83ec7c3ee..b3f17c99a 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/WatchMonitor.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/WatchMonitor.java @@ -19,7 +19,6 @@ import org.dromara.hutool.core.text.CharUtil; import org.dromara.hutool.core.text.StrUtil; import java.io.Closeable; -import java.io.IOException; import java.io.Serializable; import java.nio.file.Path; import java.nio.file.WatchEvent; @@ -47,7 +46,7 @@ public class WatchMonitor extends Thread implements Closeable, Serializable { /** * 监听的文件,对于单文件监听不为空 */ - private Path filePath; + private Path file; /** * 递归目录的最大深度,当小于1时不递归下层目录 @@ -61,7 +60,7 @@ public class WatchMonitor extends Thread implements Closeable, Serializable { /** * 构造 * - * @param dir 字符串路径 + * @param dir 字符串路径 * @param events 监听事件列表 */ public WatchMonitor(final Path dir, final WatchEvent.Kind... events) { @@ -77,7 +76,7 @@ public class WatchMonitor extends Thread implements Closeable, Serializable { * maxDepth = 3 表示监听当前目录以及下两层 * * - * @param dir 字符串路径 + * @param dir 路径 * @param maxDepth 递归目录的最大深度,当小于2时不递归下层目录 * @param events 监听事件列表 */ @@ -150,6 +149,11 @@ public class WatchMonitor extends Thread implements Closeable, Serializable { return this; } + @Override + public void close() { + this.watchService.close(); + } + //------------------------------------------------------ private method start /** @@ -171,8 +175,8 @@ public class WatchMonitor extends Thread implements Closeable, Serializable { final String lastPathEleStr = lastPathEle.toString(); //带有点表示有扩展名,按照未创建的文件对待。Linux下.d的为目录,排除之 if (StrUtil.contains(lastPathEleStr, CharUtil.DOT) && !StrUtil.endWithIgnoreCase(lastPathEleStr, ".d")) { - this.filePath = this.dir; - this.dir = this.filePath.getParent(); + this.file = this.dir; + this.dir = this.file.getParent(); } } @@ -180,8 +184,8 @@ public class WatchMonitor extends Thread implements Closeable, Serializable { PathUtil.mkdir(this.dir); } else if (PathUtil.isFile(this.dir, false)) { // 文件路径 - this.filePath = this.dir; - this.dir = this.filePath.getParent(); + this.file = this.dir; + this.dir = this.file.getParent(); } } @@ -191,19 +195,14 @@ public class WatchMonitor extends Thread implements Closeable, Serializable { * @param watcher {@link 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() { - this.watchService.registerPath(this.dir, (null != this.filePath) ? 0 : this.maxDepth); - } - - @Override - public void close() { - this.watchService.close(); + this.watchService.registerPath(this.dir, (null != this.file) ? 0 : this.maxDepth); } //------------------------------------------------------ private method end } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/WatchUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/WatchUtil.java index 8cd77b636..24563e3e3 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/WatchUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/WatchUtil.java @@ -264,14 +264,13 @@ public class WatchUtil { * @param watcher {@link Watcher} * @return {@link WatchMonitor} */ + @SuppressWarnings("resource") public static WatchMonitor ofAll(final Path path, final int maxDepth, final Watcher watcher) { - final WatchMonitor watchMonitor = of(path, maxDepth, WatchKind.ALL); - watchMonitor.setWatcher(watcher); - return watchMonitor; + return of(path, maxDepth, WatchKind.ALL).setWatcher(watcher); } // endregion - // region ----- createModify + // region ----- ofModify /** * 创建并初始化监听,监听修改事件 * @@ -280,8 +279,8 @@ public class WatchUtil { * @return {@link WatchMonitor} * @since 4.5.2 */ - public static WatchMonitor createModify(final URL url, final Watcher watcher) { - return createModify(url, 0, watcher); + public static WatchMonitor ofModify(final URL url, final Watcher watcher) { + return ofModify(url, 0, watcher); } /** @@ -293,8 +292,8 @@ public class WatchUtil { * @return {@link WatchMonitor} * @since 4.5.2 */ - public static WatchMonitor createModify(final URL url, final int maxDepth, final Watcher watcher) { - return createModify(UrlUtil.toURI(url), maxDepth, watcher); + public static WatchMonitor ofModify(final URL url, final int maxDepth, final Watcher watcher) { + return ofModify(UrlUtil.toURI(url), maxDepth, watcher); } /** @@ -305,8 +304,8 @@ public class WatchUtil { * @return {@link WatchMonitor} * @since 4.5.2 */ - public static WatchMonitor createModify(final URI uri, final Watcher watcher) { - return createModify(uri, 0, watcher); + public static WatchMonitor ofModify(final URI uri, final Watcher watcher) { + return ofModify(uri, 0, watcher); } /** @@ -318,8 +317,8 @@ public class WatchUtil { * @return {@link WatchMonitor} * @since 4.5.2 */ - public static WatchMonitor createModify(final URI uri, final int maxDepth, final Watcher watcher) { - return createModify(Paths.get(uri), maxDepth, watcher); + public static WatchMonitor ofModify(final URI uri, final int maxDepth, final Watcher watcher) { + return ofModify(Paths.get(uri), maxDepth, watcher); } /** @@ -330,8 +329,8 @@ public class WatchUtil { * @return {@link WatchMonitor} * @since 4.5.2 */ - public static WatchMonitor createModify(final File file, final Watcher watcher) { - return createModify(file, 0, watcher); + public static WatchMonitor ofModify(final File file, final Watcher watcher) { + return ofModify(file, 0, watcher); } /** @@ -343,8 +342,8 @@ public class WatchUtil { * @return {@link WatchMonitor} * @since 4.5.2 */ - public static WatchMonitor createModify(final File file, final int maxDepth, final Watcher watcher) { - return createModify(file.toPath(), maxDepth, watcher); + public static WatchMonitor ofModify(final File file, final int maxDepth, final Watcher watcher) { + return ofModify(file.toPath(), maxDepth, watcher); } /** @@ -355,8 +354,8 @@ public class WatchUtil { * @return {@link WatchMonitor} * @since 4.5.2 */ - public static WatchMonitor createModify(final String path, final Watcher watcher) { - return createModify(path, 0, watcher); + public static WatchMonitor ofModify(final String path, final Watcher watcher) { + return ofModify(path, 0, watcher); } /** @@ -368,8 +367,8 @@ public class WatchUtil { * @return {@link WatchMonitor} * @since 4.5.2 */ - public static WatchMonitor createModify(final String path, final int maxDepth, final Watcher watcher) { - return createModify(Paths.get(path), maxDepth, watcher); + public static WatchMonitor ofModify(final String path, final int maxDepth, final Watcher watcher) { + return ofModify(Paths.get(path), maxDepth, watcher); } /** @@ -380,8 +379,8 @@ public class WatchUtil { * @return {@link WatchMonitor} * @since 4.5.2 */ - public static WatchMonitor createModify(final Path path, final Watcher watcher) { - return createModify(path, 0, watcher); + public static WatchMonitor ofModify(final Path path, final Watcher watcher) { + return ofModify(path, 0, watcher); } /** @@ -393,7 +392,7 @@ public class WatchUtil { * @return {@link WatchMonitor} * @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()); watchMonitor.setWatcher(watcher); return watchMonitor; diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/Watcher.java b/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/Watcher.java index 7d3ff8985..61ffd3c7e 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/Watcher.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/Watcher.java @@ -25,7 +25,7 @@ public interface Watcher { * 文件创建时执行的方法 * * @param event 事件 - * @param key 事件发生的{@link WatchKey} + * @param key 事件发生的{@link WatchKey},可以通过{@link WatchKey#watchable()}获取监听的Path路径 */ void onCreate(WatchEvent event, WatchKey key); @@ -34,7 +34,7 @@ public interface Watcher { * 文件修改可能触发多次 * * @param event 事件 - * @param key 事件发生的{@link WatchKey} + * @param key 事件发生的{@link WatchKey},可以通过{@link WatchKey#watchable()}获取监听的Path路径 */ void onModify(WatchEvent event, WatchKey key); @@ -42,7 +42,7 @@ public interface Watcher { * 文件删除时执行的方法 * * @param event 事件 - * @param key 事件发生的{@link WatchKey} + * @param key 事件发生的{@link WatchKey},可以通过{@link WatchKey#watchable()}获取监听的Path路径 */ void onDelete(WatchEvent event, WatchKey key); @@ -50,7 +50,7 @@ public interface Watcher { * 事件丢失或出错时执行的方法 * * @param event 事件 - * @param key 事件发生的{@link WatchKey} + * @param key 事件发生的{@link WatchKey},可以通过{@link WatchKey#watchable()}获取监听的Path路径 */ void onOverflow(WatchEvent event, WatchKey key); } diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/io/WatchMonitorTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/io/WatchMonitorTest.java index edb41b3dc..8956c6260 100644 --- a/hutool-core/src/test/java/org/dromara/hutool/core/io/WatchMonitorTest.java +++ b/hutool-core/src/test/java/org/dromara/hutool/core/io/WatchMonitorTest.java @@ -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.lang.Console; +import java.nio.file.Path; import java.nio.file.WatchEvent; import java.nio.file.WatchKey; @@ -35,6 +36,7 @@ public class WatchMonitorTest { @Override public void onCreate(final WatchEvent event, final WatchKey key) { final Object obj = event.context(); + Console.log(((Path)obj).toAbsolutePath()); Console.log("创建:{}-> {}", key.watchable(), obj); } diff --git a/hutool-setting/src/main/java/org/dromara/hutool/setting/Setting.java b/hutool-setting/src/main/java/org/dromara/hutool/setting/Setting.java index ea2d403ab..3b8d67ee5 100644 --- a/hutool-setting/src/main/java/org/dromara/hutool/setting/Setting.java +++ b/hutool-setting/src/main/java/org/dromara/hutool/setting/Setting.java @@ -213,7 +213,7 @@ public class Setting extends AbsSetting implements Map { Assert.notNull(this.resource, "Setting resource must be not null !"); // 先关闭之前的监听 IoUtil.closeQuietly(this.watchMonitor); - this.watchMonitor = WatchUtil.createModify(resource.getUrl(), new SimpleWatcher() { + this.watchMonitor = WatchUtil.ofModify(resource.getUrl(), new SimpleWatcher() { @Override public void onModify(final WatchEvent event, final WatchKey key) { final boolean success = load(); diff --git a/hutool-setting/src/main/java/org/dromara/hutool/setting/props/Props.java b/hutool-setting/src/main/java/org/dromara/hutool/setting/props/Props.java index 68aaf3bb7..58462d2e6 100644 --- a/hutool-setting/src/main/java/org/dromara/hutool/setting/props/Props.java +++ b/hutool-setting/src/main/java/org/dromara/hutool/setting/props/Props.java @@ -228,11 +228,9 @@ public final class Props extends Properties implements TypeGetter public void autoLoad(final boolean autoReload) { if (autoReload) { Assert.notNull(this.resource, "Properties resource must be not null!"); - if (null != this.watchMonitor) { - // 先关闭之前的监听 - this.watchMonitor.close(); - } - this.watchMonitor = WatchUtil.createModify(this.resource.getUrl(), new SimpleWatcher() { + // 先关闭之前的监听 + IoUtil.closeQuietly(this.watchMonitor); + this.watchMonitor = WatchUtil.ofModify(this.resource.getUrl(), new SimpleWatcher() { @Override public void onModify(final WatchEvent event, final WatchKey key) { load();