Class Scanner

All Implemented Interfaces:
Container, Destroyable, Dumpable, Dumpable.DumpableContainer, LifeCycle

public class Scanner extends ContainerLifeCycle
Scanner Utility for scanning a directory for added, removed and changed files and reporting these events via registered Listeners. The scanner operates on the Path.toRealPath(LinkOption...) of the files scanned and can be configured to follow symlinks.
  • Field Details


      public static final int DEFAULT_SCAN_DEPTH
      When walking a directory, a depth of 1 ensures that the directory's descendants are visited, not just the directory itself (as a file).
      See Also:

      public static final int MAX_SCAN_DEPTH
      See Also:
  • Constructor Details

    • Scanner

      public Scanner()
    • Scanner

      public Scanner(Scheduler scheduler)
    • Scanner

      public Scanner(Scheduler scheduler, boolean reportRealPaths)
      scheduler - The scheduler to use for scanning.
      reportRealPaths - If true, the Scanner.Listeners are called with the real path of scanned files.
  • Method Details

    • getScanInterval

      public int getScanInterval()
      Get the scan interval
      interval between scans in seconds
    • setScanInterval

      public void setScanInterval(int scanInterval)
      Set the scan interval
      scanInterval - pause between scans in seconds, or 0 for no scan after the initial scan.
    • setScanDirs

      public void setScanDirs(List<File> dirs)
    • addFile

      public void addFile(Path path)
      Add a file to be scanned. The file must not be null, and must exist.
      path - the Path of the file to scan.
    • addDirectory

      public IncludeExcludeSet<PathMatcher,Path> addDirectory(Path p)
      Add a directory to be scanned. The directory must not be null and must exist.
      p - the directory to scan.
      an IncludeExcludeSet to which the caller can add PathMatcher patterns to match
    • setFilenameFilter

      @Deprecated public void setFilenameFilter(FilenameFilter filter)
      Apply a filter to files found in the scan directory. Only files matching the filter will be reported as added/changed/removed.
      filter - the filename filter to use
    • getFilenameFilter

      @Deprecated public FilenameFilter getFilenameFilter()
      Get any filter applied to files in the scan dir.
      the filename filter
    • getScannables

      public Set<Path> getScannables()
    • getScanDepth

      public int getScanDepth()
      Get the scanDepth.
      the scanDepth
    • setScanDepth

      public void setScanDepth(int scanDepth)
      Set the scanDepth.
      scanDepth - the scanDepth to set
    • setReportExistingFilesOnStartup

      public void setReportExistingFilesOnStartup(boolean reportExisting)
      Whether or not an initial scan will report all files as being added.
      reportExisting - if true, all files found on initial scan will be reported as being added, otherwise not
    • getReportExistingFilesOnStartup

      public boolean getReportExistingFilesOnStartup()
    • setReportDirs

      public void setReportDirs(boolean dirs)
      Set if found directories should be reported.
      dirs - true to report directory changes as well
    • getReportDirs

      public boolean getReportDirs()
    • addListener

      public void addListener(Scanner.Listener listener)
      Add an added/removed/changed listener
      listener - the listener to add
    • removeListener

      public void removeListener(Scanner.Listener listener)
      Remove a registered listener
      listener - the Listener to be removed
    • doStart

      public void doStart() throws Exception
      Start the scanning action.
      doStart in class ContainerLifeCycle
      AbstractLifeCycle.StopException - If thrown, the lifecycle will immediately be stopped.
      Exception - If there was a problem starting. Will cause a transition to FAILED state
    • doStop

      public void doStop() throws Exception
      Stop the scanning.
      doStop in class ContainerLifeCycle
      Exception - If there was a problem stopping. Will cause a transition to FAILED state
    • reset

      public void reset()
      Clear the list of scannables. The scanner must first be in the stopped state.
    • exists

      public boolean exists(String path)
      path - tests if the path exists
      true if the path exists in one of the scandirs
    • nudge

      public void nudge()
      Hint to the scanner to perform a scan cycle as soon as possible. NOTE that the scan is not guaranteed to have happened by the time this method returns.
    • scan

      public void scan(Callback complete)
      Get the scanner to perform a scan cycle as soon as possible and call the Callback when the scan is finished or failed.
      complete - called when the scan cycle finishes or fails.