/**
   * Install selected features in to developer studio. Note: call {@link
   * #setSelectedFeaturesToInstall(List) setSelectedFeaturesToInstall} first.
   *
   * @param monitor
   */
  public void installSelectedFeatures(IProgressMonitor monitor) {
    SubMonitor progress = SubMonitor.convert(monitor, Messages.UpdateManager_32, 2);

    URI[] repos = new URI[] {getDevStudioReleaseSite()};
    session = new ProvisioningSession(p2Agent);
    installOperation = new InstallOperation(session, selectedFeatures);
    installOperation.getProvisioningContext().setArtifactRepositories(repos);
    installOperation.getProvisioningContext().setMetadataRepositories(repos);
    IStatus status = installOperation.resolveModal(progress.newChild(1));
    if (status.getSeverity() == IStatus.CANCEL || progress.isCanceled()) {
      throw new OperationCanceledException();
    } else if (status.getSeverity() == IStatus.ERROR) {
      String message = status.getChildren()[0].getMessage();
      log.error(Messages.UpdateManager_33 + message);
    } else {
      ProvisioningJob provisioningJob = installOperation.getProvisioningJob(progress.newChild(1));
      if (provisioningJob != null) {
        provisioningJob.addJobChangeListener(
            new JobChangeAdapter() {
              @Override
              public void done(IJobChangeEvent arg0) {
                Display.getDefault()
                    .syncExec(
                        new Runnable() {
                          @Override
                          public void run() {
                            boolean restart =
                                MessageDialog.openQuestion(
                                    Display.getDefault().getActiveShell(),
                                    Messages.UpdateManager_34,
                                    Messages.UpdateManager_35 + Messages.UpdateManager_36);
                            if (restart) {
                              PlatformUI.getWorkbench().restart();
                            }
                          }
                        });
              }
            });
        provisioningJob.schedule();
        Display.getDefault()
            .syncExec(
                new Runnable() {
                  @Override
                  public void run() {
                    try {
                      PlatformUI.getWorkbench()
                          .getActiveWorkbenchWindow()
                          .getActivePage()
                          .showView(IProgressConstants.PROGRESS_VIEW_ID);
                    } catch (PartInitException e) {
                      log.error(e);
                    }
                  }
                });
      } else {
        log.error(Messages.UpdateManager_37);
      }
    }
  }
  /**
   * (inheritDoc)
   *
   * @param listener
   * @see org.goko.featuremanager.service.IFeatureManager#install(java.util.List)
   */
  @Override
  public IStatus install(
      List<GkInstallableUnit> units, IProgressMonitor monitor, IJobChangeListener listener)
      throws GkException {
    // get the repository managers and define our repositories
    IMetadataRepositoryManager manager =
        (IMetadataRepositoryManager)
            getProvisioningAgent().getService(IMetadataRepositoryManager.SERVICE_NAME);

    List<IInstallableUnit> p2Units = new ArrayList<IInstallableUnit>();
    for (GkInstallableUnit gkInstallableUnit : units) {
      if (!gkInstallableUnit.isInstalled()) {
        p2Units.add(gkInstallableUnit.getBaseUnit());
      }
    }
    final InstallOperation installOperation =
        new InstallOperation(new ProvisioningSession(getProvisioningAgent()), p2Units);
    LOG.info("installOperation created " + String.valueOf(installOperation));

    IStatus operationStatus = installOperation.resolveModal(monitor);
    LOG.info("operationStatus " + String.valueOf(operationStatus));

    if (operationStatus.getSeverity() > IStatus.WARNING) {
      LOG.error("installOperation detail :" + installOperation.getResolutionDetails());

      LOG.error("Install operation failed :" + operationStatus.toString());
      if (operationStatus.isMultiStatus()) {
        for (IStatus status : operationStatus.getChildren()) {
          LOG.error("   + " + status.getMessage());
        }
      }
      LOG.error(new CoreException(operationStatus));
      return operationStatus;
    }

    final ProvisioningJob provisioningJob = installOperation.getProvisioningJob(monitor);
    LOG.info("ProvisioningJob created " + String.valueOf(provisioningJob));

    provisioningJob.schedule();

    provisioningJob.addJobChangeListener(listener);

    LOG.info("Installation of feature done");

    return operationStatus;
  }
  public IStatus service(Command command, IProcess context)
      throws InterruptedException, CoreException {
    UpdateFeature updateFeature = (UpdateFeature) command;
    String featureId = updateFeature.getId();

    ProvisioningSession session = PlatformPlugin.createProvisioningSession();

    IProfile profile = PlatformPlugin.getProfileRegistry().getProfile(IProfileRegistry.SELF);
    IQuery<IInstallableUnit> query =
        QueryUtil.createLatestQuery(QueryUtil.createIUQuery(featureId));
    IQueryResult<IInstallableUnit> result = profile.query(query, new NullProgressMonitor());

    if (result.isEmpty()) {
      return Status.OK_STATUS;
    }

    UpdateOperation op = new UpdateOperation(session, result.toSet());
    IStatus status = op.resolveModal(new NullProgressMonitor());

    if (status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) {
      return Status.OK_STATUS;
    }
    if (status.getSeverity() == IStatus.CANCEL) {
      // should not happen,
      throw new CoreException(status);
    }

    if (status.getSeverity() == IStatus.ERROR) {
      throw new CoreException(status);
    }

    ProvisioningJob job = op.getProvisioningJob(null);
    if (job == null) {
      return Status.OK_STATUS;
    }

    status = job.runModal(new NullProgressMonitor());
    if (status.getSeverity() == IStatus.CANCEL) throw new CoreException(status);

    return Status.OK_STATUS;
  }