Java Code Examples for com.sun.nio.file.SensitivityWatchEventModifier

The following examples show how to use com.sun.nio.file.SensitivityWatchEventModifier. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: T0rlib4j   Source File: JavaWatchObserver.java    License: Apache License 2.0 6 votes vote down vote up
public JavaWatchObserver(File fileToWatch) throws IOException {
    if (fileToWatch == null || !fileToWatch.exists()) {
        throw new RuntimeException("fileToWatch must not be null and must already exist.");
    }
    this.fileToWatch = fileToWatch;
    lastModified = fileToWatch.lastModified();
    length = fileToWatch.length();

    watchService = FileSystems.getDefault().newWatchService();
    // Note that poll depends on us only registering events that are of type path
    if (OsData.getOsType() != OsData.OsType.MAC) {
        key = fileToWatch.getParentFile().toPath().register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE,
                StandardWatchEventKinds.ENTRY_MODIFY);
    } else {
        // Unfortunately the default watch service on Mac is broken, it uses a separate thread and really slow polling to detect file changes
        // rather than integrating with the OS. There is a hack to make it poll faster which we can use for now. See
        // http://stackoverflow.com/questions/9588737/is-java-7-watchservice-slow-for-anyone-else
        key = fileToWatch.getParentFile().toPath().register(watchService, new WatchEvent.Kind[]
                {StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE,
                StandardWatchEventKinds.ENTRY_MODIFY}, SensitivityWatchEventModifier.HIGH);
    }
}
 
Example 2
Source Project: Tor_Onion_Proxy_Library   Source File: JavaWatchObserver.java    License: Apache License 2.0 6 votes vote down vote up
public JavaWatchObserver(File fileToWatch) throws IOException {
    if (fileToWatch == null || !fileToWatch.exists()) {
        throw new RuntimeException("fileToWatch must not be null and must already exist.");
    }
    this.fileToWatch = fileToWatch;
    lastModified = fileToWatch.lastModified();
    length = fileToWatch.length();

    watchService = FileSystems.getDefault().newWatchService();
    // Note that poll depends on us only registering events that are of type path
    if (OsData.getOsType() != OsData.OsType.MAC) {
        key = fileToWatch.getParentFile().toPath().register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE,
                StandardWatchEventKinds.ENTRY_MODIFY);
    } else {
        // Unfortunately the default watch service on Mac is broken, it uses a separate thread and really slow polling to detect file changes
        // rather than integrating with the OS. There is a hack to make it poll faster which we can use for now. See
        // http://stackoverflow.com/questions/9588737/is-java-7-watchservice-slow-for-anyone-else
        key = fileToWatch.getParentFile().toPath().register(watchService, new WatchEvent.Kind[]
                {StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE,
                StandardWatchEventKinds.ENTRY_MODIFY}, SensitivityWatchEventModifier.HIGH);
    }
}
 
