List of usage examples for java.nio.file WatchKey reset
boolean reset();
From source file:de.elomagic.carafile.client.CaraCloud.java
/** * Starts synchronization of the given base path with the registry. * <p/>//ww w . j a v a2 s . c om * This method will block till call method {@link CaraCloud#stop() } * * @throws IOException Thrown when an I/O error occurs * @throws InterruptedException Thrown when method stop was called or application will terminate * @throws GeneralSecurityException */ public void start() throws IOException, InterruptedException, GeneralSecurityException { if (client == null) { throw new IllegalStateException("Attribute \"client\" must be set."); } if (basePath == null) { throw new IllegalStateException("Attribute \"basePath\" must be set."); } if (!Files.exists(basePath)) { throw new IllegalStateException("Path \"" + basePath + "\" must exists."); } if (!Files.isDirectory(basePath)) { throw new IllegalStateException("Path \"" + basePath + "\" must be a directory/folder."); } watcher = FileSystems.getDefault().newWatchService(); registerDefaultWatch(basePath); while (!Thread.interrupted()) { WatchKey key = watcher.take(); for (WatchEvent<?> event : key.pollEvents()) { if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) { Path path = basePath.resolve(event.context().toString()); createFile(path); } else if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) { } else if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE) { Path path = basePath.resolve(event.context().toString()); deleteFile(path); } else { LOG.error("Unsupported kind: " + event.kind() + ", path: " + event.context()); } } key.reset(); } }
From source file:org.eclipse.smarthome.core.transform.AbstractFileTransformationService.java
/** * Ensures that a modified or deleted cached files does not stay in the cache *///w ww . jav a 2s . c o m private void processFolderEvents() { WatchKey key = watchService.poll(); if (key != null) { for (WatchEvent<?> e : key.pollEvents()) { if (e.kind() == OVERFLOW) { continue; } // Context for directory entry event is the file name of entry @SuppressWarnings("unchecked") WatchEvent<Path> ev = (WatchEvent<Path>) e; Path path = ev.context(); logger.debug("Refreshing transformation file '{}'", path); for (String fileEntry : cachedFiles.keySet()) { if (fileEntry.endsWith(path.toString())) { cachedFiles.remove(fileEntry); } } } key.reset(); } }
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 ww . j av a 2 s .c o m 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.dishevelled.thumbnail.examples.ThumbnailDrop.java
/** * Process file system watcher events./*from w w w . ja v a 2 s . c o m*/ */ void processEvents() { for (;;) { WatchKey key = null; try { key = watcher.take(); } catch (InterruptedException e) { return; } for (WatchEvent<?> event : key.pollEvents()) { WatchEvent.Kind kind = event.kind(); if (kind == OVERFLOW) { continue; } WatchEvent<Path> pathEvent = cast(event); Path name = pathEvent.context(); Path path = watchDirectory.resolve(name); if (kind == ENTRY_CREATE) { created(path); } else if (kind == ENTRY_MODIFY) { modified(path); } } if (!key.reset()) { key.cancel(); try { watcher.close(); } catch (IOException e) { // ignore } break; } } }
From source file:com.aspc.cms.module.SyncResourceApp.java
private void monitorDir() throws Exception { // Create a new Watch Service WatchService watchService = FileSystems.getDefault().newWatchService(); registerDir(watchService, syncDirectory); while (true) { try {/*from w w w .java 2 s . c o m*/ // Obtaining watch keys final WatchKey key = watchService.take();//poll(5, TimeUnit.SECONDS); if (key == null) continue; long tmpLastModified = lastModify.get(); long startLastModified = tmpLastModified; try { // key value can be null if no event was triggered for (WatchEvent<?> watchEvent : key.pollEvents()) { final Kind<?> kind = watchEvent.kind(); // Overflow event if (StandardWatchEventKinds.OVERFLOW == kind) { continue; // loop } tmpLastModified = scanChanges(syncDirectory, tmpLastModified); } } finally { key.reset(); } lastModify.compareAndSet(startLastModified, tmpLastModified); } catch (Exception e) { LOGGER.warn("could not send", e); Thread.sleep(60000); } } }
From source file:org.flowerplatform.web.git.GitUtils.java
public void listenForChanges(File file) throws IOException { Path path = file.toPath();//from w w w .ja v a 2 s . com if (file.isDirectory()) { WatchService ws = path.getFileSystem().newWatchService(); path.register(ws, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY); WatchKey watch = null; while (true) { System.out.println("Watching directory: " + file.getPath()); try { watch = ws.take(); } catch (InterruptedException ex) { System.err.println("Interrupted"); } List<WatchEvent<?>> events = watch.pollEvents(); if (!watch.reset()) { break; } for (WatchEvent<?> event : events) { Kind<Path> kind = (Kind<Path>) event.kind(); Path context = (Path) event.context(); if (kind.equals(StandardWatchEventKinds.OVERFLOW)) { System.out.println("OVERFLOW"); } else if (kind.equals(StandardWatchEventKinds.ENTRY_CREATE)) { System.out.println("Created: " + context.getFileName()); } else if (kind.equals(StandardWatchEventKinds.ENTRY_DELETE)) { System.out.println("Deleted: " + context.getFileName()); } else if (kind.equals(StandardWatchEventKinds.ENTRY_MODIFY)) { System.out.println("Modified: " + context.getFileName()); } } } } else { System.err.println("Not a directory. Will exit."); } }
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 {// w w w . java2 s. c o m 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); }/*from w w w . j av a 2s .c o m*/ if (isJarFile(file)) { files.add(file); } else log.warn("[JAR Loader] Ignoring file:- " + file); } } key.reset(); } return files; }
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); }// www.ja v a2s . c o m if (isJarFile(file)) { files.add(file); } else log.warn("[JAR Loader] Ignoring file " + file); } } key.reset(); } return files; }
From source file:uk.gov.gchq.gaffer.miniaccumulocluster.MiniAccumuloClusterController.java
protected void watchShutdown() { LOGGER.info("Watching Shutdown File " + clusterPath + "/" + SHUTDOWN_FILENAME); try {//from w w w.j av a 2 s. co m WatchService watcher = FileSystems.getDefault().newWatchService(); clusterPath.register(watcher, ENTRY_CREATE); OUTER: while (true) { WatchKey key; try { key = watcher.take(); } catch (final InterruptedException e) { return; } for (final WatchEvent<?> event : key.pollEvents()) { final WatchEvent.Kind<?> kind = event.kind(); if (kind == OVERFLOW) { continue; } final WatchEvent<Path> ev = (WatchEvent<Path>) event; final Path filename = ev.context(); LOGGER.debug("Filename changed " + filename); if (filename.toString().equals(SHUTDOWN_FILENAME)) { MiniAccumuloClusterController.this.stop(); break OUTER; } } boolean valid = key.reset(); if (!valid) { break; } } LOGGER.info("Finished Watching Shutdown"); } catch (final IOException e) { LOGGER.error("Failed to watch shutdown", e); } }