Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d46953cf411cad28f3f18f84d31761a498554e37 (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
/*******************************************************************************
 *  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.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchesListener2;
import org.eclipse.debug.core.Launch;
import org.eclipse.debug.core.model.IProcess;
import org.eclipse.debug.internal.core.DebugCoreMessages;

/**
 * A specialization of launch to track sub-launches life-cycle, also terminates
 * itself when all sub-launches are terminated
 *
 * @since 3.11
 */
public class GroupLaunch extends Launch implements ILaunchesListener2 {

	/**
	 * Whether this process has been terminated
	 */
	private boolean fTerminated;

	/**
	 * Keeps track of whether launching has been finished
	 */
	private boolean fLaunched = false;

	/**
	 * A map of all our sub-launches and the current processes that belong to
	 * each one.
	 */
	private Map<ILaunch, IProcess[]> subLaunches = new HashMap<ILaunch, IProcess[]>();

	public GroupLaunch(ILaunchConfiguration launchConfiguration, String mode) {
		super(launchConfiguration, mode, null);
		getLaunchManager().addLaunchListener((ILaunchesListener2) this);
	}

	public void markLaunched() {
		fLaunched = true;
	}

	/**
	 * Associate the launch
	 *
	 * @param subLaunch
	 */
	public void addSubLaunch(ILaunch subLaunch) {
		synchronized (subLaunches) {
			subLaunches.put(subLaunch, new IProcess[] {});
		}
	}

	private boolean isChild(ILaunch launch) {
		synchronized (subLaunches) {
			for (ILaunch subLaunch : subLaunches.keySet()) {
				if (subLaunch == launch) {
					return true;
				}
			}
			return false;
		}
	}

	/**
	 * Override default behavior by querying all sub-launches to see if they are
	 * terminated
	 *
	 * @see org.eclipse.debug.core.Launch#isTerminated()
	 */
	@Override
	public boolean isTerminated() {
		if (fTerminated) {
			return true;
		}

		synchronized (subLaunches) {
			if (subLaunches.size() == 0) {
				return fLaunched; // in case we're done launching and there is
									// nobody -> terminated
			}

			for (ILaunch launch : subLaunches.keySet()) {
				if (!launch.isTerminated()) {
					return false;
				}
			}
		}
		return fLaunched; // we're done only if we're already done launching.
							// this is required for the WAIT_FOR_TERMINATION
							// mode.
	}

	/**
	 * Override default behavior by querying all sub-launches if they can be
	 * terminated
	 *
	 * @see org.eclipse.debug.core.Launch#canTerminate()
	 */
	@Override
	public boolean canTerminate() {
		synchronized (subLaunches) {
			if (subLaunches.size() == 0) {
				return false;
			}

			for (ILaunch launch : subLaunches.keySet()) {
				if (launch.canTerminate()) {
					return true;
				}
			}
			return false;
		}
	}

	/**
	 * Override default behavior by terminating all sub-launches
	 *
	 * @see org.eclipse.debug.core.Launch#terminate()
	 */
	@Override
	public void terminate() throws DebugException {
		MultiStatus status = new MultiStatus(DebugPlugin.getUniqueIdentifier(), DebugException.REQUEST_FAILED, DebugCoreMessages.Launch_terminate_failed, null);

		// somebody want's to explicitly kill the whole group, which should
		// immediately terminate and stop launching. So allow termination of the
		// group when children disappear even if launching has not finished yet.
		markLaunched();

		synchronized (subLaunches) {
			for (ILaunch launch : subLaunches.keySet()) {
				if (launch.canTerminate()) {
					try {
						launch.terminate();
					} catch (DebugException e) {
						status.merge(e.getStatus());
					}
				}
			}
		}

		if (status.isOK()) {
			return;
		}

		IStatus[] children = status.getChildren();
		if (children.length == 1) {
			throw new DebugException(children[0]);
		}

		throw new DebugException(status);
	}

	/**
	 * Handle terminated sub-launch
	 *
	 * @param launch
	 */
	private void launchTerminated(ILaunch launch) {
		if (this == launch) {
			return;
		}

		synchronized (subLaunches) {
			// Remove sub launch, keeping the processes of the terminated launch
			// to show the association and to keep the console content
			// accessible
			if (subLaunches.remove(launch) != null) {
				// terminate ourselves if this is the last sub launch
				if (subLaunches.size() == 0 && fLaunched) {
					fTerminated = true;
					fireTerminate();
				}
			}
		}
	}

	@Override
	public void launchChanged(ILaunch launch) {
		if (this == launch) {
			return;
		}

		synchronized (subLaunches) {
			// add/remove processes
			if (isChild(launch)) {
				// Remove old processes
				IProcess[] oldProcesses = subLaunches.get(launch);
				IProcess[] newProcesses = launch.getProcesses();

				// avoid notifications when processes have not changed.
				if (!Arrays.equals(oldProcesses, newProcesses)) {
					for (IProcess oldProcess : oldProcesses) {
						removeProcess(oldProcess);
					}

					// Add new processes
					for (IProcess newProcess : newProcesses) {
						addProcess(newProcess);
					}

					// Replace the processes of the changed launch
					subLaunches.put(launch, newProcesses);
				}
			}
		}
	}

	@Override
	public void launchRemoved(ILaunch launch) {
		if (this == launch) {
			super.launchRemoved(launch);
			// Remove the processes we got from the sub-launches from this
			// launch
			IProcess[] processes = getProcesses();
			for (IProcess process : processes) {
				removeProcess(process);
			}

			getLaunchManager().removeLaunchListener((ILaunchesListener2) this);
		}
	}

	@Override
	public void launchesTerminated(ILaunch[] launches) {
		for (ILaunch launch : launches) {
			launchTerminated(launch);
		}
	}

	@Override
	public void launchesAdded(ILaunch[] launches) {
		for (ILaunch launch : launches) {
			launchAdded(launch);
		}
	}

	@Override
	public void launchesChanged(ILaunch[] launches) {
		for (ILaunch launch : launches) {
			launchChanged(launch);
		}
	}

	@Override
	public void launchesRemoved(ILaunch[] launches) {
		for (ILaunch launch : launches) {
			launchRemoved(launch);
		}
	}
}

Back to the top