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.handler.update;
|
12
|
|
13
|
import java.net.URI;
|
14
|
|
15
|
import org.apache.log4j.Logger;
|
16
|
import org.eclipse.core.runtime.IProgressMonitor;
|
17
|
import org.eclipse.core.runtime.IStatus;
|
18
|
import org.eclipse.core.runtime.Status;
|
19
|
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
|
20
|
import org.eclipse.core.runtime.jobs.Job;
|
21
|
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
|
22
|
import org.eclipse.e4.core.di.annotations.Execute;
|
23
|
import org.eclipse.e4.ui.di.UISynchronize;
|
24
|
import org.eclipse.e4.ui.workbench.IWorkbench;
|
25
|
import org.eclipse.equinox.p2.core.IProvisioningAgent;
|
26
|
import org.eclipse.equinox.p2.engine.IProfile;
|
27
|
import org.eclipse.equinox.p2.engine.IProfileRegistry;
|
28
|
import org.eclipse.equinox.p2.operations.ProvisioningJob;
|
29
|
import org.eclipse.equinox.p2.operations.ProvisioningSession;
|
30
|
import org.eclipse.equinox.p2.operations.UpdateOperation;
|
31
|
import org.eclipse.jface.dialogs.MessageDialog;
|
32
|
import org.eclipse.swt.widgets.Shell;
|
33
|
|
34
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
35
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
36
|
|
37
|
|
38
|
/**
|
39
|
* UpdateHandler invokes the check for updates UI
|
40
|
*
|
41
|
* @since 3.4
|
42
|
*/
|
43
|
public class UpdateHandler {
|
44
|
|
45
|
private Logger logger = Logger.getLogger(getClass());
|
46
|
|
47
|
private UpdateOperation operation;
|
48
|
|
49
|
@Execute
|
50
|
public void execute(final IProvisioningAgent agent, final Shell shell, final UISynchronize sync,
|
51
|
final IWorkbench workbench) {
|
52
|
|
53
|
Job checkUpdateJob = new Job(Messages.UpdateHandler_CHECK_UPDATE_JOB) {
|
54
|
@Override
|
55
|
protected IStatus run(final IProgressMonitor monitor) {
|
56
|
|
57
|
return checkForUpdates(agent, shell, sync, monitor);
|
58
|
}
|
59
|
};
|
60
|
|
61
|
checkUpdateJob.schedule();
|
62
|
|
63
|
checkUpdateJob.addJobChangeListener(new JobChangeAdapter() {
|
64
|
@Override
|
65
|
public void done(IJobChangeEvent event) {
|
66
|
if (event.getResult().isOK()) {
|
67
|
sync.syncExec(new Runnable() {
|
68
|
|
69
|
@Override
|
70
|
public void run() {
|
71
|
if(MessageDialog.openConfirm(shell, Messages.UpdateHandler_UPDATES_FOUND_TITLE, Messages.UpdateHandler_UPDATES_FOUND_MESSAGE)){
|
72
|
Job installUpdateJob = new Job(Messages.UpdateHandler_INSTALL_JOB) {
|
73
|
@Override
|
74
|
protected IStatus run(final IProgressMonitor monitor) {
|
75
|
// run installation
|
76
|
ProvisioningJob provisioningJob = operation.getProvisioningJob(monitor);
|
77
|
|
78
|
// updates cannot run from within Eclipse IDE!!!
|
79
|
if (provisioningJob == null) {
|
80
|
logger.error("Trying to update from the Eclipse IDE? This won't work!"); //$NON-NLS-1$
|
81
|
return Status.CANCEL_STATUS;
|
82
|
}
|
83
|
configureProvisioningJob(provisioningJob, shell, sync, workbench);
|
84
|
provisioningJob.schedule();
|
85
|
return Status.OK_STATUS;
|
86
|
}
|
87
|
};
|
88
|
installUpdateJob.schedule();
|
89
|
}
|
90
|
}
|
91
|
});
|
92
|
}
|
93
|
}
|
94
|
});
|
95
|
}
|
96
|
|
97
|
private IStatus checkForUpdates(final IProvisioningAgent agent, final Shell shell, final UISynchronize sync,
|
98
|
IProgressMonitor monitor) {
|
99
|
|
100
|
// configure update operation
|
101
|
final ProvisioningSession session = new ProvisioningSession(agent);
|
102
|
long startTime = System.currentTimeMillis();
|
103
|
logger.info("Try to get updates" + startTime);
|
104
|
try{
|
105
|
operation = new UpdateOperation(session);
|
106
|
IProfileRegistry registry = (IProfileRegistry) agent.getService(IProfileRegistry.SERVICE_NAME);
|
107
|
|
108
|
String profileID = operation.getProfileId();
|
109
|
IProfile profile = registry.getProfile(profileID);
|
110
|
if (profile == null){
|
111
|
logger.info("There is no profile for profileID: " + profileID);
|
112
|
}
|
113
|
configureUpdate(operation);
|
114
|
// check for updates, this causes I/O
|
115
|
final IStatus status = operation.resolveModal(monitor);
|
116
|
// failed to find updates (inform user and exit)
|
117
|
long actualTime = System.currentTimeMillis();
|
118
|
if (status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) {
|
119
|
showMessage(shell, sync);
|
120
|
logger.info("update resolved, but no updates available " + (actualTime - startTime) + "Status: " + status.toString());
|
121
|
return Status.CANCEL_STATUS;
|
122
|
}
|
123
|
|
124
|
logger.info("update resolved " + (actualTime - startTime) + "Status: " + status.toString());
|
125
|
}catch(Exception e){
|
126
|
logger.warn( e.getStackTrace().toString());
|
127
|
return Status.CANCEL_STATUS;
|
128
|
}
|
129
|
|
130
|
return Status.OK_STATUS;
|
131
|
}
|
132
|
|
133
|
private void configureProvisioningJob(ProvisioningJob provisioningJob, final Shell shell, final UISynchronize sync,
|
134
|
final IWorkbench workbench) {
|
135
|
|
136
|
// register a job change listener to track
|
137
|
// installation progress and notify user upon success
|
138
|
provisioningJob.addJobChangeListener(new JobChangeAdapter() {
|
139
|
@Override
|
140
|
public void done(IJobChangeEvent event) {
|
141
|
if (event.getResult().isOK()) {
|
142
|
sync.syncExec(new Runnable() {
|
143
|
|
144
|
@Override
|
145
|
public void run() {
|
146
|
boolean restart = MessageDialog.openQuestion(shell, Messages.UpdateHandler_UPDATE_INSTALLED_TITLE,
|
147
|
Messages.UpdateHandler_UPDATE_INSTALLED_TITLE_MESSAGE);
|
148
|
if (restart) {
|
149
|
workbench.restart();
|
150
|
}
|
151
|
}
|
152
|
});
|
153
|
}
|
154
|
super.done(event);
|
155
|
}
|
156
|
});
|
157
|
|
158
|
}
|
159
|
|
160
|
private void showMessage(final Shell parent, final UISynchronize sync) {
|
161
|
sync.syncExec(()->
|
162
|
MessageDialog.openWarning(parent, Messages.UpdateHandler_NO_UPDATE_TITLE,
|
163
|
Messages.UpdateHandler_NO_UPDATE_MESSAGE)
|
164
|
);
|
165
|
}
|
166
|
|
167
|
private UpdateOperation configureUpdate(final UpdateOperation operation) {
|
168
|
// create uri and check for validity
|
169
|
URI uri = null;
|
170
|
uri = P2Util.getP2UpdateRepository();
|
171
|
|
172
|
// set location of artifact and metadata repo
|
173
|
operation.getProvisioningContext().setArtifactRepositories(new URI[] { uri });
|
174
|
operation.getProvisioningContext().setMetadataRepositories(new URI[] { uri });
|
175
|
|
176
|
//if local plugin is installed, then check for updates
|
177
|
//operation.getProvisioningContext().setExtraInstallableUnits(extraIUs);
|
178
|
return operation;
|
179
|
}
|
180
|
}
|