Package java.nio.file

Examples of java.nio.file.WatchKey


  /**
   * Process all events for keys queued to the watcher
   */
  public Boolean processEvents() {
    // wait for key to be signalled
    WatchKey key;
    try {
      key = watcher.poll(1L, TimeUnit.SECONDS);
    } catch (InterruptedException x) {
      return Boolean.FALSE;
    }
   
    if (null == key)
      return Boolean.FALSE;

    Path dir = keys.get(key);
    if (dir == null)
      throw new IllegalStateException("WatchKey not recognized!!");

    for (WatchEvent<?> event : key.pollEvents()) {
      Kind<?> kind = event.kind();

      // TBD - provide example of how OVERFLOW event is handled
      if (kind == OVERFLOW) {
        continue;
      }

      // Context for directory entry event is the file name of entry
      WatchEvent<Path> ev = cast(event);
      Path name = ev.context();
      Path child = dir.resolve(name);

      // if directory is created, and watching recursively, then
      // register it and its sub-directories
      if (kind == ENTRY_CREATE) {
        try {
          if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
            registerAll(child);
          }
        } catch (IOException x) {
          // ignore to keep sample readbale
        }
      }
    }

    // reset key and remove from set if directory no longer accessible
    boolean valid = key.reset();
    if (!valid) {
      keys.remove(key);

      // all directories are inaccessible
      if (keys.isEmpty()) {
View Full Code Here


        String dirName = pathName;
        File f = new File( pathName );
        if ( ( f.isFile() ) ) {
            dirName = f.getParent();
        }
        WatchKey watchKey = Paths.get( dirName ).register( watchService,
                                                           StandardWatchEventKinds.ENTRY_CREATE,
                                                           StandardWatchEventKinds.ENTRY_MODIFY,
                                                           StandardWatchEventKinds.ENTRY_DELETE );
        WatchedPath entry = new WatchedPath( watchKey, pathName, listener );
        synchronized ( lock ) {
View Full Code Here

        return paths;
    }

    @Override
    public void run() {
        WatchKey key;
        while ( running ) {
            try {
                key = watchService.take();
                Listener listener;
                synchronized ( lock ) {
                    listener = watchKeyMapping.get( key ).getListener();
                }
                for ( WatchEvent< ? > event : key.pollEvents() ) {
                    listener.PathChanged( event );
                }
                key.reset();
            }
            catch ( InterruptedException e ) {
                running = false;
            }
        }
View Full Code Here

    generate();
    new Thread(new Runnable() {
      @Override
      public void run() {
        while (running) {
          WatchKey key;
          if ((key = watchService.poll()) != null) {
            List<WatchEvent<?>> watchEvents = key.pollEvents();
            for (WatchEvent<?> event : watchEvents) {
              WatchEvent<Path> ev = cast(event);
              if(ev.context().equals(messages)) {
                generate();
                key.reset();
              }
            }
          } else {
            try {
              Thread.sleep(100);
View Full Code Here

   */
  private class WatcherThread implements Runnable {
    @Override
    public void run() {
      while (true) {
        WatchKey key = null;
        try {
          key = watchService.take();
        } catch (InterruptedException | ClosedWatchServiceException e) {
          return;
        }

        /* Poll the events and handle */
        for (WatchEvent<?> event : key.pollEvents()) {
          WatchEvent.Kind<?> kind = event.kind();

          if (kind == StandardWatchEventKinds.OVERFLOW) {
            continue;
          }

          if (kind == StandardWatchEventKinds.ENTRY_MODIFY) {
            for (DirectoryWatcher watcher : watchers) {
              watcher.handleModifyEvent(key, (Path) event.context());
            }

            continue;
          }

          if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
            for (DirectoryWatcher watcher : watchers) {
              watcher.handleCreateEvent(key, (Path) event.context());
            }

            continue;
          }

          for (DirectoryWatcher watcher : watchers) {
            watcher.handleDeleteEvent(key, (Path) event.context());
          }
        }

        /* Reset the Key to get more events later */
        if (!key.reset()) {
          for (DirectoryWatcher watcher : watchers) {
            watcher.handleKeyInvalid(key);
          }
        }
      }
View Full Code Here

                     dirty = true;
                  }

               }

               WatchKey key = watcher.poll();
               while (key != null)
               {
                  List<WatchEvent<?>> events = key.pollEvents();
                  if (!events.isEmpty())
                  {
                     logger.log(Level.INFO, "Detected changes in repository [" + events.iterator().next().context()
                              + "].");
                     dirty = true;
                  }
                  key.reset();
                  key = watcher.poll();
               }

               if (dirty)
               {
View Full Code Here

    try {
      WatchService watcher = FileSystems.getDefault().newWatchService();

      Path dir = FileSystems.getDefault().getPath("/usr/karianna");

      WatchKey key = dir.register(watcher, ENTRY_MODIFY);

      while (!shutdown) {
        key = watcher.take();
        for (WatchEvent<?> event : key.pollEvents()) {
          if (event.kind() == ENTRY_MODIFY) {
            System.out.println("Home dir changed!");
          }
        }
        key.reset();
      }
    } catch (IOException | InterruptedException e) {
      System.out.println(e.getMessage());
    }
  }
View Full Code Here

   */
  private class WatcherThread implements Runnable {
    @Override
    public void run() {
      while (true) {
        WatchKey key = null;
        try {
          key = watchService.take();
        } catch (InterruptedException | ClosedWatchServiceException e) {
          return;
        }

        /* Poll the events and handle */
        for (WatchEvent<?> event : key.pollEvents()) {
          WatchEvent.Kind<?> kind = event.kind();

          if (kind == StandardWatchEventKinds.OVERFLOW) {
            continue;
          }

          if (kind == StandardWatchEventKinds.ENTRY_MODIFY) {
            for (DirectoryWatcher watcher : watchers) {
              watcher.handleModifyEvent(key, (Path) event.context());
            }

            continue;
          }

          if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
            for (DirectoryWatcher watcher : watchers) {
              watcher.handleCreateEvent(key, (Path) event.context());
            }

            continue;
          }

          for (DirectoryWatcher watcher : watchers) {
            watcher.handleDeleteEvent(key, (Path) event.context());
          }
        }

        /* Reset the Key to get more events later */
        if (!key.reset()) {
          for (DirectoryWatcher watcher : watchers) {
            watcher.handleKeyInvalid(key);
          }
        }
      }
View Full Code Here

   /**
    * Register the given directory with the WatchService
    */
   private void register(Path path, ResourceMonitorImpl monitorImpl) throws IOException
   {
      WatchKey key = path.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
      keys.put(key, monitorImpl);
   }
View Full Code Here

   @Override
   public void run()
   {
      while (alive)
      {
         WatchKey key;
         try
         {
            key = watcher.take();
         }
         catch (ClosedWatchServiceException | InterruptedException e)
         {
            break;
         }
         List<WatchEvent<?>> pollEvents = key.pollEvents();
         for (WatchEvent<?> event : pollEvents)
         {
            WatchEvent.Kind<?> kind = event.kind();
            if (kind == OVERFLOW)
            {
               continue;
            }

            WatchEvent<Path> ev = (WatchEvent<Path>) event;
            Path name = ev.context();
            ResourceMonitorImpl resourceMonitor = keys.get(key);
            if (resourceMonitor == null)
            {
               log.finest("WatchKey not recognized " + name + " - " + key.watchable() + "> " + kind);
               continue;
            }
            Path resourcePath = resourceMonitor.getResourcePath();
            Path child = resourcePath.resolve(name);
            log.log(Level.FINE, String.format("%s: %s %s %s\n", event.kind().name(), child, key, keys.keySet()));
            if (kind == ENTRY_CREATE)
            {
               try
               {
                  if (Files.isDirectory(child, LinkOption.NOFOLLOW_LINKS))
                  {
                     registerAll(child, resourceMonitor);
                  }
               }
               catch (IOException e)
               {
                  log.log(Level.SEVERE, "Error while registering child directories", e);
               }
               resourceMonitor.onPathCreate(child);
            }
            else if (kind == ENTRY_DELETE)
            {
               resourceMonitor.onPathDelete(child);
            }
            else if (kind == ENTRY_MODIFY)
            {
               resourceMonitor.onPathModify(child);
            }
         }

         if (!keys.containsKey(key))
         {
            // key is no longer available in the keys Map. Cancel it
            key.cancel();
         }
         else
         {
            // reset key and remove from set if directory no longer accessible
            boolean valid = key.reset();
            if (!valid)
            {
               keys.remove(key);
            }
         }
View Full Code Here

TOP

Related Classes of java.nio.file.WatchKey

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.