Remove eclipse env check
[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.UpdateOperation;
23 import org.eclipse.equinox.p2.repository.IRepositoryManager;
24 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
25 import org.eclipse.equinox.p2.ui.LoadMetadataRepositoryJob;
26 import org.eclipse.equinox.p2.ui.ProvisioningUI;
27 import org.eclipse.jface.wizard.WizardDialog;
28 import org.osgi.framework.BundleContext;
29 import org.osgi.framework.ServiceReference;
30
31 import eu.etaxonomy.taxeditor.TaxonomicEditorPlugin;
32
33 /**
34 * UpdateHandler invokes the check for updates UI
35 *
36 * @since 3.4
37 */
38 public class UpdateHandler extends PreloadingRepositoryHandler {
39
40
41 @Override
42 protected void doExecute(LoadMetadataRepositoryJob job) {
43
44 UpdateOperation operation = new UpdateOperation(ProvisioningUI.getDefaultUI().getSession());
45
46 // check for updates
47 checkForUpdates(operation);
48 if (getProvisioningUI().getPolicy().continueWorkingWithOperation(operation, getShell())) {
49 if (UpdateSingleIUWizard.validFor(operation)) {
50 // Special case for only updating a single root
51 UpdateSingleIUWizard wizard = new UpdateSingleIUWizard(getProvisioningUI(), operation);
52 WizardDialog dialog = new WizardDialog(getShell(), wizard);
53 dialog.create();
54 dialog.open();
55 } else {
56 // Open the normal version of the update wizard
57 getProvisioningUI().openUpdateWizard(false, operation, job);
58 }
59 }
60 }
61
62
63 /**
64 * @param monitor
65 * @return
66 */
67 private IStatus checkForUpdates(UpdateOperation operation) {
68
69 // force refresh all the caches before
70 IMetadataRepositoryManager metaManager = ProvUI.getMetadataRepositoryManager(ProvisioningUI.getDefaultUI().getSession());
71 URI[] repos = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
72 for(URI repo : repos) {
73 try {
74 metaManager.refreshRepository(repo, null);
75 } catch (ProvisionException pe) {
76 IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
77 "Error occured while reloading cache.", pe);
78
79 } catch (OperationCanceledException oce) {
80 IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
81 "Error occured while reloading cache.", oce);
82 }
83 }
84 BundleContext bundleContext = TaxonomicEditorPlugin.getContext();
85 ServiceReference reference = bundleContext.getServiceReference(IProvisioningAgent.SERVICE_NAME);
86 if (reference == null) {
87 IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
88 "No provisioning agent found. This application is not set up for updates.");
89 return errorStatus;
90 }
91
92 try {
93 return operation.resolveModal(null);
94 } finally {
95 bundleContext.ungetService(reference);
96 }
97 }
98
99 }