// The actual scan logic. Switches state to RUNNING,
  // and scan the list of given dirs.
  // The list is a live object which is updated by this method.
  // This would allow us to implement methods like "pause" and "resume",
  // since all the info needed to resume would be in the list.
  //
  private void scan(ScanTask task, LinkedList<File> list) {
    setStateAndNotify(RUNNING);
    task.info = "In Progress";
    try {

      // The FileFilter will tell us which files match and which don't.
      //
      final FileFilter filter = config.buildFileFilter();

      // We have two condition to end the loop: either the list is
      // empty, meaning there's nothing more to scan, or the state of
      // the DirectoryScanner was asynchronously switched to STOPPED by
      // another thread, e.g. because someone called "stop" on the
      // ScanManagerMXBean
      //
      while (!list.isEmpty() && state == RUNNING) {

        // Get and remove the first element in the list.
        //
        final File current = list.poll();

        // Increment number of file scanned.
        task.scanned++;

        // If 'current' is a file, it's already been matched by our
        // file filter (see below): act on it.
        // Note that for the first iteration of this loop, there will
        // be one single file in the list: the root directory for this
        // scanner.
        //
        if (current.isFile()) {
          task.matching++;
          actOn(current);
        }

        // If 'current' is a directory, then
        // find files and directories that match the file filter
        // in this directory
        //
        if (current.isDirectory()) {

          // Gets matching files and directories
          final File[] content = current.listFiles(filter);
          if (content == null) continue;

          // Adds all matching file to the list.
          list.addAll(0, Arrays.asList(content));
        }
      }

      // The loop terminated. If the list is empty, then we have
      // completed our task. If not, then somebody must have called
      // stop() on this directory scanner.
      //
      if (list.isEmpty()) {
        task.info = "Successfully Completed";
        setStateAndNotify(COMPLETED);
      }
    } catch (Exception x) {
      // We got an exception: stop the scan
      //
      task.info = "Failed: " + x;
      if (LOG.isLoggable(Level.FINEST)) LOG.log(Level.FINEST, "scan task failed: " + x, x);
      else if (LOG.isLoggable(Level.FINE)) LOG.log(Level.FINE, "scan task failed: " + x);
      setStateAndNotify(STOPPED);
    } catch (Error e) {
      // We got an Error:
      // Should not happen unless we ran out of memory or
      // whatever - don't even try to notify, but
      // stop the scan anyway!
      //
      state = STOPPED;
      task.info = "Error: " + e;

      // rethrow error.
      //
      throw e;
    }
  }