java.nio.file.Watchable Java Examples

The following examples show how to use java.nio.file.Watchable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FSEventWatchService.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example #2
Source File: PollingWatchService.java    From jimfs with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #3
Source File: AbstractWatchServiceTest.java    From jimfs with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #4
Source File: FSEventWatchService.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@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();
    Uninterruptibles.awaitUninterruptibly(lock);
    loops.put(key, loop);
    callbacks.put(key, callback);
    return key;
}
 
Example #5
Source File: AbstractWatchService.java    From jimfs with Apache License 2.0 5 votes vote down vote up
public Key(
    AbstractWatchService watcher,
    @NullableDecl Watchable watchable,
    Iterable<? extends WatchEvent.Kind<?>> subscribedTypes) {
  this.watcher = checkNotNull(watcher);
  this.watchable = watchable; // nullable for Watcher poison
  this.subscribedTypes = ImmutableSet.copyOf(subscribedTypes);
}
 
Example #6
Source File: PollingWatchService.java    From jimfs with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #7
Source File: AbstractWatchKey.java    From directory-watcher with Apache License 2.0 5 votes vote down vote up
public AbstractWatchKey(
    AbstractWatchService watcher,
    Watchable watchable,
    Iterable<? extends WatchEvent.Kind<?>> subscribedTypes,
    int queueSize) {
  this.watcher = requireNonNull(watcher);
  this.watchable = watchable; // nullable for Watcher poison
  this.events = new ArrayBlockingQueue<>(queueSize);

  Set<Kind<?>> types = new HashSet<Kind<?>>();
  subscribedTypes.forEach(types::add);
  this.subscribedTypes = Collections.unmodifiableSet(types);
}
 
Example #8
Source File: NIOEventWatchServiceTest.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@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());
}
 
Example #9
Source File: NIOEventWatchService.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@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;
}
 
Example #10
Source File: FSEventWatchServiceTest.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@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());
}
 
Example #11
Source File: DisabledWatchService.java    From cyberduck with GNU General Public License v3.0 4 votes vote down vote up
@Override
public WatchKey register(final Watchable folder, final WatchEvent.Kind<?>[] events, final WatchEvent.Modifier... modifiers) {
    return null;
}
 
Example #12
Source File: MacOSXWatchKey.java    From directory-watcher with Apache License 2.0 4 votes vote down vote up
@Override
public Watchable watchable() {
  return null;
}
 
Example #13
Source File: AbstractWatchService.java    From cyberduck with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Watchable watchable() {
    return null;
}
 
Example #14
Source File: AbstractWatchKey.java    From directory-watcher with Apache License 2.0 4 votes vote down vote up
@Override
public Watchable watchable() {
  return watchable;
}
 
Example #15
Source File: JWatchKey.java    From baratine with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Watchable watchable()
{
  return _path;
}
 
Example #16
Source File: MacOSXWatchKey.java    From RxJavaFileUtils with Apache License 2.0 4 votes vote down vote up
@Override
public Watchable watchable() {
    return path;
}
 
Example #17
Source File: AbstractWatchService.java    From RxJavaFileUtils with Apache License 2.0 4 votes vote down vote up
@Override
public Watchable watchable() {
    return null;
}
 
Example #18
Source File: RegisterWatchService.java    From cyberduck with GNU General Public License v3.0 4 votes vote down vote up
WatchKey register(Watchable folder,
WatchEvent.Kind<?>[] events,
WatchEvent.Modifier... modifiers) throws IOException;
 
Example #19
Source File: AbstractWatchService.java    From jimfs with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example #20
Source File: AbstractWatchService.java    From jimfs with Apache License 2.0 4 votes vote down vote up
@Override
public Watchable watchable() {
  return watchable;
}
 
Example #21
Source File: FSEventWatchService.java    From cyberduck with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Watchable watchable() {
    return file;
}