-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto-load config files when the backing file changes
- Loading branch information
Showing
3 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
src/main/java/com/minelittlepony/common/util/io/PathMonitor.java
This file contains 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,113 @@ | ||
package com.minelittlepony.common.util.io; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardWatchEventKinds; | ||
import java.nio.file.WatchEvent; | ||
import java.nio.file.WatchKey; | ||
import java.nio.file.WatchEvent.Kind; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Consumer; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class PathMonitor implements AutoCloseable { | ||
private static final Logger LOGGER = LogManager.getLogger(); | ||
private static final Executor EXECUTOR = CompletableFuture.delayedExecutor(1, TimeUnit.SECONDS); | ||
|
||
private final Object locker = new Object(); | ||
|
||
private final Consumer<Event> callback; | ||
|
||
@Nullable | ||
private WatchKey key; | ||
@Nullable | ||
private Path watchedPath; | ||
|
||
private boolean paused; | ||
|
||
private final Runnable pollTask = () -> { | ||
tick(); | ||
EXECUTOR.execute(this.pollTask); | ||
}; | ||
|
||
public PathMonitor(Consumer<Event> callback) { | ||
this.callback = callback; | ||
pollTask.run(); | ||
} | ||
|
||
public void set(Path newFile) { | ||
synchronized (locker) { | ||
try { | ||
close(); | ||
watchedPath = newFile; | ||
key = newFile.getParent().register(newFile.getParent().getFileSystem().newWatchService(), | ||
StandardWatchEventKinds.ENTRY_MODIFY, | ||
StandardWatchEventKinds.ENTRY_DELETE, | ||
StandardWatchEventKinds.ENTRY_CREATE | ||
); | ||
} catch (IOException e) { | ||
close(); | ||
LOGGER.error(e); | ||
} | ||
} | ||
} | ||
|
||
public void wrap(Runnable action) { | ||
synchronized (locker) { | ||
try { | ||
paused = true; | ||
action.run(); | ||
} finally { | ||
pollEvents(); | ||
paused = false; | ||
} | ||
} | ||
} | ||
|
||
public List<WatchEvent<?>> pollEvents() { | ||
synchronized (locker) { | ||
if (key == null || !key.isValid()) { | ||
return List.of(); | ||
} | ||
|
||
return key.pollEvents(); | ||
} | ||
} | ||
|
||
public void tick() { | ||
synchronized (locker) { | ||
for (WatchEvent<?> ev : pollEvents()) { | ||
if (!paused && ev.context() instanceof Path p && (watchedPath != null && watchedPath.endsWith(p))) { | ||
Kind<?> kind = ev.kind(); | ||
|
||
if (StandardWatchEventKinds.ENTRY_DELETE.equals(kind)) { | ||
callback.accept(Event.DELETE); | ||
} else if (StandardWatchEventKinds.ENTRY_MODIFY.equals(kind) || StandardWatchEventKinds.ENTRY_CREATE.equals(kind)) { | ||
callback.accept(Event.MODIFY); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
if (key != null) { | ||
pollEvents(); | ||
key.cancel(); | ||
key = null; | ||
} | ||
watchedPath = null; | ||
} | ||
|
||
public enum Event { | ||
MODIFY, | ||
DELETE | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
* @author Sollace | ||
* | ||
*/ | ||
@Deprecated | ||
public class ClippingSpace { | ||
|
||
/** | ||
|
This file contains 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