1 |
|
/**
|
2 |
|
* Copyright (C) 2007 EDIT
|
3 |
|
* European Distributed Institute of Taxonomy
|
4 |
|
* http://www.e-taxonomy.eu
|
5 |
|
*
|
6 |
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7 |
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8 |
|
*/
|
|
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 |
*******************************************************************************/
|
9 |
11 |
package eu.etaxonomy.taxeditor.update;
|
10 |
12 |
|
11 |
|
import java.lang.reflect.InvocationTargetException;
|
12 |
|
import java.net.URI;
|
13 |
|
|
14 |
|
import org.eclipse.core.commands.AbstractHandler;
|
15 |
|
import org.eclipse.core.commands.ExecutionEvent;
|
16 |
|
import org.eclipse.core.commands.ExecutionException;
|
17 |
|
import org.eclipse.core.runtime.IProgressMonitor;
|
18 |
|
import org.eclipse.core.runtime.IStatus;
|
19 |
|
import org.eclipse.core.runtime.OperationCanceledException;
|
20 |
|
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
|
21 |
|
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
|
22 |
|
import org.eclipse.equinox.internal.p2.ui.ProvUI;
|
23 |
|
import org.eclipse.equinox.p2.core.IProvisioningAgent;
|
24 |
|
import org.eclipse.equinox.p2.core.ProvisionException;
|
25 |
|
import org.eclipse.equinox.p2.operations.ProvisioningJob;
|
26 |
|
import org.eclipse.equinox.p2.operations.ProvisioningSession;
|
|
13 |
import org.eclipse.equinox.internal.p2.ui.dialogs.UpdateSingleIUWizard;
|
|
14 |
import org.eclipse.equinox.p2.operations.RepositoryTracker;
|
27 |
15 |
import org.eclipse.equinox.p2.operations.UpdateOperation;
|
28 |
|
import org.eclipse.equinox.p2.repository.IRepositoryManager;
|
29 |
|
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
|
30 |
|
import org.eclipse.equinox.p2.ui.ProvisioningUI;
|
31 |
|
import org.eclipse.jface.dialogs.MessageDialog;
|
32 |
|
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
|
33 |
|
import org.eclipse.jface.operation.IRunnableWithProgress;
|
34 |
|
import org.eclipse.swt.SWT;
|
35 |
|
import org.eclipse.swt.widgets.Display;
|
36 |
|
import org.eclipse.ui.IWorkbench;
|
37 |
|
import org.eclipse.ui.PlatformUI;
|
38 |
|
import org.osgi.framework.BundleContext;
|
39 |
|
import org.osgi.framework.ServiceReference;
|
40 |
|
|
41 |
|
import eu.etaxonomy.taxeditor.TaxonomicEditorPlugin;
|
42 |
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
|
16 |
import org.eclipse.equinox.p2.ui.LoadMetadataRepositoryJob;
|
|
17 |
import org.eclipse.jface.wizard.WizardDialog;
|
43 |
18 |
|
44 |
19 |
/**
|
45 |
20 |
* UpdateHandler invokes the check for updates UI
|
46 |
21 |
*
|
|
22 |
* @since 3.4
|
47 |
23 |
*/
|
48 |
|
public class UpdateHandler extends AbstractHandler {
|
49 |
|
|
50 |
|
private static final String ERROR_PERFORMING_UPDATES = "Error performing updates";
|
51 |
|
/**
|
52 |
|
* {@inheritDoc}
|
53 |
|
*/
|
54 |
|
@Override
|
55 |
|
public Object execute(ExecutionEvent event) throws ExecutionException {
|
56 |
|
// update using a progress monitor
|
57 |
|
IRunnableWithProgress runnable = new IRunnableWithProgress() {
|
58 |
|
@Override
|
59 |
|
public void run(IProgressMonitor monitor) throws InvocationTargetException,
|
60 |
|
InterruptedException {
|
61 |
|
doExecute(monitor);
|
62 |
|
}
|
63 |
|
};
|
64 |
|
|
65 |
|
try {
|
66 |
|
new ProgressMonitorDialog(null).run(true, true, runnable);
|
67 |
|
} catch (InvocationTargetException | InterruptedException e) {
|
68 |
|
showError(e);
|
69 |
|
}
|
70 |
|
return null;
|
71 |
|
}
|
72 |
|
|
73 |
|
private void doExecute(IProgressMonitor monitor) {
|
74 |
|
|
75 |
|
//UpdateOperation operation = new UpdateOperation(ProvisioningUI.getDefaultUI().getSession());
|
76 |
|
|
77 |
|
BundleContext bundleContext = TaxonomicEditorPlugin.getContext();
|
78 |
|
ServiceReference reference = bundleContext.getServiceReference(IProvisioningAgent.SERVICE_NAME);
|
79 |
|
if (reference == null) {
|
80 |
|
showMessage(MessageDialog.ERROR, ERROR_PERFORMING_UPDATES, "No provisioning agent found. This application is not set up for updates.");
|
81 |
|
return;
|
82 |
|
}
|
83 |
|
|
84 |
|
final IProvisioningAgent agent = (IProvisioningAgent) bundleContext.getService(reference);
|
85 |
|
final IWorkbench workbench = PlatformUI.getWorkbench();
|
86 |
|
|
87 |
|
ProvisioningSession session = new ProvisioningSession(agent);
|
88 |
|
// update all user-visible installable units
|
89 |
|
UpdateOperation operation = new UpdateOperation(session);
|
|
24 |
public class UpdateHandler extends PreloadingRepositoryHandler {
|
90 |
25 |
|
91 |
|
// force refresh all the repository caches before update
|
92 |
|
IMetadataRepositoryManager metaManager = ProvUI.getMetadataRepositoryManager(ProvisioningUI.getDefaultUI().getSession());
|
93 |
|
URI[] repos = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
|
|
26 |
boolean hasNoRepos = false;
|
94 |
27 |
|
95 |
|
for(URI repo : repos) {
|
96 |
|
try {
|
97 |
|
metaManager.refreshRepository(repo, null);
|
98 |
|
} catch (ProvisionException | OperationCanceledException e) {
|
99 |
|
showError(e);
|
100 |
|
}
|
101 |
|
}
|
102 |
|
|
103 |
|
// set location of artifact and metadata repo
|
104 |
|
operation.getProvisioningContext().setArtifactRepositories(repos);
|
105 |
|
operation.getProvisioningContext().setMetadataRepositories(repos);
|
106 |
|
|
107 |
|
IStatus status = operation.resolveModal(monitor);
|
108 |
|
|
109 |
|
// if (getProvisioningUI().getPolicy().continueWorkingWithOperation(operation, getShell())) {
|
110 |
|
// if (UpdateSingleIUWizard.validFor(operation)) {
|
111 |
|
// // Special case for only updating a single root
|
112 |
|
// UpdateSingleIUWizard wizard = new UpdateSingleIUWizard(getProvisioningUI(), operation);
|
113 |
|
// WizardDialog dialog = new WizardDialog(getShell(), wizard);
|
114 |
|
// dialog.create();
|
115 |
|
// dialog.open();
|
116 |
|
// } else {
|
117 |
|
// // Open the normal version of the update wizard
|
118 |
|
// getProvisioningUI().openUpdateWizard(false, operation, job);
|
119 |
|
// }
|
120 |
|
// }
|
121 |
|
|
122 |
|
if (status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) {
|
123 |
|
showMessage(MessageDialog.INFORMATION, "Checking for updates", "No updates were found");
|
|
28 |
@Override
|
|
29 |
protected void doExecute(LoadMetadataRepositoryJob job) {
|
|
30 |
if (hasNoRepos) {
|
124 |
31 |
return;
|
125 |
32 |
}
|
126 |
|
|
127 |
|
final ProvisioningJob provisioningJob = operation.getProvisioningJob(monitor);
|
128 |
|
if (provisioningJob != null) {
|
129 |
|
Display.getDefault().syncExec(new Runnable() {
|
130 |
|
@Override
|
131 |
|
public void run() {
|
132 |
|
boolean performUpdate = MessageDialog.openQuestion(
|
133 |
|
null,
|
134 |
|
"Updates available",
|
135 |
|
"There are updates available. Do you want to install them now?");
|
136 |
|
if (performUpdate) {
|
137 |
|
provisioningJob.addJobChangeListener(new JobChangeAdapter() {
|
138 |
|
@Override
|
139 |
|
public void done(IJobChangeEvent event) {
|
140 |
|
if (event.getResult().isOK()) {
|
141 |
|
Display.getDefault().syncExec(new Runnable() {
|
142 |
|
@Override
|
143 |
|
public void run() {
|
144 |
|
boolean restart = MessageDialog.openQuestion(null,
|
145 |
|
"Updates installed, restart?",
|
146 |
|
"Updates have been installed successfully, do you want to restart?");
|
147 |
|
if (restart) {
|
148 |
|
workbench.restart();
|
149 |
|
}
|
150 |
|
}
|
151 |
|
});
|
152 |
|
} else {
|
153 |
|
MessageDialog.openInformation(
|
154 |
|
null,
|
155 |
|
ERROR_PERFORMING_UPDATES,
|
156 |
|
event.getResult().getMessage());
|
157 |
|
}
|
158 |
|
}
|
159 |
|
});
|
160 |
|
provisioningJob.schedule();
|
161 |
|
}
|
162 |
|
}
|
163 |
|
});
|
164 |
|
} else {
|
165 |
|
if (operation.hasResolved()) {
|
166 |
|
showMessage(MessageDialog.ERROR, ERROR_PERFORMING_UPDATES, "Couldn't get provisioning job: " + operation.getResolutionResult());
|
|
33 |
UpdateOperation operation = getProvisioningUI().getUpdateOperation(null, null);
|
|
34 |
|
|
35 |
|
|
36 |
// check for updates
|
|
37 |
operation.resolveModal(null);
|
|
38 |
if (getProvisioningUI().getPolicy().continueWorkingWithOperation(operation, getShell())) {
|
|
39 |
if (UpdateSingleIUWizard.validFor(operation)) {
|
|
40 |
// Special case for only updating a single root
|
|
41 |
UpdateSingleIUWizard wizard = new UpdateSingleIUWizard(getProvisioningUI(), operation);
|
|
42 |
WizardDialog dialog = new WizardDialog(getShell(), wizard);
|
|
43 |
dialog.create();
|
|
44 |
dialog.open();
|
167 |
45 |
} else {
|
168 |
|
showMessage(MessageDialog.ERROR, ERROR_PERFORMING_UPDATES, "Couldn't resolve provisioning job");
|
|
46 |
// Open the normal version of the update wizard
|
|
47 |
getProvisioningUI().openUpdateWizard(false, operation, job);
|
169 |
48 |
}
|
170 |
49 |
}
|
171 |
50 |
}
|
172 |
51 |
|
173 |
|
private void showMessage(final int kind, final String title, final String info) {
|
174 |
|
Display.getDefault().syncExec(new Runnable() {
|
175 |
|
@Override
|
176 |
|
public void run() {
|
177 |
|
MessageDialog.open(kind, null, title, info, SWT.NONE);
|
178 |
|
}
|
179 |
|
});
|
180 |
|
}
|
181 |
|
|
182 |
|
private void showError(final Throwable t) {
|
183 |
|
Display.getDefault().syncExec(new Runnable() {
|
184 |
|
@Override
|
185 |
|
public void run() {
|
186 |
|
MessagingUtils.errorDialog(ERROR_PERFORMING_UPDATES,
|
187 |
|
this,
|
188 |
|
t.getMessage(),
|
189 |
|
TaxonomicEditorPlugin.PLUGIN_ID,
|
190 |
|
t,
|
191 |
|
true);
|
192 |
|
}
|
193 |
|
});
|
|
52 |
@Override
|
|
53 |
protected boolean preloadRepositories() {
|
|
54 |
hasNoRepos = false;
|
|
55 |
RepositoryTracker repoMan = getProvisioningUI().getRepositoryTracker();
|
|
56 |
if (repoMan.getKnownRepositories(getProvisioningUI().getSession()).length == 0) {
|
|
57 |
hasNoRepos = true;
|
|
58 |
return false;
|
|
59 |
}
|
|
60 |
return super.preloadRepositories();
|
194 |
61 |
}
|
195 |
|
|
196 |
|
|
197 |
62 |
}
|
Revert update handler to earlier version