Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 14ee002566cfed183b7d7549270d457ecea2bcbc (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
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.core;

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

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationMigrationDelegate;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.model.ILaunchConfigurationDelegate;
import org.eclipse.debug.core.sourcelookup.ISourcePathComputer;

import com.ibm.icu.text.MessageFormat;

/**
 * A launch configuration type wrappers a configuration
 * element for a <code>launchConfigurationType</code>
 * extension.
 */
public class LaunchConfigurationType extends PlatformObject implements ILaunchConfigurationType {
	
	/**
	 * The configuration element of the extension.
	 */
	private IConfigurationElement fElement;
	
	/**
	 *  a listing of modes contributed to this launch configuration type
	 *  @since 3.3
	 *  
	 *  <p>
	 * <strong>EXPERIMENTAL</strong>. This field has been added as
	 * part of a work in progress. There is no guarantee that this API will
	 * remain unchanged during the 3.3 release cycle. Please do not use this API
	 * without consulting with the Platform/Debug team.
	 * </p>
	 */
	private Set fModes = null;
	
	/**
	 * the default source path computer for this config type
	 * @since 3.3
	 * 
	 * <p>
	 * <strong>EXPERIMENTAL</strong>. This field has been added as
	 * part of a work in progress. There is no guarantee that this API will
	 * remain unchanged during the 3.3 release cycle. Please do not use this API
	 * without consulting with the Platform/Debug team.
	 * </p>
	 */
	private ISourcePathComputer fSourcePathComputer = null;
	
	/**
	 * The source locator id for this config type
	 */
	private String fSourceLocator = null;
	
	/**
	 * The delegates for launch configurations of this type.
	 * Delegates are instantiated lazily as required. There may
	 * be different delegates for different modes (since 3.0).
	 * Map of mode to delegate
	 */
	private Map fDelegates;
	
	private LaunchDelegate fSourceProvider;
	
	/**
	 * Constructs a new launch configuration type on the
	 * given configuration element.
	 * 
	 * @param element configuration element
	 */
	protected LaunchConfigurationType(IConfigurationElement element) {
		fElement = element;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchConfigurationType#getAttribute(java.lang.String)
	 */
	public String getAttribute(String attributeName) {
		return fElement.getAttribute(attributeName);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchConfigurationType#getCategory()
	 */
	public String getCategory() {
		return fElement.getAttribute(IConfigurationElementConstants.CATEGORY);
	}
	
	/**
	 * @see org.eclipse.debug.core.ILaunchConfigurationType#getDelegate()
	 */
	public ILaunchConfigurationDelegate getDelegate() throws CoreException {
		return getDelegate(ILaunchManager.RUN_MODE);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchConfigurationType#getDelegate(java.lang.String)
	 */
	public ILaunchConfigurationDelegate getDelegate(String mode) throws CoreException {
		return getDelegate(mode, Collections.EMPTY_SET); 
	}
	
	/**
	 * @see org.eclipse.debug.core.ILaunchConfigurationType#getDelegate(java.lang.String, java.lang.String[])
	 */
	public ILaunchConfigurationDelegate getDelegate(String mode, Set options) throws CoreException {
		if (!supportsMode(mode)) {
			throw new CoreException(new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), DebugPlugin.INTERNAL_ERROR, MessageFormat.format(DebugCoreMessages.LaunchConfigurationType_9, new String[] {mode, getIdentifier()}), null));  
		}
		if (fDelegates == null) {
			// initialize delegate table with base modes
			fDelegates = new Hashtable();
		}
		ILaunchConfigurationDelegate delegate = (ILaunchConfigurationDelegate)fDelegates.get(mode);
		if (delegate == null) {
			if(fModes.contains(mode)) {
				LaunchDelegate[] delegates = ((LaunchManager) DebugPlugin.getDefault().getLaunchManager()).getLaunchDelegates(getIdentifier(), mode, options);
				if(delegates.length > 0) {
					return delegates[0].getDelegate();
				}
			}
		} 
		else {
			return delegate;
		}
		throw new CoreException(new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), DebugPlugin.INTERNAL_ERROR, MessageFormat.format(DebugCoreMessages.LaunchConfigurationType_10, new String[] {getIdentifier(), mode}), null));
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchConfigurationType#getIdentifier()
	 */
	public String getIdentifier() {
		return fElement.getAttribute(IConfigurationElementConstants.ID);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchConfigurationType#getName()
	 */
	public String getName() {
		return fElement.getAttribute(IConfigurationElementConstants.NAME);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchConfigurationType#getPluginId()
	 */
	public String getPluginIdentifier() {
		return fElement.getContributor().getName();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchConfigurationType#getSourceLocatorId()
	 */
	public String getSourceLocatorId() {
		if(fSourceLocator == null) {
			fSourceLocator = getAttribute(IConfigurationElementConstants.SOURCE_LOCATOR);
			//see if the cached source provider knows about it
			if(fSourceProvider != null) {
				fSourceLocator = fSourceProvider.getSourceLocatorId();
			}
			//if not provided check all the applicable delegates for one and record the delegate if found,
			//so it can be reused to try and find the source path computer
			if(fSourceLocator == null) {
				LaunchDelegate[] delegates = ((LaunchManager)DebugPlugin.getDefault().getLaunchManager()).getLaunchDelegates(getIdentifier());
				for(int i = 0; i < delegates.length; i++) {
					fSourceLocator = delegates[i].getSourceLocatorId();
					if(fSourceLocator != null) {
						fSourceProvider = delegates[i];
						return fSourceLocator;
					}
				}
				fSourceProvider = null;
			}
		}
		return fSourceLocator;
	}	

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchConfigurationType#getSourcePathComputer()
	 */
	public ISourcePathComputer getSourcePathComputer() {
		if(fSourcePathComputer == null) {
			//get the id
			String id = fElement.getAttribute(IConfigurationElementConstants.SOURCE_PATH_COMPUTER);
			//ask if the source provider knows about it
			if(fSourceProvider != null) {
				id = fSourceProvider.getSourcePathComputerId();
			}
			if(id != null) {
				fSourcePathComputer = DebugPlugin.getDefault().getLaunchManager().getSourcePathComputer(id);
			}
			else { 
			//if not provided check all the applicable delegates for one and record the delegate if found,
			//so it can be reused to try and find the source path computer
				LaunchDelegate[] delegates = ((LaunchManager)DebugPlugin.getDefault().getLaunchManager()).getLaunchDelegates(getIdentifier());
				for(int i = 0; i < delegates.length; i++) {
					id = delegates[i].getSourcePathComputerId();
					if(id != null) {
						fSourceProvider = delegates[i];
						fSourcePathComputer = DebugPlugin.getDefault().getLaunchManager().getSourcePathComputer(id);
						if(fSourcePathComputer != null) {
							return fSourcePathComputer;
						}
					}
				}
				fSourceProvider = null;
			}
			
		}
		return fSourcePathComputer;
	}

	/**
	 * @see org.eclipse.debug.core.ILaunchConfigurationType#getSupportedModes()
	 */
	public Set getSupportedModes() {
		if(fModes == null) {
			fModes = new HashSet();
			LaunchDelegate[] delegates = ((LaunchManager) DebugPlugin.getDefault().getLaunchManager()).getLaunchDelegates();
			for(int i= 0; i < delegates.length; i++) {
				if(delegates[i].appliesTo(getIdentifier())) {
					fModes.addAll(delegates[i].getModes());
				}
			}
		}
		return fModes;
	}

	/**
	 * determines if the specified candidate is suitable for migration by loading it delegate.
	 * @param candidate the candidate to inspect for migration suitability
	 * @return true if the specified launch configuration is suitable for migration, false otherwise
	 * @throws CoreException
	 * 
	 * @since 3.2
	 */
	public boolean isMigrationCandidate(ILaunchConfiguration candidate) throws CoreException {
		if(getAttribute(IConfigurationElementConstants.MIGRATION_DELEGATE) != null) {
			if(fDelegates == null) {
				fDelegates = new Hashtable();
			}
			Object delegate = fDelegates.get(IConfigurationElementConstants.MIGRATION_DELEGATE);
			if(delegate == null) {
				delegate = fElement.createExecutableExtension(IConfigurationElementConstants.MIGRATION_DELEGATE);
				fDelegates.put(IConfigurationElementConstants.MIGRATION_DELEGATE, delegate);
			}
			if(delegate instanceof ILaunchConfigurationMigrationDelegate) {
				return ((ILaunchConfigurationMigrationDelegate)delegate).isCandidate(candidate);
			}
		}
		return false;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchConfigurationType#isPublic()
	 */
	public boolean isPublic() {
		String publicString = fElement.getAttribute(IConfigurationElementConstants.PUBLIC);
		if (publicString != null) {
			if (publicString.equalsIgnoreCase("false")) { //$NON-NLS-1$
				return false;
			}
		} 
		return true;
	}

	/**
	 * migrates the specified launch configuration by loading its delegate
	 * @param candidate the candidate launch configuration to migrate
	 * @throws CoreException
	 * 
	 * @since 3.2
	 */
	public void migrate(ILaunchConfiguration candidate) throws CoreException {
		if(getAttribute(IConfigurationElementConstants.MIGRATION_DELEGATE) != null) { 
			if(fDelegates == null) {
				fDelegates = new Hashtable();
			}
			Object delegate = fDelegates.get(IConfigurationElementConstants.MIGRATION_DELEGATE);
			if(delegate == null) {
				delegate = fElement.createExecutableExtension(IConfigurationElementConstants.MIGRATION_DELEGATE);
				fDelegates.put(IConfigurationElementConstants.MIGRATION_DELEGATE, delegate);
			}
			if(delegate instanceof ILaunchConfigurationMigrationDelegate) {
				((ILaunchConfigurationMigrationDelegate)delegate).migrate(candidate);
			}
		}
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchConfigurationType#newInstance(org.eclipse.core.resources.IContainer, java.lang.String)
	 */
	public ILaunchConfigurationWorkingCopy newInstance(IContainer container, String name) {
		return new LaunchConfigurationWorkingCopy(container, name, this);
	}

	/**
	 * @see org.eclipse.debug.core.ILaunchConfigurationType#supportsMode(java.lang.String)
	 */
	public boolean supportsMode(String mode) {
		return getSupportedModes().contains(mode);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchConfigurationType#getContributorName()
	 */
	public String getContributorName() {
		return fElement.getContributor().getName();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchConfigurationType#getImageDescriptorId()
	 */
	public String getImageDescriptorPath() {
		return fElement.getAttribute(IConfigurationElementConstants.ICON);
	}
}

Back to the top