Class SimpleDirectoryWatchService

java.lang.Object
edu.wisc.ssec.mcidasv.util.pathwatcher.SimpleDirectoryWatchService
All Implemented Interfaces:
DirectoryWatchService, Service, Runnable

A simple class which can monitor files and notify interested parties (i.e. listeners) of file changes. This class is kept lean by only keeping methods that are actually being called.
  • Field Details

  • Constructor Details

  • Method Details

    • cast

      private static <T> WatchEvent<T> cast(WatchEvent<?> event)
      Utility method used to make "valid" casts of the given event to a specific type of WatchEvent.
      Type Parameters:
      T - Type to which event will be casted.
      Parameters:
      event - Event to cast.
      Returns:
      event casted to WatchEvent<T>.
    • matcherForGlobExpression

      private static PathMatcher matcherForGlobExpression(String globPattern) throws IOException
      Returns a PathMatcher that performs "glob" matches with the given globPattern against the String representation of Path objects.
      Parameters:
      globPattern - Pattern to match against. null or empty String values will be converted to *.
      Returns:
      Path matching object for the given globPattern.
      Throws:
      IOException - if there was a problem creating the PathMatcher.
    • matches

      public static boolean matches(Path input, PathMatcher pattern)
      Check the given input Path against the given pattern.
      Parameters:
      input - Path to check.
      pattern - Pattern to check against. Cannot be null.
      Returns:
      Whether or not input matches pattern.
    • matchesAny

      private static boolean matchesAny(Path input, Set<PathMatcher> patterns)
      Check the given input Path against all of the specified patterns.
      Parameters:
      input - Path to check.
      patterns - Set of patterns to attempt to match input against. Cannot be null.
      Returns:
      Whether or not input matches any of the given patterns.
    • getDirPath

      private Path getDirPath(WatchKey key)
      Get the path associated with the given WatchKey.
      Parameters:
      key - WatchKey whose corresponding Path is being requested.
      Returns:
      Either the correspond Path or null.
    • getListeners

      Get the OnFileChangeListeners associated with the given path.
      Parameters:
      path - Path whose listeners should be returned. Cannot be null.
      Returns:
      Either the Set of listeners associated with path or null.
    • getPatterns

      Get the Set of patterns associated with the given OnFileChangeListener.
      Parameters:
      listener - Listener of interest.
      Returns:
      Either the Set of patterns associated with listener or null.
    • getDir

      private Path getDir(OnFileChangeListener listener)
      Get the Path associated with the given OnFileChangeListener.
      Parameters:
      listener - Listener whose path is requested.
      Returns:
      Either the Path associated with listener or null.
    • getWatchKey

      private WatchKey getWatchKey(Path dir)
      Get the monitoring "registration" key associated with the given Path.
      Parameters:
      dir - Path whose WatchKey is requested.
      Returns:
      Either the WatchKey corresponding to dir or null.
    • matchedListeners

      Get the Set of OnFileChangeListeners that should be notified that file has changed.
      Parameters:
      dir - Directory containing file.
      file - File that changed.
      Returns:
      Set of listeners that should be notified that file has changed.
    • notifyListeners

      private void notifyListeners(WatchKey key)
      Method responsible for notifying listeners when a file matching their relevant pattern has changed. Note: "change" means one of:
      • file creation
      • file removal
      • file contents changing
      Parameters:
      key - mWatchService "registration" key for one of the Paths being watched. Cannot be null.
      See Also:
    • notifyListenersOfInvalidation

      Method responsible for notifying listeners when the path they are watching has been deleted (or otherwise "invalidated" somehow).
      Parameters:
      key - Key that has become invalid. Cannot be null.
    • register

      public void register(OnFileChangeListener listener, String dirPath, String... globPatterns) throws IOException
      Notifies the implementation of this interface that dirPath should be monitored for file system events. If the changed file matches any of the globPatterns, listener should be notified.
      Specified by:
      register in interface DirectoryWatchService
      Parameters:
      listener - The listener.
      dirPath - The directory path.
      globPatterns - Zero or more file patterns to be matched against file names. If none provided, matches any file.
      Throws:
      IOException - If dirPath is not a directory.
    • unregister

      public void unregister(OnFileChangeListener listener)
      Notifies the implementation of this interface that listener should cease to be notified of file system events.
      Specified by:
      unregister in interface DirectoryWatchService
      Parameters:
      listener - Listener to unregister. Cannot be null.
    • unregisterAll

      public void unregisterAll()
      Unregisters all listeners.
      Specified by:
      unregisterAll in interface DirectoryWatchService
    • start

      public void start()
      Start this SimpleDirectoryWatchService instance by spawning a new thread.
      Specified by:
      start in interface DirectoryWatchService
      Specified by:
      start in interface Service
      See Also:
    • stop

      public void stop()
      Stop this SimpleDirectoryWatchService thread.

      The killing happens lazily, giving the running thread an opportunity to finish the work at hand.

      Specified by:
      stop in interface Service
      See Also:
    • isRunning

      public boolean isRunning()
      Checks to see if the service is still running.
      Specified by:
      isRunning in interface Service
      Returns:
      true if the service is running, false otherwise.
    • run

      public void run()
      Specified by:
      run in interface Runnable