@Override
 public void undeploy(TargetModuleID targetModuleID) throws Exception {
   String deploymentName = targetModuleID.getModuleID();
   DeploymentPlanBuilder builder = deploymentManager.newDeploymentPlan();
   DeploymentPlan plan = builder.undeploy(deploymentName).remove(deploymentName).build();
   Future<ServerDeploymentPlanResult> future = deploymentManager.execute(plan);
   future.get();
 }
 @Override
 public void deploy(TargetModuleID targetModuleID) throws Exception {
   ROOT_LOGGER.beginDeploy(targetModuleID);
   String deploymentName = targetModuleID.getModuleID();
   DeploymentPlanBuilder builder = deploymentManager.newDeploymentPlan();
   builder = builder.add(deploymentName, new URL(deploymentName)).andDeploy();
   DeploymentPlan plan = builder.build();
   DeploymentAction deployAction = builder.getLastAction();
   executeDeploymentPlan(plan, deployAction);
   ROOT_LOGGER.endDeploy(targetModuleID);
 }
  @Test
  public void testListAvailableModules() throws Exception {
    DeploymentManager manager = getDeploymentManager();
    Target[] targets = manager.getTargets();
    TargetModuleID[] availableModules = manager.getAvailableModules(ModuleType.EAR, targets);
    assertNull(availableModules);

    ProgressObject progress = jsr88Deploy(getEarArchive());
    TargetModuleID[] targetModules = progress.getResultTargetModuleIDs();
    try {
      availableModules = manager.getAvailableModules(ModuleType.EAR, targets);
      assertNotNull(availableModules);
      assertEquals(1, availableModules.length);

      TargetModuleID targetModuleID = availableModules[0];
      String moduleID = targetModuleID.getModuleID();
      assertTrue("Ends with deployment-app.ear", moduleID.endsWith("deployment-app.ear"));

      // [TODO] verify child modules

    } finally {
      jsr88Undeploy(targetModules);
    }
  }
  protected boolean test(TargetModuleID[] moduleIDs, String path) throws Exception {

    // hack for std modules

    TargetModuleID[] aTargetModuleIDs = findApplication("sayhello", ModuleType.WAR, null);

    dumpModulesIDs("war", aTargetModuleIDs);

    if (moduleIDs.length == 0) {
      // deployment failed ?
      log("Deployment failed, got zero TargetModuleID");
      System.exit(1);
    }

    // we are loading the deployed file and checking that the moduleIDs are
    // correct
    Application app = ApplicationArchivist.openArchive(new File(path));

    // check of non running modules
    ModuleType modType;
    if (app.isVirtual()) {
      modType = app.getStandaloneBundleDescriptor().getModuleType();
    } else {
      modType = ModuleType.EAR;
    }

    // now we need to start the application
    for (int i = 0; i < moduleIDs.length; i++) {
      TargetModuleID aTargetModuleID = moduleIDs[i];

      // should be part of the non running
      TargetModuleID[] targetModuleIDs =
          findApplication(aTargetModuleID.getModuleID(), modType, Boolean.FALSE);
      check(path, app, targetModuleIDs);

      // should be part of the available apps
      targetModuleIDs = findApplication(aTargetModuleID.getModuleID(), modType, null);
      check(path, app, targetModuleIDs);

      // now we start it..
      start(aTargetModuleID.getModuleID());

      // should be part of the running
      targetModuleIDs = findApplication(aTargetModuleID.getModuleID(), modType, Boolean.TRUE);
      check(path, app, targetModuleIDs);

      // should be part of the available apps
      targetModuleIDs = findApplication(aTargetModuleID.getModuleID(), modType, null);
      check(path, app, targetModuleIDs);
    }
    return true;
  }
  private boolean check(String path, Application app, TargetModuleID[] moduleIDs) {

    if (app.isVirtual()) {
      BundleDescriptor bd = app.getStandaloneBundleDescriptor();

      // standalone module, should be fast.
      if (moduleIDs.length != 1) {
        // wrong number...
        log("Error " + path + " is a standalone module, got more than 1 targetmoduleID");
        dumpModulesIDs("", moduleIDs);
        return false;
      }
    } else {
      for (int i = 0; i < moduleIDs.length; i++) {
        TargetModuleID parent = moduleIDs[i];
        Target target = parent.getTarget();
        log("Deployed on " + target.getName() + " with module ID " + parent.getModuleID());

        // now look at all the children
        TargetModuleID[] children = parent.getChildTargetModuleID();
        if (children == null) {
          log(
              "ERROR : App from "
                  + path
                  + " has "
                  + app.getBundleDescriptors().size()
                  + " modules but I didn't get any children TagetModuleID");
          return false;
        }

        // size is consistent ?
        if (children.length != app.getBundleDescriptors().size()) {
          log(
              "ERROR : App from "
                  + path
                  + " has "
                  + app.getBundleDescriptors().size()
                  + " modules but I got only "
                  + children.length
                  + " children TagetModuleID");
          return false;
        } else {
          log(
              "Expected "
                  + app.getBundleDescriptors().size()
                  + " children TargetModuleIDs and got "
                  + children.length);
        }

        for (int j = 0; j < children.length; j++) {
          TargetModuleID aChild = children[j];
          log("Processing " + aChild.getModuleID());

          String childModuleID = aChild.getModuleID();
          String[] splitted = childModuleID.split("#");
          if (splitted.length != 2) {
            log("Unknown sub module id " + childModuleID);
            return false;
          }

          // check that parent TargeTModuleID is correct
          if (aChild.getParentTargetModuleID().equals(parent)) {
            log("Child's parent TargetModuleID is correctly set");
          } else {
            log("Child's parent TargetModuleID is incorrect");
            return false;
          }

          String first = splitted[0];
          if (first.equals(parent.getModuleID())) {
            log("Correct parent module id for child " + childModuleID);
          } else {
            log("Incorrect parent module id for child " + childModuleID);
          }

          // look for the right module descriptor..
          ModuleDescriptor md = app.getModuleDescriptorByUri(splitted[1]);
          if (md == null) {
            log("Cannot find module descriptor for " + childModuleID);
            // return false;
          } else {
            log("Found module descriptor for " + childModuleID);
          }
          if (md.getModuleType().equals(ModuleType.WAR)) {
            log("Web module deployed at : " + aChild.getWebURL());
          }
        }
      }
    }
    // if we are here, it's good !
    return true;
  }
  public void handleProgressEvent(ProgressEvent progressEvent) {
    DeploymentStatus deploymentStatus = progressEvent.getDeploymentStatus();

    if (_log.isInfoEnabled()) {
      _log.info(deploymentStatus.getMessage());
    }

    if (deploymentStatus.isCompleted()) {
      try {
        TargetModuleID[] targetModuleIDs =
            _deploymentManager.getNonRunningModules(
                ModuleType.WAR, _deploymentManager.getTargets());

        if ((targetModuleIDs != null) && (targetModuleIDs.length > 0)) {
          for (TargetModuleID targetModuleID : targetModuleIDs) {
            if (!_warContext.equals(targetModuleID.getModuleID())) {
              continue;
            }

            ProgressObject startProgress =
                _deploymentManager.start(new TargetModuleID[] {targetModuleID});

            startProgress.addProgressListener(new StartProgressListener(_deploymentHandler));

            _deploymentHandler.setError(false);
            _deploymentHandler.setStarted(true);

            break;
          }
        } else {
          targetModuleIDs =
              _deploymentManager.getAvailableModules(
                  ModuleType.WAR, _deploymentManager.getTargets());

          for (TargetModuleID targetModuleID : targetModuleIDs) {
            if (!_warContext.equals(targetModuleID.getModuleID())) {
              continue;
            }

            ProgressObject startProgress =
                _deploymentManager.start(new TargetModuleID[] {targetModuleID});

            startProgress.addProgressListener(new StartProgressListener(_deploymentHandler));

            _deploymentHandler.setError(false);
            _deploymentHandler.setStarted(true);

            break;
          }
        }
      } catch (Exception e) {
        _log.error(e, e);

        _deploymentHandler.setError(true);
        _deploymentHandler.setStarted(false);
      }
    } else if (deploymentStatus.isFailed()) {
      _deploymentHandler.setError(true);
      _deploymentHandler.setStarted(false);
    }
  }