Example 3
Source Project: cssfx   Source File: PathsWatcher.java    License: Apache License 2.0 6 votes vote down vote up
public void monitor(Path directory, Path sourceFile, Runnable action) {
    if (watchService != null) {
        logger(PathsWatcher.class).info("registering action %d for monitoring %s in %s", System.identityHashCode(action), sourceFile, directory);
        Map<String, List<Runnable>> fileAction = filesActions.computeIfAbsent(
                directory.toString(), (p) -> {
                    try {
                        directory.register(watchService, new WatchEvent.Kind[]{ StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE}, SensitivityWatchEventModifier.HIGH);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return new HashMap<>();
                });

        List<Runnable> actions = fileAction.computeIfAbsent(sourceFile.toString(), k -> new LinkedList<>());
        actions.add(action);
        logger(PathsWatcher.class).debug("%d CSS modification actions registered for file %s", actions.size(), sourceFile);
    } else {
        logger(PathsWatcher.class).warn("no WatchService active, CSS monitoring cannot occur");
    }
}
 
Example 4
Source Project: flink   Source File: FileBasedOneShotLatch.java    License: Apache License 2.0 5 votes vote down vote up
private static void watchForLatchFile(final WatchService watchService, final Path parentDir) {
	try {
		parentDir.register(
			watchService,
			new WatchEvent.Kind[]{StandardWatchEventKinds.ENTRY_CREATE},
			SensitivityWatchEventModifier.HIGH);
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example 5
private PollingWatchKey doPrivilegedRegister(Path path,
                                             Set<? extends WatchEvent.Kind<?>> events,
                                             SensitivityWatchEventModifier sensivity)
    throws IOException
{
    // check file is a directory and get its file key if possible
    BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
    if (!attrs.isDirectory()) {
        throw new NotDirectoryException(path.toString());
    }
    Object fileKey = attrs.fileKey();
    if (fileKey == null)
        throw new AssertionError("File keys must be supported");

    // grab close lock to ensure that watch service cannot be closed
    synchronized (closeLock()) {
        if (!isOpen())
            throw new ClosedWatchServiceException();

        PollingWatchKey watchKey;
        synchronized (map) {
            watchKey = map.get(fileKey);
            if (watchKey == null) {
                // new registration
                watchKey = new PollingWatchKey(path, this, fileKey);
                map.put(fileKey, watchKey);
            } else {
                // update to existing registration
                watchKey.disable();
            }
        }
        watchKey.enable(events, sensivity.sensitivityValueInSeconds());
        return watchKey;
    }

}
 
Example 6
static void register(Path[] dirs, WatchService watcher) throws IOException {
    SensitivityWatchEventModifier[] sensitivtives =
        SensitivityWatchEventModifier.values();
    for (int i=0; i<dirs.length; i++) {
        SensitivityWatchEventModifier sensivity =
            sensitivtives[ rand.nextInt(sensitivtives.length) ];
        Path dir = dirs[i];
        dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY }, sensivity);
    }
}
 
Example 7
private PollingWatchKey doPrivilegedRegister(Path path,
                                             Set<? extends WatchEvent.Kind<?>> events,
                                             SensitivityWatchEventModifier sensivity)
    throws IOException
{
    // check file is a directory and get its file key if possible
    BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
    if (!attrs.isDirectory()) {
        throw new NotDirectoryException(path.toString());
    }
    Object fileKey = attrs.fileKey();
    if (fileKey == null)
        throw new AssertionError("File keys must be supported");

    // grab close lock to ensure that watch service cannot be closed
    synchronized (closeLock()) {
        if (!isOpen())
            throw new ClosedWatchServiceException();

        PollingWatchKey watchKey;
        synchronized (map) {
            watchKey = map.get(fileKey);
            if (watchKey == null) {
                // new registration
                watchKey = new PollingWatchKey(path, this, fileKey);
                map.put(fileKey, watchKey);
            } else {
                // update to existing registration
                watchKey.disable();
            }
        }
        watchKey.enable(events, sensivity.sensitivityValueInSeconds());
        return watchKey;
    }

}
 
Example 8
static void register(Path[] dirs, WatchService watcher) throws IOException {
    SensitivityWatchEventModifier[] sensitivtives =
        SensitivityWatchEventModifier.values();
    for (int i=0; i<dirs.length; i++) {
        SensitivityWatchEventModifier sensivity =
            sensitivtives[ rand.nextInt(sensitivtives.length) ];
        Path dir = dirs[i];
        dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY }, sensivity);
    }
}
 
Example 9
Source Project: jdk8u60   Source File: PollingWatchService.java    License: GNU General Public License v2.0 5 votes vote down vote up
private PollingWatchKey doPrivilegedRegister(Path path,
                                             Set<? extends WatchEvent.Kind<?>> events,
                                             SensitivityWatchEventModifier sensivity)
    throws IOException
{
    // check file is a directory and get its file key if possible
    BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
    if (!attrs.isDirectory()) {
        throw new NotDirectoryException(path.toString());
    }
    Object fileKey = attrs.fileKey();
    if (fileKey == null)
        throw new AssertionError("File keys must be supported");

    // grab close lock to ensure that watch service cannot be closed
    synchronized (closeLock()) {
        if (!isOpen())
            throw new ClosedWatchServiceException();

        PollingWatchKey watchKey;
        synchronized (map) {
            watchKey = map.get(fileKey);
            if (watchKey == null) {
                // new registration
                watchKey = new PollingWatchKey(path, this, fileKey);
                map.put(fileKey, watchKey);
            } else {
                // update to existing registration
                watchKey.disable();
            }
        }
        watchKey.enable(events, sensivity.sensitivityValueInSeconds());
        return watchKey;
    }

}
 
