Example #1
0
  private void refresh() throws IOException {
    while (true) {
      WatchKey watchKey = watchService.poll();
      if (watchKey == null) {
        return;
      }

      Path parentDir = (Path) watchKey.watchable();

      for (WatchEvent<?> watchEvent : watchKey.pollEvents()) {
        WatchEvent.Kind<?> eventKind = watchEvent.kind();
        if (eventKind == OVERFLOW) {
          fullRefresh();
          return;
        }

        Path child = parentDir.resolve((Path) watchEvent.context());
        if (eventKind == ENTRY_CREATE) {
          onNewPath(child);
        } else if (eventKind == ENTRY_DELETE) {
          onRemovedPath(child);
        }
      }

      watchKey.reset();
    }
  }
Example #2
0
  private void onNewDirectory(Path directory) throws IOException {
    String relativePath = getRelativePath(directory);
    if (!relativePath.isEmpty() && !getPathPrefixSet().includesDirectory(relativePath)) {
      return;
    }

    if (watchService != null) {
      // Start watching the directory.
      directory.register(watchService, ENTRY_CREATE, ENTRY_DELETE);
    }

    try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory)) {
      for (Path child : stream) {
        childPathsByParentPath.put(directory, child);
        onNewPath(child);
      }
    }
  }