From b00a4a6602d64c035761d93a9fa7fbdc51ef9ecf Mon Sep 17 00:00:00 2001 From: Cherian Mathew Date: Mon, 23 Nov 2015 13:03:47 +0100 Subject: [PATCH] Move update checks from P2Util to UpdateHandler --- .../eu/etaxonomy/taxeditor/update/P2Util.java | 169 ------------------ .../taxeditor/update/UpdateHandler.java | 144 +++++++++++---- 2 files changed, 111 insertions(+), 202 deletions(-) diff --git a/eu.etaxonomy.taxeditor.application/src/main/java/eu/etaxonomy/taxeditor/update/P2Util.java b/eu.etaxonomy.taxeditor.application/src/main/java/eu/etaxonomy/taxeditor/update/P2Util.java index a286718ff..e39cdb618 100644 --- a/eu.etaxonomy.taxeditor.application/src/main/java/eu/etaxonomy/taxeditor/update/P2Util.java +++ b/eu.etaxonomy.taxeditor.application/src/main/java/eu/etaxonomy/taxeditor/update/P2Util.java @@ -5,30 +5,13 @@ import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.OperationCanceledException; -import org.eclipse.core.runtime.Status; -import org.eclipse.core.runtime.jobs.IJobChangeEvent; -import org.eclipse.core.runtime.jobs.Job; -import org.eclipse.core.runtime.jobs.JobChangeAdapter; import org.eclipse.equinox.internal.p2.ui.ProvUI; import org.eclipse.equinox.internal.p2.ui.model.ElementUtils; import org.eclipse.equinox.internal.p2.ui.model.MetadataRepositoryElement; -import org.eclipse.equinox.p2.core.IProvisioningAgent; -import org.eclipse.equinox.p2.core.ProvisionException; -import org.eclipse.equinox.p2.operations.ProvisioningJob; -import org.eclipse.equinox.p2.operations.ProvisioningSession; -import org.eclipse.equinox.p2.operations.Update; -import org.eclipse.equinox.p2.operations.UpdateOperation; import org.eclipse.equinox.p2.repository.IRepository; import org.eclipse.equinox.p2.repository.IRepositoryManager; import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager; import org.eclipse.equinox.p2.ui.ProvisioningUI; -import org.eclipse.swt.widgets.Display; -import org.eclipse.ui.PlatformUI; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceReference; import eu.etaxonomy.taxeditor.TaxonomicEditorPlugin; import eu.etaxonomy.taxeditor.model.MessagingUtils; @@ -137,156 +120,4 @@ public class P2Util { } PreferencesUtil.setP2Repositories(repoElements); } - - /** - * - * - */ - 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; - } } diff --git a/eu.etaxonomy.taxeditor.application/src/main/java/eu/etaxonomy/taxeditor/update/UpdateHandler.java b/eu.etaxonomy.taxeditor.application/src/main/java/eu/etaxonomy/taxeditor/update/UpdateHandler.java index bca8c9923..6a110d40f 100644 --- a/eu.etaxonomy.taxeditor.application/src/main/java/eu/etaxonomy/taxeditor/update/UpdateHandler.java +++ b/eu.etaxonomy.taxeditor.application/src/main/java/eu/etaxonomy/taxeditor/update/UpdateHandler.java @@ -10,11 +10,28 @@ *******************************************************************************/ package eu.etaxonomy.taxeditor.update; +import java.net.URI; + +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.OperationCanceledException; +import org.eclipse.core.runtime.Status; +import org.eclipse.equinox.internal.p2.ui.ProvUI; import org.eclipse.equinox.internal.p2.ui.dialogs.UpdateSingleIUWizard; +import org.eclipse.equinox.p2.core.IProvisioningAgent; +import org.eclipse.equinox.p2.core.ProvisionException; +import org.eclipse.equinox.p2.operations.ProvisioningJob; import org.eclipse.equinox.p2.operations.RepositoryTracker; import org.eclipse.equinox.p2.operations.UpdateOperation; +import org.eclipse.equinox.p2.repository.IRepositoryManager; +import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager; import org.eclipse.equinox.p2.ui.LoadMetadataRepositoryJob; +import org.eclipse.equinox.p2.ui.ProvisioningUI; import org.eclipse.jface.wizard.WizardDialog; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; + +import eu.etaxonomy.taxeditor.TaxonomicEditorPlugin; +import eu.etaxonomy.taxeditor.model.MessagingUtils; /** * UpdateHandler invokes the check for updates UI @@ -23,40 +40,101 @@ import org.eclipse.jface.wizard.WizardDialog; */ public class UpdateHandler extends PreloadingRepositoryHandler { - boolean hasNoRepos = false; + boolean hasNoRepos = false; - @Override + @Override protected void doExecute(LoadMetadataRepositoryJob job) { - if (hasNoRepos) { - return; - } - UpdateOperation operation = getProvisioningUI().getUpdateOperation(null, null); - - - // check for updates - operation.resolveModal(null); - if (getProvisioningUI().getPolicy().continueWorkingWithOperation(operation, getShell())) { - if (UpdateSingleIUWizard.validFor(operation)) { - // Special case for only updating a single root - UpdateSingleIUWizard wizard = new UpdateSingleIUWizard(getProvisioningUI(), operation); - WizardDialog dialog = new WizardDialog(getShell(), wizard); - dialog.create(); - dialog.open(); - } else { - // Open the normal version of the update wizard - getProvisioningUI().openUpdateWizard(false, operation, job); - } - } - } - - @Override + if (hasNoRepos) { + return; + } + UpdateOperation operation = getProvisioningUI().getUpdateOperation(null, null); + + // check for updates + IStatus status = checkForUpdates(operation); + + if (status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) { + MessagingUtils.info(status); + return; + } + + if (status.isOK() && status.getSeverity() != IStatus.ERROR) { + + boolean doInstall = MessagingUtils.confirmDialog("Updates available", "Do you want to install the available updates ?"); + if(doInstall) { + ProvisioningJob provisioningJob = operation.getProvisioningJob(null); + 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 { + if (getProvisioningUI().getPolicy().continueWorkingWithOperation(operation, getShell())) { + if (UpdateSingleIUWizard.validFor(operation)) { + // Special case for only updating a single root + UpdateSingleIUWizard wizard = new UpdateSingleIUWizard(getProvisioningUI(), operation); + WizardDialog dialog = new WizardDialog(getShell(), wizard); + dialog.create(); + dialog.open(); + } else { + // Open the normal version of the update wizard + getProvisioningUI().openUpdateWizard(false, operation, job); + } + } + } + } + } + + } + + + /** + * @param monitor + * @return + */ + private IStatus checkForUpdates(UpdateOperation operation) { + + // 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, null); + } 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; + } + + try { + return operation.resolveModal(null); + } finally { + bundleContext.ungetService(reference); + } + } + + + @Override protected boolean preloadRepositories() { - hasNoRepos = false; - RepositoryTracker repoMan = getProvisioningUI().getRepositoryTracker(); - if (repoMan.getKnownRepositories(getProvisioningUI().getSession()).length == 0) { - hasNoRepos = true; - return false; - } - return super.preloadRepositories(); - } + hasNoRepos = false; + RepositoryTracker repoMan = getProvisioningUI().getRepositoryTracker(); + if (repoMan.getKnownRepositories(getProvisioningUI().getSession()).length == 0) { + hasNoRepos = true; + return false; + } + return super.preloadRepositories(); + } } -- 2.30.2