/
FileWatcher.java
71 lines (59 loc) · 1.89 KB
/
FileWatcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package mezz.jei.core.config.file;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.nio.file.Path;
public class FileWatcher {
private static final Logger LOGGER = LogManager.getLogger();
private @Nullable FileWatcherThread thread;
public FileWatcher(String threadName) {
this.thread = createThread(threadName);
}
@Nullable
private static FileWatcherThread createThread(String threadName) {
try {
return new FileWatcherThread(threadName);
} catch (UnsupportedOperationException | IOException e) {
LOGGER.error("Unable to create file watcher: ", e);
return null;
}
}
/**
* @param path a config file to watch
* @param callback a callbacks to call when the file changes.
* Callbacks must be thread-safe, they will be called from this thread.
*/
public void addCallback(Path path, Runnable callback) {
if (thread != null) {
this.thread.addCallback(path, callback);
}
}
/**
* Start the file watcher thread
*/
public void start() {
if (thread != null) {
this.thread.start();
}
}
/**
* Stop the file watcher thread and clear all callbacks.
*/
public void reset() {
if (this.thread == null) {
return;
}
String threadName = this.thread.getName();
this.thread.interrupt();
try {
this.thread.join(1000);
} catch (InterruptedException consumed) {
Thread.currentThread().interrupt();
}
if (this.thread.isAlive()) {
LOGGER.error("File Watcher thread could not be stopped and will be abandoned.");
}
this.thread = createThread(threadName);
}
}