示例#1
0
文件: Util.java 项目: ENGYS/HELYX-OS
  public static void watchDirectoryPath(Path path) {
    // Sanity check - Check if path is a folder
    try {
      Boolean isFolder = (Boolean) Files.getAttribute(path, "basic:isDirectory", NOFOLLOW_LINKS);
      if (!isFolder) {
        throw new IllegalArgumentException("Path: " + path + " is not a folder");
      }
    } catch (IOException ioe) {
      // Folder does not exists
      ioe.printStackTrace();
    }

    // System.out.println("Watching path: " + path);

    // We obtain the file system of the Path
    FileSystem fs = path.getFileSystem();

    // We create the new WatchService using the new try() block
    try (WatchService service = fs.newWatchService()) {

      // We register the path to the service
      // We watch for creation events
      path.register(service, ENTRY_CREATE);

      // Start the infinite polling loop
      WatchKey key = null;
      while (true) {
        key = service.take();

        // Dequeueing events
        Kind<?> kind = null;
        for (WatchEvent<?> watchEvent : key.pollEvents()) {
          // Get the type of the event
          kind = watchEvent.kind();
          if (OVERFLOW == kind) {
            continue; // loop
          } else if (ENTRY_CREATE == kind) {
            // A new Path was created
            // Path newPath = ((WatchEvent<Path>)
            // watchEvent).context();
            // Output
            // System.out.println("New path created: " + newPath);
          }
        }

        if (!key.reset()) {
          break; // loop
        }
      }

    } catch (IOException ioe) {
      ioe.printStackTrace();
    } catch (InterruptedException ie) {
      ie.printStackTrace();
    }
  }
示例#2
0
  public static void watchDirectoryPath(Path path) {
    if (path != null) {

      System.out.println("Watching path: " + path);

      FileSystem fs = path.getFileSystem();

      try (WatchService service = fs.newWatchService()) {

        path.register(service, ENTRY_CREATE);

        WatchKey key;
        while (true) {
          key = service.take();

          // Dequeueing events
          Kind<?> kind;
          for (WatchEvent<?> watchEvent : key.pollEvents()) {
            // Get the type of the event
            kind = watchEvent.kind();
            if (OVERFLOW == kind) {
              continue; // loop
            } else if (ENTRY_CREATE == kind) {
              // A new Path was created
              Path newPath = ((WatchEvent<Path>) watchEvent).context();
              // Output
              System.out.println("New path created/modified: " + newPath);
              checkPath(newPath.toString());
            }
          }

          if (!key.reset()) {
            break; // resets the key so it continues to look for new creation events.
          }
        }

      } catch (IOException | InterruptedException ioe) {
        System.out.println("The file watcher was interrupted.");
      }
    }
  }
示例#3
0
文件: Settings.java 项目: dr01g/j2trp
  public Settings(File configFile) throws IllegalArgumentException, IOException {

    this.propertiesFile = configFile;
    reload();

    synchronized (watchKeys) {
      if (watchKeys.isEmpty()) {
        FileSystem fs = FileSystems.getDefault();
        watchService = fs.newWatchService();
        backgroundThread =
            new Thread(new PropertiesFileWatcher(watchService), "PropertiesFileWatcher thread");
        backgroundThread.setDaemon(true);
        backgroundThread.start();
        LOG.info("Started background thread");
      }

      Path pathToConfigFile = configFile.toPath();
      Path watchedDir = pathToConfigFile.getParent();

      WatchKey key =
          watchedDir.register(
              watchService,
              StandardWatchEventKinds.ENTRY_MODIFY,
              StandardWatchEventKinds.ENTRY_DELETE,
              StandardWatchEventKinds.ENTRY_CREATE);
      // Add check to see if we are already watching this dir/file.
      watchKeys.put(key, watchedDir);
      LOG.info("Added new watch for properties file " + configFile);
      metaDataMap.put(configFile, this);

      LOG.info(
          String.format(
              "Currently watching %d file%s",
              metaDataMap.size(), (metaDataMap.size() == 1 ? "" : "s")));
    }
  }
