Class Scanner

All Implemented Interfaces:
LifeCycle

public class Scanner extends AbstractLifeCycle
Scanner Utility for scanning a directory for added, removed and changed files and reporting these events via registered Listeners.
  • Field Details

  • Constructor Details

    • Scanner

      public Scanner()
  • Method Details

    • getScanInterval

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

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

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

      @Deprecated public void addScanDir(File dir)
      Deprecated.
    • addFile

      public void addFile(Path p) throws IOException
      Add a file to be scanned. The file must not be null, and must exist.
      Parameters:
      p - the Path of the file to scan.
      Throws:
      IOException
    • addDirectory

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

      @Deprecated public List<File> getScanDirs()
      Deprecated.
    • getScannables

      public Set<Path> getScannables()
    • setRecursive

      @Deprecated public void setRecursive(boolean recursive)
      Deprecated.
      Parameters:
      recursive - True if scanning is recursive
      See Also:
    • getRecursive

      @Deprecated public boolean getRecursive()
      Deprecated.
      Returns:
      True if scanning is recursive
      See Also:
    • getScanDepth

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

      public void setScanDepth(int scanDepth)
      Set the scanDepth.
      Parameters:
      scanDepth - the scanDepth to set
    • setFilenameFilter

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

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

      public void setReportExistingFilesOnStartup(boolean reportExisting)
      Whether or not an initial scan will report all files as being added.
      Parameters:
      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.
      Parameters:
      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
      Parameters:
      listener - the listener to add
    • removeListener

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

      public void doStart()
      Start the scanning action.
      Overrides:
      doStart in class AbstractLifeCycle
    • newTimerTask

      public TimerTask newTimerTask()
    • newTimer

      public Timer newTimer()
    • schedule

      public void schedule()
    • doStop

      public void doStop()
      Stop the scanning.
      Overrides:
      doStop in class AbstractLifeCycle
    • reset

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

      public boolean exists(String path)
      Parameters:
      path - tests if the path exists
      Returns:
      true if the path exists in one of the scandirs
    • scan

      public void scan()
      Perform a pass of the scanner and report changes
    • scanFiles

      public void scanFiles()
      Scan all of the given paths.
    • reportDifferences

      private void reportDifferences(Map<String,Scanner.TimeNSize> currentScan, Map<String,Scanner.TimeNSize> oldScan)
      Report the adds/changes/removes to the registered listeners
      Parameters:
      currentScan - the info from the most recent pass
      oldScan - info from the previous pass
    • warn

      private void warn(Object listener, String filename, Throwable th)
    • reportAddition

      private void reportAddition(String filename)
      Report a file addition to the registered FileAddedListeners
      Parameters:
      filename - the filename
    • reportRemoval

      private void reportRemoval(String filename)
      Report a file removal to the FileRemovedListeners
      Parameters:
      filename - the filename
    • reportChange

      private void reportChange(String filename)
      Report a file change to the FileChangedListeners
      Parameters:
      filename - the filename
    • reportBulkChanges

      private void reportBulkChanges(List<String> filenames)
      Report the list of filenames for which changes were detected.
      Parameters:
      filenames - names of all files added/changed/removed
    • reportScanStart

      private void reportScanStart(int cycle)
      Call ScanCycleListeners with start of scan
    • reportScanEnd

      private void reportScanEnd(int cycle)
      Call ScanCycleListeners with end of scan.