Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: aaae094b833cd3198cdad396422afac6124017f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*******************************************************************************
 * Copyright (c) 2007, 2017 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.operations;

import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.equinox.internal.p2.operations.Activator;
import org.eclipse.equinox.internal.p2.operations.Messages;
import org.eclipse.equinox.p2.core.ProvisionException;
import org.eclipse.osgi.util.NLS;

/**
 * Abstract class representing provisioning jobs.  Provisioning jobs
 * can be run in the background by scheduling them, or they can
 * be run by a client in a modal context.  An additional progress monitor
 * can be set into the job for progress reporting.
 * 
 * @since 2.0
 */
public abstract class ProvisioningJob extends Job {

	/**
	 * Class for multiplexing progress across multiple progress monitors.
	 */
	private static class DoubleProgressMonitor extends ProgressMonitorWrapper {

		IProgressMonitor additionalMonitor;

		protected DoubleProgressMonitor(IProgressMonitor monitor1, IProgressMonitor monitor2) {
			super(monitor1);
			additionalMonitor = monitor2;
		}

		@Override
		public void beginTask(String name, int totalWork) {
			super.beginTask(name, totalWork);
			additionalMonitor.beginTask(name, totalWork);
		}

		@Override
		public void clearBlocked() {
			super.clearBlocked();
			if (additionalMonitor instanceof IProgressMonitorWithBlocking)
				((IProgressMonitorWithBlocking) additionalMonitor).clearBlocked();
		}

		@Override
		public void done() {
			super.done();
			additionalMonitor.done();
		}

		@Override
		public void internalWorked(double work) {
			super.internalWorked(work);
			additionalMonitor.internalWorked(work);
		}

		@Override
		public boolean isCanceled() {
			if (super.isCanceled())
				return true;
			return additionalMonitor.isCanceled();
		}

		@Override
		public void setBlocked(IStatus reason) {
			super.setBlocked(reason);
			if (additionalMonitor instanceof IProgressMonitorWithBlocking)
				((IProgressMonitorWithBlocking) additionalMonitor).setBlocked(reason);
		}

		@Override
		public void setCanceled(boolean b) {
			super.setCanceled(b);
			additionalMonitor.setCanceled(b);
		}

		@Override
		public void setTaskName(String name) {
			super.setTaskName(name);
			additionalMonitor.setTaskName(name);
		}

		@Override
		public void subTask(String name) {
			super.subTask(name);
			additionalMonitor.subTask(name);
		}

		@Override
		public void worked(int work) {
			super.worked(work);
			additionalMonitor.worked(work);
		}
	}

	/**
	 * Constant which indicates that the job does not require a restart
	 * upon completion.  This constant is typically used for operations that 
	 * do not modify the running profile.
	 * 
	 * @since 2.0
	 */
	public static final int RESTART_NONE = 1;

	/**
	 * Constant which indicates that the job requires the user to either
	 * restart or apply the configuration changes in order to pick up the
	 * changes performed by the job.  This constant is typically used for
	 * operations that modify the running profile.
	 * 
	 * @since 2.0
	 */
	public static final int RESTART_OR_APPLY = 2;
	/**
	 * Constant which indicates that the job requires the user to restart
	 * in order to pick up the changes performed by the job.  This constant
	 * is typically used for operations that modify the running profile but don't 
	 * handle dynamic changes without restarting the workbench.
	 * 
	 * @since 2.0
	 */
	public static final int RESTART_ONLY = 3;

	private ProvisioningSession session;
	private IProgressMonitor additionalMonitor;

	/**
	 * Create a provisioning job with the given name that uses the
	 * provided provisioning session for retrieving any services
	 * needed.
	 * 
	 * @param name the name of the job
	 * @param session the session providing the services
	 */
	public ProvisioningJob(String name, ProvisioningSession session) {
		super(name);
		this.session = session;
	}

	/**
	 * Return the provisioning session that is used by the receiver
	 * when retrieving necessary provisioning services.
	 * 
	 * @return the session
	 * @see ProvisioningSession
	 */
	protected ProvisioningSession getSession() {
		return session;
	}

	private IProgressMonitor getCombinedProgressMonitor(IProgressMonitor mon1, IProgressMonitor mon2) {
		if (mon1 == null)
			return mon2;
		if (mon2 == null)
			return mon1;
		return new DoubleProgressMonitor(mon1, mon2);
	}

	public void setAdditionalProgressMonitor(IProgressMonitor monitor) {
		additionalMonitor = monitor;
	}

	/**
	 * Executes this job.  Returns the result of the execution.
	 * This method is overridden to look for a wrapped progress monitor for
	 * reporting progress.
	 * 
	 * @noreference This method is not intended to be referenced by clients.
	 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
	 * 
	 */
	@Override
	public final IStatus run(IProgressMonitor monitor) {
		IProgressMonitor wrappedMonitor = getCombinedProgressMonitor(monitor, additionalMonitor);
		IStatus status = Status.OK_STATUS;
		try {
			status = runModal(wrappedMonitor);
		} catch (OperationCanceledException e) {
			status = Status.CANCEL_STATUS;
		}
		return status;
	}

	/**
	 * Perform the specific work involved in running this job in
	 * the current thread.  This method can be called directly by
	 * clients, or in the course of running the job in the
	 * background.
	 * 
	 * @param monitor
	 *            the progress monitor to use for the operation
	 *            
	 * @return a status indicating the result of the operation.
	 * 
	 */
	public abstract IStatus runModal(IProgressMonitor monitor);

	/**
	 * Return the restart policy that is appropriate for this job.
	 * 
	 * @return a constant indicating the restart policy
	 * 
	 * @see #RESTART_NONE
	 * @see #RESTART_ONLY
	 * @see #RESTART_OR_APPLY
	 */
	public int getRestartPolicy() {
		return RESTART_NONE;
	}

	/**
	 * Return an error status that can be used to report the specified exception.
	 * 
	 * @param message the message that should be used in the status
	 * @param e the exception to be reported
	 * @return a status that can be used to describe the exception
	 */
	protected IStatus getErrorStatus(String message, ProvisionException e) {
		if (message == null)
			if (e == null)
				message = NLS.bind(Messages.ProvisioningJob_GenericErrorStatusMessage, getName());
			else
				message = e.getLocalizedMessage();
		return new Status(IStatus.ERROR, Activator.ID, message, e);
	}

}

Back to the top