Example 10
Source Project: jdk8u60   Source File: SensitivityModifier.java    License: GNU General Public License v2.0 5 votes vote down vote up
static void register(Path[] dirs, WatchService watcher) throws IOException {
    SensitivityWatchEventModifier[] sensitivtives =
        SensitivityWatchEventModifier.values();
    for (int i=0; i<dirs.length; i++) {
        SensitivityWatchEventModifier sensivity =
            sensitivtives[ rand.nextInt(sensitivtives.length) ];
        Path dir = dirs[i];
        dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY }, sensivity);
    }
}
 
Example 11
public void run(){
    isRunning.set(true);

    clearErroredFiles();

    try (final WatchService watchService = FileSystems.getDefault().newWatchService()) {
        final WatchKey watchKey = FileSystems.getDefault().getPath(serverConfiguration.getParentDirectoryPath()).register(watchService, new WatchEvent.Kind[]{StandardWatchEventKinds.ENTRY_CREATE}, SensitivityWatchEventModifier.HIGH);

        while (isRunning.get()) {
            final WatchKey wk = watchService.take();

            for (WatchEvent<?> event : wk.pollEvents()) {
                final Path changed = (Path) event.context();

                if (changed.toFile().getName().startsWith(SCRIPT_ERROR_FILE_PREFIX)) {
                    processError(ErrorFileType.SCRIPT, changed.toFile());
                }
            }
            boolean valid = wk.reset();
            if (!valid) {
                ServerLogger.log(ServerLogger.Level.INFO, TAG, "The watcher key has been unregistered");
            }
        }
    }catch(Exception ex){
        if (isRunning.get()) {
            ServerLogger.error(TAG, "An error occurred while watching for errors. Shutting down!", ex);
            messageServer.shutdown(-1, false);
        }
    }
}
 
Example 12
private PollingWatchKey doPrivilegedRegister(Path path,
                                             Set<? extends WatchEvent.Kind<?>> events,
                                             SensitivityWatchEventModifier sensivity)
    throws IOException
{
    // check file is a directory and get its file key if possible
    BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
    if (!attrs.isDirectory()) {
        throw new NotDirectoryException(path.toString());
    }
    Object fileKey = attrs.fileKey();
    if (fileKey == null)
        throw new AssertionError("File keys must be supported");

    // grab close lock to ensure that watch service cannot be closed
    synchronized (closeLock()) {
        if (!isOpen())
            throw new ClosedWatchServiceException();

        PollingWatchKey watchKey;
        synchronized (map) {
            watchKey = map.get(fileKey);
            if (watchKey == null) {
                // new registration
                watchKey = new PollingWatchKey(path, this, fileKey);
                map.put(fileKey, watchKey);
            } else {
                // update to existing registration
                watchKey.disable();
            }
        }
        watchKey.enable(events, sensivity.sensitivityValueInSeconds());
        return watchKey;
    }

}
 
Example 13
static void register(Path[] dirs, WatchService watcher) throws IOException {
    SensitivityWatchEventModifier[] sensitivtives =
        SensitivityWatchEventModifier.values();
    for (int i=0; i<dirs.length; i++) {
        SensitivityWatchEventModifier sensivity =
            sensitivtives[ rand.nextInt(sensitivtives.length) ];
        Path dir = dirs[i];
        dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY }, sensivity);
    }
}
 
