diff --git a/src/main/java/engineering/swat/watch/ActiveWatch.java b/src/main/java/engineering/swat/watch/ActiveWatch.java index 36f0a4b9..4be79e54 100644 --- a/src/main/java/engineering/swat/watch/ActiveWatch.java +++ b/src/main/java/engineering/swat/watch/ActiveWatch.java @@ -27,12 +27,17 @@ package engineering.swat.watch; import java.io.Closeable; +import java.nio.file.Path; /** - *
Marker interface for an active watch, in the future might get properties you can inspect.
+ *Marker interface for an active watch, in the future might get more properties you can inspect.
* - *For now, make sure to close the watch when not interested in new events
+ *For now, make sure to close the watch when not interested in new events.
*/ public interface ActiveWatch extends Closeable { + /** + * Gets the path watched by this watch. + */ + Path getPath(); } diff --git a/src/main/java/engineering/swat/watch/impl/EventHandlingWatch.java b/src/main/java/engineering/swat/watch/impl/EventHandlingWatch.java new file mode 100644 index 00000000..525a09eb --- /dev/null +++ b/src/main/java/engineering/swat/watch/impl/EventHandlingWatch.java @@ -0,0 +1,55 @@ +/* + * BSD 2-Clause License + * + * Copyright (c) 2023, Swat.engineering + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package engineering.swat.watch.impl; + +import engineering.swat.watch.ActiveWatch; +import engineering.swat.watch.WatchEvent; + +public interface EventHandlingWatch extends ActiveWatch { + + /** + * Handles `event`. The purpose of this method is to trigger the event + * handler of this watch "from the outside" (in addition to having native + * file system libraries trigger the event handler "from the inside"). This + * is useful to report synthetic events (e.g., while handling overflows). + */ + void handleEvent(WatchEvent event); + + /** + * Relativizes the full path of `event` against the path watched by this + * watch (as per `getPath()`). Returns a new event whose root path and + * relative path are set in accordance with the relativization. + */ + default WatchEvent relativize(WatchEvent event) { + var fullPath = event.calculateFullPath(); + + var kind = event.getKind(); + var rootPath = getPath(); + var relativePath = rootPath.relativize(fullPath); + return new WatchEvent(kind, rootPath, relativePath); + } +} diff --git a/src/main/java/engineering/swat/watch/impl/jdk/JDKBaseWatch.java b/src/main/java/engineering/swat/watch/impl/jdk/JDKBaseWatch.java index d0dc6c67..292499be 100644 --- a/src/main/java/engineering/swat/watch/impl/jdk/JDKBaseWatch.java +++ b/src/main/java/engineering/swat/watch/impl/jdk/JDKBaseWatch.java @@ -37,10 +37,10 @@ import org.apache.logging.log4j.Logger; import org.checkerframework.checker.nullness.qual.Nullable; -import engineering.swat.watch.ActiveWatch; import engineering.swat.watch.WatchEvent; +import engineering.swat.watch.impl.EventHandlingWatch; -public abstract class JDKBaseWatch implements ActiveWatch { +public abstract class JDKBaseWatch implements EventHandlingWatch { private final Logger logger = LogManager.getLogger(); protected final Path path; @@ -115,4 +115,16 @@ private WatchEvent.Kind translate(java.nio.file.WatchEvent.Kind> jdkKind) { throw new IllegalArgumentException("Unexpected watch kind: " + jdkKind); } + + // -- EventHandlingWatch -- + + @Override + public void handleEvent(WatchEvent e) { + eventHandler.accept(e); + } + + @Override + public Path getPath() { + return path; + } } diff --git a/src/main/java/engineering/swat/watch/impl/jdk/JDKDirectoryWatch.java b/src/main/java/engineering/swat/watch/impl/jdk/JDKDirectoryWatch.java index 82a8d097..79cd65c2 100644 --- a/src/main/java/engineering/swat/watch/impl/jdk/JDKDirectoryWatch.java +++ b/src/main/java/engineering/swat/watch/impl/jdk/JDKDirectoryWatch.java @@ -62,7 +62,7 @@ private void handleJDKEvents(List