Search in sources :

Example 1 with FileWatcher

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;
}
Also used : java.util(java.util) Getter(lombok.Getter) ApplicationException(org.rx.exception.ApplicationException) NonNull(lombok.NonNull) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Extends.values(org.rx.core.Extends.values) InvalidException(org.rx.exception.InvalidException) Extends.as(org.rx.core.Extends.as) CollectionUtils(org.apache.commons.collections4.CollectionUtils) File(java.io.File) Yaml(org.yaml.snakeyaml.Yaml) Slf4j(lombok.extern.slf4j.Slf4j) FileWatcher(org.rx.io.FileWatcher) Files(org.rx.io.Files) JSONObject(com.alibaba.fastjson.JSONObject) FileStream(org.rx.io.FileStream) ErrorCode(org.rx.annotation.ErrorCode) InputStream(java.io.InputStream) FileWatcher(org.rx.io.FileWatcher) InvalidException(org.rx.exception.InvalidException) FileStream(org.rx.io.FileStream) Yaml(org.yaml.snakeyaml.Yaml)

Aggregations

JSONObject (com.alibaba.fastjson.JSONObject)1 File (java.io.File)1 InputStream (java.io.InputStream)1 java.util (java.util)1 Getter (lombok.Getter)1 NonNull (lombok.NonNull)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 Slf4j (lombok.extern.slf4j.Slf4j)1 CollectionUtils (org.apache.commons.collections4.CollectionUtils)1 ErrorCode (org.rx.annotation.ErrorCode)1 Extends.as (org.rx.core.Extends.as)1 Extends.values (org.rx.core.Extends.values)1 ApplicationException (org.rx.exception.ApplicationException)1 InvalidException (org.rx.exception.InvalidException)1 FileStream (org.rx.io.FileStream)1 FileWatcher (org.rx.io.FileWatcher)1 Files (org.rx.io.Files)1 Yaml (org.yaml.snakeyaml.Yaml)1