Example 14
private PollingWatchKey doPrivilegedRegister(Path path,
                                             Set<? extends WatchEvent.Kind<?>> events,
                                             SensitivityWatchEventModifier sensivity)
    throws IOException
{
    // check file is a directory and get its file key if possible
    BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
    if (!attrs.isDirectory()) {
        throw new NotDirectoryException(path.toString());
    }
    Object fileKey = attrs.fileKey();
    if (fileKey == null)
        throw new AssertionError("File keys must be supported");

    // grab close lock to ensure that watch service cannot be closed
    synchronized (closeLock()) {
        if (!isOpen())
            throw new ClosedWatchServiceException();

        PollingWatchKey watchKey;
        synchronized (map) {
            watchKey = map.get(fileKey);
            if (watchKey == null) {
                // new registration
                watchKey = new PollingWatchKey(path, this, fileKey);
                map.put(fileKey, watchKey);
            } else {
                // update to existing registration
                watchKey.disable();
            }
        }
        watchKey.enable(events, sensivity.sensitivityValueInSeconds());
        return watchKey;
    }

}
 
Example 15
static void register(Path[] dirs, WatchService watcher) throws IOException {
    SensitivityWatchEventModifier[] sensitivtives =
        SensitivityWatchEventModifier.values();
    for (int i=0; i<dirs.length; i++) {
        SensitivityWatchEventModifier sensivity =
            sensitivtives[ rand.nextInt(sensitivtives.length) ];
        Path dir = dirs[i];
        dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY }, sensivity);
    }
}
 
Example 16
static void register(Path[] dirs, WatchService watcher) throws IOException {
    SensitivityWatchEventModifier[] sensitivtives =
        SensitivityWatchEventModifier.values();
    for (int i=0; i<dirs.length; i++) {
        SensitivityWatchEventModifier sensivity =
            sensitivtives[ rand.nextInt(sensitivtives.length) ];
        Path dir = dirs[i];
        dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY }, sensivity);
    }
}
 
Example 17
private PollingWatchKey doPrivilegedRegister(Path path,
                                             Set<? extends WatchEvent.Kind<?>> events,
                                             SensitivityWatchEventModifier sensivity)
    throws IOException
{
    // check file is a directory and get its file key if possible
    BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
    if (!attrs.isDirectory()) {
        throw new NotDirectoryException(path.toString());
    }
    Object fileKey = attrs.fileKey();
    if (fileKey == null)
        throw new AssertionError("File keys must be supported");

    // grab close lock to ensure that watch service cannot be closed
    synchronized (closeLock()) {
        if (!isOpen())
            throw new ClosedWatchServiceException();

        PollingWatchKey watchKey;
        synchronized (map) {
            watchKey = map.get(fileKey);
            if (watchKey == null) {
                // new registration
                watchKey = new PollingWatchKey(path, this, fileKey);
                map.put(fileKey, watchKey);
            } else {
                // update to existing registration
                watchKey.disable();
            }
        }
        watchKey.enable(events, sensivity.sensitivityValueInSeconds());
        return watchKey;
    }

}
 
Example 18
static void register(Path[] dirs, WatchService watcher) throws IOException {
    SensitivityWatchEventModifier[] sensitivtives =
        SensitivityWatchEventModifier.values();
    for (int i=0; i<dirs.length; i++) {
        SensitivityWatchEventModifier sensivity =
            sensitivtives[ rand.nextInt(sensitivtives.length) ];
        Path dir = dirs[i];
        dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY }, sensivity);
    }
}
 
Example 19
Source Project: hottub   Source File: PollingWatchService.java    License: GNU General Public License v2.0 5 votes vote down vote up
private PollingWatchKey doPrivilegedRegister(Path path,
                                             Set<? extends WatchEvent.Kind<?>> events,
                                             SensitivityWatchEventModifier sensivity)
    throws IOException
{
    // check file is a directory and get its file key if possible
    BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
    if (!attrs.isDirectory()) {
        throw new NotDirectoryException(path.toString());
    }
    Object fileKey = attrs.fileKey();
    if (fileKey == null)
        throw new AssertionError("File keys must be supported");

    // grab close lock to ensure that watch service cannot be closed
    synchronized (closeLock()) {
        if (!isOpen())
            throw new ClosedWatchServiceException();

        PollingWatchKey watchKey;
        synchronized (map) {
            watchKey = map.get(fileKey);
            if (watchKey == null) {
                // new registration
                watchKey = new PollingWatchKey(path, this, fileKey);
                map.put(fileKey, watchKey);
            } else {
                // update to existing registration
                watchKey.disable();
            }
        }
        watchKey.enable(events, sensivity.sensitivityValueInSeconds());
        return watchKey;
    }

}
 
