-
- /**
- *
- *
- */
- public static void checkForUpdates() {
-
- Job updateJob = new Job("Update Job") {
- @Override
- public IStatus run(IProgressMonitor monitor) {
- return doCheckForUpdates(monitor);
- }
- };
- updateJob.schedule();
- }
-
- /**
- * @param monitor
- * @return
- */
- private static IStatus doCheckForUpdates(IProgressMonitor monitor) {
-
- // force refresh all the caches before
- IMetadataRepositoryManager metaManager = ProvUI.getMetadataRepositoryManager(ProvisioningUI.getDefaultUI().getSession());
- URI[] repos = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
- for(URI repo : repos) {
- try {
- metaManager.refreshRepository(repo, monitor);
- } catch (ProvisionException pe) {
- IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
- "Error occured while reloading cache.", pe);
-
- } catch (OperationCanceledException oce) {
- IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
- "Error occured while reloading cache.", oce);
- }
- }
- BundleContext bundleContext = TaxonomicEditorPlugin.getContext();
- ServiceReference reference = bundleContext.getServiceReference(IProvisioningAgent.SERVICE_NAME);
- if (reference == null) {
- IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
- "No provisioning agent found. This application is not set up for updates.");
- return errorStatus;
- }
-
- final IProvisioningAgent agent = (IProvisioningAgent) bundleContext.getService(reference);
- IStatus updateStatus;
- try {
- updateStatus = P2Util.checkForUpdates(agent, monitor);
- MessagingUtils.info(updateStatus);
- } finally {
- bundleContext.ungetService(reference);
- }
- return updateStatus;
- }
-
- /**
- * @param agent
- * @param monitor
- * @return
- * @throws OperationCanceledException
- */
- static IStatus checkForUpdates(IProvisioningAgent agent, final IProgressMonitor monitor) {
- ProvisioningSession session = new ProvisioningSession(agent);
- // the default update operation looks for updates to the currently
- // running profile, using the default profile root marker. To change
- // which installable units are being updated, use the more detailed
- // constructors.
- final UpdateOperation operation = new UpdateOperation(session);
-// try {
-// setUpdateRepositories(operation);
-// } catch (URISyntaxException e) {
-// MessagingUtils.errorDialog("Invalid update site URI",
-// operation,
-// "The update site URI has an invalid syntax",
-// TaxonomicEditorPlugin.PLUGIN_ID,
-// e,
-// false);
-// return null;
-// }
-
- final IStatus status = operation.resolveModal(monitor);
-
- if (status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) {
- return status;
- }
-
- if (status.isOK() && status.getSeverity() != IStatus.ERROR) {
- // We need this block of code to be in async execution
- // since the confirm dialogs work only on the UI thread
- Display.getDefault().asyncExec(new Runnable() {
- @Override
- public void run() {
- String updates = "";
- Update[] possibleUpdates = operation
- .getPossibleUpdates();
- for (Update update : possibleUpdates) {
- updates += update + "\n";
- }
-
- boolean doInstall = MessagingUtils.confirmDialog("Updates available", "Do you want to install the available updates ?");
- // We may need to think whether we still run in async mode once
- // the user agrees to update. Maybe be reasonable to change to blocking
- // from this point until the update is complete.
-
- // More complex status handling might include showing the user what
- // updates are available if there are multiples, differentiating
- // patches vs. updates, etc. In this example, we simply update as
- // suggested by the operation.
- if(doInstall) {
- ProvisioningJob provisioningJob = operation.getProvisioningJob(monitor);
- if (provisioningJob == null) {
- MessagingUtils.messageDialog("Error in performing update",
- operation,
- "ProvisioningJob could not be created." + System.getProperty("line.separator") +
- "Either this application does not support p2 software installation or this application has been launched from within the Eclipse IDE",
- null,
- false);
-
- } else {
- // register a job change listener to track
- // installation progress and notify user upon success
- provisioningJob
- .addJobChangeListener(new JobChangeAdapter() {
- @Override
- public void done(IJobChangeEvent event) {
- if (event.getResult().isOK()) {
- // We need this block of code to be in async execution
- // since the confirm dialogs work only on the UI thread
- Display.getDefault().asyncExec(new Runnable() {
- @Override
- public void run() {
- boolean restart = MessagingUtils.confirmDialog(
- "Updates installed, restart?",
- "Updates have been installed successfully, do you want to restart?");
- if (restart) {
- PlatformUI.getWorkbench().restart();
- }
- }
- });
- }
- super.done(event);
- }
- });
- provisioningJob.schedule();
- }
- }
- }
- });
- }
- return status;
- }