src/share/sample/nio/file/WatchDir.java
Print this page
*** 31,53 ****
import java.nio.file.*;
import static java.nio.file.StandardWatchEventKind.*;
import static java.nio.file.LinkOption.*;
import java.nio.file.attribute.*;
! import java.io.*;
! import java.util.*;
/**
* Example to watch a directory (or tree) for changes to files.
*/
public class WatchDir {
private final WatchService watcher;
- private final Map<WatchKey,Path> keys;
private final boolean recursive;
private boolean trace = false;
@SuppressWarnings("unchecked")
static <T> WatchEvent<T> cast(WatchEvent<?> event) {
return (WatchEvent<T>)event;
}
--- 31,52 ----
import java.nio.file.*;
import static java.nio.file.StandardWatchEventKind.*;
import static java.nio.file.LinkOption.*;
import java.nio.file.attribute.*;
! import java.io.IOException;
/**
* Example to watch a directory (or tree) for changes to files.
*/
public class WatchDir {
private final WatchService watcher;
private final boolean recursive;
private boolean trace = false;
+ private int count;
@SuppressWarnings("unchecked")
static <T> WatchEvent<T> cast(WatchEvent<?> event) {
return (WatchEvent<T>)event;
}
*** 55,76 ****
/**
* Register the given directory with the WatchService
*/
private void register(Path dir) throws IOException {
WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
! if (trace) {
! Path prev = keys.get(key);
! if (prev == null) {
System.out.format("register: %s\n", dir);
- } else {
- if (!dir.equals(prev)) {
- System.out.format("update: %s -> %s\n", prev, dir);
}
- }
- }
- keys.put(key, dir);
- }
/**
* Register the given directory, and all its sub-directories, with the
* WatchService.
*/
--- 54,67 ----
/**
* Register the given directory with the WatchService
*/
private void register(Path dir) throws IOException {
WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
! count++;
! if (trace)
System.out.format("register: %s\n", dir);
}
/**
* Register the given directory, and all its sub-directories, with the
* WatchService.
*/
*** 90,100 ****
/**
* Creates a WatchService and registers the given directory
*/
WatchDir(Path dir, boolean recursive) throws IOException {
this.watcher = FileSystems.getDefault().newWatchService();
- this.keys = new HashMap<WatchKey,Path>();
this.recursive = recursive;
if (recursive) {
System.out.format("Scanning %s ...\n", dir);
registerAll(dir);
--- 81,90 ----
*** 119,134 ****
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();
// TBD - provide example of how OVERFLOW event is handled
if (kind == OVERFLOW) {
--- 109,118 ----
*** 136,146 ****
}
// Context for directory entry event is the file name of entry
WatchEvent<Path> ev = cast(event);
Path name = ev.context();
! Path child = dir.resolve(name);
// print out event
System.out.format("%s: %s\n", event.kind().name(), child);
// if directory is created, and watching recursively, then
--- 120,130 ----
}
// Context for directory entry event is the file name of entry
WatchEvent<Path> ev = cast(event);
Path name = ev.context();
! Path child = ((Path)key.watchable()).resolve(name);
// print out event
System.out.format("%s: %s\n", event.kind().name(), child);
// if directory is created, and watching recursively, then
*** 154,174 ****
// ignore to keep sample readbale
}
}
}
! // reset key and remove from set if directory no longer accessible
boolean valid = key.reset();
if (!valid) {
! keys.remove(key);
!
! // all directories are inaccessible
! if (keys.isEmpty()) {
break;
}
}
- }
}
static void usage() {
System.err.println("usage: java WatchDir [-r] dir");
System.exit(-1);
--- 138,156 ----
// ignore to keep sample readbale
}
}
}
! // reset key
boolean valid = key.reset();
if (!valid) {
! // directory no longer accessible
! count--;
! if (count == 0)
break;
}
}
}
static void usage() {
System.err.println("usage: java WatchDir [-r] dir");
System.exit(-1);