Search in sources :

Example 56 with WatchEvent

use of java.nio.file.WatchEvent in project Orchid by JavaEden.

the class FileWatcher method processEvents.

private void processEvents() {
    while (true) {
        WatchKey key;
        try {
            key = watcher.take();
        } catch (InterruptedException x) {
            return;
        }
        Path dir = keys.get(key);
        if (dir == null) {
            System.err.println("WatchKey not recognized!!");
            continue;
        }
        for (WatchEvent<?> event : key.pollEvents()) {
            WatchEvent.Kind kind = event.kind();
            if (kind == OVERFLOW) {
                continue;
            }
            WatchEvent<Path> ev = (WatchEvent<Path>) event;
            Path name = ev.context();
            Path child = dir.resolve(name);
            context.broadcast(Orchid.Events.FILES_CHANGED);
            if (kind == ENTRY_CREATE) {
                try {
                    if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
                        registerAll(child);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        boolean valid = key.reset();
        if (!valid) {
            keys.remove(key);
            if (keys.isEmpty()) {
                break;
            }
        }
    }
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) WatchEvent(java.nio.file.WatchEvent) IOException(java.io.IOException)

Example 57 with WatchEvent

use of java.nio.file.WatchEvent in project ddf by codice.

the class ContentProducerDataAccessObject method getFileUsingRefKey.

public File getFileUsingRefKey(boolean storeRefKey, Message in) throws ContentComponentException {
    File ingestedFile;
    try {
        if (!storeRefKey) {
            ingestedFile = ((GenericFile<File>) in.getBody()).getFile();
        } else {
            WatchEvent<Path> pathWatchEvent = (WatchEvent<Path>) ((GenericFileMessage) in).getGenericFile().getFile();
            ingestedFile = pathWatchEvent.context().toFile();
        }
    } catch (ClassCastException e) {
        throw new ContentComponentException("Unable to cast message body to Camel GenericFile, so unable to process ingested file");
    }
    return ingestedFile;
}
Also used : Path(java.nio.file.Path) GenericFileMessage(org.apache.camel.component.file.GenericFileMessage) WatchEvent(java.nio.file.WatchEvent) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File)

Example 58 with WatchEvent

use of java.nio.file.WatchEvent in project Gargoyle by callakrsos.

the class FileUtilTest method watchTest.

@Test
public void watchTest() throws IOException {
    File file = new File("c:\\someDir");
    file.mkdirs();
    WatchService newWatchService = FileSystems.getDefault().newWatchService();
    WatchKey register = file.toPath().register(newWatchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
    System.out.println("Watch Service Registered ..");
    while (true) {
        try {
            System.out.println("start");
            WatchKey key = newWatchService.take();
            for (WatchEvent<?> event : key.pollEvents()) {
                WatchEvent.Kind<?> kind = event.kind();
                @SuppressWarnings("unchecked") WatchEvent<Path> ev = (WatchEvent<Path>) event;
                Path fileName = ev.context();
                System.out.println(kind.name() + ": " + fileName);
                if (key == ENTRY_MODIFY && fileName.toString().equals("DirectoryWatchDemo.java")) {
                    System.out.println("My source file has changed!!!");
                }
            }
            boolean valid = key.reset();
            if (!valid) {
                break;
            }
        } catch (InterruptedException e) {
            break;
        }
    }
    System.out.println("end ");
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) WatchEvent(java.nio.file.WatchEvent) File(java.io.File) WatchService(java.nio.file.WatchService) Test(org.junit.Test)

Example 59 with WatchEvent

use of java.nio.file.WatchEvent in project OpenGrok by OpenGrok.

the class RuntimeEnvironment method startWatchDogService.

/**
 * Starts a watch dog service for a directory. It automatically reloads the
 * AuthorizationFramework if there was a change in <b>real-time</b>.
 * Suitable for plugin development.
 *
 * You can control start of this service by a configuration parameter
 * {@link Configuration#authorizationWatchdogEnabled}
 *
 * @param directory root directory for plugins
 */
public void startWatchDogService(File directory) {
    stopWatchDogService();
    if (directory == null || !directory.isDirectory() || !directory.canRead()) {
        LOGGER.log(Level.INFO, "Watch dog cannot be started - invalid directory: {0}", directory);
        return;
    }
    LOGGER.log(Level.INFO, "Starting watchdog in: {0}", directory);
    watchDogThread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                watchDogWatcher = FileSystems.getDefault().newWatchService();
                Path dir = Paths.get(directory.getAbsolutePath());
                Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {

                    @Override
                    public FileVisitResult postVisitDirectory(Path d, IOException exc) throws IOException {
                        // attach monitor
                        LOGGER.log(Level.FINEST, "Watchdog registering {0}", d);
                        d.register(watchDogWatcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
                        return CONTINUE;
                    }
                });
                LOGGER.log(Level.INFO, "Watch dog started {0}", directory);
                while (!Thread.currentThread().isInterrupted()) {
                    final WatchKey key;
                    try {
                        key = watchDogWatcher.take();
                    } catch (ClosedWatchServiceException x) {
                        break;
                    }
                    boolean reload = false;
                    for (WatchEvent<?> event : key.pollEvents()) {
                        final WatchEvent.Kind<?> kind = event.kind();
                        if (kind == ENTRY_CREATE) {
                            reload = true;
                        } else if (kind == ENTRY_DELETE) {
                            reload = true;
                        } else if (kind == ENTRY_MODIFY) {
                            reload = true;
                        }
                    }
                    if (reload) {
                        // experimental wait if file is being written right now
                        Thread.sleep(THREAD_SLEEP_TIME);
                        getAuthorizationFramework().reload();
                    }
                    if (!key.reset()) {
                        break;
                    }
                }
            } catch (InterruptedException | IOException ex) {
                LOGGER.log(Level.FINEST, "Watchdog finishing (exiting)", ex);
                Thread.currentThread().interrupt();
            }
            LOGGER.log(Level.FINER, "Watchdog finishing (exiting)");
        }
    }, "watchDogService");
    watchDogThread.start();
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) ClosedWatchServiceException(java.nio.file.ClosedWatchServiceException) WatchEvent(java.nio.file.WatchEvent)

