/** * Creates a recursive watcher for the given root path. The returned watcher * will ignore the ignore paths and fire an event through the {@link WatchListener} * as soon as the settle delay (in ms) has passed. * * <p>The method returns a platform-specific recursive watcher: {@link WindowsRecursiveWatcher} * for Windows and {@link DefaultRecursiveWatcher} for other operating systems. */ public static RecursiveWatcher createRecursiveWatcher(Path root, List<Path> ignorePaths, int settleDelay, WatchListener listener) { if (EnvironmentUtil.isWindows()) { return new WindowsRecursiveWatcher(root, ignorePaths, settleDelay, listener); } else { return new DefaultRecursiveWatcher(root, ignorePaths, settleDelay, listener); } }
@Override public boolean pollEvents() throws InterruptedException { WatchKey watchKey = watchService.take(); List<WatchEvent<?>> watchEvents = watchKey.pollEvents(); boolean hasRelevantEvents = false; // Filter ignored events for (WatchEvent<?> watchEvent : watchEvents) { if (watchEvent.kind() == ENTRY_CREATE || watchEvent.kind() == ENTRY_MODIFY || watchEvent.kind() == ENTRY_DELETE) { boolean ignoreEvent = false; name.pachler.nio.file.Path extLibFilePath = (name.pachler.nio.file.Path) watchEvent.context(); Path filePath = toAbsoluteNormalizedPath(extLibFilePath.toString()); for (Path ignorePath : ignorePaths) { if (filePath.startsWith(ignorePath.toAbsolutePath().normalize())) { ignoreEvent = true; break; } } if (!ignoreEvent) { hasRelevantEvents = true; break; } } } watchKey.reset(); return hasRelevantEvents; }