/** * This is required to speed up mac based file watcher implementations * * @return sensitivity watch event modifier */ private static Modifier[] getWatchEventModifiers() { String className = "com.sun.nio.file.SensitivityWatchEventModifier"; try { Class<?> c = Class.forName(className); Field f = c.getField("HIGH"); Modifier modifier = cast(f.get(c)); LOG.debug("Class '{}' is found in classpath setting corresponding watch modifier", className); return new Modifier[] {modifier}; } catch (Exception e) { LOG.debug("Class '{}' is not found in classpath, falling to default mode", className, e); return new Modifier[] {}; } }
@SuppressWarnings("unchecked") @Test public void registerTreeRecursive() throws Exception { new MockUnit(Injector.class, Env.class, WatchService.class, FileEventOptions.class, Path.class, WatchEvent.Kind.class, WatchEvent.Modifier.class, WatchKey.class) .expect(newThread) .expect(registerTree(true, false)) .expect(takeInterrupt) .run(unit -> { FileMonitor monitor = new FileMonitor(unit.get(Injector.class), unit.get(Env.class), unit.get(WatchService.class), ImmutableSet.of(unit.get(FileEventOptions.class))); unit.captured(ThreadFactory.class).get(0).newThread(monitor); }, unit -> { unit.captured(Runnable.class).get(0).run(); unit.captured(FileVisitor.class).get(0).preVisitDirectory(unit.get(Path.class), null); }); }
@Test public void registerTree() throws Exception { new MockUnit(Injector.class, Env.class, WatchService.class, FileEventOptions.class, Path.class, WatchEvent.Kind.class, WatchEvent.Modifier.class, WatchKey.class) .expect(newThread) .expect(registerTree(false, false)) .expect(takeInterrupt) .run(unit -> { FileMonitor monitor = new FileMonitor(unit.get(Injector.class), unit.get(Env.class), unit.get(WatchService.class), ImmutableSet.of(unit.get(FileEventOptions.class))); unit.captured(ThreadFactory.class).get(0).newThread(monitor); }, unit -> { unit.captured(Runnable.class).get(0).run(); }); }
@Test public void registerTreeErr() throws Exception { new MockUnit(Injector.class, Env.class, WatchService.class, FileEventOptions.class, Path.class, WatchEvent.Kind.class, WatchEvent.Modifier.class, WatchKey.class) .expect(newThread) .expect(registerTree(false, true)) .expect(takeInterrupt) .run(unit -> { FileMonitor monitor = new FileMonitor(unit.get(Injector.class), unit.get(Env.class), unit.get(WatchService.class), ImmutableSet.of(unit.get(FileEventOptions.class))); unit.captured(ThreadFactory.class).get(0).newThread(monitor); }, unit -> { unit.captured(Runnable.class).get(0).run(); }); }
@Test public void pollIgnoreMissing() throws Exception { new MockUnit(Injector.class, Env.class, WatchService.class, FileEventOptions.class, Path.class, WatchEvent.Kind.class, WatchEvent.Modifier.class, WatchKey.class) .expect(newThread) .expect(registerTree(false, false)) .expect(takeIgnore) .expect(takeInterrupt) .run(unit -> { FileMonitor monitor = new FileMonitor(unit.get(Injector.class), unit.get(Env.class), unit.get(WatchService.class), ImmutableSet.of(unit.get(FileEventOptions.class))); unit.captured(ThreadFactory.class).get(0).newThread(monitor); }, unit -> { unit.captured(Runnable.class).get(0).run(); }); }
@Test public void pollEvents() throws Exception { Path path = Paths.get("target/foo.txt"); new MockUnit(Injector.class, Env.class, WatchService.class, FileEventOptions.class, Path.class, WatchEvent.Kind.class, WatchEvent.Modifier.class, WatchKey.class, WatchEvent.class, PathMatcher.class, FileEventHandler.class) .expect(newThread) .expect(registerTree(false, false)) .expect(take) .expect(poll(StandardWatchEventKinds.ENTRY_MODIFY, path)) .expect(filter(true)) .expect(handler(StandardWatchEventKinds.ENTRY_MODIFY, false)) .expect(recursive(false, false)) .expect(reset(true)) .expect(takeInterrupt) .run(unit -> { FileMonitor monitor = new FileMonitor(unit.get(Injector.class), unit.get(Env.class), unit.get(WatchService.class), ImmutableSet.of(unit.get(FileEventOptions.class))); unit.captured(ThreadFactory.class).get(0).newThread(monitor); }, unit -> { unit.captured(Runnable.class).get(0).run(); }); }
@Test public void pollEventsInvalid() throws Exception { Path path = Paths.get("target/foo.txt"); new MockUnit(Injector.class, Env.class, WatchService.class, FileEventOptions.class, Path.class, WatchEvent.Kind.class, WatchEvent.Modifier.class, WatchKey.class, WatchEvent.class, PathMatcher.class, FileEventHandler.class) .expect(newThread) .expect(registerTree(false, false)) .expect(take) .expect(poll(StandardWatchEventKinds.ENTRY_MODIFY, path)) .expect(filter(true)) .expect(handler(StandardWatchEventKinds.ENTRY_MODIFY, false)) .expect(recursive(false, false)) .expect(reset(false)) .run(unit -> { FileMonitor monitor = new FileMonitor(unit.get(Injector.class), unit.get(Env.class), unit.get(WatchService.class), ImmutableSet.of(unit.get(FileEventOptions.class))); unit.captured(ThreadFactory.class).get(0).newThread(monitor); }, unit -> { unit.captured(Runnable.class).get(0).run(); }); }
@Test public void pollEventsRecursive() throws Exception { Path path = Paths.get("target/foo.txt"); new MockUnit(Injector.class, Env.class, WatchService.class, FileEventOptions.class, Path.class, WatchEvent.Kind.class, WatchEvent.Modifier.class, WatchKey.class, WatchEvent.class, PathMatcher.class, FileEventHandler.class) .expect(newThread) .expect(registerTree(false, false)) .expect(take) .expect(poll(StandardWatchEventKinds.ENTRY_CREATE, path)) .expect(filter(true)) .expect(handler(StandardWatchEventKinds.ENTRY_CREATE, false)) .expect(recursive(true, false)) .expect(reset(true)) .expect(takeInterrupt) .run(unit -> { FileMonitor monitor = new FileMonitor(unit.get(Injector.class), unit.get(Env.class), unit.get(WatchService.class), ImmutableSet.of(unit.first(FileEventOptions.class))); unit.captured(ThreadFactory.class).get(0).newThread(monitor); }, unit -> { unit.captured(Runnable.class).get(0).run(); }); }
@Test public void pollEventsRecursiveErr() throws Exception { Path path = Paths.get("target/foo.txt"); new MockUnit(Injector.class, Env.class, WatchService.class, FileEventOptions.class, Path.class, WatchEvent.Kind.class, WatchEvent.Modifier.class, WatchKey.class, WatchEvent.class, PathMatcher.class, FileEventHandler.class) .expect(newThread) .expect(registerTree(false, false)) .expect(take) .expect(poll(StandardWatchEventKinds.ENTRY_CREATE, path)) .expect(filter(true)) .expect(handler(StandardWatchEventKinds.ENTRY_CREATE, false)) .expect(recursive(true, true)) .expect(reset(true)) .expect(takeInterrupt) .run(unit -> { FileMonitor monitor = new FileMonitor(unit.get(Injector.class), unit.get(Env.class), unit.get(WatchService.class), ImmutableSet.of(unit.first(FileEventOptions.class))); unit.captured(ThreadFactory.class).get(0).newThread(monitor); }, unit -> { unit.captured(Runnable.class).get(0).run(); }); }
@Test public void pollEventWithHandleErr() throws Exception { Path path = Paths.get("target/foo.txt"); new MockUnit(Injector.class, Env.class, WatchService.class, FileEventOptions.class, Path.class, WatchEvent.Kind.class, WatchEvent.Modifier.class, WatchKey.class, WatchEvent.class, PathMatcher.class, FileEventHandler.class) .expect(newThread) .expect(registerTree(false, false)) .expect(take) .expect(poll(StandardWatchEventKinds.ENTRY_MODIFY, path)) .expect(filter(true)) .expect(handler(StandardWatchEventKinds.ENTRY_MODIFY, true)) .expect(recursive(false, false)) .expect(reset(true)) .expect(takeInterrupt) .run(unit -> { FileMonitor monitor = new FileMonitor(unit.get(Injector.class), unit.get(Env.class), unit.get(WatchService.class), ImmutableSet.of(unit.get(FileEventOptions.class))); unit.captured(ThreadFactory.class).get(0).newThread(monitor); }, unit -> { unit.captured(Runnable.class).get(0).run(); }); }
@Test public void pollEventsNoMatches() throws Exception { Path path = Paths.get("target/foo.txt"); new MockUnit(Injector.class, Env.class, WatchService.class, FileEventOptions.class, Path.class, WatchEvent.Kind.class, WatchEvent.Modifier.class, WatchKey.class, WatchEvent.class, PathMatcher.class, FileEventHandler.class) .expect(newThread) .expect(registerTree(false, false)) .expect(take) .expect(poll(StandardWatchEventKinds.ENTRY_MODIFY, path)) .expect(filter(false)) .expect(recursive(false, false)) .expect(reset(true)) .expect(takeInterrupt) .run(unit -> { FileMonitor monitor = new FileMonitor(unit.get(Injector.class), unit.get(Env.class), unit.get(WatchService.class), ImmutableSet.of(unit.get(FileEventOptions.class))); unit.captured(ThreadFactory.class).get(0).newThread(monitor); }, unit -> { unit.captured(Runnable.class).get(0).run(); }); }
@Override public WatchKey register(WatchService watcher, Kind<?>[] events, Modifier... modifiers) throws IOException { if (!CloudWatchService.class.isAssignableFrom(watcher.getClass())) { throw new IllegalArgumentException("The file system watcher must be of type " + CloudWatchService.class + " but was " + watcher.getClass()); } return ((CloudWatchService)watcher).register(this, events, modifiers); }
protected JWatchKey register(JPath path, Kind<?>[] events, Modifier ... modifiers) { JWatchKey key = new JWatchKey(this, path, events, modifiers); synchronized (this) { _watchList.add(key); } return key; }
public JWatchKey(JWatchService watchService, JPath path, Kind<?>[] events, Modifier ... modifiers) { Objects.requireNonNull(events); _watchService = watchService; _path = path; _events = events; _modifiers = modifiers; _watchHandle = path.getBfsFile().watch(pathString -> onWatch(pathString)); }
@Override public WatchKey register(WatchService watcher, Kind<?>[] events, Modifier... modifiers) throws IOException { if (events.length == 0) { throw new IllegalArgumentException(L.l("no events specified to watch on: {0}", toUri())); } JWatchService jWatcher = (JWatchService) watcher; WatchKey key = jWatcher.register(this, events, modifiers); return key; }
public ArtifactSourceFileWatcherThread(ArtifactDescriptor artifact, Modifier... modifiers) { this.artifact = artifact; this.modifiers = modifiers; setDaemon(true); setName("FS File Watcher Thread"); init(); start(); }
@Override public WatchKey register(WatchService watcher, Kind<?>[] events, Modifier... modifiers) throws IOException { //no standard modifiers are defined if(modifiers != null && modifiers.length != 0) { throw new IllegalStateException("unsupported modifiers"); } synchronized(fs.fsLock) { ResolvedPath resolvedThis = ResolvedPath.resolve(this, false); if(!resolvedThis.hasTarget()) { throw new NoSuchFileException(toString()); } if(!resolvedThis.getTarget().isDir()) { throw new NotDirectoryException(toString()); } if(!(watcher instanceof EphemeralFsWatchService)) { throw new IllegalArgumentException("watcher not created by this fs"); } EphemeralFsWatchService service = (EphemeralFsWatchService) watcher; if(service.getFs() != fs) { throw new IllegalArgumentException("watch service from different fs"); } EphemeralFsWatchKey answer = new EphemeralFsWatchKey(service, this, resolvedThis.getTarget(), fs, events); fs.getWatchRegistry().register(resolvedThis.getTarget(), answer); return answer; } }
@Override public WatchKey register(WatchService watcher, Kind<?>[] events, Modifier... modifiers) throws IOException { if (watcher == null || events == null || modifiers == null) { throw new NullPointerException(); } // Not implemented now // The Hadoop API for notification is not stable throw new IOException("Not implemented"); }
/** * "HIGH" modifier should improve the file detection speed. * * @return SensitivityWatchEventModifier.HIGH */ private Modifier get_com_sun_nio_file_SensitivityWatchEventModifier_HIGH() { try { Class<?> c = Class .forName("com.sun.nio.file.SensitivityWatchEventModifier"); //$NON-NLS-1$ Field f = c.getField("HIGH"); //$NON-NLS-1$ return (Modifier) f.get(c); } catch (Exception e) { return null; } }
private void register(Path dir) throws IOException { Modifier high = get_com_sun_nio_file_SensitivityWatchEventModifier_HIGH(); WatchKey key = (high == null) ? dir.register(watcher, ENTRY_CREATE, ENTRY_MODIFY) : dir.register(watcher, new WatchEvent.Kind<?>[]{ENTRY_CREATE, ENTRY_MODIFY}, high); keys.put(key, dir); }
private Modifier get_com_sun_nio_file_SensitivityWatchEventModifier_HIGH() { try { Class<?> c = Class.forName("com.sun.nio.file.SensitivityWatchEventModifier"); Field f = c.getField("HIGH"); return (Modifier) f.get(c); } catch (Exception e) { return null; } }
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); }
/** @throws UnsupportedOperationException always */ @Override public WatchKey register(WatchService watcher, Kind<?>[] events, Modifier... modifiers) throws IOException { throw new UnsupportedOperationException("Not supported yet."); }
@Override public WatchKey register(WatchService watcher, Kind<?>[] events, Modifier... modifiers) throws IOException { return null; }
@Override public WatchKey register(final WatchService watcher, final Kind<?>... events) throws IOException { return register(watcher, events, new WatchEvent.Modifier[0]); }
@Override public WatchKey register(final WatchService watcher, final Kind<?>[] events, final Modifier... modifiers) throws IOException { throw new UnsupportedOperationException(); }
@Override public WatchKey register(WatchService watcher, Kind<?>[] events, Modifier... modifiers) throws IOException { throw new UnsupportedOperationException(); }
@Override public WatchKey register(WatchService watcher, Kind<?>... events) throws IOException { return register(watcher, events, new Modifier[0]); }
public ArtifactSourceFileWatcherThread(ArtifactDescriptor artifact) { this(artifact, new Modifier[0]); }
@Override public WatchKey register(WatchService watcher, Kind<?>[] events, Modifier... modifiers) throws IOException { return getPath().register(watcher, events, modifiers); }
@Override public WatchKey register(WatchService watcher, Kind<?>[] events, Modifier... modifiers) throws IOException { return path.register(watcher, events, modifiers); }
/** {@inheritDoc} */ @Override public WatchKey register(WatchService watcher, Kind< ? >[] events, Modifier... modifiers) throws IOException { return p.register(watcher, events, modifiers); }
@Override public WatchKey register(WatchService watcher, Kind<?>[] events, Modifier... modifiers) throws IOException { // TODO Auto-generated method stub return null; }
@Override public WatchKey register(WatchService watcher, Kind<?>... events) throws IOException { return register(watcher, events, new WatchEvent.Modifier[0]); }
Modifier modifier() { return modifier; }
/** * Registers this path for watching * @param path * @param events * @param modifiers * @return The watch key */ WatchKey register(CloudPath path, Kind<?>[] events, Modifier... modifiers);