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