Example 20
Source Project: hottub   Source File: SensitivityModifier.java    License: GNU General Public License v2.0 5 votes vote down vote up
static void register(Path[] dirs, WatchService watcher) throws IOException {
    SensitivityWatchEventModifier[] sensitivtives =
        SensitivityWatchEventModifier.values();
    for (int i=0; i<dirs.length; i++) {
        SensitivityWatchEventModifier sensivity =
            sensitivtives[ rand.nextInt(sensitivtives.length) ];
        Path dir = dirs[i];
        dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY }, sensivity);
    }
}
 
Example 21
private PollingWatchKey doPrivilegedRegister(Path path,
                                             Set<? extends WatchEvent.Kind<?>> events,
                                             SensitivityWatchEventModifier sensivity)
    throws IOException
{
    // check file is a directory and get its file key if possible
    BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
    if (!attrs.isDirectory()) {
        throw new NotDirectoryException(path.toString());
    }
    Object fileKey = attrs.fileKey();
    if (fileKey == null)
        throw new AssertionError("File keys must be supported");

    // grab close lock to ensure that watch service cannot be closed
    synchronized (closeLock()) {
        if (!isOpen())
            throw new ClosedWatchServiceException();

        PollingWatchKey watchKey;
        synchronized (map) {
            watchKey = map.get(fileKey);
            if (watchKey == null) {
                // new registration
                watchKey = new PollingWatchKey(path, this, fileKey);
                map.put(fileKey, watchKey);
            } else {
                // update to existing registration
                watchKey.disable();
            }
        }
        watchKey.enable(events, sensivity.sensitivityValueInSeconds());
        return watchKey;
    }

}
 
Example 22
static void register(Path[] dirs, WatchService watcher) throws IOException {
    SensitivityWatchEventModifier[] sensitivtives =
        SensitivityWatchEventModifier.values();
    for (int i=0; i<dirs.length; i++) {
        SensitivityWatchEventModifier sensivity =
            sensitivtives[ rand.nextInt(sensitivtives.length) ];
        Path dir = dirs[i];
        dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY }, sensivity);
    }
}
 
Example 23
Source Project: rubix   Source File: CacheWatcher.java    License: Apache License 2.0 5 votes vote down vote up
private void register(Path cacheDir) throws IOException
{
  WatchKey cacheDirKey = cacheDir.register(
      watcher,
      new WatchEvent.Kind[]{ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE, OVERFLOW},
      SensitivityWatchEventModifier.HIGH);

  watchKeys.put(cacheDirKey, cacheDir);
  log.debug("* Registered " + cacheDir);
}
 
Example 24
private PollingWatchKey doPrivilegedRegister(Path path,
                                             Set<? extends WatchEvent.Kind<?>> events,
                                             SensitivityWatchEventModifier sensivity)
    throws IOException
{
    // check file is a directory and get its file key if possible
    BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
    if (!attrs.isDirectory()) {
        throw new NotDirectoryException(path.toString());
    }
    Object fileKey = attrs.fileKey();
    if (fileKey == null)
        throw new AssertionError("File keys must be supported");

    // grab close lock to ensure that watch service cannot be closed
    synchronized (closeLock()) {
        if (!isOpen())
            throw new ClosedWatchServiceException();

        PollingWatchKey watchKey;
        synchronized (map) {
            watchKey = map.get(fileKey);
            if (watchKey == null) {
                // new registration
                watchKey = new PollingWatchKey(path, this, fileKey);
                map.put(fileKey, watchKey);
            } else {
                // update to existing registration
                watchKey.disable();
            }
        }
        watchKey.enable(events, sensivity.sensitivityValueInSeconds());
        return watchKey;
    }

}
 
Example 25
static void register(Path[] dirs, WatchService watcher) throws IOException {
    SensitivityWatchEventModifier[] sensitivtives =
        SensitivityWatchEventModifier.values();
    for (int i=0; i<dirs.length; i++) {
        SensitivityWatchEventModifier sensivity =
            sensitivtives[ rand.nextInt(sensitivtives.length) ];
        Path dir = dirs[i];
        dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY }, sensivity);
    }
}
 
