1
|
/*******************************************************************************
|
2
|
* Copyright (c) 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
|
|
12
|
package eu.etaxonomy.taxeditor;
|
13
|
|
14
|
import org.apache.log4j.Logger;
|
15
|
import org.eclipse.core.runtime.IProgressMonitor;
|
16
|
import org.eclipse.core.runtime.IStatus;
|
17
|
import org.eclipse.core.runtime.OperationCanceledException;
|
18
|
import org.eclipse.core.runtime.SubMonitor;
|
19
|
import org.eclipse.equinox.p2.core.IProvisioningAgent;
|
20
|
import org.eclipse.equinox.p2.operations.ProvisioningJob;
|
21
|
import org.eclipse.equinox.p2.operations.ProvisioningSession;
|
22
|
import org.eclipse.equinox.p2.operations.UpdateOperation;
|
23
|
|
24
|
/**
|
25
|
* This class shows an example for checking for updates and performing the
|
26
|
* update synchronously. It is up to the caller to run this in a job if
|
27
|
* a background update check is desired. This is a reasonable way to run an
|
28
|
* operation when user intervention is not required. Another approach is
|
29
|
* to separately perform the resolution and provisioning steps, deciding
|
30
|
* whether to perform these synchronously or in a job.
|
31
|
*
|
32
|
* Any p2 operation can be run modally (synchronously), or the job
|
33
|
* can be requested and scheduled by the caller.
|
34
|
*
|
35
|
* @see UpdateOperation#resolveModal(IProgressMonitor)
|
36
|
* @see UpdateOperation#getResolveJob(IProgressMonitor)
|
37
|
* @see UpdateOperation#getProvisioningJob(IProgressMonitor)
|
38
|
* @see UpdateOperation#resolveModal(IProgressMonitor)
|
39
|
* @see UpdateOperation#getResolveJob(IProgressMonitor)
|
40
|
* @see UpdateOperation#getProvisioningJob(IProgressMonitor)
|
41
|
* @see UpdateOperation#resolveModal(IProgressMonitor)
|
42
|
* @see UpdateOperation#getResolveJob(IProgressMonitor)
|
43
|
* @see UpdateOperation#getProvisioningJob(IProgressMonitor)
|
44
|
* @author n.hoffmann
|
45
|
* @version $Id: $
|
46
|
*/
|
47
|
public class P2Util {
|
48
|
|
49
|
private static final Logger logger = Logger.getLogger(P2Util.class);
|
50
|
|
51
|
// XXX Check for updates to this application and return a status.
|
52
|
static IStatus checkForUpdates(IProvisioningAgent agent, IProgressMonitor monitor) throws OperationCanceledException {
|
53
|
ProvisioningSession session = new ProvisioningSession(agent);
|
54
|
|
55
|
// the default update operation looks for updates to the currently
|
56
|
// running profile, using the default profile root marker. To change
|
57
|
// which installable units are being updated, use the more detailed
|
58
|
// constructors.
|
59
|
UpdateOperation operation = new UpdateOperation(session);
|
60
|
SubMonitor sub = SubMonitor.convert(monitor,
|
61
|
"Checking for application updates...", 200);
|
62
|
IStatus status = operation.resolveModal(sub.newChild(100));
|
63
|
|
64
|
if (status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) {
|
65
|
return status;
|
66
|
}
|
67
|
if (status.getSeverity() == IStatus.CANCEL)
|
68
|
throw new OperationCanceledException();
|
69
|
|
70
|
if (status.getSeverity() != IStatus.ERROR) {
|
71
|
// More complex status handling might include showing the user what updates
|
72
|
// are available if there are multiples, differentiating patches vs. updates, etc.
|
73
|
// In this example, we simply update as suggested by the operation.
|
74
|
ProvisioningJob job = operation.getProvisioningJob(null);
|
75
|
if(job == null){
|
76
|
return status;
|
77
|
}
|
78
|
|
79
|
status = job.runModal(sub.newChild(100));
|
80
|
if (status.getSeverity() == IStatus.CANCEL)
|
81
|
throw new OperationCanceledException();
|
82
|
}
|
83
|
return status;
|
84
|
}
|
85
|
}
|