Example #1
0
  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;
  }
  /**
   * Install selected updates in to developer studio. Note: call {@link #setSelectedUpdates(List)
   * setSelectedUpdates} first.
   *
   * @param monitor
   */
  public void installSelectedUpdates(IProgressMonitor monitor) {
    SubMonitor progress = SubMonitor.convert(monitor, Messages.UpdateManager_26, 2);
    URI[] repos = new URI[] {getDevStudioUpdateSite()};
    session = new ProvisioningSession(p2Agent);
    updateOperation = new UpdateOperation(session);
    updateOperation.getProvisioningContext().setArtifactRepositories(repos);
    updateOperation.getProvisioningContext().setMetadataRepositories(repos);

    updateOperation.setSelectedUpdates(selectedUpdates);
    IStatus status = updateOperation.resolveModal(progress.newChild(1));
    if (status.getSeverity() == IStatus.CANCEL) {
      throw new OperationCanceledException();
    } else if (status.getSeverity() == IStatus.ERROR) {
      String message = status.getChildren()[0].getMessage();
      log.error(Messages.UpdateManager_27 + message);
    } else {
      final ProvisioningJob provisioningJob =
          updateOperation.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_28,
                                    Messages.UpdateManager_29 + Messages.UpdateManager_30);
                            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_31);
      }
    }
  }