aboutsummaryrefslogtreecommitdiffstats
blob: 1ed089acf89f9c23694ef270bf01fbfbdee6da5a (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
/*******************************************************************************
 * Copyright (c) 2012 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.launch.core.bindings;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.expressions.EvaluationResult;
import org.eclipse.core.expressions.Expression;
import org.eclipse.core.expressions.ExpressionConverter;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.tcf.te.launch.core.bindings.internal.LaunchConfigTypeBinding;
import org.eclipse.tcf.te.launch.core.bindings.internal.OverwritableLaunchBinding;
import org.eclipse.tcf.te.launch.core.extensions.internal.LaunchModeVariantDelegateExtensionPointManager;
import org.eclipse.tcf.te.launch.core.lm.interfaces.ILaunchManagerDelegate;
import org.eclipse.tcf.te.launch.core.lm.internal.ExtensionPointManager;
import org.eclipse.tcf.te.launch.core.selection.interfaces.ILaunchSelection;
import org.eclipse.tcf.te.launch.core.selection.interfaces.ISelectionContext;
import org.eclipse.tcf.te.runtime.extensions.ExtensionPointComparator;
import org.eclipse.tcf.te.runtime.stepper.StepperManager;
import org.eclipse.tcf.te.runtime.stepper.interfaces.IStep;
import org.eclipse.tcf.te.runtime.stepper.interfaces.IStepGroup;
import org.eclipse.tcf.te.runtime.stepper.interfaces.IStepper;
import org.eclipse.tcf.te.runtime.stepper.interfaces.IVariantDelegate;


/**
 * Manager that controls the launch configuration type bindings.
 */
public class LaunchConfigTypeBindingsManager {
	// Map of all launch configuration type bindings by id
	private final Map<String, LaunchConfigTypeBinding> bindings = new Hashtable<String, LaunchConfigTypeBinding>();

	/*
	 * Thread save singleton instance creation.
	 */
	private static class LazyInstanceHolder {
		public static LaunchConfigTypeBindingsManager instance = new LaunchConfigTypeBindingsManager();
	}

	/**
	 * Returns the singleton instance.
	 */
	public static LaunchConfigTypeBindingsManager getInstance() {
		return LazyInstanceHolder.instance;
	}

	/**
	 * Constructor.
	 */
	LaunchConfigTypeBindingsManager() {
		// Load the launch configuration type bindings on instantiation.
		loadBindingsExtensions();
	}

	/**
	 * Get all valid launch configuration type id's for the given selection.
	 *
	 * @param selection The selection or <code>null</code>.
	 * @return The list of valid launch configuration type id's for the selection or an empty list.
	 */
	public String[] getValidLaunchConfigTypes(ILaunchSelection selection) {
		Set<String> validLaunchTypes = new HashSet<String>();
		if (selection != null && selection.getSelectedContexts() != null && selection.getSelectedContexts().length > 0) {
			for (String launchConfigTypeId : bindings.keySet()) {
				if (isValidLaunchConfigType(launchConfigTypeId, selection)) {
					validLaunchTypes.add(launchConfigTypeId);
				}
			}
		}
		return validLaunchTypes.toArray(new String[validLaunchTypes.size()]);
	}

	/**
	 * Validates the given launch selection.
	 *
	 * @param typeId The launch configuration type id. Must not be <code>null</code>.
	 * @param selection The selection. Must not be <code>null</code>.
	 */
	public boolean isValidLaunchConfigType(String typeId, ILaunchSelection selection) {
		Assert.isNotNull(typeId);
		Assert.isNotNull(selection);

		LaunchConfigTypeBinding binding = bindings.get(typeId);
		ILaunchConfigurationType launchConfigType = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationType(typeId);
		return (launchConfigType != null && launchConfigType.isPublic() &&
						(selection.getLaunchMode() == null || launchConfigType.supportsMode(selection.getLaunchMode())) &&
						binding != null && binding.validate(selection) != EvaluationResult.FALSE);
	}

	/**
	 * Validates the given launch selection.
	 *
	 * @param typeId The launch configuration type id. Must not be <code>null</code>.
	 * @param mode The launch mode or <code>null</code>.
	 * @param context The selection context. Must not be <code>null</code>.
	 */
	public boolean isValidLaunchConfigType(String typeId, String mode, ISelectionContext context) {
		Assert.isNotNull(typeId);
		Assert.isNotNull(context);

		LaunchConfigTypeBinding binding = bindings.get(typeId);
		ILaunchConfigurationType launchConfigType = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationType(typeId);
		return (launchConfigType != null && launchConfigType.isPublic() &&
						(mode == null || launchConfigType.supportsMode(mode)) &&
						binding != null && binding.validate(mode, context) != EvaluationResult.FALSE);
	}

	/**
	 * Get the registered launch manager delegate for the given launch configuration type and launch mode.
	 *
	 * @param typeId The launch configuration type id. Must not be <code>null</code>.
	 * @param mode The launch mode. Must not be <code>null</code>.
	 *
	 * @return The launch manager delegate, or a default delegate if no delegate is registered for the
	 *         given launch configuration type id and launch mode.
	 */
	public ILaunchManagerDelegate getLaunchManagerDelegate(String typeId, String mode) {
		Assert.isNotNull(typeId);
		Assert.isNotNull(mode);

		LaunchConfigTypeBinding binding = bindings.get(typeId);
		if (binding != null) {
			String id = binding.getLaunchManagerDelegate(mode);
			if (id != null) {
				return ExtensionPointManager.getInstance().getLaunchManagerDelegate(id);
			}
		}
		return ExtensionPointManager.getInstance().getDefaultLaunchManagerDelegate();
	}

	/**
	 * Get the registered launch mode variant delegate for the given launch configuration type and launch mode.
	 *
	 * @param typeId The launch configuration type id. Must not be <code>null</code>.
	 * @param mode The launch mode. Must not be <code>null</code>.
	 *
	 * @return The launch mode variant delegate, or a <code>null</code> if no delegate is registered for the
	 *         given launch configuration type id and launch mode.
	 */
	public IVariantDelegate getLaunchModeVariantDelegate(String typeId, String mode) {
		Assert.isNotNull(typeId);
		Assert.isNotNull(mode);

		LaunchConfigTypeBinding binding = bindings.get(typeId);
		if (binding != null) {
			String id = binding.getLaunchModeVariantDelegate(mode);
			if (id != null) {
				return LaunchModeVariantDelegateExtensionPointManager.getInstance().getLaunchModeVariantDelegate(id);
			}
		}
		return null;
	}

	/**
	 * Get the registered stepper for the given launch configuration type id and launch mode.
	 *
	 * @param typeId The launch configuration type id. Must not be <code>null</code>.
	 * @param mode The launch mode. Must not be <code>null</code>.

	 * @return The stepper or <code>null</code> if no stepper is registered for the
	 *         given launch configuration type id and launch mode.
	 */
	public IStepper getStepper(String typeId, String mode) {
		Assert.isNotNull(typeId);
		Assert.isNotNull(mode);

		IStepper stepper = null;

		LaunchConfigTypeBinding binding = bindings.get(typeId);
		if (binding != null) {
			String id = binding.getStepper(mode);
			if (id != null) {
				stepper = StepperManager.getInstance().getStepperExtManager().getStepper(id, false);
			}
		}

		return stepper;
	}

	/**
	 * Get the registered step group id for the given launch configuration type, launch mode and variant.
	 *
	 * @param typeId The launch configuration type id. Must not be <code>null</code>.
	 * @param mode The launch mode. Must not be <code>null</code>.
	 * @param variant The launch mode variant or <code>null</code>.
	 *
	 * @return The launch step group id or <code>null</code> if no step group is registered for the
	 *         given launch configuration type id, launch mode and variant.
	 */
	public String getStepGroupId(String typeId, String mode, String variant) {
		Assert.isNotNull(typeId);
		Assert.isNotNull(mode);

		LaunchConfigTypeBinding binding = bindings.get(typeId);
		if (binding != null) {
			return binding.getStepGroupId(mode, variant);
		}
		return null;
	}

	/**
	 * Get the registered step group identified by the specified unique id.
	 *
	 * @param id The step group id. Must not be <code>null</code>.
	 * @return The step group or <code>null</code>, if no step group is registered for the specified id.
	 */
	public IStepGroup getStepGroup(String id) {
		Assert.isNotNull(id);
		return StepperManager.getInstance().getStepGroupExtManager().getStepGroup(id, true);
	}

	/**
	 * Get the registered step identified by the specified unique id.
	 *
	 * @param id The launch step id. Must not be <code>null</code>.
	 * @return The launch step or <code>null</code> if no step is registered for the specified id.
	 */
	public IStep getStep(String id) {
		Assert.isNotNull(id);
		return StepperManager.getInstance().getStepExtManager().getStep(id, true);
	}

	/*
	 * Load and register all launch configuration type bindings.
	 */
	private void loadBindingsExtensions() {
		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint point = registry.getExtensionPoint("org.eclipse.tcf.te.launch.core.launchConfigTypeBindings"); //$NON-NLS-1$
		if (point != null) {
			IExtension[] bindings = point.getExtensions();
			Arrays.sort(bindings, new ExtensionPointComparator());
			for (IExtension binding : bindings) {
				IConfigurationElement[] elements = binding.getConfigurationElements();
				for (IConfigurationElement element : elements) {
					loadBinding(element);
				}
			}
		}
	}

	/**
	 * Load a single launch configuration type binding.
	 *
	 * @param element The configuration element. Must not be <code>null</code>.
	 */
	private void loadBinding(IConfigurationElement element) {
		Assert.isNotNull(element);

		if (!element.getName().equals("launchConfigTypeBinding")) { //$NON-NLS-1$
			return;
		}

		String launchConfigTypeId = element.getAttribute("launchConfigTypeId"); //$NON-NLS-1$
		if (!bindings.containsKey(launchConfigTypeId)) {
			bindings.put(launchConfigTypeId, new LaunchConfigTypeBinding(launchConfigTypeId));
		}
		LaunchConfigTypeBinding binding = bindings.get(launchConfigTypeId);

		IConfigurationElement[] lmDelegateBindings = element.getChildren("launchManagerDelegate"); //$NON-NLS-1$
		for (IConfigurationElement lmDelegateBinding : lmDelegateBindings) {
			String id = lmDelegateBinding.getAttribute("id"); //$NON-NLS-1$
			String overwrites = lmDelegateBinding.getAttribute("overwrites"); //$NON-NLS-1$
			String modes = lmDelegateBinding.getAttribute("modes"); //$NON-NLS-1$

			binding.addLaunchManagerDelegate(new OverwritableLaunchBinding(id, overwrites, modes));
		}

		IConfigurationElement[] launchModeVariantDelegateBindings = element.getChildren("launchModeVariantDelegate"); //$NON-NLS-1$
		for (IConfigurationElement launchModeVariantDelegateBinding : launchModeVariantDelegateBindings) {
			String id = launchModeVariantDelegateBinding.getAttribute("id"); //$NON-NLS-1$
			String overwrites = launchModeVariantDelegateBinding.getAttribute("overwrites"); //$NON-NLS-1$
			String modes = launchModeVariantDelegateBinding.getAttribute("modes"); //$NON-NLS-1$

			binding.addLaunchModeVariantDelegate(new OverwritableLaunchBinding(id, overwrites, modes));
		}

		IConfigurationElement[] stepperBindings = element.getChildren("stepper"); //$NON-NLS-1$
		for (IConfigurationElement stepperBinding : stepperBindings) {
			String id = stepperBinding.getAttribute("id"); //$NON-NLS-1$
			String overwrites = stepperBinding.getAttribute("overwrites"); //$NON-NLS-1$
			String modes = stepperBinding.getAttribute("modes"); //$NON-NLS-1$

			binding.addStepper(new OverwritableLaunchBinding(id, overwrites, modes));
		}

		IConfigurationElement[] stepGroupBindings = element.getChildren("stepGroup"); //$NON-NLS-1$
		for (IConfigurationElement stepGroupBinding : stepGroupBindings) {
			String id = stepGroupBinding.getAttribute("id"); //$NON-NLS-1$
			String overwrites = stepGroupBinding.getAttribute("overwrites"); //$NON-NLS-1$
			String modes = stepGroupBinding.getAttribute("modes"); //$NON-NLS-1$
			String variants = stepGroupBinding.getAttribute("variants"); //$NON-NLS-1$

			binding.addStepGroup(new OverwritableLaunchBinding(id, overwrites, modes, variants));
		}

		IConfigurationElement[] enablements = element.getChildren("enablement"); //$NON-NLS-1$
		for (IConfigurationElement enablement : enablements) {
			Expression expression = null;
			try {
				expression = ExpressionConverter.getDefault().perform(enablement);
			} catch (CoreException e) {
				if (Platform.inDebugMode()) {
					e.printStackTrace();
				}
			}

			if (expression != null) {
				binding.addEnablement(expression);
			}
		}
	}

}