List of usage examples for java.nio.file WatchKey pollEvents
List<WatchEvent<?>> pollEvents();
From source file:org.wso2.carbon.inbound.localfile.LocalFileOneTimePolling.java
@SuppressWarnings("unchecked") private Object watchDirectory() throws IOException { Path newPath = Paths.get(watchedDir); WatchService watchService = FileSystems.getDefault().newWatchService(); try {/*from ww w . j a v a 2 s . c om*/ newPath.register(watchService, ENTRY_MODIFY); while (true) { WatchKey key = watchService.take(); if (key != null) { for (WatchEvent<?> watchEvent : key.pollEvents()) { WatchEvent.Kind<?> kind = watchEvent.kind(); WatchEvent<Path> watchEventPath = (WatchEvent<Path>) watchEvent; Path entry = watchEventPath.context(); Path filePath = Paths.get(watchedDir, entry.toString()); if (kind == ENTRY_MODIFY) { processFile(filePath, contentType); } else if (kind == OVERFLOW) { continue; } if (log.isDebugEnabled()) { log.debug("Processing file is : " + entry); } } key.reset(); if (!key.isValid()) { break; } } } } catch (IOException e) { log.error("Error while watching directory: " + e.getMessage(), e); } catch (InterruptedException ie) { log.error("Error while get the WatchKey : " + ie.getMessage(), ie); } finally { watchService.close(); } return null; }
From source file:com.reactive.hzdfs.dll.JarModuleLoader.java
private Set<File> filesFromEvents() throws InterruptedException { WatchKey key = watcher.take(); Set<File> files = new LinkedHashSet<File>(); if (key != null && key.isValid()) { for (WatchEvent<?> event : key.pollEvents()) { if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE || event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) { Path item = (Path) event.context(); File file = new File( ((Path) key.watchable()).toAbsolutePath() + File.separator + item.getFileName()); if (log.isDebugEnabled()) { log.debug("Watch Event: " + event.kind() + ": " + file); }/* w w w . ja v a 2 s . c o m*/ if (isJarFile(file)) { files.add(file); } else log.warn("[JAR Loader] Ignoring file:- " + file); } } key.reset(); } return files; }
From source file:eu.edisonproject.rest.FolderWatcherRunnable.java
@Override public void run() { final Path path = FileSystems.getDefault().getPath(dir); try (final WatchService watchService = FileSystems.getDefault().newWatchService()) { final WatchKey watchKey = path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE); while (true) { final WatchKey wk = watchService.take(); for (WatchEvent<?> event : wk.pollEvents()) { final Path changed = (Path) event.context(); executeClassification(new File(dir + File.separator + changed)); }// ww w .j a v a2s. co m // reset the key boolean valid = wk.reset(); if (!valid) { Logger.getLogger(FolderWatcherRunnable.class.getName()).log(Level.WARNING, "Key has been unregisterede"); } } } catch (IOException ex) { Logger.getLogger(FolderWatcherRunnable.class.getName()).log(Level.SEVERE, null, ex); } catch (InterruptedException ex) { Logger.getLogger(FolderWatcherRunnable.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(FolderWatcherRunnable.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:org.springframework.cloud.gcp.stream.binder.pubsub.PubSubEmulator.java
/** * Wait until a PubSub emulator configuration file is updated. * Fail if the file does not update after 1 second. * @param watchService the watch-service to poll * @throws InterruptedException which should interrupt the peaceful slumber and bubble up * to fail the test.//from w w w .j a v a 2 s.c o m */ private void updateConfig(WatchService watchService) throws InterruptedException { int attempts = 10; while (--attempts >= 0) { WatchKey key = watchService.poll(100, TimeUnit.MILLISECONDS); if (key != null) { Optional<Path> configFilePath = key.pollEvents().stream().map((event) -> (Path) event.context()) .filter((path) -> ENV_FILE_NAME.equals(path.toString())).findAny(); if (configFilePath.isPresent()) { return; } } } fail("Configuration file update could not be detected"); }
From source file:com.reactivetechnologies.platform.rest.dll.JarModuleLoader.java
private Set<File> filesFromEvents() throws InterruptedException { WatchKey key = watcher.take(); Set<File> files = new LinkedHashSet<File>(); if (key != null && key.isValid()) { for (WatchEvent<?> event : key.pollEvents()) { if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE || event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) { Path item = (Path) event.context(); File file = new File( ((Path) key.watchable()).toAbsolutePath() + File.separator + item.getFileName()); if (log.isDebugEnabled()) { log.debug("Watch Event: " + event.kind() + ": " + file); }//ww w. j av a2 s .c o m if (isJarFile(file)) { files.add(file); } else log.warn("[JAR Loader] Ignoring file " + file); } } key.reset(); } return files; }
From source file:org.apache.nifi.minifi.bootstrap.configuration.ingestors.FileChangeIngestor.java
protected boolean targetChanged() { boolean targetChanged = false; final WatchKey watchKey = this.watchService.poll(); if (watchKey == null) { return targetChanged; }//w w w . j a v a 2 s .c om for (WatchEvent<?> watchEvt : watchKey.pollEvents()) { final WatchEvent.Kind<?> evtKind = watchEvt.kind(); final WatchEvent<Path> pathEvent = (WatchEvent<Path>) watchEvt; final Path changedFile = pathEvent.context(); // determine target change by verifying if the changed file corresponds to the config file monitored for this path targetChanged = (evtKind == ENTRY_MODIFY && changedFile.equals(configFilePath.getName(configFilePath.getNameCount() - 1))); } // After completing inspection, reset for detection of subsequent change events boolean valid = watchKey.reset(); if (!valid) { throw new IllegalStateException("Unable to reinitialize file system watcher."); } return targetChanged; }
From source file:org.springframework.integration.file.WatchServiceDirectoryScanner.java
private Set<File> filesFromEvents() { WatchKey key = watcher.poll(); Set<File> files = new LinkedHashSet<File>(); while (key != null) { for (WatchEvent<?> event : key.pollEvents()) { if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) { Path item = (Path) event.context(); File file = new File( ((Path) key.watchable()).toAbsolutePath() + File.separator + item.getFileName()); if (logger.isDebugEnabled()) { logger.debug("Watch Event: " + event.kind() + ": " + file); }//from ww w . j a va 2 s. com if (file.isDirectory()) { files.addAll(walkDirectory(file.toPath())); } else { files.add(file); } } else if (event.kind() == StandardWatchEventKinds.OVERFLOW) { if (logger.isDebugEnabled()) { logger.debug("Watch Event: " + event.kind() + ": context: " + event.context()); } if (event.context() != null && event.context() instanceof Path) { files.addAll(walkDirectory((Path) event.context())); } else { files.addAll(walkDirectory(this.directory)); } } else { if (logger.isDebugEnabled()) { logger.debug("Watch Event: " + event.kind() + ": context: " + event.context()); } } } key.reset(); key = watcher.poll(); } return files; }
From source file:org.springframework.cloud.config.monitor.FileMonitorConfiguration.java
private Set<File> filesFromEvents() { Set<File> files = new LinkedHashSet<File>(); if (this.watcher == null) { return files; }/* ww w. j av a2 s.c o m*/ WatchKey key = this.watcher.poll(); while (key != null) { for (WatchEvent<?> event : key.pollEvents()) { if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE || event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) { Path item = (Path) event.context(); File file = new File( ((Path) key.watchable()).toAbsolutePath() + File.separator + item.getFileName()); if (file.isDirectory()) { files.addAll(walkDirectory(file.toPath())); } else { if (!file.getPath().contains(".git") && !PatternMatchUtils.simpleMatch(this.excludes, file.getName())) { if (log.isDebugEnabled()) { log.debug("Watch Event: " + event.kind() + ": " + file); } files.add(file); } } } else if (event.kind() == StandardWatchEventKinds.OVERFLOW) { if (log.isDebugEnabled()) { log.debug("Watch Event: " + event.kind() + ": context: " + event.context()); } if (event.context() != null && event.context() instanceof Path) { files.addAll(walkDirectory((Path) event.context())); } else { for (Path path : this.directory) { files.addAll(walkDirectory(path)); } } } else { if (log.isDebugEnabled()) { log.debug("Watch Event: " + event.kind() + ": context: " + event.context()); } } } key.reset(); key = this.watcher.poll(); } return files; }
From source file:org.codice.ddf.configuration.store.ConfigurationFilesPoller.java
@Override public void run() { try {/*from w w w. j a v a2 s. c om*/ try { LOGGER.debug("Registering path [{}] with Watch Service.", configurationDirectoryPath.toString()); configurationDirectoryPath.register(watchService, ENTRY_CREATE); } catch (IOException e) { LOGGER.error("Unable to register path [{}] with Watch Service", configurationDirectoryPath.toString(), e); return; } WatchKey key; while (!Thread.currentThread().isInterrupted()) { key = watchService.take(); // blocking LOGGER.debug("Key has been signalled. Looping over events."); for (WatchEvent<?> genericEvent : key.pollEvents()) { WatchEvent.Kind<?> kind = genericEvent.kind(); if (kind == OVERFLOW || kind == ENTRY_MODIFY || kind == ENTRY_DELETE) { LOGGER.debug("Skipping event [{}]", kind); continue; } Path filename = (Path) genericEvent.context(); if (!filename.toString().endsWith(fileExtension)) { LOGGER.debug("Skipping event for [{}] due to unsupported file extension of [{}].", filename, fileExtension); continue; // just skip to the next event } if (configurationDirectory != null) { LOGGER.debug("Notifying [{}] of event [{}] for file [{}].", configurationDirectory.getClass().getName(), kind, configurationDirectoryPath.resolve(filename)); configurationDirectory.notify(configurationDirectoryPath.resolve(filename)); } } // Reset key, shutdown watcher if directory not able to be observed // (possibly deleted) if (!key.reset()) { LOGGER.warn("Configurations in [{}] are no longer able to be observed.", configurationDirectoryPath.toString()); break; } } } catch (InterruptedException | RuntimeException e) { LOGGER.error("The [{}] was interrupted.", this.getClass().getName(), e); Thread.currentThread().interrupt(); } }
From source file:org.codice.ddf.configuration.admin.ConfigurationFilesPoller.java
@Override public void run() { try {/*from ww w .jav a 2 s . co m*/ try { LOGGER.debug("Registering path [{}] with Watch Service.", configurationDirectoryPath.toString()); configurationDirectoryPath.register(watchService, ENTRY_CREATE); } catch (IOException e) { LOGGER.error("Unable to register path [{}] with Watch Service", configurationDirectoryPath.toString(), e); return; } WatchKey key; while (!Thread.currentThread().isInterrupted()) { key = watchService.take(); // blocking LOGGER.debug("Key has been signalled. Looping over events."); for (WatchEvent<?> genericEvent : key.pollEvents()) { WatchEvent.Kind<?> kind = genericEvent.kind(); if (kind == OVERFLOW || kind == ENTRY_MODIFY || kind == ENTRY_DELETE) { LOGGER.debug("Skipping event [{}]", kind); continue; } Path filename = (Path) genericEvent.context(); if (!filename.toString().endsWith(fileExtension)) { LOGGER.debug("Skipping event for [{}] due to unsupported file extension of [{}].", filename, fileExtension); continue; // just skip to the next event } if (changeListener != null) { // Sleeping before notifying the listener to make sure file is // done writing, otherwise the listener may read the file too soon. TimeUnit.SECONDS.sleep(1); LOGGER.debug("Notifying [{}] of event [{}] for file [{}].", changeListener.getClass().getName(), kind, configurationDirectoryPath.resolve(filename)); changeListener.notify(configurationDirectoryPath.resolve(filename)); } } // Reset key, shutdown watcher if directory not able to be observed // (possibly deleted) if (!key.reset()) { LOGGER.warn("Configurations in [{}] are no longer able to be observed.", configurationDirectoryPath.toString()); break; } } } catch (InterruptedException | RuntimeException e) { LOGGER.error("The [{}] was interrupted.", this.getClass().getName(), e); Thread.currentThread().interrupt(); } }