private void deploy(String deploymentName, URL url) throws DeploymentException {
   Exception failure = null;
   try {
     DeploymentProgress distribute = deploymentManager.distribute(deploymentName, url, true);
     distribute.run();
     DeploymentStatus uploadStatus = distribute.getDeploymentStatus();
     if (uploadStatus.isFailed()) {
       failure = uploadStatus.getFailure();
       undeploy(deploymentName);
     } else {
       DeploymentProgress progress = deploymentManager.start(deploymentName);
       progress.run();
       DeploymentStatus status = progress.getDeploymentStatus();
       if (status.isFailed()) {
         failure = status.getFailure();
         undeploy(deploymentName);
       }
     }
   } catch (Exception e) {
     throw new DeploymentException("Could not deploy " + deploymentName, e);
   }
   if (failure != null) {
     throw new DeploymentException("Failed to deploy " + deploymentName, failure);
   }
 }
  private void undeploy(String name) throws DeploymentException {
    try {
      DeploymentProgress stopProgress = deploymentManager.stop(name);
      stopProgress.run();

      DeploymentProgress undeployProgress = deploymentManager.remove(name);
      undeployProgress.run();
    } catch (Exception e) {
      throw new DeploymentException("Could not undeploy " + name, e);
    }
  }