private BundleDeploymentCriteria getCriteriaFromContext(JobExecutionContext context) {
   int bundleDeploymentId =
       context.getJobDetail().getJobDataMap().getInt(BUNDLE_DEPLOYMENT_ID_KEY);
   BundleDeploymentCriteria crit = new BundleDeploymentCriteria();
   crit.addFilterId(bundleDeploymentId);
   return crit;
 }
  public void cancel() {
    // delete a newly created deployment but only if it's status is failure and it has
    // no deployments.  This should be rare, or maybe impossible, but in an odd case that
    // the deployment fails and they user wants to Cancel as opposed to Finish, let's
    // clean up as best as possible.
    if ((null != getNewDeployment()) && (0 < getNewDeployment().getId())) {
      BundleGWTServiceAsync bundleServer = GWTServiceLookup.getBundleService();
      BundleDeploymentCriteria c = new BundleDeploymentCriteria();
      c.addFilterId(getNewDeployment().getId());
      c.fetchResourceDeployments(true);
      bundleServer.findBundleDeploymentsByCriteria(
          c, //
          new AsyncCallback<PageList<BundleDeployment>>() {
            public void onSuccess(PageList<BundleDeployment> newDeploymentList) {
              if (!newDeploymentList.isEmpty()) {
                BundleDeployment newDeployment = newDeploymentList.get(0);
                boolean isFailedToLaunch =
                    BundleDeploymentStatus.FAILURE.equals(newDeployment.getStatus())
                        || BundleDeploymentStatus.PENDING.equals(newDeployment.getStatus());
                boolean hasNoResourceDeployments =
                    ((null == newDeployment.getResourceDeployments())
                        || newDeployment.getResourceDeployments().isEmpty());

                // go ahead and delete it if it hasn't really done anything but get created.
                // otherwise, let folks inspect via the ui and take further action.
                // if the deployment can't be deleted then don't try to delete the destination,
                // it's now in use by the deployment
                if (isFailedToLaunch && hasNoResourceDeployments) {
                  BundleGWTServiceAsync bundleServer = GWTServiceLookup.getBundleService();
                  bundleServer.deleteBundleDeployment(
                      newDeployment.getId(), //
                      new AsyncCallback<Void>() {
                        public void onSuccess(Void voidReturn) {
                          deleteNewDestination();
                        }

                        public void onFailure(Throwable caught) {
                          CoreGUI.getErrorHandler()
                              .handleError(MSG.view_bundle_deployWizard_error_1(), caught);
                        }
                      });
                }
              }
            }

            public void onFailure(Throwable caught) {
              // should not really get here
              CoreGUI.getErrorHandler().handleError(MSG.view_bundle_deployWizard_error_1(), caught);
              deleteNewDestination();
            }
          });
    } else {
      deleteNewDestination();
    }
  }
 /**
  * waits until given BundleDeployment is not in PENDING or IN_PROGRESS state, then returns
  * BundleDeployment instance, which is going to be either SUCCESS or FAILURE
  *
  * @param deployment
  * @return
  */
 private BundleDeployment waitForBundleDeployment(BundleDeployment deployment) {
   while (deployment.getStatus().equals(BundleDeploymentStatus.IN_PROGRESS)
       || deployment.getStatus().equals(BundleDeploymentStatus.PENDING)) {
     try {
       Thread.currentThread().join(3 * 1000);
     } catch (InterruptedException e) {
       e.printStackTrace();
     }
     BundleDeploymentCriteria criteria = new BundleDeploymentCriteria();
     criteria.addFilterId(deployment.getId());
     deployment =
         bundleManager.findBundleDeploymentsByCriteria(client.getSubject(), criteria).get(0);
   }
   return deployment;
 }