Simplify update process
[taxeditor.git] / eu.etaxonomy.taxeditor.application / src / main / java / eu / etaxonomy / taxeditor / update / UpdateHandler.java
1 /*******************************************************************************
2 * Copyright (c) 2007, 2009 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package eu.etaxonomy.taxeditor.update;
12
13 import java.net.URI;
14
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.OperationCanceledException;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.equinox.internal.p2.ui.ProvUI;
19 import org.eclipse.equinox.internal.p2.ui.dialogs.UpdateSingleIUWizard;
20 import org.eclipse.equinox.p2.core.IProvisioningAgent;
21 import org.eclipse.equinox.p2.core.ProvisionException;
22 import org.eclipse.equinox.p2.operations.ProvisioningJob;
23 import org.eclipse.equinox.p2.operations.UpdateOperation;
24 import org.eclipse.equinox.p2.repository.IRepositoryManager;
25 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
26 import org.eclipse.equinox.p2.ui.LoadMetadataRepositoryJob;
27 import org.eclipse.equinox.p2.ui.ProvisioningUI;
28 import org.eclipse.jface.wizard.WizardDialog;
29 import org.osgi.framework.BundleContext;
30 import org.osgi.framework.ServiceReference;
31
32 import eu.etaxonomy.taxeditor.TaxonomicEditorPlugin;
33 import eu.etaxonomy.taxeditor.model.MessagingUtils;
34
35 /**
36 * UpdateHandler invokes the check for updates UI
37 *
38 * @since 3.4
39 */
40 public class UpdateHandler extends PreloadingRepositoryHandler {
41
42
43 @Override
44 protected void doExecute(LoadMetadataRepositoryJob job) {
45
46 UpdateOperation operation = new UpdateOperation(ProvisioningUI.getDefaultUI().getSession());
47
48 // check for updates
49 checkForUpdates(operation);
50
51 ProvisioningJob provisioningJob = operation.getProvisioningJob(null);
52 if (provisioningJob == null) {
53 MessagingUtils.messageDialog("Error in performing update",
54 operation,
55 "ProvisioningJob could not be created." + System.getProperty("line.separator") +
56 "Either this application does not support p2 software installation or this application has been launched from within the Eclipse IDE",
57 null,
58 false);
59
60 } else if (getProvisioningUI().getPolicy().continueWorkingWithOperation(operation, getShell())) {
61 if (UpdateSingleIUWizard.validFor(operation)) {
62 // Special case for only updating a single root
63 UpdateSingleIUWizard wizard = new UpdateSingleIUWizard(getProvisioningUI(), operation);
64 WizardDialog dialog = new WizardDialog(getShell(), wizard);
65 dialog.create();
66 dialog.open();
67 } else {
68 // Open the normal version of the update wizard
69 getProvisioningUI().openUpdateWizard(false, operation, job);
70 }
71 }
72 }
73
74
75 /**
76 * @param monitor
77 * @return
78 */
79 private IStatus checkForUpdates(UpdateOperation operation) {
80
81 // force refresh all the caches before
82 IMetadataRepositoryManager metaManager = ProvUI.getMetadataRepositoryManager(ProvisioningUI.getDefaultUI().getSession());
83 URI[] repos = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
84 for(URI repo : repos) {
85 try {
86 metaManager.refreshRepository(repo, null);
87 } catch (ProvisionException pe) {
88 IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
89 "Error occured while reloading cache.", pe);
90
91 } catch (OperationCanceledException oce) {
92 IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
93 "Error occured while reloading cache.", oce);
94 }
95 }
96 BundleContext bundleContext = TaxonomicEditorPlugin.getContext();
97 ServiceReference reference = bundleContext.getServiceReference(IProvisioningAgent.SERVICE_NAME);
98 if (reference == null) {
99 IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
100 "No provisioning agent found. This application is not set up for updates.");
101 return errorStatus;
102 }
103
104 try {
105 return operation.resolveModal(null);
106 } finally {
107 bundleContext.ungetService(reference);
108 }
109 }
110 }