Example usage for java.nio.file StandardWatchEventKinds OVERFLOW

List of usage examples for java.nio.file StandardWatchEventKinds OVERFLOW

Introduction

In this page you can find the example usage for java.nio.file StandardWatchEventKinds OVERFLOW.

Prototype

WatchEvent.Kind OVERFLOW

To view the source code for java.nio.file StandardWatchEventKinds OVERFLOW.

Click Source Link

Document

A special event to indicate that events may have been lost or discarded.

Usage

From source file:org.springframework.cloud.config.monitor.FileMonitorConfiguration.java

private Set<File> filesFromEvents() {
    Set<File> files = new LinkedHashSet<File>();
    if (this.watcher == null) {
        return files;
    }// w w  w  .ja  v a  2s  . c o  m
    WatchKey key = this.watcher.poll();
    while (key != null) {
        for (WatchEvent<?> event : key.pollEvents()) {
            if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE
                    || event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) {
                Path item = (Path) event.context();
                File file = new File(
                        ((Path) key.watchable()).toAbsolutePath() + File.separator + item.getFileName());
                if (file.isDirectory()) {
                    files.addAll(walkDirectory(file.toPath()));
                } else {
                    if (!file.getPath().contains(".git")
                            && !PatternMatchUtils.simpleMatch(this.excludes, file.getName())) {
                        if (log.isDebugEnabled()) {
                            log.debug("Watch Event: " + event.kind() + ": " + file);
                        }
                        files.add(file);
                    }
                }
            } else if (event.kind() == StandardWatchEventKinds.OVERFLOW) {
                if (log.isDebugEnabled()) {
                    log.debug("Watch Event: " + event.kind() + ": context: " + event.context());
                }
                if (event.context() != null && event.context() instanceof Path) {
                    files.addAll(walkDirectory((Path) event.context()));
                } else {
                    for (Path path : this.directory) {
                        files.addAll(walkDirectory(path));
                    }
                }
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("Watch Event: " + event.kind() + ": context: " + event.context());
                }
            }
        }
        key.reset();
        key = this.watcher.poll();
    }
    return files;
}

From source file:org.apache.tomee.jul.handler.rotating.ArchivingTest.java

private static void watch(final WatchKey key) {

    if (watcherThread != null) {
        // tell the old watchter thread to shutdown
        watcherThread.interrupt();//from w  ww. j a  va  2 s . co  m
    }

    watcherThread = new Thread("ArchivingTest.watch") {
        @Override
        public void run() {

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

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

                    if (watcherThread != this || isInterrupted()) {
                        return;
                    }

                    lastEvent.set(event);

                    latch.get().countDown();
                }

                final boolean valid = key.reset();
                if (!valid) {
                    System.out.println("ArchivingTest.watch terminated");
                    break;
                }
            }
        }
    };

    watcherThread.start();
}

From source file:com.facebook.buck.util.ProjectFilesystemTest.java

@Test
public void testCreateContextStringForOverflowEvent() {
    WatchEvent<Object> overflowEvent = new WatchEvent<Object>() {
        @Override//from ww w.  j a va2s .c  o m
        public Kind<Object> kind() {
            return StandardWatchEventKinds.OVERFLOW;
        }

        @Override
        public int count() {
            return 0;
        }

        @Override
        public Object context() {
            return new Object() {
                @Override
                public String toString() {
                    return "I am the context string.";
                }
            };
        }
    };
    assertEquals("I am the context string.", filesystem.createContextString(overflowEvent));
}

From source file:MonitorSaurausRex.MainMenu.java

public boolean MonitorDirectory() {

    Path directory = Paths.get("C:/Users/" + user + "/Documents/");
    try {/*w  ww  .  j  ava  2  s.co m*/
        WatchService fileSystemWatchService = FileSystems.getDefault().newWatchService();
        WatchKey watchKey = directory.register(fileSystemWatchService, StandardWatchEventKinds.ENTRY_CREATE,
                StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);

        testTimer = new TimerTask() {

            @Override
            public void run() {
                checkPause = true;
                test = changeCheck();

                if (test) {
                    testTimer.cancel();
                    System.out.println("Quaritnen sucsessfully activates");
                }
            }

        };

        Timer timer = new Timer();
        timer.schedule(testTimer, 0, 1000);
        while (true) {
            WatchKey watchKeyActual = fileSystemWatchService.take();
            for (WatchEvent<?> event : watchKeyActual.pollEvents()) {

                WatchEvent.Kind<?> eventKind = event.kind();

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

                WatchEvent<Path> eventPath = (WatchEvent<Path>) event;
                Path fileName = eventPath.context();

                //timerCheck(); ????? 
                //http://stackoverflow.com/questions/4044726/how-to-set-a-timer-in-java
                //  boolean test = false;
                if (checkPause == false) {
                    checkPause = true;
                } else {
                    ChangeCounter++;
                    System.out.println("Event " + eventKind + " occurred on " + fileName);
                }
                if (test)
                    break;
            }
            boolean isReset = watchKeyActual.reset();
            if (!isReset || test) {
                break;
            }
        }

    } catch (IOException | InterruptedException ioe) {
    }

    return true; /// EXIT METHOD
}

From source file:org.flowerplatform.web.git.GitUtils.java

public void listenForChanges(File file) throws IOException {
    Path path = file.toPath();/* www .  j  a  va  2 s  .  co m*/
    if (file.isDirectory()) {
        WatchService ws = path.getFileSystem().newWatchService();
        path.register(ws, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE,
                StandardWatchEventKinds.ENTRY_MODIFY);
        WatchKey watch = null;
        while (true) {
            System.out.println("Watching directory: " + file.getPath());
            try {
                watch = ws.take();
            } catch (InterruptedException ex) {
                System.err.println("Interrupted");
            }
            List<WatchEvent<?>> events = watch.pollEvents();
            if (!watch.reset()) {
                break;
            }
            for (WatchEvent<?> event : events) {
                Kind<Path> kind = (Kind<Path>) event.kind();
                Path context = (Path) event.context();
                if (kind.equals(StandardWatchEventKinds.OVERFLOW)) {
                    System.out.println("OVERFLOW");
                } else if (kind.equals(StandardWatchEventKinds.ENTRY_CREATE)) {
                    System.out.println("Created: " + context.getFileName());
                } else if (kind.equals(StandardWatchEventKinds.ENTRY_DELETE)) {
                    System.out.println("Deleted: " + context.getFileName());
                } else if (kind.equals(StandardWatchEventKinds.ENTRY_MODIFY)) {
                    System.out.println("Modified: " + context.getFileName());
                }
            }
        }
    } else {
        System.err.println("Not a directory. Will exit.");
    }
}