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
|
boolean hasNoRepos = false;
|
43
|
|
44
|
@Override
|
45
|
protected void doExecute(LoadMetadataRepositoryJob job) {
|
46
|
if (hasNoRepos) {
|
47
|
return;
|
48
|
}
|
49
|
UpdateOperation operation = new UpdateOperation(ProvisioningUI.getDefaultUI().getSession());
|
50
|
|
51
|
// check for updates
|
52
|
IStatus status = checkForUpdates(operation);
|
53
|
|
54
|
|
55
|
if (status.isOK() && status.getSeverity() != IStatus.ERROR) {
|
56
|
|
57
|
boolean doInstall = MessagingUtils.confirmDialog("Updates available", "Do you want to install the available updates ?");
|
58
|
if(doInstall) {
|
59
|
ProvisioningJob provisioningJob = operation.getProvisioningJob(null);
|
60
|
if (provisioningJob == null) {
|
61
|
MessagingUtils.messageDialog("Error in performing update",
|
62
|
operation,
|
63
|
"ProvisioningJob could not be created." + System.getProperty("line.separator") +
|
64
|
"Either this application does not support p2 software installation or this application has been launched from within the Eclipse IDE",
|
65
|
null,
|
66
|
false);
|
67
|
|
68
|
} else {
|
69
|
if (getProvisioningUI().getPolicy().continueWorkingWithOperation(operation, getShell())) {
|
70
|
if (UpdateSingleIUWizard.validFor(operation)) {
|
71
|
// Special case for only updating a single root
|
72
|
UpdateSingleIUWizard wizard = new UpdateSingleIUWizard(getProvisioningUI(), operation);
|
73
|
WizardDialog dialog = new WizardDialog(getShell(), wizard);
|
74
|
dialog.create();
|
75
|
dialog.open();
|
76
|
} else {
|
77
|
// Open the normal version of the update wizard
|
78
|
getProvisioningUI().openUpdateWizard(false, operation, job);
|
79
|
}
|
80
|
}
|
81
|
}
|
82
|
}
|
83
|
} else {
|
84
|
MessagingUtils.informationDialog("Checking for updates", status);
|
85
|
}
|
86
|
}
|
87
|
|
88
|
|
89
|
/**
|
90
|
* @param monitor
|
91
|
* @return
|
92
|
*/
|
93
|
private IStatus checkForUpdates(UpdateOperation operation) {
|
94
|
|
95
|
// force refresh all the caches before
|
96
|
IMetadataRepositoryManager metaManager = ProvUI.getMetadataRepositoryManager(ProvisioningUI.getDefaultUI().getSession());
|
97
|
URI[] repos = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
|
98
|
for(URI repo : repos) {
|
99
|
try {
|
100
|
metaManager.refreshRepository(repo, null);
|
101
|
} catch (ProvisionException pe) {
|
102
|
IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
|
103
|
"Error occured while reloading cache.", pe);
|
104
|
|
105
|
} catch (OperationCanceledException oce) {
|
106
|
IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
|
107
|
"Error occured while reloading cache.", oce);
|
108
|
}
|
109
|
}
|
110
|
BundleContext bundleContext = TaxonomicEditorPlugin.getContext();
|
111
|
ServiceReference reference = bundleContext.getServiceReference(IProvisioningAgent.SERVICE_NAME);
|
112
|
if (reference == null) {
|
113
|
IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
|
114
|
"No provisioning agent found. This application is not set up for updates.");
|
115
|
return errorStatus;
|
116
|
}
|
117
|
|
118
|
try {
|
119
|
return operation.resolveModal(null);
|
120
|
} finally {
|
121
|
bundleContext.ungetService(reference);
|
122
|
}
|
123
|
}
|
124
|
}
|