src/share/classes/sun/nio/fs/PollingWatchService.java

Print this page

        

@@ -144,11 +144,11 @@
                                                  Set<? extends WatchEvent.Kind<?>> events,
                                                  SensitivityWatchEventModifier sensivity)
         throws IOException
     {
         // check file is a directory and get its file key if possible
-        BasicFileAttributes attrs = Attributes.readBasicFileAttributes(path);
+        BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
         if (!attrs.isDirectory()) {
             throw new NotDirectoryException(path.toString());
         }
         Object fileKey = attrs.fileKey();
         if (fileKey == null)

@@ -162,11 +162,11 @@
             PollingWatchKey watchKey;
             synchronized (map) {
                 watchKey = map.get(fileKey);
                 if (watchKey == null) {
                     // new registration
-                    watchKey = new PollingWatchKey(this, path, fileKey);
+                    watchKey = new PollingWatchKey(path, this, fileKey);
                     map.put(fileKey, watchKey);
                 } else {
                     // update to existing registration
                     watchKey.disable();
                 }

@@ -226,11 +226,10 @@
      * WatchKey implementation that encapsulates a map of the entries of the
      * entries in the directory. Polling the key causes it to re-scan the
      * directory and queue keys when entries are added, modified, or deleted.
      */
     private class PollingWatchKey extends AbstractWatchKey {
-        private final Path dir;
         private final Object fileKey;
 
         // current event set
         private Set<? extends WatchEvent.Kind<?>> events;
 

@@ -244,48 +243,32 @@
         private int tickCount;
 
         // map of entries in directory
         private Map<Path,CacheEntry> entries;
 
-        PollingWatchKey(PollingWatchService watcher,
-                        Path dir,
-                        Object fileKey)
+        PollingWatchKey(Path dir, PollingWatchService watcher, Object fileKey)
             throws IOException
         {
-            super(watcher);
-            this.dir = dir;
+            super(dir, watcher);
             this.fileKey = fileKey;
             this.valid = true;
             this.tickCount = 0;
             this.entries = new HashMap<Path,CacheEntry>();
 
             // get the initial entries in the directory
-            DirectoryStream<Path> stream = dir.newDirectoryStream();
-            try {
+            try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
                 for (Path entry: stream) {
                     // don't follow links
-                    long lastModified = Attributes
-                        .readBasicFileAttributes(entry, LinkOption.NOFOLLOW_LINKS)
-                        .lastModifiedTime().toMillis();
-                    entries.put(entry.getName(),
-                                new CacheEntry(lastModified, tickCount));
+                    long lastModified =
+                        Files.getLastModifiedTime(entry, LinkOption.NOFOLLOW_LINKS).toMillis();
+                    entries.put(entry.getFileName(), new CacheEntry(lastModified, tickCount));
                 }
-            } catch (ConcurrentModificationException cme) {
-                // thrown if directory iteration fails
-                Throwable cause = cme.getCause();
-                if (cause != null && cause instanceof IOException)
-                    throw (IOException)cause;
-                throw new AssertionError(cme);
-            } finally {
-                stream.close();
+            } catch (DirectoryIteratorException e) {
+                throw e.getCause();
             }
         }
 
-        FileRef directory() {
-            return dir;
-        }
-
         Object fileKey() {
             return fileKey;
         }
 
         @Override

@@ -340,11 +323,11 @@
             tickCount++;
 
             // open directory
             DirectoryStream<Path> stream = null;
             try {
-                stream = dir.newDirectoryStream();
+                stream = Files.newDirectoryStream(watchable());
             } catch (IOException x) {
                 // directory is no longer accessible so cancel key
                 cancel();
                 signal();
                 return;

@@ -353,55 +336,56 @@
             // iterate over all entries in directory
             try {
                 for (Path entry: stream) {
                     long lastModified = 0L;
                     try {
-                        lastModified = Attributes
-                            .readBasicFileAttributes(entry, LinkOption.NOFOLLOW_LINKS)
-                            .lastModifiedTime().toMillis();
+                        lastModified =
+                            Files.getLastModifiedTime(entry, LinkOption.NOFOLLOW_LINKS).toMillis();
                     } catch (IOException x) {
                         // unable to get attributes of entry. If file has just
                         // been deleted then we'll report it as deleted on the
                         // next poll
                         continue;
                     }
 
                     // lookup cache
-                    CacheEntry e = entries.get(entry.getName());
+                    CacheEntry e = entries.get(entry.getFileName());
                     if (e == null) {
                         // new file found
-                        entries.put(entry.getName(),
+                        entries.put(entry.getFileName(),
                                      new CacheEntry(lastModified, tickCount));
 
                         // queue ENTRY_CREATE if event enabled
                         if (events.contains(StandardWatchEventKind.ENTRY_CREATE)) {
-                            signalEvent(StandardWatchEventKind.ENTRY_CREATE, entry.getName());
+                            signalEvent(StandardWatchEventKind.ENTRY_CREATE, entry.getFileName());
                             continue;
                         } else {
                             // if ENTRY_CREATE is not enabled and ENTRY_MODIFY is
                             // enabled then queue event to avoid missing out on
                             // modifications to the file immediately after it is
                             // created.
                             if (events.contains(StandardWatchEventKind.ENTRY_MODIFY)) {
-                                signalEvent(StandardWatchEventKind.ENTRY_MODIFY, entry.getName());
+                                signalEvent(StandardWatchEventKind.ENTRY_MODIFY, entry.getFileName());
                             }
                         }
                         continue;
                     }
 
                     // check if file has changed
                     if (e.lastModified != lastModified) {
                         if (events.contains(StandardWatchEventKind.ENTRY_MODIFY)) {
-                            signalEvent(StandardWatchEventKind.ENTRY_MODIFY, entry.getName());
+                            signalEvent(StandardWatchEventKind.ENTRY_MODIFY, 
+                                        entry.getFileName());
                         }
                     }
                     // entry in cache so update poll time
                     e.update(lastModified, tickCount);
 
                 }
-            } catch (ConcurrentModificationException x) {
-                // FIXME - should handle this
+            } catch (DirectoryIteratorException e) {
+                // ignore for now; if the directory is no longer accessible
+                // then the key will be cancelled on the next poll
             } finally {
 
                 // close directory stream
                 try {
                     stream.close();