use of java.nio.file.WatchEvent in project sldeditor by robward-scisys.
the class FileSystemWatcher method internal_watchDirectoryPath.
/**
* Internal watch directory path method.
*
* @throws IOException Signals that an I/O exception has occurred.
*/
private void internal_watchDirectoryPath() throws IOException {
WatchKey key = null;
boolean stopPolling = false;
// Poll for events in an infinite loop
for (; ; ) {
try {
// The take method waits till watch service receives a
// notification
key = watchService.take();
} catch (InterruptedException e) {
// Ignore
}
// once a key is obtained, we poll for events on that key
List<WatchEvent<?>> keys = key.pollEvents();
for (WatchEvent<?> watchEvent : keys) {
Kind<?> watchEventKind = watchEvent.kind();
// kind overflow is returned. We ignore this case for now
if (watchEventKind == StandardWatchEventKinds.OVERFLOW) {
continue;
}
Path dir = (Path) key.watchable();
Path fullPath = dir.resolve((Path) watchEvent.context());
FileWatcherUpdateInterface parentObj = watcherMap.get(key);
if (watchEventKind == StandardWatchEventKinds.ENTRY_CREATE) {
// A new file has been created
if (parentObj != null) {
parentObj.fileAdded(fullPath);
}
} else if (watchEventKind == StandardWatchEventKinds.ENTRY_MODIFY) {
ReloadManager.getInstance().fileModified(fullPath);
// The file has been modified.
if (parentObj != null) {
parentObj.fileModified(fullPath);
}
} else if (watchEventKind == StandardWatchEventKinds.ENTRY_DELETE) {
if (parentObj != null) {
parentObj.fileDeleted(fullPath);
}
}
// Reset the key so the further key events may be polled
key.reset();
}
if (stopPolling) {
break;
}
}
// Close the watcher service
watchService.close();
}
use of java.nio.file.WatchEvent in project opennms by OpenNMS.
the class DirectoryWatcher method run.
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
@Override
@SuppressWarnings("unchecked")
public void run() {
LOG.info("starting run loop");
try (WatchService watcher = m_path.getFileSystem().newWatchService()) {
LOG.debug("registering create watcher on {}", m_path.toAbsolutePath().toString());
m_path.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
LOG.debug("watcher registration complete for {}", m_path.toAbsolutePath().toString());
synchronized (this) {
this.notifyAll();
}
while (true) {
if (m_thread.isInterrupted()) {
break;
}
WatchKey key = null;
try {
LOG.debug("waiting for create event");
key = watcher.take();
LOG.debug("got an event, process it");
} catch (InterruptedException ie) {
LOG.info("interruped, must be time to shut down...");
break;
}
for (WatchEvent<?> watchEvent : key.pollEvents()) {
WatchEvent.Kind<?> kind = watchEvent.kind();
Path pathChanged = ((WatchEvent<Path>) watchEvent).context();
final String fileName = pathChanged.toString();
final File file = new File(m_directory, fileName);
if (file.isDirectory()) {
// Ignoring changes on directories.
LOG.debug("{} is a directory, ignoring.", file);
continue;
}
if (kind == StandardWatchEventKinds.OVERFLOW) {
LOG.debug("overflow receiving, ignoring changes.");
continue;
} else if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
LOG.info("file '{}' created. Ignoring...", fileName);
} else if (kind == StandardWatchEventKinds.ENTRY_MODIFY) {
LOG.info("file '{}' modified. Removing entry from cache.", fileName);
m_contents.remove(fileName);
} else if (kind == StandardWatchEventKinds.ENTRY_DELETE) {
LOG.info("file '{}' deleted. Removing entry from cache.", fileName);
m_contents.remove(fileName);
}
// IMPORTANT: The key must be reset after processed
if (!key.reset()) {
break;
}
}
}
} catch (IOException ioe) {
LOG.error(ioe.getMessage(), ioe);
}
LOG.info("existing run loop");
}
use of java.nio.file.WatchEvent in project LuckPerms by lucko.
the class FileWatcher method run.
@Override
public void run() {
long expireTime = System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(4);
// was either processed last time, or recently modified by the system.
this.internalChanges.values().removeIf(lastChange -> lastChange < expireTime);
List<String> expired = new ArrayList<>();
for (Map.Entry<String, WatchedLocation> ent : this.keyMap.entrySet()) {
String id = ent.getKey();
Path path = ent.getValue().getPath();
WatchKey key = ent.getValue().getKey();
List<WatchEvent<?>> watchEvents = key.pollEvents();
for (WatchEvent<?> event : watchEvents) {
Path context = (Path) event.context();
if (context == null) {
continue;
}
Path file = path.resolve(context);
String fileName = context.toString();
// ignore temporary changes
if (fileName.endsWith(".tmp")) {
continue;
}
if (this.internalChanges.containsKey(id + "/" + fileName)) {
// This file was modified by the system.
continue;
}
this.internalChanges.put(id + "/" + fileName, System.currentTimeMillis());
this.plugin.getLogger().info("[FileWatcher] Detected change in file: " + file.toString());
// Process the change
ent.getValue().getFileConsumer().accept(fileName);
}
boolean valid = key.reset();
if (!valid) {
new RuntimeException("WatchKey no longer valid: " + key.toString()).printStackTrace();
expired.add(id);
}
}
expired.forEach(this.keyMap::remove);
}
use of java.nio.file.WatchEvent in project rxjava-file by davidmoten.
the class FileObservableTest method testCreateAndModifyEventsForANonDirectoryFilePollEveryInterval.
@Test
public void testCreateAndModifyEventsForANonDirectoryFilePollEveryInterval() throws InterruptedException, IOException {
File file = new File("target/f");
Observable<WatchEvent<?>> events = FileObservable.from(file).kind(ENTRY_MODIFY).kind(ENTRY_CREATE).pollInterval(100, TimeUnit.MILLISECONDS).events();
checkCreateAndModifyEvents(file, events);
}
use of java.nio.file.WatchEvent in project rxjava-file by davidmoten.
the class FileObservableTest method testCreateAndModifyEventsForANonDirectoryFileBlockingPollEveryInterval.
@Test
public void testCreateAndModifyEventsForANonDirectoryFileBlockingPollEveryInterval() throws InterruptedException, IOException {
File file = new File("target/f");
Observable<WatchEvent<?>> events = FileObservable.from(file).kind(ENTRY_MODIFY).kind(ENTRY_CREATE).pollInterval(100, TimeUnit.MILLISECONDS).pollDuration(100, TimeUnit.MILLISECONDS).events();
checkCreateAndModifyEvents(file, events);
}
Aggregations