Example 26
private PollingWatchKey doPrivilegedRegister(Path path,
                                             Set<? extends WatchEvent.Kind<?>> events,
                                             SensitivityWatchEventModifier sensivity)
    throws IOException
{
    // check file is a directory and get its file key if possible
    BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
    if (!attrs.isDirectory()) {
        throw new NotDirectoryException(path.toString());
    }
    Object fileKey = attrs.fileKey();
    if (fileKey == null)
        throw new AssertionError("File keys must be supported");

    // grab close lock to ensure that watch service cannot be closed
    synchronized (closeLock()) {
        if (!isOpen())
            throw new ClosedWatchServiceException();

        PollingWatchKey watchKey;
        synchronized (map) {
            watchKey = map.get(fileKey);
            if (watchKey == null) {
                // new registration
                watchKey = new PollingWatchKey(path, this, fileKey);
                map.put(fileKey, watchKey);
            } else {
                // update to existing registration
                watchKey.disable();
            }
        }
        watchKey.enable(events, sensivity.sensitivityValueInSeconds());
        return watchKey;
    }

}
 
Example 27
static void register(Path[] dirs, WatchService watcher) throws IOException {
    SensitivityWatchEventModifier[] sensitivtives =
        SensitivityWatchEventModifier.values();
    for (int i=0; i<dirs.length; i++) {
        SensitivityWatchEventModifier sensivity =
            sensitivtives[ rand.nextInt(sensitivtives.length) ];
        Path dir = dirs[i];
        dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY }, sensivity);
    }
}
 
Example 28
/**
 * Constructor for DirectoryChangeProcessor
 * @param datastreamTask The datastream task this processor is responsible for
 * @param producer The event producer this connector uses to send change events
 *                 to the underlying {@link com.linkedin.datastream.server.api.transport.TransportProvider}.
 * @throws IOException if an I/O error occurs
 */
public DirectoryChangeProcessor(DatastreamTask datastreamTask, DatastreamEventProducer producer) throws IOException {
  Validate.notNull(datastreamTask);
  Validate.notNull(producer);

  final String path = datastreamTask.getDatastreamSource().getConnectionString();
  Validate.isTrue(isDirectory(path), "path does not refer to a valid directory");

  _task = datastreamTask;
  _producer = producer;
  _dirPath = Paths.get(path);
  _watchService = FileSystems.getDefault().newWatchService();
  _watchKey = _dirPath.register(_watchService, new WatchEvent.Kind<?>[] {ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE},
      SensitivityWatchEventModifier.HIGH);
}
 
Example 29
private PollingWatchKey doPrivilegedRegister(Path path,
                                             Set<? extends WatchEvent.Kind<?>> events,
                                             SensitivityWatchEventModifier sensivity)
    throws IOException
{
    // check file is a directory and get its file key if possible
    BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
    if (!attrs.isDirectory()) {
        throw new NotDirectoryException(path.toString());
    }
    Object fileKey = attrs.fileKey();
    if (fileKey == null)
        throw new AssertionError("File keys must be supported");

    // grab close lock to ensure that watch service cannot be closed
    synchronized (closeLock()) {
        if (!isOpen())
            throw new ClosedWatchServiceException();

        PollingWatchKey watchKey;
        synchronized (map) {
            watchKey = map.get(fileKey);
            if (watchKey == null) {
                // new registration
                watchKey = new PollingWatchKey(path, this, fileKey);
                map.put(fileKey, watchKey);
            } else {
                // update to existing registration
                watchKey.disable();
            }
        }
        watchKey.enable(events, sensivity.sensitivityValueInSeconds());
        return watchKey;
    }

}
 
Example 30
static void register(Path[] dirs, WatchService watcher) throws IOException {
    SensitivityWatchEventModifier[] sensitivtives =
        SensitivityWatchEventModifier.values();
    for (int i=0; i<dirs.length; i++) {
        SensitivityWatchEventModifier sensivity =
            sensitivtives[ rand.nextInt(sensitivtives.length) ];
        Path dir = dirs[i];
        dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY }, sensivity);
    }
}