-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor common features of JDK...Watcher into common JDKBaseWatch
#13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d860b15
Rename `JDK...Watcher` to `JDK...Watch` (to align with existing `Acti…
sungshik 21890c7
Extract common base class from `JDK...Watch` classes
sungshik 8a97a4a
Extract start method from from `JDK...Watch` classes into base class
sungshik 28ba48c
Move event translation function from `JDKDirectoryWatch` to base clas…
sungshik a9251f0
Use extracted start method in base class to simplify `Watcher`
sungshik 175bf5e
Fix exception handler logic to use the fallback recursive directory w…
sungshik b9761bb
Fix Checker Framework errors
sungshik b216116
Add license
sungshik f9904b6
Remove now-redundant start method in `JDKRecursiveDirectoryWatch`
sungshik 7c8482f
Fix some warnings in the test code
sungshik b87ea2c
Fix some warnings in the test code
sungshik 1e9c9ca
Rename a few variables
sungshik 377df56
Revert change that didn't improve code quality that much
sungshik 333e7ef
Merge branch 'main' into jdk-base-watcher
sungshik 5c4b337
Fix relative path of overflow events
sungshik 8da56d1
Make `JDKBaseWatch` responsible for run-only-once management
sungshik 5cf298c
Simplify code to initialize paths in constructor of `JDKFileWatch`
sungshik fee22fd
Rename in `JDKBaseWatch`: (a) `start` to `open`; (b) `run` to `start`
sungshik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
src/main/java/engineering/swat/watch/impl/jdk/JDKBaseWatch.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /* | ||
| * 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.jdk; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.StandardWatchEventKinds; | ||
| import java.util.concurrent.Executor; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.function.Consumer; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
|
||
| import engineering.swat.watch.ActiveWatch; | ||
| import engineering.swat.watch.WatchEvent; | ||
|
|
||
| public abstract class JDKBaseWatch implements ActiveWatch { | ||
| private final Logger logger = LogManager.getLogger(); | ||
|
|
||
| protected final Path path; | ||
| protected final Executor exec; | ||
| protected final Consumer<WatchEvent> eventHandler; | ||
| protected final AtomicBoolean started = new AtomicBoolean(); | ||
|
|
||
| protected JDKBaseWatch(Path path, Executor exec, Consumer<WatchEvent> eventHandler) { | ||
| this.path = path; | ||
| this.exec = exec; | ||
| this.eventHandler = eventHandler; | ||
| } | ||
|
|
||
| public void open() throws IOException { | ||
| try { | ||
| if (!startIfFirstTime()) { | ||
| throw new IllegalStateException("Could not restart already-started watch for: " + path); | ||
| } | ||
| logger.debug("Started watch for: {}", path); | ||
| } catch (Exception e) { | ||
| throw new IOException("Could not start watch for: " + path, e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Starts this watch. | ||
| * | ||
| * @throws IOException When an I/O exception of some sort has occurred | ||
| * (e.g., a nested watch failed to start) | ||
| */ | ||
| protected abstract void start() throws IOException; | ||
|
|
||
| /** | ||
| * Starts this watch if it's the first time. | ||
| * | ||
| * @return `true` iff it's the first time this method is called | ||
| * @throws IOException When an I/O exception of some sort has occurred | ||
| * (e.g., a nested watch failed to start) | ||
| */ | ||
| protected boolean startIfFirstTime() throws IOException { | ||
| if (started.compareAndSet(false, true)) { | ||
| start(); | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| protected WatchEvent translate(java.nio.file.WatchEvent<?> jdkEvent) { | ||
| WatchEvent.Kind kind; | ||
| if (jdkEvent.kind() == StandardWatchEventKinds.ENTRY_CREATE) { | ||
| kind = WatchEvent.Kind.CREATED; | ||
| } | ||
| else if (jdkEvent.kind() == StandardWatchEventKinds.ENTRY_MODIFY) { | ||
| kind = WatchEvent.Kind.MODIFIED; | ||
| } | ||
| else if (jdkEvent.kind() == StandardWatchEventKinds.ENTRY_DELETE) { | ||
| kind = WatchEvent.Kind.DELETED; | ||
| } | ||
| else if (jdkEvent.kind() == StandardWatchEventKinds.OVERFLOW) { | ||
| kind = WatchEvent.Kind.OVERFLOW; | ||
| } | ||
| else { | ||
| throw new IllegalArgumentException("Unexpected watch event: " + jdkEvent); | ||
| } | ||
| var rootPath = path; | ||
| var relativePath = kind == WatchEvent.Kind.OVERFLOW ? Path.of("") : (@Nullable Path)jdkEvent.context(); | ||
|
|
||
| var event = new WatchEvent(kind, rootPath, relativePath); | ||
| logger.trace("Translated: {} to {}", jdkEvent, event); | ||
| return event; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.