Package java.nio.file

Examples of java.nio.file.WatchKey


                     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


        }
    }

    private void processEvents() {
        while (true) {
            WatchKey key;
            try {
                key = watcher.take();
            } catch (InterruptedException e) {
                return;
            }
            Path dir = keys.get(key);
            if (dir == null) {
                LOGGER.warn("Could not find key for " + key);
                continue;
            }

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

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

                LOGGER.debug("Processing event {} on path {}", kind, child);

                if (kind == OVERFLOW) {
//                    rescan();
                    continue;
                }

                try {
                    if (kind == ENTRY_CREATE) {
                        if (Files.isDirectory(child, NOFOLLOW_LINKS)) {

                            // if directory is created, and watching recursively, then
                            // register it and its sub-directories
                            Files.walkFileTree(child, new FilteringFileVisitor());
                        } else if (Files.isRegularFile(child, NOFOLLOW_LINKS)) {
                            scan(child);
                        }
                    } else if (kind == ENTRY_MODIFY) {
                        if (Files.isRegularFile(child, NOFOLLOW_LINKS)) {
                            scan(child);
                        }
                    } else if (kind == ENTRY_DELETE) {
                        unscan(child);
                    }
                } catch (IOException x) {
                    // ignore to keep sample readbale
                    x.printStackTrace();
                }
            }

            // reset key and remove from set if directory no longer accessible
            boolean valid = key.reset();
            if (!valid) {
                LOGGER.debug("Removing key " + key + " and dir " + dir + " from keys");
                keys.remove(key);

                // all directories are inaccessible
View Full Code Here

        }
    }

    private void watch(final Path path) throws IOException {
        if (watcher != null) {
            WatchKey key = path.register(watcher, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE);
            keys.put(key, path);
            LOGGER.debug("Watched path " + path + " key " + key);
        } else {
            LOGGER.warn("No watcher yet for path " + path);
        }
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);
      if (OperatingSystemUtils.isWindows())
      {
         JDK_8029516.patch(key);
      }
      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

      Path evdev = Paths.get("/dev/input");
     
      WatchService watcher = evdev.getFileSystem().newWatchService();
      evdev.register(watcher, StandardWatchEventKinds.ENTRY_CREATE);
     
      WatchKey watckKey = watcher.take();
      List<WatchEvent<?>> events = watckKey.pollEvents();
      for (WatchEvent event:events) {
        if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
          String name = event.context().toString();
          if (filter.accept(input, name)) {
            LimeLog.info("Input " + name + " added");
View Full Code Here

            Main.debug("File watcher thread started");
        }
        while (true) {

            // wait for key to be signaled
            WatchKey key;
            try {
                key = watcher.take();
            } catch (InterruptedException x) {
                return;
            }

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

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

                // The filename is the context of the event.
                @SuppressWarnings("unchecked")
                WatchEvent<Path> ev = (WatchEvent<Path>)event;
                Path filename = ev.context();
                if (filename == null) {
                    continue;
                }

                // Only way to get full path (http://stackoverflow.com/a/7802029/2257172)
                Path fullPath = ((Path)key.watchable()).resolve(filename);

                synchronized(this) {
                    StyleSource style = styleMap.get(fullPath);
                    SourceEntry rule = ruleMap.get(fullPath);
                    if (style != null) {
                        Main.info("Map style "+style.getDisplayString()+" has been modified. Reloading style...");
                        Main.worker.submit(new MapPaintStyleLoader(Collections.singleton(style)));
                    } else if (rule != null) {
                        Main.info("Validator rule "+rule.getDisplayString()+" has been modified. Reloading rule...");
                        MapCSSTagChecker tagChecker = OsmValidator.getTest(MapCSSTagChecker.class);
                        if (tagChecker != null) {
                            try {
                                tagChecker.addMapCSS(rule.url);
                            } catch (IOException | ParseException e) {
                                Main.warn(e);
                            }
                        }
                    } else if (Main.isDebugEnabled()) {
                        Main.debug("Received "+kind.name()+" event for unregistered file: "+fullPath);
                    }
                }
            }

            // Reset the key -- this step is critical to receive
            // further watch events. If the key is no longer valid, the directory
            // is inaccessible so exit the loop.
            if (!key.reset()) {
                break;
            }
        }
    }
View Full Code Here

            Path file = dir.resolve("users.properties");
            encryptedPassword(new Properties(file.toFile()));

            while (true) {
                WatchKey key = watchService.take();
                for (WatchEvent<?> event : key.pollEvents()) {
                    WatchEvent.Kind kind = event.kind();
                    WatchEvent<Path> ev = (WatchEvent<Path>) event;

                    // Context for directory entry event is the file name of entry
                    Path name = dir.resolve(ev.context());
                    if (file.equals(name)) {
                        encryptedPassword(new Properties(file.toFile()));
                    }
                }
                key.reset();
            }

        } catch (ClosedWatchServiceException | InterruptedException e) {
            // Ignore
        } catch (IOException e) {
View Full Code Here

  }
 
  @Override
  protected boolean pollEvents() throws Exception {
    // Take events, but don't care what they are!
    WatchKey watchKey = watchService.take();
   
    watchKey.pollEvents();
    watchKey.reset();
   
    // Events are always relevant; ignored paths are not monitored
    return true;
  }
View Full Code Here

  private synchronized void registerWatch(Path dir) {
    if (!watchPathKeyMap.containsKey(dir)) {
      logger.log(Level.INFO, "- Registering " + dir);

      try {
        WatchKey watchKey = dir.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY, OVERFLOW);
        watchPathKeyMap.put(dir, watchKey);
      }
      catch (IOException e) {
        // Don't care!
      }
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.