Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fccd5f8c37cfe4ab150f2563c413065adc370a58 (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
/*******************************************************************************
 *  Copyright (c) 2009, 2016 QNX Software Systems 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:
 *      QNX Software Systems - initial API and implementation
 *      Freescale Semiconductor
 *      SSI Schaefer
 *******************************************************************************/
package org.eclipse.debug.internal.core.groups;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.resources.IProject;
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.SubMonitor;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.IStatusHandler;
import org.eclipse.debug.core.model.ILaunchConfigurationDelegate2;
import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
import org.eclipse.debug.internal.core.DebugCoreMessages;
import org.eclipse.debug.internal.core.IInternalDebugCoreConstants;
import org.eclipse.debug.internal.core.groups.GroupLaunchElement.GroupElementPostLaunchAction;
import org.eclipse.osgi.util.NLS;

/**
 * Group Launch delegate. Launches each configuration in the user selected mode
 *
 * @since 3.11
 */
public class GroupLaunchConfigurationDelegate extends LaunchConfigurationDelegate implements ILaunchConfigurationDelegate2 {
	public static final int CODE_GROUP_LAUNCH_START = 233;
	public static final int CODE_GROUP_LAUNCH_DONE = 234;

	private static final String NAME_PROP = "name"; //$NON-NLS-1$
	private static final String ENABLED_PROP = "enabled"; //$NON-NLS-1$
	private static final String ADOPT_PROP = "adoptIfRunning"; //$NON-NLS-1$
	private static final String MODE_PROP = "mode"; //$NON-NLS-1$
	private static final String ACTION_PROP = "action"; //$NON-NLS-1$
	private static final String ACTION_PARAM_PROP = "actionParam"; //$NON-NLS-1$
	private static final String MULTI_LAUNCH_CONSTANTS_PREFIX = "org.eclipse.debug.core.launchGroup"; //$NON-NLS-1$

	private static final String DEBUG_CORE = "org.eclipse.debug.core"; //$NON-NLS-1$

	private static final Status UNSUPPORTED_MODE = new Status(IStatus.ERROR, DEBUG_CORE, 230, IInternalDebugCoreConstants.EMPTY_STRING, null);
	private static final Status GROUP_ELEMENT_STARTED = new Status(IStatus.OK, DEBUG_CORE, 231, IInternalDebugCoreConstants.EMPTY_STRING, null);
	private static final Status GROUP_CYCLE = new Status(IStatus.ERROR, DEBUG_CORE, 232, IInternalDebugCoreConstants.EMPTY_STRING, null);

	private static final Status GROUP_LAUNCH_START = new Status(IStatus.INFO, DEBUG_CORE, CODE_GROUP_LAUNCH_START, IInternalDebugCoreConstants.EMPTY_STRING, null);
	private static final Status GROUP_LAUNCH_DONE = new Status(IStatus.INFO, DEBUG_CORE, CODE_GROUP_LAUNCH_DONE, IInternalDebugCoreConstants.EMPTY_STRING, null);

	@Override
	public ILaunch getLaunch(ILaunchConfiguration configuration, String mode) throws CoreException {
		return new GroupLaunch(configuration, mode);
	}

	@Override
	public void launch(ILaunchConfiguration groupConfig, String mode, final ILaunch groupLaunch, IProgressMonitor monitor) throws CoreException {
		final GroupLaunch group = (GroupLaunch) groupLaunch;

		IStatusHandler groupStateHandler = DebugPlugin.getDefault().getStatusHandler(GROUP_LAUNCH_START);
		groupStateHandler.handleStatus(GROUP_LAUNCH_START, group);

		try {
			SubMonitor progress = SubMonitor.convert(monitor, NLS.bind(DebugCoreMessages.GroupLaunchConfigurationDelegate_Launching, groupConfig.getName()), 1000);

			List<GroupLaunchElement> launches = createLaunchElements(groupConfig);
			for (int i = 0; i < launches.size(); ++i) {
				GroupLaunchElement le = launches.get(i);

				if (!le.enabled) {
					continue;
				}

				// find launch; if not found, skip (error?)
				final ILaunchConfiguration conf = findLaunchConfiguration(le.name);
				if (conf == null) {
					continue;
				}

				// determine mode for each launch
				final String localMode;
				if (!le.mode.equals(GroupLaunchElement.MODE_INHERIT)) {
					localMode = le.mode;
				} else {
					localMode = mode;
				}
				if (!conf.supportsMode(localMode)) {
					IStatusHandler handler = DebugPlugin.getDefault().getStatusHandler(UNSUPPORTED_MODE);
					handler.handleStatus(UNSUPPORTED_MODE, new String[] {
							conf.getName(), localMode });
					continue;
				}

				if (groupConfig.getName().equals(conf.getName())) {
					// loop detected. report as appropriate and die.
					IStatusHandler cycleHandler = DebugPlugin.getDefault().getStatusHandler(GROUP_CYCLE);
					cycleHandler.handleStatus(GROUP_CYCLE, conf.getName());
				} else {
					launchChild(progress.newChild(1000 / launches.size()), group, le, conf, localMode, (i == launches.size() - 1));
				}

				// in case the group has been terminated while waiting in the
				// post launch action.
				if (group.isTerminated()) {
					break;
				}
			}


			if (!group.hasChildren()) {
				DebugPlugin.getDefault().getLaunchManager().removeLaunch(group);
			}
		} finally {
			// safety net - launching is finished also in case of a problem.
			group.markLaunched();
			groupStateHandler.handleStatus(GROUP_LAUNCH_DONE, group);
			monitor.done();
		}
	}

	private void launchChild(SubMonitor monitor, final GroupLaunch group, GroupLaunchElement le, final ILaunchConfiguration child, final String localMode, boolean lastConfig) throws CoreException {
		final Set<ILaunch> running = le.adoptIfRunning ? findRunningLaunch(le.name) : Collections.emptySet();
		ILaunch subLaunch = running.stream().findFirst().orElse(null);
		if (subLaunch == null) {
			subLaunch = child.launch(localMode, monitor);
		}

		group.addSubLaunch(subLaunch);

		// Now that we added the launch in our list, we have already
		// received the real launchChanged event, and did not know
		// it was part of our list
		// So, fake another event now.
		group.launchChanged(subLaunch);

		// give handler a chance to perform additional actions after
		// launching each of the members.
		IStatusHandler postLaunchHandler = DebugPlugin.getDefault().getStatusHandler(GROUP_ELEMENT_STARTED);
		postLaunchHandler.handleStatus(GROUP_ELEMENT_STARTED, new ILaunch[] {
				group, subLaunch });

		// if this is the last child, mark the group as "launching finished", so
		// that from now on the last terminating child will also terminate the
		// group.
		if (lastConfig) {
			group.markLaunched();
		}

		postLaunchAction(subLaunch, le, monitor);
	}

	private void postLaunchAction(ILaunch subLaunch, GroupLaunchElement le, IProgressMonitor monitor) {
		switch (le.action) {
			case NONE:
				return;
			case WAIT_FOR_TERMINATION:
				monitor.subTask(NLS.bind(DebugCoreMessages.GroupLaunchConfigurationDelegate_Waiting_for_termination, subLaunch.getLaunchConfiguration().getName()));
				while (!subLaunch.isTerminated() && !monitor.isCanceled()) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						break;
					}
				}
				monitor.subTask(""); //$NON-NLS-1$
				break;
			case DELAY:
				Integer waitSecs = (Integer) le.actionParam;
				if (waitSecs != null) {
					monitor.subTask(NLS.bind(DebugCoreMessages.GroupLaunchConfigurationDelegate_Delaying, waitSecs.toString()));
					try {
						Thread.sleep(waitSecs * 1000); // param is milliseconds
					} catch (InterruptedException e) {
						// ok
					}
				}
				break;

			default:
				assert false : "new post launch action type is missing logic"; //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.eclipse.debug.core.model.LaunchConfigurationDelegate#buildProjects(
	 * org.eclipse.core.resources.IProject[],
	 * org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	protected void buildProjects(IProject[] projects, IProgressMonitor monitor) throws CoreException {
		// do nothing, project can be rebuild for each launch individually
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.eclipse.debug.core.model.LaunchConfigurationDelegate#buildForLaunch(
	 * org.eclipse.debug.core.ILaunchConfiguration, java.lang.String,
	 * org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public boolean buildForLaunch(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor) throws CoreException {
		// not build for this one
		return false;
	}

	protected static ILaunchConfiguration findLaunchConfiguration(String name) throws CoreException {
		ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
		ILaunchConfiguration[] launchConfigurations = launchManager.getLaunchConfigurations();
		for (int i = 0; i < launchConfigurations.length; i++) {
			ILaunchConfiguration lConf = launchConfigurations[i];
			if (lConf.getName().equals(name)) {
				return lConf;
			}
		}
		return null;
	}

	protected static Set<ILaunch> findRunningLaunch(String name) {
		Set<ILaunch> result = new HashSet<>();
		ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
		for (ILaunch l : launchManager.getLaunches()) {
			if (l.isTerminated()) {
				continue;
			}
			if (l.getLaunchConfiguration().getName().equals(name)) {
				result.add(l);
			}
		}
		return result;
	}

	/**
	 * (Re-)reads all {@link GroupLaunchElement}s from a
	 * {@link ILaunchConfiguration}s attributes.
	 *
	 * @param configuration the launch configuration; this must be of the type
	 *            handled by this delegate
	 * @return all {@link GroupLaunchElement}s found in the given
	 *         {@link ILaunchConfiguration}s attributes. Never
	 *         <code>null</code>.
	 */
	public static List<GroupLaunchElement> createLaunchElements(ILaunchConfiguration configuration) {
		List<GroupLaunchElement> result = new ArrayList<>();
		try {
			Map<?, ?> attrs = configuration.getAttributes();
			for (Iterator<?> iterator = attrs.keySet().iterator(); iterator.hasNext();) {
				String attr = (String) iterator.next();
				try {
					if (attr.startsWith(MULTI_LAUNCH_CONSTANTS_PREFIX)) {
						String prop = attr.substring(MULTI_LAUNCH_CONSTANTS_PREFIX.length() + 1);
						int k = prop.indexOf('.');
						String num = prop.substring(0, k);
						int index = Integer.parseInt(num);
						String name = prop.substring(k + 1);
						if (name.equals(NAME_PROP)) {
							GroupLaunchElement el = new GroupLaunchElement();
							el.index = index;
							el.name = (String) attrs.get(attr);

							Object actionParam = null;
							String actionStr = (String) attrs.get(getProp(index, ACTION_PROP));

							GroupElementPostLaunchAction action;
							try {
								action = GroupElementPostLaunchAction.valueOf(actionStr);
							} catch (Exception e) {
								action = GroupElementPostLaunchAction.NONE;
							}
							if (action == GroupElementPostLaunchAction.DELAY) {
								try {
									actionParam = Integer.parseInt((String) attrs.get(getProp(index, ACTION_PARAM_PROP)));
								} catch (NumberFormatException exc) {
									DebugPlugin.log(exc);
								}
							}
							el.action = action;
							el.actionParam = actionParam;
							if (attrs.containsKey(getProp(index, ADOPT_PROP))) {
								el.adoptIfRunning = (Boolean) attrs.get(getProp(index, ADOPT_PROP));
							}
							el.mode = (String) attrs.get(getProp(index, MODE_PROP));
							el.enabled = (Boolean) attrs.get(getProp(index, ENABLED_PROP));
							try {
								el.data = findLaunchConfiguration(el.name);
							} catch (Exception e) {
								el.data = null;
							}
							while (index >= result.size()) {
								result.add(null);
							}
							result.set(index, el);

						}
					}
				} catch (Exception e) {
					DebugPlugin.log(e);
				}
			}
		} catch (CoreException e) {
			DebugPlugin.log(e);
		}
		return result;
	}

	public static void storeLaunchElements(ILaunchConfigurationWorkingCopy configuration, List<GroupLaunchElement> input) {
		int i = 0;
		removeLaunchElements(configuration);
		for (GroupLaunchElement el : input) {
			if (el == null) {
				continue;
			}
			configuration.setAttribute(getProp(i, NAME_PROP), el.name);
			configuration.setAttribute(getProp(i, ACTION_PROP), el.action.toString());
			configuration.setAttribute(getProp(i, ADOPT_PROP), el.adoptIfRunning);
			// note: the saving of the action param will need to be enhanced if
			// ever an action type is introduced that uses something that can't
			// be reconstructed from its toString()
			configuration.setAttribute(getProp(i, ACTION_PARAM_PROP), el.actionParam != null ? el.actionParam.toString() : null);
			configuration.setAttribute(getProp(i, MODE_PROP), el.mode);
			configuration.setAttribute(getProp(i, ENABLED_PROP), el.enabled);
			i++;
		}
	}

	public static void removeLaunchElements(ILaunchConfigurationWorkingCopy configuration) {
		try {
			Map<?, ?> attrs = configuration.getAttributes();
			for (Iterator<?> iterator = attrs.keySet().iterator(); iterator.hasNext();) {
				String attr = (String) iterator.next();
				try {
					if (attr.startsWith(MULTI_LAUNCH_CONSTANTS_PREFIX)) {
						configuration.removeAttribute(attr);
					}
				} catch (Exception e) {
					DebugPlugin.log(e);
				}
			}
		} catch (CoreException e) {
			DebugPlugin.log(e);
		}
	}

	public static String getProp(int index, String string) {
		return MULTI_LAUNCH_CONSTANTS_PREFIX + "." + index + "." + string; //$NON-NLS-1$ //$NON-NLS-2$
	}
}

Back to the top