private void registerWatcher(WatchService watcher, Path directory) throws IOException { directory.register(watcher, new WatchEvent.Kind[]{ StandardWatchEventKinds.ENTRY_MODIFY }, SensitivityWatchEventModifier.HIGH); }
@Override public WatchKey register(WatchService watcher, WatchEvent.Kind<?>... events) throws IOException { return delegate.register(watcher, events); }
private void register(Path dir) throws IOException{ WatchKey key = dir.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY); pathMap.put(key,dir); }
private void register(Path dir) throws IOException{ WatchKey key = dir.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY); pathMap.put(key,dir); }
/** * Register the given directory with the WatchService */ private void register(final Path dir) throws IOException { WatchKey key = dir .register(watcher, new Kind[]{ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY}, HIGH); keys.put(key, dir); }
/** * Register the given directory with the WatchService */ private void register(final Path dir) throws IOException { WatchKey key = dir.register(watcher, new Kind[]{ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY }, HIGH); keys.put(key, dir); }
private void addWatchedDirectory(PathData data, File dir) throws IOException { Path path = Paths.get(dir.toURI()); WatchKey key = path.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); pathDataByKey.put(key, data); data.keys.add(key); }
@Override public WatchedResource watch( File file ) throws IOException { if ( !file.isDirectory() ) { throw new IllegalArgumentException( format( "File `%s` is not a directory. Only directories can be " + "registered to be monitored.", file.getCanonicalPath() ) ); } WatchKey watchKey = file.toPath().register( watchService, OBSERVED_EVENTS, SensitivityWatchEventModifier.HIGH ); return new WatchedFile( watchKey ); }
private synchronized void registerWatch(Path dir) { if (!watchPathKeyMap.containsKey(dir)) { logger.log(Level.INFO, "- Registering " + dir); try { WatchKey watchKey = dir.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY, OVERFLOW); watchPathKeyMap.put(dir, watchKey); } catch (IOException e) { logger.log(Level.FINE, "IO Failed", e); } } }
private void registerWatch(Path dir) throws IOException { if (log.isDebugEnabled()) { log.debug("registering: " + dir + " for file creation events"); } try { dir.register(this.watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY); } catch (IOException e) { throw e; } catch (Exception e) { throw new IOException("Cannot register watcher for " + dir, e); } }
private void registerSingle(Path path) throws IOException { WatchKey key = path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY); keysToPath.put(key, path); pathsToKey.put(path, key); refCount.put(path, refCount.getOrDefault(path, 0) + 1); Path parent = path.getParent(); refCount.put(parent, refCount.getOrDefault(parent, 0) + 1); }
private WatchKey registerDirectory(final Path directory) { if (directoryToKey.containsKey(directory)) { return directoryToKey.get(directory); } try { WatchKey key = directory.register(service, new WatchEvent.Kind[]{ENTRY_MODIFY}, HIGH); directoryToKey.put(directory, key); return key; } catch (IOException e) { throw new MocoException(e); } }
public void watch(File file) throws IOException { String dirString; if (file.isFile()) { dirString = file.getParentFile().getAbsolutePath(); } else { throw new IOException(file.getName() + " is not a file"); } if (dirString == null) { dirString = "/"; } Path dir = FileSystems.getDefault().getPath(dirString); logger.info("watch " + dir); WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); synchronized (watchKeys) { watchKeys.put(key, new File(dirString)); watchFiles.add(file); } }
/** * Creates a watcher that watches <code>dirPath</code> and invokes <code>callback</code> on changes. * * @param dirPath the directory to watch. * @param callback the callback to invoke with events. <code>event.kind()</code> will return the type of event, * and <code>event.context()</code> will return the filename relative to <code>dirPath</code>. * @throws IOException if there is an error creating the WatchService. */ public FileChangeWatcher(Path dirPath, Consumer<WatchEvent<?>> callback) throws IOException { FileSystem fs = dirPath.getFileSystem(); WatchService watchService = fs.newWatchService(); if (LOG.isDebugEnabled()) { LOG.debug("Registering with watch service: " + dirPath); } dirPath.register( watchService, new WatchEvent.Kind<?>[]{ StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.OVERFLOW}, SensitivityWatchEventModifier.HIGH); state = State.NEW; this.watcherThread = new WatcherThread(watchService, callback); this.watcherThread.setDaemon(true); }
private void register(final Path path, final FileEventOptions options) throws IOException { Kind<?>[] kinds = options.kinds(); Modifier modifier = options.modifier(); WatchKey key = path.register(watcher, kinds, modifier); this.keys.put(key, path); this.options.put(path, options); }
final Path path = FileSystems.getDefault().getPath(System.getProperty("user.home"), "Desktop"); System.out.println(path); try (final WatchService watchService = FileSystems.getDefault().newWatchService()) { final WatchKey watchKey = path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY); while (true) { final WatchKey wk = watchService.take(); for (WatchEvent<?> event : wk.pollEvents()) { //we only register "ENTRY_MODIFY" so the context is always a Path. final Path changed = (Path) event.context(); System.out.println(changed); if (changed.endsWith("myFile.txt")) { System.out.println("My file has changed"); } } // reset the key boolean valid = wk.reset(); if (!valid) { System.out.println("Key has been unregisterede"); } } }
private void registerDir(Path path, WatchedPathInfo watchedPathInfo) { watchedPaths.put(path, watchedPathInfo); try { WatchKey watchKey = path.register(watchService, ENTRY_CREATE); watchedPathInfo.setWatchKey(watchKey); watchedPathQueue.add(watchedPathInfo); if (watchedPathInfo.type == Type.FINAL) { trackWatchForAttempt(watchedPathInfo, watchKey); } } catch (IOException e) { LOG.warn("Unable to setup watch for: " + path); } }
/** * Register a base dir for an application * @param pathString the full path including jobId, user - /${local.dir}/appCache/${appId}/userCache/${user} * @param appId the appId * @param user the user * @param expiry when to expire the watch - in ms * @throws IOException */ void registerDagDir(String pathString, String appId, int dagIdentifier, String user, long expiry) throws IOException { // The path string contains the dag Identifier Path path = FileSystems.getDefault().getPath(pathString); WatchedPathInfo watchedPathInfo = new WatchedPathInfo(System.currentTimeMillis() + expiry, Type.BASE, appId, dagIdentifier, user); watchedPaths.put(path, watchedPathInfo); WatchKey watchKey = path.register(watchService, ENTRY_CREATE); watchedPathInfo.setWatchKey(watchKey); watchedPathQueue.add(watchedPathInfo); // TODO Watches on the output dirs need to be cancelled at some point. For now - via the expiry. }
private void init(Path dir) { Log.debug("Registering directory watch", "dir", dir); try { WatchKey key = dir.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); U.notNull(key, "watch key"); keys.put(key, dir); } catch (IOException e) { Log.warn("Couldn't register to watch for changes", "dir", dir); } }
private void register() throws IOException { Path target = target(path); Path dir = parentDir(target); WatchKey oldWatchKey = watchKey; watchKey = dir.register(watchService, CollectionsHelper.listOf(ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY) .toArray(new WatchEvent.Kind[0]), watchServiceModifiers.toArray(new WatchEvent.Modifier[0])); if (oldWatchKey != null) { oldWatchKey.cancel(); } }