private void scan() throws OperationFailedException {

    try {
      scanLock.lockInterruptibly();
    } catch (InterruptedException ie) {
      Thread.currentThread().interrupt();
      return;
    }
    try {
      if (scanEnabled) { // confirm the scan is still wanted

        log.tracef(
            "Scanning directory %s for deployment content changes",
            deploymentDir.getAbsolutePath());

        final List<ModelNode> updates = new ArrayList<ModelNode>();

        Map<String, File> foundDeployed = new HashMap<String, File>();
        Set<String> newlyAdded = new HashSet<String>();
        Set<String> registeredDeployments = getDeploymentNames();
        scanDirectory(deploymentDir, updates, foundDeployed, newlyAdded, registeredDeployments);

        // Add remove actions to the plan for anything we count as
        // deployed that we didn't find on the scan
        Set<String> toRemove = new HashSet<String>(deployed);
        toRemove.removeAll(foundDeployed.keySet());
        toRemove.removeAll(newlyAdded); // in case user removed the marker and added replacement
        for (String missing : toRemove) {
          updates.add(getUndeployOperation(missing));
          updates.add(getRemoveOperation(missing));
        }

        if (updates.size() > 0) {
          if (log.isDebugEnabled()) {
            for (ModelNode update : updates) {
              log.debugf("Deployment scan of [%s] found update action [%s]", deploymentDir, update);
            }
          }
          Operation composite = getCompositeUpdate(updates);
          ModelNode results = serverController.execute(composite);
          //                    System.out.println(composite);
          //                    System.out.println(results);
          // FIXME deal with result
        }

        // Throw away any found marker files that we didn't already know about
        Set<String> validFinds = cleanSpuriousMarkerFiles(foundDeployed);
        validFinds.addAll(newlyAdded);
        this.deployed = validFinds;

        log.tracef("Scan complete");
      }
    } finally {
      scanLock.unlock();
    }
  }
 private Set<String> getDeploymentNames() throws CancellationException, OperationFailedException {
   ModelNode op = Util.getEmptyOperation(READ_CHILDREN_NAMES_OPERATION, new ModelNode());
   op.get(CHILD_TYPE).set(DEPLOYMENT);
   ModelNode response = serverController.execute(OperationBuilder.Factory.create(op).build());
   // TODO use the proper response structure
   ModelNode result = response.get("result");
   Set<String> deploymentNames = new HashSet<String>();
   if (result.isDefined()) {
     List<ModelNode> deploymentNodes = result.asList();
     for (ModelNode node : deploymentNodes) {
       deploymentNames.add(node.asString());
     }
   }
   return deploymentNames;
 }
  /** This method isn't private solely to allow a unit test in the same package to call it */
  void scan() {

    try {
      scanLock.lockInterruptibly();
    } catch (InterruptedException ie) {
      Thread.currentThread().interrupt();
      return;
    }
    try {
      if (scanEnabled) { // confirm the scan is still wanted
        log.tracef(
            "Scanning directory %s for deployment content changes",
            deploymentDir.getAbsolutePath());

        List<ScannerTask> scannerTasks = new ArrayList<ScannerTask>();

        final Set<String> registeredDeployments = getDeploymentNames();
        final Set<String> toRemove = new HashSet<String>(deployed.keySet());
        scanDirectory(deploymentDir, scannerTasks, registeredDeployments, toRemove);

        // Add remove actions to the plan for anything we count as
        // deployed that we didn't find on the scan
        for (String missing : toRemove) {
          // TODO -- minor -- this assumes the deployment was in the root deploymentDir,
          // not a child dir, and therefore puts the '.undeploying' file there
          File parent = deploymentDir;
          scannerTasks.add(new UndeployTask(missing, parent));
        }

        if (scannerTasks.size() > 0) {
          List<ModelNode> updates = new ArrayList<ModelNode>(scannerTasks.size());

          for (ScannerTask task : scannerTasks) {
            final ModelNode update = task.getUpdate();
            if (log.isDebugEnabled()) {
              log.debugf("Deployment scan of [%s] found update action [%s]", deploymentDir, update);
            }
            updates.add(update);
          }

          while (!updates.isEmpty()) {
            ModelNode composite = getCompositeUpdate(updates);
            final ModelNode results =
                serverController.execute(OperationBuilder.Factory.create(composite).build());
            final List<Property> resultList = results.get(RESULT).asPropertyList();
            final List<ModelNode> toRetry = new ArrayList<ModelNode>();
            final List<ScannerTask> retryTasks = new ArrayList<ScannerTask>();
            for (int i = 0; i < resultList.size(); i++) {
              final ModelNode result = resultList.get(i).getValue();
              final ScannerTask task = scannerTasks.get(i);
              final ModelNode outcome = result.get(OUTCOME);
              if (outcome.isDefined() && SUCCESS.equals(outcome.asString())) {
                task.handleSuccessResult();
              } else if (outcome.isDefined() && CANCELLED.equals(outcome.asString())) {
                toRetry.add(updates.get(i));
                retryTasks.add(task);
              } else {
                task.handleFailureResult(result);
              }
            }
            updates = toRetry;
            scannerTasks = retryTasks;
          }
        }
        log.tracef("Scan complete");
      }
    } finally {
      scanLock.unlock();
    }
  }