示例#4
0
  /** Simple test of each of the standard events */
  static void testEvents(Path dir) throws IOException {
    System.out.println("-- Standard Events --");

    FileSystem fs = FileSystems.getDefault();
    Path name = fs.getPath("foo");

    try (WatchService watcher = fs.newWatchService()) {
      // --- ENTRY_CREATE ---

      // register for event
      System.out.format("register %s for ENTRY_CREATE\n", dir);
      WatchKey myKey = dir.register(watcher, new WatchEvent.Kind<?>[] {ENTRY_CREATE});
      checkKey(myKey, dir);

      // create file
      Path file = dir.resolve("foo");
      System.out.format("create %s\n", file);
      Files.createFile(file);

      // remove key and check that we got the ENTRY_CREATE event
      takeExpectedKey(watcher, myKey);
      checkExpectedEvent(myKey.pollEvents(), StandardWatchEventKinds.ENTRY_CREATE, name);

      System.out.println("reset key");
      if (!myKey.reset()) throw new RuntimeException("key has been cancalled");

      System.out.println("OKAY");

      // --- ENTRY_DELETE ---

      System.out.format("register %s for ENTRY_DELETE\n", dir);
      WatchKey deleteKey = dir.register(watcher, new WatchEvent.Kind<?>[] {ENTRY_DELETE});
      if (deleteKey != myKey) throw new RuntimeException("register did not return existing key");
      checkKey(deleteKey, dir);

      System.out.format("delete %s\n", file);
      Files.delete(file);
      takeExpectedKey(watcher, myKey);
      checkExpectedEvent(myKey.pollEvents(), StandardWatchEventKinds.ENTRY_DELETE, name);

      System.out.println("reset key");
      if (!myKey.reset()) throw new RuntimeException("key has been cancalled");

      System.out.println("OKAY");

      // create the file for the next test
      Files.createFile(file);

      // --- ENTRY_MODIFY ---

      System.out.format("register %s for ENTRY_MODIFY\n", dir);
      WatchKey newKey = dir.register(watcher, new WatchEvent.Kind<?>[] {ENTRY_MODIFY});
      if (newKey != myKey) throw new RuntimeException("register did not return existing key");
      checkKey(newKey, dir);

      System.out.format("update: %s\n", file);
      try (OutputStream out = Files.newOutputStream(file, StandardOpenOption.APPEND)) {
        out.write("I am a small file".getBytes("UTF-8"));
      }

      // remove key and check that we got the ENTRY_MODIFY event
      takeExpectedKey(watcher, myKey);
      checkExpectedEvent(myKey.pollEvents(), StandardWatchEventKinds.ENTRY_MODIFY, name);
      System.out.println("OKAY");

      // done
      Files.delete(file);
    }
  }
示例#5
0
  /**
   * Test that directory can be registered with more than one watch service and that events don't
   * interfere with each other
   */
  static void testTwoWatchers(Path dir) throws IOException {
    System.out.println("-- Two watchers test --");

    FileSystem fs = FileSystems.getDefault();
    WatchService watcher1 = fs.newWatchService();
    WatchService watcher2 = fs.newWatchService();
    try {
      Path name1 = fs.getPath("gus1");
      Path name2 = fs.getPath("gus2");

      // create gus1
      Path file1 = dir.resolve(name1);
      System.out.format("create %s\n", file1);
      Files.createFile(file1);

      // register with both watch services (different events)
      System.out.println("register for different events");
      WatchKey key1 = dir.register(watcher1, new WatchEvent.Kind<?>[] {ENTRY_CREATE});
      WatchKey key2 = dir.register(watcher2, new WatchEvent.Kind<?>[] {ENTRY_DELETE});

      if (key1 == key2) throw new RuntimeException("keys should be different");

      // create gus2
      Path file2 = dir.resolve(name2);
      System.out.format("create %s\n", file2);
      Files.createFile(file2);

      // check that key1 got ENTRY_CREATE
      takeExpectedKey(watcher1, key1);
      checkExpectedEvent(key1.pollEvents(), StandardWatchEventKinds.ENTRY_CREATE, name2);

      // check that key2 got zero events
      WatchKey key = watcher2.poll();
      if (key != null) throw new RuntimeException("key not expected");

      // delete gus1
      Files.delete(file1);

      // check that key2 got ENTRY_DELETE
      takeExpectedKey(watcher2, key2);
      checkExpectedEvent(key2.pollEvents(), StandardWatchEventKinds.ENTRY_DELETE, name1);

      // check that key1 got zero events
      key = watcher1.poll();
      if (key != null) throw new RuntimeException("key not expected");

      // reset for next test
      key1.reset();
      key2.reset();

      // change registration with watcher2 so that they are both
      // registered for the same event
      System.out.println("register for same event");
      key2 = dir.register(watcher2, new WatchEvent.Kind<?>[] {ENTRY_CREATE});

      // create file and key2 should be queued
      System.out.format("create %s\n", file1);
      Files.createFile(file1);
      takeExpectedKey(watcher2, key2);
      checkExpectedEvent(key2.pollEvents(), StandardWatchEventKinds.ENTRY_CREATE, name1);

      System.out.println("OKAY");

    } finally {
      watcher2.close();
      watcher1.close();
    }
  }