com.sun.nio.file.SensitivityWatchEventModifier Java Examples

The following examples show how to use com.sun.nio.file.SensitivityWatchEventModifier. 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 File: PathsWatcher.java    From cssfx with 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 #2
Source File: JavaWatchObserver.java    From Tor_Onion_Proxy_Library with 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 File: JavaWatchObserver.java    From T0rlib4j with 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 #4
Source File: PollingWatchService.java    From openjdk-8-source with 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 #5
Source File: PollingWatchService.java    From hottub with 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 #6
Source File: SensitivityModifier.java    From openjdk-8-source with 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 #7
Source File: CacheWatcher.java    From rubix with 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 #8
Source File: PollingWatchService.java    From openjdk-8 with 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 #9
Source File: SensitivityModifier.java    From openjdk-8 with 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 #10
Source File: PollingWatchService.java    From jdk8u_jdk with 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 #11
Source File: SensitivityModifier.java    From jdk8u_jdk with 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 #12
Source File: DirectoryChangeProcessor.java    From brooklin with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * 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 #13
Source File: PollingWatchService.java    From jdk8u-jdk with 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 #14
Source File: SensitivityModifier.java    From jdk8u-jdk with 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 #15
Source File: PollingWatchService.java    From jdk8u-dev-jdk with 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 #16
Source File: SensitivityModifier.java    From jdk8u-dev-jdk with 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 #17
Source File: FileBasedOneShotLatch.java    From flink with 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 #18
Source File: SimpleFileWatchScanner.java    From jesterj with Apache License 2.0 5 votes vote down vote up
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
  try {
    //todo investigate BarbaryWatchService for osx... even with the sun package modifier this takes 2 seconds :(
    dir.register(watchers.get(dir.toFile()), new WatchEvent.Kind[]{ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY}, SensitivityWatchEventModifier.HIGH);
  } catch (IOException e) {
    log.error("Failed to register watcher for:" + dir, e);
  }
  return FileVisitResult.CONTINUE;
}
 
Example #19
Source File: PollingWatchService.java    From openjdk-jdk8u with 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 File: FileBasedOneShotLatch.java    From flink with 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 #21
Source File: PollingWatchService.java    From dragonwell8_jdk with 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 #22
Source File: SensitivityModifier.java    From dragonwell8_jdk with 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 #23
Source File: PollingWatchService.java    From TencentKona-8 with 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 #24
Source File: SensitivityModifier.java    From TencentKona-8 with 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 #25
Source File: PollingWatchService.java    From jdk8u60 with 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 #26
Source File: SensitivityModifier.java    From jdk8u60 with 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 #27
Source File: ErrorWatcher.java    From weMessage with GNU Affero General Public License v3.0 5 votes vote down vote up
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 #28
Source File: SensitivityModifier.java    From hottub with 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 #29
Source File: SensitivityModifier.java    From openjdk-jdk8u with 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 #30
Source File: PollingWatchService.java    From openjdk-jdk8u-backup with 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;
    }

}