use of java.nio.file.WatchKey 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.WatchKey in project sldeditor by robward-scisys.
the class FileSystemWatcher method addWatch.
/**
* Instantiates a new file system watcher.
*
* @param parent the parent
* @param path the path
*/
public void addWatch(FileWatcherUpdateInterface parent, Path path) {
if (path != null) {
// modified the watcher gets informed
try {
WatchKey key = path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
watcherMap.put(key, parent);
} catch (IOException e) {
// Ignore
}
}
}
use of java.nio.file.WatchKey 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.WatchKey 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.WatchKey in project streamline by hortonworks.
the class FileWatcher method register.
public void register() {
try {
watchService = FileSystems.getDefault().newWatchService();
String message = "Could not register %s with file watch service.";
if (fileEventHandlers != null && fileEventHandlers.size() > 0) {
for (FileEventHandler fileEventHandler : fileEventHandlers) {
try {
Path path = Paths.get(fileEventHandler.getDirectoryToWatch());
WatchKey watchKey = path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
watchKeyFileEventHandlerMap.put(watchKey, fileEventHandler);
watchKeyPathMap.put(watchKey, path);
LOG.info("Successfully registered " + fileEventHandler.getDirectoryToWatch() + " with file watch service.");
} catch (NotDirectoryException e) {
LOG.warn(String.format(message, fileEventHandler.getDirectoryToWatch()) + "Reason: Not a directory", e);
} catch (RuntimeException | IOException e) {
LOG.warn(String.format(message, fileEventHandler.getDirectoryToWatch()), e);
}
}
if (watchKeyFileEventHandlerMap.isEmpty()) {
LOG.warn("Could not register any file event handler successfully.");
}
} else {
LOG.info("No file event handlers passed.");
}
} catch (IOException e) {
LOG.warn("Unable to get the default file system watch service. Streamline FileWatcher is not active.", e);
}
}
Aggregations