Search in sources :

Example 1 with WatchKey

use of java.nio.file.WatchKey in project jetty.project by eclipse.

the class PathWatcher method register.

/**
     * Register a path (directory) with the WatchService.
     * 
     * @param dir the directory to register
     * @param root the configuration root
     * @throws IOException if unable to register the path with the watch service.
     */
protected void register(Path dir, Config root) throws IOException {
    LOG.debug("Registering watch on {}", dir);
    if (watchModifiers != null) {
        // Java Watcher
        WatchKey key = dir.register(watchService, WATCH_EVENT_KINDS, watchModifiers);
        keys.put(key, root.asSubConfig(dir));
    } else {
        // Native Watcher
        WatchKey key = dir.register(watchService, WATCH_EVENT_KINDS);
        keys.put(key, root.asSubConfig(dir));
    }
}
Also used : WatchKey(java.nio.file.WatchKey)

Example 2 with WatchKey

use of java.nio.file.WatchKey in project cas by apereo.

the class ServiceRegistryConfigWatcher method handleEvent.

/**
     * Handle event.
     *
     * @param key the key
     */
private void handleEvent(final WatchKey key) {
    this.readLock.lock();
    try {
        //The filename is the context of the event.
        key.pollEvents().stream().filter(event -> event.count() <= 1).forEach(event -> {
            final WatchEvent.Kind kind = event.kind();
            final WatchEvent<Path> ev = (WatchEvent<Path>) event;
            final Path filename = ev.context();
            final Path parent = (Path) key.watchable();
            final Path fullPath = parent.resolve(filename);
            final File file = fullPath.toFile();
            LOGGER.trace("Detected event [{}] on file [{}]. Loading change...", kind, file);
            if (kind.name().equals(ENTRY_CREATE.name()) && file.exists()) {
                handleCreateEvent(file);
            } else if (kind.name().equals(ENTRY_DELETE.name())) {
                handleDeleteEvent();
            } else if (kind.name().equals(ENTRY_MODIFY.name()) && file.exists()) {
                handleModifyEvent(file);
            }
        });
    } finally {
        this.readLock.unlock();
    }
}
Also used : CasRegisteredServicesRefreshEvent(org.apereo.cas.support.events.service.CasRegisteredServicesRefreshEvent) Logger(org.slf4j.Logger) WatchEvent(java.nio.file.WatchEvent) LoggerFactory(org.slf4j.LoggerFactory) Throwables(com.google.common.base.Throwables) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) File(java.io.File) WatchKey(java.nio.file.WatchKey) IOUtils(org.apache.commons.io.IOUtils) WatchService(java.nio.file.WatchService) Lock(java.util.concurrent.locks.Lock) StandardWatchEventKinds(java.nio.file.StandardWatchEventKinds) Closeable(java.io.Closeable) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Path(java.nio.file.Path) FileSystems(java.nio.file.FileSystems) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Path(java.nio.file.Path) WatchEvent(java.nio.file.WatchEvent) File(java.io.File)

Example 3 with WatchKey

use of java.nio.file.WatchKey in project che by eclipse.

the class FileWatcherService method register.

/**
     * Registers a directory for tracking of corresponding entry creation,
     * modification or deletion events. Each call of this method increase
     * by one registration counter that corresponds to each folder being
     * watched. Any event related to such directory entry is passed further to
     * the specific handler only if registration counter related to the
     * directory is above zero, otherwise registration watch key is canceled
     * and no further directory watching is being performed.
     *
     * @param dir
     *         directory
     */
public void register(Path dir) {
    LOG.debug("Registering directory '{}'", dir);
    if (keys.values().contains(dir)) {
        int previous = registrations.get(dir);
        LOG.debug("Directory is already being watched, increasing watch counter, previous value: {}", previous);
        registrations.put(dir, previous + 1);
    } else {
        try {
            LOG.debug("Starting watching directory '{}'", dir);
            WatchKey watchKey = dir.register(service, eventKinds, eventModifiers);
            keys.put(watchKey, dir);
            registrations.put(dir, 1);
        } catch (IOException e) {
            LOG.error("Can't register dir {} in file watch service", dir, e);
        }
    }
}
Also used : WatchKey(java.nio.file.WatchKey) IOException(java.io.IOException)

Example 4 with WatchKey

use of java.nio.file.WatchKey in project asterixdb by apache.

the class FileSystemWatcher method register.

/**
     * Register the given directory, and all its sub-directories, with the
     * WatchService.
     */
private void register(Path dir) throws IOException {
    WatchKey key = dir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
    keys.put(key, dir);
}
Also used : WatchKey(java.nio.file.WatchKey)

Example 5 with WatchKey

use of java.nio.file.WatchKey in project asterixdb by apache.

the class FileSystemWatcher method take.

// take is blocking
public File take() throws IOException {
    File next = poll();
    if (next != null) {
        return next;
    }
    if (done || !isFeed) {
        return null;
    }
    // No file was found, wait for the filesystem to push events
    WatchKey key;
    while (!it.hasNext()) {
        try {
            key = watcher.take();
        } catch (InterruptedException x) {
            if (LOGGER.isEnabledFor(Level.WARN)) {
                LOGGER.warn("Feed Closed");
            }
            if (watcher == null) {
                return null;
            }
            continue;
        } catch (ClosedWatchServiceException e) {
            if (LOGGER.isEnabledFor(Level.WARN)) {
                LOGGER.warn("The watcher has exited");
            }
            if (watcher == null) {
                return null;
            }
            continue;
        }
        handleEvents(key);
        if (endOfEvents(key)) {
            return null;
        }
    }
    // files were found, re-create the iterator and move it one step
    return it.next();
}
Also used : WatchKey(java.nio.file.WatchKey) ClosedWatchServiceException(java.nio.file.ClosedWatchServiceException) File(java.io.File)

Aggregations

WatchKey (java.nio.file.WatchKey)134 Path (java.nio.file.Path)88 WatchEvent (java.nio.file.WatchEvent)65 IOException (java.io.IOException)49 WatchService (java.nio.file.WatchService)30 File (java.io.File)27 ClosedWatchServiceException (java.nio.file.ClosedWatchServiceException)18 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)10 HashMap (java.util.HashMap)7 Map (java.util.Map)7 FileSystems (java.nio.file.FileSystems)6 FileVisitResult (java.nio.file.FileVisitResult)6 HashSet (java.util.HashSet)6 List (java.util.List)6 StandardWatchEventKinds (java.nio.file.StandardWatchEventKinds)5 InputStream (java.io.InputStream)4 AccessDeniedException (java.nio.file.AccessDeniedException)4 Kind (java.nio.file.WatchEvent.Kind)4 Logger (org.slf4j.Logger)4