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 Project: cyberduck   Author: iterate-ch   File: FSEventWatchService.java    License: 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 Project: jimfs   Author: google   File: PollingWatchService.java    License: 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 Project: cyberduck   Author: iterate-ch   File: FSEventWatchService.java    License: 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 #4
Source Project: cyberduck   Author: iterate-ch   File: FSEventWatchServiceTest.java    License: 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 #5
Source Project: cyberduck   Author: iterate-ch   File: NIOEventWatchService.java    License: 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 #6
Source Project: cyberduck   Author: iterate-ch   File: NIOEventWatchServiceTest.java    License: 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 #7
Source Project: directory-watcher   Author: gmethvin   File: AbstractWatchKey.java    License: 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 Project: jimfs   Author: google   File: PollingWatchService.java    License: 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 #9
Source Project: jimfs   Author: google   File: AbstractWatchService.java    License: 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 #10
Source Project: jimfs   Author: google   File: AbstractWatchServiceTest.java    License: 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 #11
Source Project: cyberduck   Author: iterate-ch   File: FSEventWatchService.java    License: GNU General Public License v3.0 4 votes vote down vote up
@Override
public Watchable watchable() {
    return file;
}
 
Example #12
Source Project: cyberduck   Author: iterate-ch   File: RegisterWatchService.java    License: 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 #13
Source Project: cyberduck   Author: iterate-ch   File: AbstractWatchService.java    License: GNU General Public License v3.0 4 votes vote down vote up
@Override
public Watchable watchable() {
    return null;
}
 
Example #14
Source Project: cyberduck   Author: iterate-ch   File: DisabledWatchService.java    License: 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 #15
Source Project: directory-watcher   Author: gmethvin   File: MacOSXWatchKey.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Watchable watchable() {
  return null;
}
 
Example #16
Source Project: directory-watcher   Author: gmethvin   File: AbstractWatchKey.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Watchable watchable() {
  return watchable;
}
 
Example #17
Source Project: baratine   Author: baratine   File: JWatchKey.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public Watchable watchable()
{
  return _path;
}
 
Example #18
Source Project: RxJavaFileUtils   Author: ReactiveX   File: MacOSXWatchKey.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Watchable watchable() {
    return path;
}
 
Example #19
Source Project: RxJavaFileUtils   Author: ReactiveX   File: AbstractWatchService.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Watchable watchable() {
    return null;
}
 
Example #20
Source Project: jimfs   Author: google   File: AbstractWatchService.java    License: 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 #21
Source Project: jimfs   Author: google   File: AbstractWatchService.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Watchable watchable() {
  return watchable;
}