Package gw.fs.watcher

Class DirectoryWatcher


  • public class DirectoryWatcher
    extends java.lang.Object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Map<java.nio.file.WatchKey,​java.nio.file.Path> _watchedDirectories  
      private java.nio.file.WatchService _watchService  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.Map<java.nio.file.Path,​DirectoryWatcher.FileEvent> getChangesSinceLastTime()
      Checks all the watched directories (that are being kept in a map) for changes since the last call to this method.
      void stopWatching()
      Close the watch service.
      private void watchDirectory​(java.nio.file.Path dir)  
      void watchDirectoryTree​(java.nio.file.Path dir)
      Walk the directories under given path, and register a watcher for every directory.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • _watchService

        private java.nio.file.WatchService _watchService
      • _watchedDirectories

        private java.util.Map<java.nio.file.WatchKey,​java.nio.file.Path> _watchedDirectories
    • Constructor Detail

      • DirectoryWatcher

        public DirectoryWatcher()
    • Method Detail

      • stopWatching

        public void stopWatching()
        Close the watch service. Releases resources. After calling, this instance becomes invalid and can't be used any more.
      • watchDirectoryTree

        public void watchDirectoryTree​(java.nio.file.Path dir)
        Walk the directories under given path, and register a watcher for every directory.
        Parameters:
        dir - the starting point under which to listen for changes, if it doesn't exist, do nothing.
      • watchDirectory

        private void watchDirectory​(java.nio.file.Path dir)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • getChangesSinceLastTime

        public java.util.Map<java.nio.file.Path,​DirectoryWatcher.FileEvent> getChangesSinceLastTime()
        Checks all the watched directories (that are being kept in a map) for changes since the last call to this method.
      • If a file is already marked as CREATE, leave it as CREATE; if it's marked as DELETE, that's totally invalid, so just ignore that. If it's marked as MODIFY, no need to change it. So only put something in the map if it's not already in there.
      • If a create follows a delete, it means the editor deleted and re-created the file, and who knows what the contents are now, so it should be treated as a modification.
      • If the file was created, then deleted, then just ignore it entirely and remove the event.
Returns:
FileEvent enum for each watched Path found, indicating the type of modification that happened.