Skip to main content
summaryrefslogtreecommitdiffstats
blob: 505e68db7481c16e928922b4bda34c31b2466f64 (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
package org.eclipse.debug.core;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */

import java.util.*;

import org.eclipse.core.runtime.*;
import org.eclipse.debug.core.model.*;
import org.eclipse.debug.internal.core.DebugCoreMessages;

/**
 * An implementation of <code>ILaunch</code>.
 * <p>
 * Clients may instantiate this class. Clients may subclass this class.
 * All of the methods in this class that are part of the launcher interface are
 * final. Clients that subclass this class are not intended to change the behavior
 * or implementation of the provided methods. Subclassing is only intended
 * to add additional information to a specific launch. For example, a client that
 * implements a launch object representing a Java launch might store a classpath
 * with the launch.
 * </p>
 * <p>
 * <b>Note:</b> This class/interface is part of an interim API that is still under development and expected to 
 * change significantly before reaching stability. It is being made available at this early stage to solicit feedback 
 * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
 * (repeatedly) as the API evolves.
 * </p>
 * @see ILaunch
 * @see ILaunchManager
 */

public class Launch extends PlatformObject implements ILaunch {
	
	/**
	 * Shared, immutable empty collection used for efficiency.
	 */
	private static List fgEmptyChildren= Collections.EMPTY_LIST;

	/**
	 * The target being debugged, or <code>null</code> if
	 * there is no debug target.
	 */
	private IDebugTarget fTarget= null;

	/**
	 * The element that was launched.
	 */
	private Object fElement= null;

	/**
	 * The launcher that was used.
	 */
	private ILauncher fLauncher= null;

	/**
	 * The system processes resulting from the launch.
	 */
	private IProcess[] fProcesses= null;

	/**
	 * The source locator to use in the debug session
	 * or <code>null</code> if not available.
	 */
	private ISourceLocator fLocator= null;

	/**
	 * The mode this launch was launched in.
	 */
	 private String fMode;
	 
	/**
	 * Collection of children.
	 */
	private List fChildren= null;
	
	/**
	 * Constructs a launch based on the specified attributes. A launch must
	 * have at least one of a process or debug target.
	 *
	 * @param launcher the launcher that created this launch
	 * @param mode the mode of this launch - run or debug
	 * @param launchedElement the element that was launched
	 * @param locator the source locator to use for this debug session, or
	 * 	<code>null</code> if not supported
	 * @param processes the processes created by this launch, empty
	 *    or <code>null</code> if none
	 * @param target the debug target created by this launch, or <code>null</code>
	 *	if none 
	 */
	public Launch(ILauncher launcher, String mode, Object launchedElement, ISourceLocator locator, IProcess[] processes, IDebugTarget target) {
		super();
		fLauncher= launcher;			
		fElement= launchedElement;
		fLocator= locator;
		if (processes == null) {
			fProcesses= new IProcess[0];
		} else {
			fProcesses= processes;
		}
		fTarget= target;
		fMode= mode;
		initialize();
	}
	
	/**
	 * @see ITerminate
	 */
	public final boolean canTerminate() {
		return !isTerminated();
	}

	/**
	 * @see ILaunch
	 */
	public final Object[] getChildren() {
		return fChildren.toArray();
	}

	/**
	 * @see ILaunch
	 */
	public final IDebugTarget getDebugTarget() {
		return fTarget;
	}

	/**
	 * @see ILaunch
	 */
	public final Object getElement() {
		return fElement;
	}

	/**
	 * @see ILaunch
	 */
	public final ILauncher getLauncher() {
		return fLauncher;
	}

	/**
	 * @see ILaunch
	 */
	public final IProcess[] getProcesses() {
		return fProcesses;
	}

	/**
	 * @see ILaunch
	 */
	public final ISourceLocator getSourceLocator() {
		return fLocator;
	}

	/**
	 * Build my children collection.
	 */
	private final void initialize() {

		IProcess[] ps= getProcesses();
		if (ps != null) {
			fChildren= new ArrayList(ps.length + 1);
			for (int i= 0; i < ps.length; i++) {
				fChildren.add(ps[i]);
			}
		}
		if (getDebugTarget() != null) {
			if (fChildren == null) {
				fChildren= new ArrayList(1);
			}
			fChildren.add(getDebugTarget());
		}
		if (fChildren == null) {
			fChildren= fgEmptyChildren;
		}
	}

	/**
	 * @see ITerminate
	 */
	public final boolean isTerminated() {
		Iterator children= fChildren.iterator();
		while (children.hasNext()) {
			ITerminate t= (ITerminate) children.next();
			if (!t.isTerminated()) {
				if (t instanceof IDisconnect) {
					IDisconnect d= (IDisconnect)t;
					if (!d.isDisconnected()) {
						return false;
					}
				} else {
					return false;
				}
			}

		}
		return true;
	}

	/**
	 * @see ITerminate
	 */
	public final void terminate() throws DebugException {
		MultiStatus status= 
			new MultiStatus(DebugPlugin.getDefault().getDescriptor().getUniqueIdentifier(), IDebugStatusConstants.REQUEST_FAILED, DebugCoreMessages.getString("Launch.terminate_failed"), null); //$NON-NLS-1$
		IProcess[] ps= getProcesses();
		
		// terminate the system processes
		if (ps != null) {
			for (int i = 0; i < ps.length; i++) {
				IProcess process = ps[i];
				if (process.canTerminate()) {
					try {
						process.terminate();
					} catch (DebugException e) {
						status.merge(e.getStatus());
					}
				}
			}
		}
		
		// disconnect debug target if it is still connected
		IDebugTarget target= getDebugTarget();
		if (target != null) {
			if (target.canTerminate()) {
				try {
					target.terminate();
				} catch (DebugException e) {
					status.merge(e.getStatus());
				}
			} else {
				if (target.canDisconnect()) {
					try {
						target.disconnect();
					} catch (DebugException de) {
						status.merge(de.getStatus());
					}
				}
			}
		}
		if (status.isOK())
			return;
		IStatus[] children= status.getChildren();
		if (children.length == 1) {
			throw new DebugException(children[0]);
		} else {
			throw new DebugException(status);
		}
	}

	/**
	 * @see ILaunch
	 */
	public final String getLaunchMode() {
		return fMode;
	}
}


Back to the top