Package name.pachler.nio.file

Examples of name.pachler.nio.file.WatchKey


    public void testEventsStackerRunnable() throws Exception {
        PathWatch.log.append("\n\n");
        PathWatch.log.appendN('-', 50);
        PathWatch.log.append("testEventsStackerRunnable\n");
        WatchKey key = new WatchKey() {

            public boolean reset() {
                return false;
            }
View Full Code Here


            if (log != null) {
                log.append("Track: ").appendObject(path).append("Listener: ").appendObject(listener).append('\n');
            }
            boolean add = true;
            WatchKey key = null;
            try {
                key = watchedPath.register(watchService, StandardWatchEventKind.ENTRY_CREATE,
                        StandardWatchEventKind.ENTRY_DELETE, StandardWatchEventKind.ENTRY_MODIFY,
                        StandardWatchEventKind.OVERFLOW, ExtendedWatchEventKind.KEY_INVALID);
            } catch (UnsupportedOperationException uox) {
View Full Code Here

        public void run() {

            for (;;) {
                // take() will block until a file has been created/deleted
                WatchKey signalledKey;
                try {
                    signalledKey = watchService.take();
                } catch (InterruptedException ix) {
                    // we'll ignore being interrupted
                    if (log != null) {
                        log.append("Interrupted\n");
                    }
                    continue;
                } catch (ClosedWatchServiceException cwse) {
                    // other thread closed watch service
                    System.out.println("watch service closed, terminating.");
                    break;
                }

                List<WatchEvent<?>> list;
                Path watchedPath;
                EventsStackerRunnable stacker;

                synchronized (lock) {
                    synchronized (keyToPathLock) {
                        watchedPath = keyToPath.get(signalledKey);
                    }
                    if (watchedPath == null) {
                        continue;
                    }

                    // get list of events from key
                    list = signalledKey.pollEvents();

                    stacker = pathToStacker.get(watchedPath);
                    if (stacker == null) {
                        //if the stacker does not exist, go on without rescheduling the key!
                        if (log != null) {
                            log.append("Stacker for: ").appendObject(watchedPath).append("is null\n");
                        }
                        continue;
                    }

                    runnables.add(stacker);

                    for (WatchEvent<?> e : list) {
                        Path context = (Path) e.context();
                        Path resolve = watchedPath.resolve(context);
                        File file = new File(resolve.toString());
                        Kind<?> kind = e.kind();
                        if (log != null) {
                            log.append("Event: ").appendObject(e).append('\n');
                        }
                        if (kind == StandardWatchEventKind.OVERFLOW) {
                            if (!file.exists()) {
                                //It may be that it became invalid...
                                synchronized (keyToPathLock) {
                                    keyToPath.remove(signalledKey);
                                }
                                stacker.key = null;
                                addInvalidPath(stacker);
                                stacker.removed(file);
                            } else {
                                // VERY IMPORTANT! call reset() AFTER pollEvents() to allow the
                                // key to be reported again by the watch service.
                                signalledKey.reset();
                                if (log != null) {
                                    log.append("Key reset to hear changes");
                                }
                            }
                            //On an overflow, wait a bit and signal that all files being watched were removed,
                            //do a list and say that the current files were added again.
                            stacker.overflow(file);

                        } else {
                            if (kind == StandardWatchEventKind.ENTRY_CREATE
                                    || kind == StandardWatchEventKind.ENTRY_MODIFY) {
                                // VERY IMPORTANT! call reset() AFTER pollEvents() to allow the
                                // key to be reported again by the watch service.
                                signalledKey.reset();
                                if (log != null) {
                                    log.append("Key reset to hear changes");
                                }

                                stacker.added(file);

                            } else if (kind == StandardWatchEventKind.ENTRY_DELETE) {
                                // VERY IMPORTANT! call reset() AFTER pollEvents() to allow the
                                // key to be reported again by the watch service.
                                signalledKey.reset();
                                if (log != null) {
                                    log.append("Key reset to hear changes");
                                }
                                stacker.removed(file);
View Full Code Here

    // Nothing must happen before the event loop.   
  }

  @Override
  public boolean pollEvents() throws Exception {
    WatchKey watchKey = watchService.take();

    List<WatchEvent<?>> watchEvents = watchKey.pollEvents();
    boolean hasRelevantEvents = false;
   
    // Filter ignored events
    for (WatchEvent<?> watchEvent : watchEvents) {
      if (watchEvent.kind() == ENTRY_CREATE || watchEvent.kind() == ENTRY_MODIFY || watchEvent.kind() == ENTRY_DELETE) {       
        boolean ignoreEvent = false;   

        name.pachler.nio.file.Path extLibFilePath = (name.pachler.nio.file.Path) watchEvent.context();
        Path filePath = Paths.get(extLibFilePath.toString()).toAbsolutePath().normalize();
       
        for (Path ignorePath : ignorePaths) {
          if (filePath.startsWith(ignorePath.toAbsolutePath().normalize())) {
            ignoreEvent = true;
            break;
          }
        }
       
        if (!ignoreEvent) {
          hasRelevantEvents = true;
          break;
        }
      }
    }

    watchKey.reset();
    return hasRelevantEvents;
  }
View Full Code Here

  public WatchDir(File dir, WatchedCallback watched) throws IOException {
    this.watched = watched;
    this.keys = newHashMap();
    watchService = FileSystems.getDefault().newWatchService();
    Path watchedPath = Paths.get(dir.getAbsolutePath());
    WatchKey signalledKey = watchedPath.register(watchService, StandardWatchEventKind.ENTRY_CREATE, StandardWatchEventKind.ENTRY_DELETE, StandardWatchEventKind.ENTRY_MODIFY);
    // Store the path that we're watching, so we can later retrieve it and build a proper path to the file
    keys.put(signalledKey, watchedPath);
  }
View Full Code Here

  /** */
  @Override
  public void run() {
    for (;;) {
      // take() will block until a file has been created/deleted
      WatchKey signalledKey;
      try {
        signalledKey = watchService.take();
      } catch (InterruptedException ix) {
        // we'll ignore being interrupted
        continue;
      } catch (ClosedWatchServiceException cwse) {
        // other thread closed watch service
        log.debug("Watch service closed, terminating.");
        break;
      }

      // get list of events from key
      List<WatchEvent<?>> list = signalledKey.pollEvents();

      // VERY IMPORTANT! call reset() AFTER pollEvents() to allow the
      // key to be reported again by the watch service
      signalledKey.reset();

      // Retrieve the path for the files from the key created above; this is used by the
      // resolver below.
      Path dir = keys.get(signalledKey);
      try {
View Full Code Here

TOP

Related Classes of name.pachler.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.