use of java.nio.file.WatchKey in project jdk8u_jdk by JetBrains.
the class LotsOfCancels method poll.
/**
* Polls the given WatchService in a tight loop. This keeps the event
* queue drained, it also hogs a CPU core which seems necessary to
* tickle the original bug.
*/
static void poll(WatchService watcher) {
try {
for (; ; ) {
WatchKey key = watcher.take();
if (key != null) {
key.pollEvents();
key.reset();
}
}
} catch (ClosedWatchServiceException expected) {
// nothing to do
} catch (Exception e) {
e.printStackTrace();
failed = true;
}
}
use of java.nio.file.WatchKey in project jdk8u_jdk by JetBrains.
the class LotsOfCancels method handle.
/**
* Stress the given WatchService, specifically the cancel method, in
* the given directory. Closes the WatchService when done.
*/
static void handle(Path dir, WatchService watcher) {
try {
try {
Path file = dir.resolve("anyfile");
for (int i = 0; i < 2000; i++) {
WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE);
Files.createFile(file);
Files.delete(file);
key.cancel();
}
} finally {
watcher.close();
}
} catch (Exception e) {
e.printStackTrace();
failed = true;
}
}
use of java.nio.file.WatchKey in project karaf by apache.
the class AutoEncryptionSupport method run.
@Override
public void run() {
WatchService watchService = null;
try {
watchService = FileSystems.getDefault().newWatchService();
Path dir = Paths.get(System.getProperty("karaf.etc"));
dir.register(watchService, ENTRY_MODIFY);
Path file = dir.resolve("users.properties");
encryptedPassword(new Properties(file.toFile()));
while (running) {
try {
WatchKey key = watchService.poll(1, TimeUnit.SECONDS);
if (key == null) {
continue;
}
for (WatchEvent<?> event : key.pollEvents()) {
@SuppressWarnings("unchecked") WatchEvent<Path> ev = (WatchEvent<Path>) event;
// Context for directory entry event is the file name of entry
Path name = dir.resolve(ev.context());
if (file.equals(name)) {
encryptedPassword(new Properties(file.toFile()));
}
}
key.reset();
} catch (IOException e) {
LOGGER.warn(e.getMessage(), e);
} catch (InterruptedException e) {
// Ignore as this happens on shutdown
}
}
} catch (IOException e) {
LOGGER.warn(e.getMessage(), e);
} finally {
StreamUtils.close(watchService);
}
}
use of java.nio.file.WatchKey in project asterixdb by apache.
the class FileSystemWatcher method poll.
// poll is not blocking
public synchronized File poll() throws IOException {
if (it.hasNext()) {
return it.next();
}
if (done || !isFeed) {
return null;
}
files.clear();
it = files.iterator();
if (keys.isEmpty()) {
close();
return null;
}
// Read new Events (Polling first to add all available files)
WatchKey key;
key = watcher.poll();
while (key != null) {
handleEvents(key);
if (endOfEvents(key)) {
close();
return null;
}
key = watcher.poll();
}
return null;
}
use of java.nio.file.WatchKey in project JMRI by JMRI.
the class WebAppManager method lifeCycleStarted.
private void lifeCycleStarted(LifeCycle lc, Profile profile) {
// register watcher to watch web/app directories everywhere
if (this.watcher.get(profile) != null) {
FileUtil.findFiles("web", ".").stream().filter((file) -> (file.isDirectory())).forEachOrdered((file) -> {
try {
Path path = file.toPath();
WebAppManager.this.watchPaths.put(path.register(this.watcher.get(profile), StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY), path);
} catch (IOException ex) {
log.error("Unable to watch {} for changes.", file);
}
(new Thread() {
@Override
public void run() {
while (WebAppManager.this.watcher.get(profile) != null) {
WatchKey key;
try {
key = WebAppManager.this.watcher.get(profile).take();
} catch (InterruptedException ex) {
return;
}
key.pollEvents().stream().filter((event) -> (event.kind() != OVERFLOW)).forEachOrdered((event) -> {
WebAppManager.this.savePreferences(profile);
});
if (!key.reset()) {
WebAppManager.this.watcher.remove(profile);
}
}
}
}).start();
});
}
}
Aggregations