From 942521862dbf9caae80b05d0a6236304fc6d918b Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 10 Apr 2020 15:59:16 +0800 Subject: [PATCH] fix monitor --- CHANGELOG.md | 1 + .../java/cn/hutool/core/io/watch/WatchKind.java | 10 ++++++++++ .../cn/hutool/core/io/watch/WatchMonitor.java | 16 +++++----------- .../cn/hutool/core/io/watch/WatchServer.java | 6 ++++-- .../hutool/http/server/HttpServerResponse.java | 5 ++++- 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b9a5945b3..d36acb35f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ * 【extra 】 修复模板中无效引用的问题 * 【extra 】 修复读取JSON文本配置未应用到子对象的问题(issue#818@Github) * 【extra 】 修复XmlUtil.createXml中namespace反向问题 +* 【core 】 修复WatchMonitor默认无event问题 ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/io/watch/WatchKind.java b/hutool-core/src/main/java/cn/hutool/core/io/watch/WatchKind.java index e49dd7f0b..546dd09ba 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/watch/WatchKind.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/watch/WatchKind.java @@ -35,6 +35,16 @@ public enum WatchKind { */ DELETE(StandardWatchEventKinds.ENTRY_DELETE); + /** + * 全部事件 + */ + public static final WatchEvent.Kind[] ALL = {// + OVERFLOW.getValue(), //事件丢失 + MODIFY.getValue(), //修改 + CREATE.getValue(), //创建 + DELETE.getValue() //删除 + }; + private WatchEvent.Kind value; /** diff --git a/hutool-core/src/main/java/cn/hutool/core/io/watch/WatchMonitor.java b/hutool-core/src/main/java/cn/hutool/core/io/watch/WatchMonitor.java index 6cca21fcf..daaefe156 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/watch/WatchMonitor.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/watch/WatchMonitor.java @@ -15,7 +15,6 @@ import java.nio.file.Files; import java.nio.file.LinkOption; import java.nio.file.Path; import java.nio.file.Paths; -import java.nio.file.StandardWatchEventKinds; import java.nio.file.WatchEvent; import java.nio.file.WatchService; @@ -35,28 +34,23 @@ public class WatchMonitor extends WatchServer { /** * 事件丢失 */ - public static final WatchEvent.Kind OVERFLOW = StandardWatchEventKinds.OVERFLOW; + public static final WatchEvent.Kind OVERFLOW = WatchKind.OVERFLOW.getValue(); /** * 修改事件 */ - public static final WatchEvent.Kind ENTRY_MODIFY = StandardWatchEventKinds.ENTRY_MODIFY; + public static final WatchEvent.Kind ENTRY_MODIFY = WatchKind.MODIFY.getValue(); /** * 创建事件 */ - public static final WatchEvent.Kind ENTRY_CREATE = StandardWatchEventKinds.ENTRY_CREATE; + public static final WatchEvent.Kind ENTRY_CREATE = WatchKind.CREATE.getValue(); /** * 删除事件 */ - public static final WatchEvent.Kind ENTRY_DELETE = StandardWatchEventKinds.ENTRY_DELETE; + public static final WatchEvent.Kind ENTRY_DELETE = WatchKind.DELETE.getValue(); /** * 全部事件 */ - public static final WatchEvent.Kind[] EVENTS_ALL = {// - OVERFLOW, //事件丢失 - ENTRY_MODIFY, //修改 - ENTRY_CREATE, //创建 - ENTRY_DELETE //删除 - }; + public static final WatchEvent.Kind[] EVENTS_ALL = WatchKind.ALL; /** * 监听路径,必须为目录 diff --git a/hutool-core/src/main/java/cn/hutool/core/io/watch/WatchServer.java b/hutool-core/src/main/java/cn/hutool/core/io/watch/WatchServer.java index 57ee1f4af..31ffa0e8a 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/watch/WatchServer.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/watch/WatchServer.java @@ -95,12 +95,14 @@ public class WatchServer extends Thread implements Closeable, Serializable { * @param maxDepth 递归下层目录的最大深度 */ public void registerPath(Path path, int maxDepth) { + final WatchEvent.Kind[] kinds = ArrayUtil.defaultIfEmpty(this.events, WatchKind.ALL); + try { final WatchKey key; if (ArrayUtil.isEmpty(this.modifiers)) { - key = path.register(this.watchService, this.events); + key = path.register(this.watchService, kinds); } else { - key = path.register(this.watchService, this.events, this.modifiers); + key = path.register(this.watchService, kinds, this.modifiers); } watchKeyPathMap.put(key, path); diff --git a/hutool-http/src/main/java/cn/hutool/http/server/HttpServerResponse.java b/hutool-http/src/main/java/cn/hutool/http/server/HttpServerResponse.java index 85966760a..2b82b144d 100644 --- a/hutool-http/src/main/java/cn/hutool/http/server/HttpServerResponse.java +++ b/hutool-http/src/main/java/cn/hutool/http/server/HttpServerResponse.java @@ -255,7 +255,8 @@ public class HttpServerResponse extends HttpServerBase { /** * 写出数据到客户端 * - * @param data 数据 + * @param data 数据 + * @param contentType Content-Type类型 * @return this */ public HttpServerResponse write(String data, String contentType) { @@ -301,6 +302,7 @@ public class HttpServerResponse extends HttpServerBase { * * @param in 需要返回客户端的内容 * @param contentType 返回的类型 + * @return this * @since 5.2.6 */ public HttpServerResponse write(InputStream in, String contentType) { @@ -330,6 +332,7 @@ public class HttpServerResponse extends HttpServerBase { * 返回文件给客户端(文件下载) * * @param file 写出的文件对象 + * @return this * @since 5.2.6 */ public HttpServerResponse write(File file) {