Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 15b204aad5688f4431b3221421417d886097eef1 (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*******************************************************************************
 * Copyright (c) 2006, 2018 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.debug.core.commands;

import java.util.LinkedHashSet;

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.debug.core.DebugPlugin;
import org.eclipse.debug.core.IRequest;
import org.eclipse.debug.internal.core.DebugOptions;

/**
 * Abstract implementation of a debug command handler. Handles {@link IDebugCommandRequest}
 * and {@link IEnabledStateRequest} updates asynchronously using jobs.
 * <p>
 * Clients may subclass this class.
 * </p>
 * @since 3.6
 */
public abstract class AbstractDebugCommand implements IDebugCommandHandler {

	/**
	 * Job to update enabled state of action.
	 */
	private class UpdateJob extends Job implements IJobChangeListener {

		/**
		 * The request to update
		 */
		private IEnabledStateRequest request;

		/**
		 * Whether this job has been run
		 */
		private boolean run = false;

		/**
		 * Creates a new job to update the specified request
		 *
		 * @param stateRequest the {@link IEnabledStateRequest}
		 */
		UpdateJob(IEnabledStateRequest stateRequest) {
			super(getEnabledStateTaskName());
			request = stateRequest;
			setSystem(true);
			setRule(getEnabledStateSchedulingRule(request));
			getJobManager().addJobChangeListener(this);
		}

		@Override
		protected IStatus run(IProgressMonitor monitor) {
			run = true;
			if (DebugOptions.DEBUG_COMMANDS) {
				DebugOptions.trace("can execute command: " + AbstractDebugCommand.this); //$NON-NLS-1$
			}
			if (monitor.isCanceled()) {
				if (DebugOptions.DEBUG_COMMANDS) {
					DebugOptions.trace(" >> *CANCELED* <<"); //$NON-NLS-1$
				}
				request.cancel();
			}
			Object[] elements = request.getElements();
			Object[] targets = new Object[elements.length];
			if (!request.isCanceled()) {
				for (int i = 0; i < elements.length; i++) {
					targets[i] = getTarget(elements[i]);
					if (targets[i] == null) {
						request.setEnabled(false);
						request.cancel();
						if (DebugOptions.DEBUG_COMMANDS) {
							DebugOptions.trace(" >> false (no adapter)"); //$NON-NLS-1$
						}
					}
				}
				if (monitor.isCanceled()) {
					request.cancel();
				}
			}
			if (!request.isCanceled()) {
				targets = coalesce(targets);
				monitor.beginTask(getEnabledStateTaskName(), targets.length);
				try {
					boolean executable = isExecutable(targets, monitor, request);
					if (DebugOptions.DEBUG_COMMANDS) {
						DebugOptions.trace(" >> " + executable); //$NON-NLS-1$
					}
					request.setEnabled(executable);
				} catch (CoreException e) {
					request.setStatus(e.getStatus());
					request.setEnabled(false);
					if (DebugOptions.DEBUG_COMMANDS) {
						DebugOptions.trace(" >> ABORTED"); //$NON-NLS-1$
						DebugOptions.trace("\t" + e.getStatus().getMessage()); //$NON-NLS-1$
					}
				}
			}
			monitor.setCanceled(request.isCanceled());
			request.done();
			monitor.done();
			return Status.OK_STATUS;
		}

		@Override
		public boolean belongsTo(Object family) {
			Object myFamily = getEnabledStateJobFamily(request);
			if (myFamily != null) {
				return myFamily.equals(family);
			}
			return false;
		}

		@Override
		public void aboutToRun(IJobChangeEvent event) {
		}

		@Override
		public void awake(IJobChangeEvent event) {
		}

		@Override
		public void done(IJobChangeEvent event) {
			if (event.getJob() == this) {
				if (!run) {
					request.cancel();
					request.done();
					if (DebugOptions.DEBUG_COMMANDS) {
						DebugOptions.trace(" >> *CANCELED* <<" + AbstractDebugCommand.this); //$NON-NLS-1$
					}
				}
				getJobManager().removeJobChangeListener(this);
			}
		}

		@Override
		public void running(IJobChangeEvent event) {
		}

		@Override
		public void scheduled(IJobChangeEvent event) {
		}

		@Override
		public void sleeping(IJobChangeEvent event) {
		}

	}

	/**
	 * Scheduling rule to serialize commands on an object
	 */
	private class SerialPerObjectRule implements ISchedulingRule {

		private Object fObject = null;

		public SerialPerObjectRule(Object lock) {
			fObject = lock;
		}

		@Override
		public boolean contains(ISchedulingRule rule) {
			return rule == this;
		}

		@Override
		public boolean isConflicting(ISchedulingRule rule) {
			if (rule instanceof SerialPerObjectRule) {
				SerialPerObjectRule vup = (SerialPerObjectRule) rule;
				return fObject == vup.fObject;
			}
			return false;
		}

	}

	@Override
	public boolean execute(final IDebugCommandRequest request) {
		Job job = new Job(getExecuteTaskName()) {
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				if (DebugOptions.DEBUG_COMMANDS) {
					DebugOptions.trace("execute: " + AbstractDebugCommand.this); //$NON-NLS-1$
				}
				Object[] elements = request.getElements();
				Object[] targets = new Object[elements.length];
				for (int i = 0; i < elements.length; i++) {
					targets[i]= getTarget(elements[i]);
				}
				targets = coalesce(targets);
				monitor.beginTask(getExecuteTaskName(), targets.length);
				try {
					doExecute(targets, monitor, request);
				} catch (CoreException e) {
					request.setStatus(e.getStatus());
					if (DebugOptions.DEBUG_COMMANDS) {
						DebugOptions.trace("\t" + e.getStatus().getMessage()); //$NON-NLS-1$
					}
				}
				request.done();
				monitor.setCanceled(request.isCanceled());
				monitor.done();
				return Status.OK_STATUS;
			}
			@Override
			public boolean belongsTo(Object family) {
				Object jobFamily = getExecuteJobFamily(request);
				if (jobFamily != null) {
					return jobFamily.equals(family);
				}
				return false;
			}
		};
		job.setSystem(true);
		job.setRule(getExecuteSchedulingRule(request));
		job.schedule();
		return isRemainEnabled(request);
	}

	/**
	 * Returns whether this command should remain enabled after starting execution of the specified request.
	 *
	 * @param request the request being executed
	 * @return whether to remain enabled while executing the request
	 */
	protected boolean isRemainEnabled(IDebugCommandRequest request) {
		return false;
	}

	@Override
	public void canExecute(final IEnabledStateRequest request) {
		Job job = new UpdateJob(request);
		job.schedule();
	}

	/**
	 * Returns the name to use for a job and progress monitor task names when performing
	 * an {@link IEnabledStateRequest}.
	 *
	 * @return task name
	 */
	protected String getEnabledStateTaskName() {
		// this is a system job name and does not need to be NLS'd
		return "Check Debug Command"; //$NON-NLS-1$
	}

	/**
	 * Returns the name to use for jobs and progress monitor task names when executing
	 * an {@link IDebugCommandRequest}.
	 *
	 * @return task name
	 */
	protected String getExecuteTaskName() {
		// this is a system job name and does not need to be NLS'd
		return "Execute Debug Command"; //$NON-NLS-1$
	}

	/**
	 * Executes this command synchronously on the specified targets, reporting progress. This method
	 * is called by a job. If an exception is thrown, the calling job will set the associated status
	 * on the request object. The calling job also calls #done() on the request object after this method
	 * is called, and sets a cancel status on the progress monitor if the request is canceled.
	 * <p>
	 * Handlers must override this method.
	 * </p>
	 * @param targets objects to perform this command on
	 * @param monitor progress monitor
	 * @param request can be used to cancel this command
	 * @exception CoreException if this handler fails to perform the request
	 */
	protected abstract void doExecute(Object[] targets, IProgressMonitor monitor, IRequest request) throws CoreException;

	/**
	 * Returns whether this command is executable on the specified targets, reporting progress. This method
	 * is called by a job. If an exception is thrown, the calling job will set the associated status
	 * on the request object and report that this command is not enabled. The calling job also calls #done()
	 * on the request object after this method is called, and sets a cancel status on the progress monitor if
	 * the request is canceled. Enabled state is set to <code>false</code> if the request is canceled.
	 * <p>
	 * Handlers must override this method.
	 * </p>
	 * @param targets objects to check command enabled state for
	 * @param monitor progress monitor
	 * @param request can be used to cancel this update request
	 * @return whether this command can be executed for the given targets
	 * @throws CoreException if a problem is encountered
	 */
	protected abstract boolean isExecutable(Object[] targets, IProgressMonitor monitor, IEnabledStateRequest request) throws CoreException;

	/**
	 * Returns the appropriate target for this command handler for the given object.
	 * This method is called to map each element in a command request to the target
	 * object that is used in {@link #doExecute(Object[], IProgressMonitor, IRequest)}
	 * and {@link #isExecutable(Object[], IProgressMonitor, IEnabledStateRequest)}.
	 * The target may be the element itself, or some other object. Allows for redirection.
	 * <p>
	 * Clients must override this method.
	 * </p>
	 * @param element element from a {@link IDebugCommandRequest}
	 * @return associated target object for execution or enabled state update. Cannot return <code>null</code>.
	 */
	protected abstract Object getTarget(Object element);

	/**
	 * Convenience method to return an adapter of the specified type for the given object or <code>null</code>
	 * if none.
	 *
	 * @param element element to retrieve adapter for
	 * @param type adapter type
	 * @return adapter or <code>null</code>
	 */
	protected Object getAdapter(Object element, Class<?> type) {
		return DebugPlugin.getAdapter(element, type);
	}

	/**
	 * Returns a scheduling rule for this command's {@link IEnabledStateRequest} update job
	 * or <code>null</code> if none. By default a rule is created to serialize
	 * jobs on the first element in the request.
	 * <p>
	 * Clients may override this method as required.
	 * </p>
	 * @param request request that a scheduling rule is required for
	 * @return scheduling rule or <code>null</code>
	 */
	protected ISchedulingRule getEnabledStateSchedulingRule(IDebugCommandRequest request) {
		return new SerialPerObjectRule(request.getElements()[0]);
	}

	/**
	 * Returns a scheduling rule for this command's {@link IDebugCommandRequest} execute job
	 * or <code>null</code> if none. By default, execution jobs have no scheduling rule.
	 * <p>
	 * Clients may override this method as required.
	 * </p>
	 * @param request request that a scheduling rule is required for
	 * @return scheduling rule or <code>null</code>
	 */
	protected ISchedulingRule getExecuteSchedulingRule(IDebugCommandRequest request) {
		return null;
	}

	/**
	 * Returns the job family for the this command's {@link IEnabledStateRequest} update job
	 * or <code>null</code> if none. The default implementation returns <code>null</code>.
	 * <p>
	 * Clients may override this method as required.
	 * </p>
	 * @param request request the job family is required for
	 * @return job family object or <code>null</code> if none
	 */
	protected Object getEnabledStateJobFamily(IDebugCommandRequest request) {
		return null;
	}

	/**
	 * Returns the job family for the this command's {@link IDebugCommandRequest} execute job
	 * or <code>null</code> if none. The default implementation returns <code>null</code>.
	 * <p>
	 * Clients may override this method as required.
	 * </p>
	 * @param request request the job family is required for
	 * @return job family object or <code>null</code> if none
	 */
	protected Object getExecuteJobFamily(IDebugCommandRequest request) {
		return null;
	}

	/**
	 * Returns an array of objects with duplicates removed, if any.
	 *
	 * @param objects array of objects
	 * @return array of object in same order with duplicates removed, if any.
	 */
	private Object[] coalesce(Object[] objects) {
		if (objects.length == 1) {
			return objects;
		} else {
			LinkedHashSet<Object> set = new LinkedHashSet<>(objects.length);
			for (int i = 0; i < objects.length; i++) {
				set.add(objects[i]);
			}
			return set.toArray();
		}
	}

}

Back to the top