예제 #1
0
  // see DirectoryScannerMXBean
  public void scan() {
    final ScanTask task;

    synchronized (this) {
      final LinkedList<File> list;
      switch (state) {
        case RUNNING:
        case SCHEDULED:
          throw new IllegalStateException(state.toString());
        case STOPPED:
        case COMPLETED:
          // only accept to scan if state is STOPPED or COMPLETED.
          list = new LinkedList<File>();
          list.add(rootFile);
          break;
        default:
          throw new IllegalStateException(String.valueOf(state));
      }

      // Create a new ScanTask object for our root directory file.
      //
      currentTask = task = new ScanTask(list, this);

      // transient state... will be switched to RUNNING when
      // task.execute() is called. This code could in fact be modified
      // to use java.util.concurent.Future and, to push the task to
      // an executor. We would then need to wait for the task to
      // complete before returning.  However, this wouldn't buy us
      // anything - since this method should wait for the task to
      // finish anyway: so why would we do it?
      // As it stands, we simply call task.execute() in the current
      // thread - brave and fearless readers may want to attempt the
      // modification ;-)
      //
      setStateAndNotify(SCHEDULED);
    }
    task.execute();
  }
예제 #2
0
  // 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;
    }
  }
예제 #3
0
 // see DirectoryScannerMXBean
 public String getCurrentScanInfo() {
   final ScanTask currentOrLastTask = currentTask;
   if (currentOrLastTask == null) return "Never Run";
   return currentOrLastTask.getScanInfo();
 }