-
Notifications
You must be signed in to change notification settings - Fork 45
/
FileWatcher.java
123 lines (112 loc) · 4.04 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package ru.yoomoney.tech.dbqueue.settings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.ThreadSafe;
import java.io.File;
import java.io.IOException;
import java.nio.file.ClosedWatchServiceException;
import java.nio.file.Path;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Provides tracking changes in a target file
*
* @author Oleg Kandaurov
* @since 12.10.2021
*/
@ThreadSafe
class FileWatcher {
private static final Logger log = LoggerFactory.getLogger(FileWatcher.class);
@Nonnull
private final ExecutorService executor;
@Nonnull
private final Path watchedFile;
@Nonnull
private final Path watchedDir;
@Nonnull
private final Runnable onChangeCallback;
private WatchService watchServiceFileDir;
/**
* Constructor
*
* @param watchedFile file to watch
* @param onChangeCallback callback invoked on file change
*/
FileWatcher(@Nonnull Path watchedFile,
@Nonnull Runnable onChangeCallback) {
this.onChangeCallback = Objects.requireNonNull(onChangeCallback, "onChangeCallback must not be null");
this.executor = Executors.newSingleThreadExecutor();
this.watchedFile = Objects.requireNonNull(watchedFile, "watchedFile must not be null");
this.watchedDir = watchedFile.getParent();
if (watchedDir == null) {
throw new IllegalArgumentException("directory of watched file is empty");
}
if (!watchedFile.toFile().isFile()) {
throw new IllegalArgumentException("watched file is not a file: file=" + watchedFile);
}
}
/**
* Start track file changes
*/
synchronized void startWatch() {
log.info("Starting watch for file changes: file={}", watchedFile);
try {
startWatchFileDirectory();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Stop track file changes
*/
synchronized void stopWatch() {
try {
log.info("Stopping watch for file changes: file={}", watchedFile);
if (watchServiceFileDir != null) {
watchServiceFileDir.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private synchronized void startWatchFileDirectory() throws IOException {
if (watchServiceFileDir != null) {
watchServiceFileDir.close();
}
watchServiceFileDir = watchedDir.getFileSystem().newWatchService();
watchedDir.register(
watchServiceFileDir,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY
);
executor.execute(() -> doWatch(watchServiceFileDir, watchedFile.toFile(), onChangeCallback));
}
private static void doWatch(WatchService watchService, File file, Runnable callback) {
try {
WatchKey watchKey;
while ((watchKey = watchService.take()) != null) {
List<WatchEvent<?>> polledEvents = watchKey.pollEvents();
boolean fileModified = polledEvents
.stream()
.filter(watchEvent -> !Objects.equals(watchEvent.kind(), StandardWatchEventKinds.OVERFLOW))
.map(watchEvent -> watchEvent.context().toString())
.anyMatch(fileName -> fileName.equals(file.getName()));
if (fileModified) {
callback.run();
}
watchKey.reset();
}
} catch (InterruptedException exc) {
Thread.currentThread().interrupt();
} catch (ClosedWatchServiceException ignored) {
}
}
}