public MacOSXWatchKey(final Watchable file, final FSEventWatchService service, final WatchEvent.Kind<?>[] events) { super(service); this.file = file; boolean reportCreateEvents = false; boolean reportModifyEvents = false; boolean reportDeleteEvents = false; for(WatchEvent.Kind<?> event : events) { if(event == StandardWatchEventKinds.ENTRY_CREATE) { reportCreateEvents = true; } else if(event == StandardWatchEventKinds.ENTRY_MODIFY) { reportModifyEvents = true; } else if(event == StandardWatchEventKinds.ENTRY_DELETE) { reportDeleteEvents = true; } } this.reportCreateEvents = reportCreateEvents; this.reportDeleteEvents = reportDeleteEvents; this.reportModifyEvents = reportModifyEvents; }
@Override public Key register(Watchable watchable, Iterable<? extends WatchEvent.Kind<?>> eventTypes) throws IOException { JimfsPath path = checkWatchable(watchable); Key key = super.register(path, eventTypes); Snapshot snapshot = takeSnapshot(path); synchronized (this) { snapshots.put(key, snapshot); if (pollingFuture == null) { startPolling(); } } return key; }
@Override public WatchKey register(final Watchable folder, final WatchEvent.Kind<?>[] events, final WatchEvent.Modifier... modifiers) throws IOException { if(log.isInfoEnabled()) { log.info(String.format("Register file %s for events %s", folder, Arrays.toString(events))); } final Pointer[] values = { CFStringRef.toCFString(folder.toString()).getPointer()}; final MacOSXWatchKey key = new MacOSXWatchKey(folder, this, events); final double latency = 1.0; // Latency in seconds final Map<File, Long> timestamps = createLastModifiedMap(new File(folder.toString())); final FSEvents.FSEventStreamCallback callback = new Callback(key, timestamps); final FSEventStreamRef stream = library.FSEventStreamCreate( Pointer.NULL, callback, Pointer.NULL, library.CFArrayCreate(null, values, CFIndex.valueOf(1), null), -1, latency, kFSEventStreamCreateFlagNoDefer); final CountDownLatch lock = new CountDownLatch(1); final CFRunLoop loop = new CFRunLoop(lock, stream); threadFactory.newThread(loop).start(); try { lock.await(); } catch(InterruptedException e) { throw new IOException(String.format("Failure registering for events in %s", folder), e); } loops.put(key, loop); callbacks.put(key, callback); return key; }
@Test public void testRegister() throws Exception { final RegisterWatchService fs = new FSEventWatchService(); final Watchable folder = Paths.get( File.createTempFile(UUID.randomUUID().toString(), "t").getParent()); final WatchKey key = fs.register(folder, new WatchEvent.Kind[]{ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY}); assertTrue(key.isValid()); fs.close(); assertFalse(key.isValid()); }
@Override public WatchKey register(final Watchable folder, final WatchEvent.Kind<?>[] events, final WatchEvent.Modifier... modifiers) throws IOException { if(null == monitor) { monitor = FileSystems.getDefault().newWatchService(); } final WatchKey key = folder.register(monitor, events, modifiers); if(log.isInfoEnabled()) { log.info(String.format("Registered for events for %s", key)); } return key; }
@Test public void testRegister() throws Exception { final RegisterWatchService fs = new NIOEventWatchService(); final Watchable folder = Paths.get( File.createTempFile(UUID.randomUUID().toString(), "t").getParent()); final WatchKey key = fs.register(folder, new WatchEvent.Kind[]{ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY}); assertTrue(key.isValid()); fs.close(); assertFalse(key.isValid()); }
public DirectoryWatcher(Path directory, Watchable directoryToMonitor, WatchService watchService, DirectoryChangeListener listener, boolean isMac) throws IOException { this.directory = directory; this.watchService = watchService; this.listener = listener; this.isMac = isMac; if (isMac) { register(directoryToMonitor); } else { jdkWatcher = new DirectoryWatcherJdk(directory, listener); } }
public AbstractWatchKey( AbstractWatchService watcher, @Nullable Watchable watchable, Iterable<? extends WatchEvent.Kind<?>> subscribedTypes) { this.watcher = checkNotNull(watcher); this.watchable = watchable; // nullable for Watcher poison this.subscribedTypes = ImmutableSet.copyOf(subscribedTypes); }
private JimfsPath checkWatchable(Watchable watchable) { if (!(watchable instanceof JimfsPath) || !isSameFileSystem((Path) watchable)) { throw new IllegalArgumentException( "watchable (" + watchable + ") must be a Path " + "associated with the same file system as this watch service"); } return (JimfsPath) watchable; }
public Key( AbstractWatchService watcher, @Nullable Watchable watchable, Iterable<? extends WatchEvent.Kind<?>> subscribedTypes) { this.watcher = checkNotNull(watcher); this.watchable = watchable; // nullable for Watcher poison this.subscribedTypes = ImmutableSet.copyOf(subscribedTypes); }
@Test public void testRegister() throws IOException { Watchable watchable = new StubWatchable(); AbstractWatchService.Key key = watcher.register(watchable, ImmutableSet.of(ENTRY_CREATE)); assertThat(key.isValid()).isTrue(); assertThat(key.pollEvents()).isEmpty(); assertThat(key.subscribesTo(ENTRY_CREATE)).isTrue(); assertThat(key.subscribesTo(ENTRY_DELETE)).isFalse(); assertThat(key.watchable()).isEqualTo(watchable); assertThat(key.state()).isEqualTo(READY); }
@Override public <T extends Watchable> T getWatchableResource() { final Watchable watchable = this.serviceRegistryDirectory; return (T) watchable; }
@Override public Watchable watchable() { return file; }
WatchKey register(Watchable folder, WatchEvent.Kind<?>[] events, WatchEvent.Modifier... modifiers) throws IOException;
@Override public Watchable watchable() { return null; }
@Override public WatchKey register(final Watchable folder, final WatchEvent.Kind<?>[] events, final WatchEvent.Modifier... modifiers) throws IOException { return null; }
@Override public Watchable watchable() { return _path; }
private void register(Watchable dir) throws IOException { dir.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); }
@Override public Watchable watchable() { return watchable; }
@Override public Watchable watchable() { return path; }
/** * Registers the given watchable with this service, returning a new watch key for it. This * implementation just checks that the service is open and creates a key; subclasses may override * it to do other things as well. */ public Key register(Watchable watchable, Iterable<? extends WatchEvent.Kind<?>> eventTypes) throws IOException { checkOpen(); return new Key(this, watchable, eventTypes); }
/** * Gets the watchable resource. * * @param <T> the type parameter * @return the watchable resource */ <T extends Watchable> T getWatchableResource();