Debian Codegraph @

bundles/org.eclipse.equinox.p2.operations/src/org/eclipse/equinox/p2/operations/ProvisioningJob.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2007, 2020 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.equinox.p2.operations;
15
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.core.runtime.jobs.Job;
18 import org.eclipse.equinox.internal.p2.operations.Constants;
19 import org.eclipse.equinox.internal.p2.operations.Messages;
20 import org.eclipse.equinox.p2.core.ProvisionException;
21 import org.eclipse.osgi.util.NLS;
22
23 /**
24 * Abstract class representing provisioning jobs. Provisioning jobs
25 * can be run in the background by scheduling them, or they can
26 * be run by a client in a modal context. An additional progress monitor
27 * can be set into the job for progress reporting.
28 *
29 * @since 2.0
30 */
31 public abstract class ProvisioningJob extends Job {
32
33 /**
34 * Class for multiplexing progress across multiple progress monitors.
35 */
36 private static class DoubleProgressMonitor extends ProgressMonitorWrapper {
37
38 IProgressMonitor additionalMonitor;
39
40 protected DoubleProgressMonitor(IProgressMonitor monitor1, IProgressMonitor monitor2) {
41 super(monitor1);
42 additionalMonitor = monitor2;
43 }
44
45 @Override
46 public void beginTask(String name, int totalWork) {
47 super.beginTask(name, totalWork);
48 additionalMonitor.beginTask(name, totalWork);
49 }
50
51 @Override
52 public void clearBlocked() {
53 super.clearBlocked();
54 additionalMonitor.clearBlocked();
55 }
56
57 @Override
58 public void done() {
59 super.done();
60 additionalMonitor.done();
61 }
62
63 @Override
64 public void internalWorked(double work) {
65 super.internalWorked(work);
66 additionalMonitor.internalWorked(work);
67 }
68
69 @Override
70 public boolean isCanceled() {
71 if (super.isCanceled())
72 return true;
73 return additionalMonitor.isCanceled();
74 }
75
76 @Override
77 public void setBlocked(IStatus reason) {
78 super.setBlocked(reason);
79 additionalMonitor.setBlocked(reason);
80 }
81
82 @Override
83 public void setCanceled(boolean b) {
84 super.setCanceled(b);
85 additionalMonitor.setCanceled(b);
86 }
87
88 @Override
89 public void setTaskName(String name) {
90 super.setTaskName(name);
91 additionalMonitor.setTaskName(name);
92 }
93
94 @Override
95 public void subTask(String name) {
96 super.subTask(name);
97 additionalMonitor.subTask(name);
98 }
99
100 @Override
101 public void worked(int work) {
102 super.worked(work);
103 additionalMonitor.worked(work);
104 }
105 }
106
107 /**
108 * Constant which indicates that the job does not require a restart
109 * upon completion. This constant is typically used for operations that
110 * do not modify the running profile.
111 *
112 * @since 2.0
113 */
114 public static final int RESTART_NONE = 1;
115
116 /**
117 * Constant which indicates that the job requires the user to either
118 * restart or apply the configuration changes in order to pick up the
119 * changes performed by the job. This constant is typically used for
120 * operations that modify the running profile.
121 *
122 * @since 2.0
123 */
124 public static final int RESTART_OR_APPLY = 2;
125 /**
126 * Constant which indicates that the job requires the user to restart
127 * in order to pick up the changes performed by the job. This constant
128 * is typically used for operations that modify the running profile but don't
129 * handle dynamic changes without restarting the workbench.
130 *
131 * @since 2.0
132 */
133 public static final int RESTART_ONLY = 3;
134
135 private ProvisioningSession session;
136 private IProgressMonitor additionalMonitor;
137
138 /**
139 * Create a provisioning job with the given name that uses the
140 * provided provisioning session for retrieving any services
141 * needed.
142 *
143 * @param name the name of the job
144 * @param session the session providing the services
145 */
146 public ProvisioningJob(String name, ProvisioningSession session) {
147 super(name);
148 this.session = session;
149 }
150
151 /**
152 * Return the provisioning session that is used by the receiver
153 * when retrieving necessary provisioning services.
154 *
155 * @return the session
156 * @see ProvisioningSession
157 */
158 protected ProvisioningSession getSession() {
159 return session;
160 }
161
162 private IProgressMonitor getCombinedProgressMonitor(IProgressMonitor mon1, IProgressMonitor mon2) {
163 if (mon1 == null)
164 return mon2;
165 if (mon2 == null)
166 return mon1;
167 return new DoubleProgressMonitor(mon1, mon2);
168 }
169
170 public void setAdditionalProgressMonitor(IProgressMonitor monitor) {
171 additionalMonitor = monitor;
172 }
173
174 /**
175 * Executes this job. Returns the result of the execution.
176 * This method is overridden to look for a wrapped progress monitor for
177 * reporting progress.
178 *
179 * @noreference This method is not intended to be referenced by clients.
180 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
181 *
182 */
183 @Override
184 public final IStatus run(IProgressMonitor monitor) {
185 IProgressMonitor wrappedMonitor = getCombinedProgressMonitor(monitor, additionalMonitor);
186 IStatus status = Status.OK_STATUS;
187 try {
188 status = runModal(wrappedMonitor);
189 } catch (OperationCanceledException e) {
190 status = Status.CANCEL_STATUS;
191 }
192 return status;
193 }
194
195 /**
196 * Perform the specific work involved in running this job in
197 * the current thread. This method can be called directly by
198 * clients, or in the course of running the job in the
199 * background.
200 *
201 * @param monitor
202 * the progress monitor to use for the operation
203 *
204 * @return a status indicating the result of the operation.
205 *
206 */
207 public abstract IStatus runModal(IProgressMonitor monitor);
208
209 /**
210 * Return the restart policy that is appropriate for this job.
211 *
212 * @return a constant indicating the restart policy
213 *
214 * @see #RESTART_NONE
215 * @see #RESTART_ONLY
216 * @see #RESTART_OR_APPLY
217 */
218 public int getRestartPolicy() {
219 return RESTART_NONE;
220 }
221
222 /**
223 * Return an error status that can be used to report the specified exception.
224 *
225 * @param message the message that should be used in the status
226 * @param e the exception to be reported
227 * @return a status that can be used to describe the exception
228 */
229 protected IStatus getErrorStatus(String message, ProvisionException e) {
230 if (message == null)
231 if (e == null)
232 message = NLS.bind(Messages.ProvisioningJob_GenericErrorStatusMessage, getName());
233 else
234 message = e.getLocalizedMessage();
235 return new Status(IStatus.ERROR, Constants.BUNDLE_ID, message, e);
236 }
237
238 }