Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9f9abfa90783a77067907feb00ffc061acd7b10e (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*******************************************************************************
 * Copyright (c) 2013, 2014 Wind River Systems, Inc. and others. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.runtime.stepper.job;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.IJobChangeListener;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.tcf.te.runtime.interfaces.callback.ICallback;
import org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer;
import org.eclipse.tcf.te.runtime.services.ServiceManager;
import org.eclipse.tcf.te.runtime.services.interfaces.IPropertiesAccessService;
import org.eclipse.tcf.te.runtime.statushandler.StatusHandlerManager;
import org.eclipse.tcf.te.runtime.statushandler.interfaces.IStatusHandler;
import org.eclipse.tcf.te.runtime.stepper.StepperAttributeUtil;
import org.eclipse.tcf.te.runtime.stepper.interfaces.IFullQualifiedId;
import org.eclipse.tcf.te.runtime.stepper.interfaces.IStepAttributes;
import org.eclipse.tcf.te.runtime.stepper.interfaces.IStepContext;
import org.eclipse.tcf.te.runtime.stepper.interfaces.IStepper;
import org.eclipse.tcf.te.runtime.stepper.interfaces.IStepperService;
import org.eclipse.tcf.te.runtime.stepper.stepper.Stepper;

/**
 * Stepper job implementation.
 */
public final class StepperJob extends Job {

	final private IStepContext stepContext;
	final private IPropertiesContainer data;
	final private String stepGroupId;
	final protected String operation;
	private final boolean handleStatus;

	private final boolean isCancelable;

	private ICallback jobCallback = null;
	private boolean isFinished = false;
	private boolean isCanceled = false;
	private boolean statusHandled = false;

	private class NotCancelableProgressMonitor implements IProgressMonitor {

		private final IProgressMonitor monitor;

        public NotCancelableProgressMonitor(IProgressMonitor monitor) {
        	this.monitor = monitor;
        }

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

        @Override
        public void done() {
        	monitor.done();
        }

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

        @Override
        public boolean isCanceled() {
	        return false;
        }

        @Override
        public void setCanceled(boolean value) {
        	monitor.setCanceled(false);
        }

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

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

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

	private class JobChangeListener extends JobChangeAdapter {

		/**
		 * Constructor.
		 */
        public JobChangeListener() {
        }

		/* (non-Javadoc)
		 * @see org.eclipse.core.runtime.jobs.JobChangeAdapter#done(org.eclipse.core.runtime.jobs.IJobChangeEvent)
		 */
		@Override
		public void done(IJobChangeEvent event) {
			handleStatus(event.getResult());

		    removeJobChangeListener(this);
		}
	}

	/**
	 * Constructor.
	 *
	 * @param name The job name.
	 * @param stepContext The step context.
	 * @param data The stepper data.
	 * @param stepGroupId The step group id to execute.
	 * @param operation The operation to execute.
	 * @param isCancelable <code>true</code> if the job can be canceled.
	 * @param handleStatus <code>true</code> if the job should handle the status itself and return always <code>Status.OK_STATUS</code>.
	 */
    public StepperJob(String name, IStepContext stepContext, IPropertiesContainer data, String stepGroupId, String operation, boolean isCancelable, boolean handleStatus) {
		super(name);
		setPriority(Job.INTERACTIVE);

		Assert.isNotNull(stepContext);
		Assert.isNotNull(data);
		Assert.isNotNull(stepGroupId);
		Assert.isNotNull(operation);

		this.stepContext = stepContext;
		this.data = data;
		this.stepGroupId = stepGroupId;
		this.operation = operation;
		this.isCancelable = isCancelable;
		this.handleStatus = handleStatus;

		ISchedulingRule rule = null;
		IStepperService service = ServiceManager.getInstance().getService(stepContext.getContextObject(), IStepperService.class, true);
		if (service != null) {
			rule = service.getSchedulingRule(stepContext.getContextObject(), operation);
		}
		else if (stepContext.getContextObject() instanceof ISchedulingRule) {
			rule = (ISchedulingRule)stepContext.getContextObject();
		}

		setRule(rule);

		Map<String,List<Job>> jobs = getJobs(stepContext.getContextObject());
		addJob(jobs, this, operation);
		setJobs(jobs, stepContext.getContextObject());

		data.setProperty(IStepAttributes.ATTR_STEPPER_JOB, this);
		data.setProperty(IStepAttributes.ATTR_STEPPER_JOB_OPERATION, operation);
	}

    public static Map<String,List<Job>> getJobs(Object context) {
		IPropertiesAccessService service = ServiceManager.getInstance().getService(context, IPropertiesAccessService.class);
		Map<String,List<Job>> jobs = null;
		if (service == null && context instanceof IPropertiesContainer)
			jobs = (Map<String,List<Job>>)((IPropertiesContainer)context).getProperty(StepperJob.class.getName());
		else
			jobs = service != null ? (Map<String,List<Job>>)service.getProperty(context, StepperJob.class.getName()) : null;

		if (jobs == null)
			jobs = new HashMap<String, List<Job>>();

		return jobs;
    }

    public static void addJob(Map<String,List<Job>> jobs, Job job, String operation) {
		List<Job> jobsForOperation = jobs.get(operation);
		if (jobsForOperation == null) {
			jobsForOperation = new ArrayList<Job>();
			jobs.put(operation, jobsForOperation);
		}
		jobsForOperation.add(job);
    }

    public static void removeJob(Map<String,List<Job>> jobs, Job job, String operation) {
		List<Job> jobsForOperation = jobs.get(operation);
		if (jobsForOperation != null) {
			jobsForOperation.remove(job);
		}
    }

    public static void setJobs(Map<String,List<Job>> jobs, Object context) {
		IPropertiesAccessService service = ServiceManager.getInstance().getService(context, IPropertiesAccessService.class);
		if (service != null)
			service.setProperty(context, StepperJob.class.getName(), jobs);
		else if (context instanceof IPropertiesContainer)
			((IPropertiesContainer)context).setProperty(StepperJob.class.getName(), jobs);
    }

	/**
	 * Set the callback for the job.
	 * @param callback The callback.
	 */
	public final void setJobCallback(ICallback callback) {
		jobCallback = callback;
	}

	/**
	 * Return the job callback.
	 */
	public final ICallback getJobCallback() {
		return jobCallback;
	}

	/**
	 * Return <code>true</code> if thsi job is cancelable.
	 */
	public boolean isCancelable() {
		return isCancelable;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public final IStatus run(IProgressMonitor monitor) {

		if (!isCancelable) {
			monitor = new NotCancelableProgressMonitor(monitor);
		}

		IJobChangeListener listener = new JobChangeListener();
		addJobChangeListener(listener);

		// The stepper instance to be used
		IStepper stepper = new Stepper(getName()) {
			/* (non-Javadoc)
			 * @see org.eclipse.tcf.te.runtime.stepper.stepper.Stepper#onInitialize(org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer, org.eclipse.tcf.te.runtime.stepper.interfaces.IFullQualifiedId, org.eclipse.core.runtime.IProgressMonitor)
			 */
			@Override
			protected void onInitialize(IPropertiesContainer data, IFullQualifiedId fullQualifiedId, IProgressMonitor monitor) {
			    super.onInitialize(data, fullQualifiedId, monitor);
			    StepperAttributeUtil.setProperty(IStepAttributes.ATTR_STEPPER_JOB, fullQualifiedId, data, StepperJob.this);
			    StepperAttributeUtil.setProperty(IStepAttributes.ATTR_STEPPER_JOB_OPERATION, fullQualifiedId, data, operation);
			}
		};
		IStatus status = Status.OK_STATUS;

		try {
			// Initialize stepper
			stepper.initialize(stepContext, stepGroupId, data, monitor);

			// Execute stepper
			stepper.execute();
		} catch (CoreException e) {
			status = e.getStatus();
		} finally {
			// Cleanup the stepper
			stepper.cleanup();

			Map<String,List<Job>> jobs = getJobs(stepContext.getContextObject());
			removeJob(jobs, this, operation);
			setJobs(jobs, stepContext.getContextObject());
		}

		if (jobCallback != null)
			jobCallback.done(this, status);

		isFinished = true;

		if (handleStatus) {
			handleStatus(status);
		}

		return statusHandled ? Status.OK_STATUS : status;
	}

	protected void handleStatus(IStatus status) {
		if (!statusHandled && status != null && status.matches(IStatus.ERROR|IStatus.WARNING|IStatus.INFO)) {
			IStatusHandler[] handler = StatusHandlerManager.getInstance().getHandler(StepperJob.this);
			if (handler != null && handler.length > 0) {
				handler[0].handleStatus(status, null, null);
			}
		}
		statusHandled = true;
	}

	public boolean isFinished() {
		return isFinished;
	}

	public boolean isCanceled() {
		return isCanceled && isCancelable;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.jobs.Job#canceling()
	 */
	@Override
	protected void canceling() {
		if (isCancelable) {
			super.canceling();
			isCanceled = true;
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.jobs.Job#belongsTo(java.lang.Object)
	 */
	@Override
	public boolean belongsTo(Object family) {
	    return StepperJob.class.getName().equals(family);
	}
}

Back to the top