2021-11-23 18:20:40 +01:00
|
|
|
package de.swingbe.mon_dir;
|
|
|
|
|
2021-11-30 21:12:05 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2021-11-23 18:20:40 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.*;
|
|
|
|
|
|
|
|
public class Main {
|
2021-11-30 21:12:05 +01:00
|
|
|
public final static Logger LOG = LoggerFactory.getLogger(Main.class);
|
|
|
|
|
|
|
|
private static final String FOLDER = "/opt/npm";
|
2021-11-23 18:20:40 +01:00
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException,
|
|
|
|
InterruptedException {
|
2021-11-30 21:41:31 +01:00
|
|
|
LOG.trace("app started...");
|
2021-11-23 18:20:40 +01:00
|
|
|
|
2021-11-30 21:41:31 +01:00
|
|
|
Path folderPath = Paths.get(FOLDER);
|
|
|
|
LOG.debug("folderPath: " + folderPath);
|
2021-11-23 18:20:40 +01:00
|
|
|
|
|
|
|
//obtain WatchService instance using FileSystems class
|
|
|
|
WatchService watchService = FileSystems.getDefault().newWatchService();
|
|
|
|
|
2021-11-29 18:01:29 +01:00
|
|
|
//register a Path instance for events using a WatchService instance
|
2021-11-30 21:41:31 +01:00
|
|
|
folderPath.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
|
2021-11-30 21:12:05 +01:00
|
|
|
StandardWatchEventKinds.ENTRY_MODIFY,
|
|
|
|
StandardWatchEventKinds.ENTRY_DELETE);
|
2021-11-23 18:20:40 +01:00
|
|
|
|
|
|
|
boolean valid = true;
|
|
|
|
do {
|
|
|
|
WatchKey watchKey = watchService.take();
|
|
|
|
|
|
|
|
for (WatchEvent event : watchKey.pollEvents()) {
|
|
|
|
WatchEvent.Kind kind = event.kind();
|
|
|
|
|
|
|
|
if (StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind())) {
|
|
|
|
String fileName = event.context().toString();
|
2021-11-30 21:12:05 +01:00
|
|
|
LOG.debug("File created: " + fileName);
|
2021-11-23 18:20:40 +01:00
|
|
|
} else if (StandardWatchEventKinds.ENTRY_MODIFY.equals(event.kind())) {
|
2021-11-29 20:30:30 +01:00
|
|
|
Path context = (Path) event.context();
|
|
|
|
String fileName = context.toString();
|
2021-11-30 21:12:05 +01:00
|
|
|
LOG.debug("File modified: " + fileName);
|
2021-11-23 18:20:40 +01:00
|
|
|
} else if (StandardWatchEventKinds.ENTRY_DELETE.equals(event.kind())) {
|
|
|
|
String fileName = event.context().toString();
|
2021-11-30 21:12:05 +01:00
|
|
|
LOG.debug("File deleted: " + fileName);
|
2021-11-23 18:20:40 +01:00
|
|
|
} else {
|
2021-11-30 21:12:05 +01:00
|
|
|
LOG.warn("Event unknown");
|
2021-11-23 18:20:40 +01:00
|
|
|
}
|
|
|
|
valid = watchKey.reset();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (valid);
|
2021-11-30 21:41:31 +01:00
|
|
|
|
|
|
|
LOG.trace("app done.");
|
2021-11-23 18:20:40 +01:00
|
|
|
}
|
2021-12-01 15:18:46 +01:00
|
|
|
}
|