Example 60 with WatchEvent

use of java.nio.file.WatchEvent in project felix by apache.

the class Watcher method processEvents.

public void processEvents() {
    while (true) {
        WatchKey key = watcher.poll();
        if (key == null) {
            break;
        }
        Path dir = keys.get(key);
        if (dir == null) {
            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);
            debug("Processing event {} on path {}", kind, child);
            if (kind == OVERFLOW) {
                // rescan();
                continue;
            }
            try {
                if (kind == ENTRY_CREATE) {
                    if (Files.isDirectory(child)) {
                        // if directory is created, and watching recursively, then
                        // register it and its sub-directories
                        Files.walkFileTree(child, new FilteringFileVisitor());
                    } else if (Files.isRegularFile(child)) {
                        scan(child);
                    }
                } else if (kind == ENTRY_MODIFY) {
                    if (Files.isRegularFile(child)) {
                        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) {
            debug("Removing key " + key + " and dir " + dir + " from keys");
            keys.remove(key);
            // all directories are inaccessible
            if (keys.isEmpty()) {
                break;
            }
        }
    }
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) WatchEvent(java.nio.file.WatchEvent) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Aggregations

WatchEvent (java.nio.file.WatchEvent)92 Path (java.nio.file.Path)68 WatchKey (java.nio.file.WatchKey)58 IOException (java.io.IOException)33 File (java.io.File)27 Test (org.junit.Test)24 WatchService (java.nio.file.WatchService)20 BuckEventBus (com.facebook.buck.event.BuckEventBus)13 FakeClock (com.facebook.buck.timing.FakeClock)13 EventBus (com.google.common.eventbus.EventBus)12 ClosedWatchServiceException (java.nio.file.ClosedWatchServiceException)10 EasyMock.anyObject (org.easymock.EasyMock.anyObject)10 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)6 Subscribe (com.google.common.eventbus.Subscribe)5 FakeWatchmanClient (com.facebook.buck.io.FakeWatchmanClient)4 FileSystems (java.nio.file.FileSystems)4 StandardWatchEventKinds (java.nio.file.StandardWatchEventKinds)4 HashSet (java.util.HashSet)4 List (java.util.List)4