src/share/sample/nio/file/WatchDir.java

Print this page

        

@@ -31,23 +31,22 @@
 
 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.*;
+import java.io.IOException;
 
 /**
  * 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;
+    private int count;
 
     @SuppressWarnings("unchecked")
     static <T> WatchEvent<T> cast(WatchEvent<?> event) {
         return (WatchEvent<T>)event;
     }

@@ -55,22 +54,14 @@
     /**
      * 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) {
+        count++;
+        if (trace)
                 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.
      */

@@ -90,11 +81,10 @@
     /**
      * 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);

@@ -119,16 +109,10 @@
                 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) {

@@ -136,11 +120,11 @@
                 }
 
                 // 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);
+                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,21 +138,19 @@
                         // ignore to keep sample readbale
                     }
                 }
             }
 
-            // reset key and remove from set if directory no longer accessible
+            // reset key 
             boolean valid = key.reset();
             if (!valid) {
-                keys.remove(key);
-
-                // all directories are inaccessible
-                if (keys.isEmpty()) {
+                // directory no longer accessible
+                count--;
+                if (count == 0)
                     break;
                 }
             }
-        }
     }
 
     static void usage() {
         System.err.println("usage: java WatchDir [-r] dir");
         System.exit(-1);