use of org.rx.io.FileWatcher in project rxlib by RockyLOMO.
the class YamlConfiguration method enableWatch.
public synchronized YamlConfiguration enableWatch(@NonNull String outputFile) {
if (watcher != null) {
throw new InvalidException("Already watched");
}
if (!yaml.isEmpty()) {
try (FileStream fs = new FileStream(outputFile)) {
fs.setPosition(0);
fs.writeString(new Yaml().dumpAsMap(yaml));
fs.flip();
}
}
watcher = new FileWatcher(Files.getFullPath(this.outputFile = outputFile), p -> p.toString().equals(this.outputFile));
watcher.onChanged.combine((s, e) -> {
String filePath = e.getPath().toString();
log.info("Config changing {} {} -> {}", e.isCreate(), filePath, yaml);
synchronized (this) {
yaml.clear();
if (!e.isDelete()) {
write(filePath);
}
}
log.info("Config changed {} {} -> {}", e.isCreate(), filePath, yaml);
raiseEvent(onChanged, new ChangedEventArgs(filePath));
});
return this;
}
Aggregations