KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > core > LaunchConfigurationType


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.core;
12
13  
14 import java.util.Collections JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Hashtable JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.eclipse.core.resources.IContainer;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IConfigurationElement;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.PlatformObject;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.debug.core.DebugPlugin;
30 import org.eclipse.debug.core.ILaunchConfiguration;
31 import org.eclipse.debug.core.ILaunchConfigurationMigrationDelegate;
32 import org.eclipse.debug.core.ILaunchConfigurationType;
33 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
34 import org.eclipse.debug.core.ILaunchDelegate;
35 import org.eclipse.debug.core.ILaunchManager;
36 import org.eclipse.debug.core.ILaunchMode;
37 import org.eclipse.debug.core.model.ILaunchConfigurationDelegate;
38 import org.eclipse.debug.core.sourcelookup.ISourcePathComputer;
39
40 import com.ibm.icu.text.MessageFormat;
41
42 /**
43  * A launch configuration type wrappers a configuration
44  * element for a <code>launchConfigurationType</code>
45  * extension.
46  */

47 public class LaunchConfigurationType extends PlatformObject implements ILaunchConfigurationType {
48     
49     /**
50      * The configuration element of the extension.
51      */

52     private IConfigurationElement fElement;
53     
54     /**
55      * a listing of modes contributed to this launch configuration type
56      *
57      * @since 3.3
58      */

59     private Set JavaDoc fModes = null;
60     
61     /**
62      * A set of sets containing all of the supported mode combinations of this type
63      *
64      * @since 3.3
65      */

66     private Set JavaDoc fModeCombinations = null;
67     
68     /**
69      * the default source path computer for this config type
70      *
71      * @since 3.3
72      */

73     private ISourcePathComputer fSourcePathComputer = null;
74     
75     /**
76      * Cache for the migration delegate
77      *
78      * @since 3.3
79      */

80     private ILaunchConfigurationMigrationDelegate fMigrationDelegate = null;
81     
82     /**
83      * The source locator id for this config type
84      */

85     private String JavaDoc fSourceLocator = null;
86     
87     /**
88      * The delegates for launch configurations of this type.
89      * Delegates are instantiated lazily as required. There may
90      * be different delegates for different modes (since 3.0).
91      * Map of modes (Set of modes) to list of delegates
92      */

93     private Map JavaDoc fDelegates = null;
94     
95     /**
96      * The source provider cache entry
97      */

98     private LaunchDelegate fSourceProvider = null;
99     
100     /**
101      * A map of preferred launch delegates for mode combinations
102      *
103      * @since 3.3
104      */

105     private Map JavaDoc fPreferredDelegates = null;
106     
107     /**
108      * Constructs a new launch configuration type on the
109      * given configuration element.
110      *
111      * @param element configuration element
112      */

113     protected LaunchConfigurationType(IConfigurationElement element) {
114         fElement = element;
115         initializePreferredDelegates();
116     }
117     
118     /* (non-Javadoc)
119      * @see org.eclipse.debug.core.ILaunchConfigurationType#getAttribute(java.lang.String)
120      */

121     public String JavaDoc getAttribute(String JavaDoc attributeName) {
122         return fElement.getAttribute(attributeName);
123     }
124
125     /* (non-Javadoc)
126      * @see org.eclipse.debug.core.ILaunchConfigurationType#getCategory()
127      */

128     public String JavaDoc getCategory() {
129         return fElement.getAttribute(IConfigurationElementConstants.CATEGORY);
130     }
131
132     /* (non-Javadoc)
133      * @see org.eclipse.debug.core.ILaunchConfigurationType#getDelegate()
134      */

135     public ILaunchConfigurationDelegate getDelegate() throws CoreException {
136         return getDelegate(ILaunchManager.RUN_MODE);
137     }
138
139     /* (non-Javadoc)
140      * @see org.eclipse.debug.core.ILaunchConfigurationType#getDelegate(java.lang.String)
141      */

142     public ILaunchConfigurationDelegate getDelegate(String JavaDoc mode) throws CoreException {
143         Set JavaDoc modes = new HashSet JavaDoc();
144         modes.add(mode);
145         ILaunchDelegate[] delegates = getDelegates(modes);
146         if (delegates.length > 0) {
147             return delegates[0].getDelegate();
148         }
149         IStatus status = null;
150         ILaunchMode launchMode = DebugPlugin.getDefault().getLaunchManager().getLaunchMode(mode);
151         if (launchMode == null) {
152             status = new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(),
153                     MessageFormat.format(DebugCoreMessages.LaunchConfigurationType_7,
154                             new String JavaDoc[]{mode}));
155         } else {
156             status = new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(),
157                 MessageFormat.format(DebugCoreMessages.LaunchConfigurationType_7,
158                         new String JavaDoc[]{((LaunchManager)DebugPlugin.getDefault().getLaunchManager()).getLaunchModeName(mode)}));
159         }
160         throw new CoreException(status);
161     }
162     
163     /* (non-Javadoc)
164      * @see org.eclipse.debug.core.ILaunchConfigurationType#getDelegates(java.util.Set)
165      */

166     public ILaunchDelegate[] getDelegates(Set JavaDoc modes) throws CoreException {
167         initializeDelegates();
168         Set JavaDoc delegates = (Set JavaDoc) fDelegates.get(modes);
169         if (delegates == null) {
170             delegates = Collections.EMPTY_SET;
171         }
172         return (ILaunchDelegate[]) delegates.toArray(new ILaunchDelegate[delegates.size()]);
173     }
174     
175     /* (non-Javadoc)
176      * @see org.eclipse.debug.core.ILaunchConfigurationType#setPreferredDelegate(java.util.Set, org.eclipse.debug.core.ILaunchDelegate)
177      */

178     public void setPreferredDelegate(Set JavaDoc modes, ILaunchDelegate delegate) {
179         if(fPreferredDelegates == null) {
180             fPreferredDelegates = new HashMap JavaDoc();
181         }
182         if (delegate == null) {
183             fPreferredDelegates.remove(modes);
184         } else {
185             fPreferredDelegates.put(modes, delegate);
186         }
187     }
188     
189     /**
190      * @see org.eclipse.debug.core.ILaunchConfigurationType#getPreferredDelegate(java.util.Set)
191      */

192     public ILaunchDelegate getPreferredDelegate(Set JavaDoc modes) {
193         return (ILaunchDelegate) fPreferredDelegates.get(modes);
194     }
195     
196     /**
197      * Internal use method to allow access to the listing of preferred delegates. Delegates are stored in the map by their mode set combinations.
198      * <p>
199      * preferred delegates are stored as:
200      * <pre>
201      * Map&lt;modeset, delegate&gt;
202      * </pre>
203      * </p>
204      * @return the <code>java.util.Map</code> of preferred delegates or an empty <code>java.util.Map</code> if no preferred delegates are specified, never <code>null</code>
205      *
206      * @since 3.3
207      */

208     public Map JavaDoc getPreferredDelegates() {
209         return fPreferredDelegates;
210     }
211     
212     /**
213      * This method is used to initialize the listing of preferred launch delegates for this type
214      *
215      * <p>
216      * Undecided if this code should live in the launch manager and have it load a listing of all preferred launch
217      * delegates that each config type could then query as needed when looking for their preferred delegate.
218      * Seems like it would be alot less work...
219      * </p>
220      * @since 3.3
221      */

222     private synchronized void initializePreferredDelegates() {
223         if(fPreferredDelegates == null) {
224             fPreferredDelegates = new HashMap JavaDoc();
225             initializeDelegates();
226             LaunchManager lm = (LaunchManager) DebugPlugin.getDefault().getLaunchManager();
227             ILaunchDelegate delegate = null;
228             Set JavaDoc modes = null;
229             for(Iterator JavaDoc iter = fDelegates.keySet().iterator(); iter.hasNext();) {
230                 modes = (Set JavaDoc) iter.next();
231                 delegate = lm.getPreferredDelegate(getIdentifier(), modes);
232                 if(delegate != null) {
233                     fPreferredDelegates.put(modes, delegate);
234                 }
235             }
236         }
237     }
238     
239     /**
240      * Initializes the listing of launch delegates for this type
241      */

242     private synchronized void initializeDelegates() {
243         if (fDelegates == null) {
244             // initialize delegate
245
fDelegates = new Hashtable JavaDoc();
246             LaunchDelegate[] launchDelegates = getLaunchDelegateExtensions();
247             LaunchDelegate delegate = null;
248             List JavaDoc modelist = null;
249             Set JavaDoc modes = null, tmp = null;
250             for (int i = 0; i < launchDelegates.length; i++) {
251                 delegate = launchDelegates[i];
252                 modelist = delegate.getModes();
253                 for(int j = 0; j < modelist.size(); j++) {
254                     //cache the delegate based on its set of modes and delegate
255
modes = (Set JavaDoc) modelist.get(j);
256                     tmp = (Set JavaDoc) fDelegates.get(modes);
257                     if (tmp == null) {
258                         tmp = new HashSet JavaDoc();
259                         fDelegates.put(modes, tmp);
260                     }
261                     tmp.add(delegate);
262                 }
263             }
264         }
265     }
266     
267     /**
268      * Returns all launch delegate extensions registered for this configuration type.
269      *
270      * @return all launch delegate extensions
271      */

272     private LaunchDelegate[] getLaunchDelegateExtensions() {
273         return ((LaunchManager) DebugPlugin.getDefault().getLaunchManager()).getLaunchDelegates(getIdentifier());
274     }
275
276     /* (non-Javadoc)
277      * @see org.eclipse.debug.core.ILaunchConfigurationType#getIdentifier()
278      */

279     public String JavaDoc getIdentifier() {
280         return fElement.getAttribute(IConfigurationElementConstants.ID);
281     }
282
283     /* (non-Javadoc)
284      * @see org.eclipse.debug.core.ILaunchConfigurationType#getName()
285      */

286     public String JavaDoc getName() {
287         return fElement.getAttribute(IConfigurationElementConstants.NAME);
288     }
289
290     /* (non-Javadoc)
291      * @see org.eclipse.debug.core.ILaunchConfigurationType#getPluginId()
292      */

293     public String JavaDoc getPluginIdentifier() {
294         return fElement.getContributor().getName();
295     }
296     
297     /* (non-Javadoc)
298      * @see org.eclipse.debug.core.ILaunchConfigurationType#getSourceLocatorId()
299      */

300     public String JavaDoc getSourceLocatorId() {
301         if(fSourceLocator == null) {
302             fSourceLocator = getAttribute(IConfigurationElementConstants.SOURCE_LOCATOR);
303             //see if the cached source provider knows about it
304
if(fSourceProvider != null) {
305                 fSourceLocator = fSourceProvider.getSourceLocatorId();
306             }
307             //if not provided check all the applicable delegates for one and record the delegate if found,
308
//so it can be reused to try and find the source path computer
309
if(fSourceLocator == null) {
310                 LaunchDelegate[] delegates = getLaunchDelegateExtensions();
311                 for(int i = 0; i < delegates.length; i++) {
312                     fSourceLocator = delegates[i].getSourceLocatorId();
313                     if(fSourceLocator != null) {
314                         fSourceProvider = delegates[i];
315                         return fSourceLocator;
316                     }
317                 }
318                 fSourceProvider = null;
319             }
320         }
321         return fSourceLocator;
322     }
323
324     /* (non-Javadoc)
325      * @see org.eclipse.debug.core.ILaunchConfigurationType#getSourcePathComputer()
326      */

327     public ISourcePathComputer getSourcePathComputer() {
328         if(fSourcePathComputer == null) {
329             //get the id
330
String JavaDoc id = fElement.getAttribute(IConfigurationElementConstants.SOURCE_PATH_COMPUTER);
331             //ask if the source provider knows about it
332
if(fSourceProvider != null) {
333                 id = fSourceProvider.getSourcePathComputerId();
334             }
335             if(id != null) {
336                 fSourcePathComputer = DebugPlugin.getDefault().getLaunchManager().getSourcePathComputer(id);
337             }
338             else {
339             //if not provided check all the applicable delegates for one and record the delegate if found,
340
//so it can be reused to try and find the source path computer
341
LaunchDelegate[] delegates = getLaunchDelegateExtensions();
342                 for(int i = 0; i < delegates.length; i++) {
343                     id = delegates[i].getSourcePathComputerId();
344                     if(id != null) {
345                         fSourceProvider = delegates[i];
346                         fSourcePathComputer = DebugPlugin.getDefault().getLaunchManager().getSourcePathComputer(id);
347                         if(fSourcePathComputer != null) {
348                             return fSourcePathComputer;
349                         }
350                     }
351                 }
352                 fSourceProvider = null;
353             }
354             
355         }
356         return fSourcePathComputer;
357     }
358
359     /* (non-Javadoc)
360      * @see org.eclipse.debug.core.ILaunchConfigurationType#getSupportedModes()
361      */

362     public Set JavaDoc getSupportedModes() {
363         if(fModes == null) {
364             fModes = new HashSet JavaDoc();
365             LaunchDelegate[] delegates = getLaunchDelegateExtensions();
366             List JavaDoc modesets = null;
367             for(int i= 0; i < delegates.length; i++) {
368                 modesets = delegates[i].getModes();
369                 for(Iterator JavaDoc iter = modesets.iterator(); iter.hasNext();) {
370                     fModes.addAll((Set JavaDoc) iter.next());
371                 }
372             }
373         }
374         return fModes;
375     }
376     
377     /**
378      * @see org.eclipse.debug.core.ILaunchConfigurationType#getSupportedModeCombinations()
379      */

380     public Set JavaDoc getSupportedModeCombinations() {
381         if(fModeCombinations == null) {
382             initializeDelegates();
383             fModeCombinations = new HashSet JavaDoc();
384             fModeCombinations = fDelegates.keySet();
385         }
386         //return a clone or bad things happen
387
HashSet JavaDoc set = new HashSet JavaDoc();
388         for(Iterator JavaDoc iter = fModeCombinations.iterator(); iter.hasNext();) {
389             set.add(new HashSet JavaDoc((Set JavaDoc) iter.next()));
390         }
391         return set;
392     }
393     
394     /**
395      * determines if the specified candidate is suitable for migration by loading its delegate.
396      * if we initialize the delegate and it has not been provided, return false instead of failing
397      * @param candidate the candidate to inspect for migration suitability
398      * @return true if the specified launch configuration is suitable for migration, false otherwise
399      * @throws CoreException
400      *
401      * @since 3.2
402      */

403     public boolean isMigrationCandidate(ILaunchConfiguration candidate) throws CoreException {
404         initializeMigrationDelegate();
405         if(fMigrationDelegate != null) {
406             return fMigrationDelegate.isCandidate(candidate);
407         }
408         return false;
409     }
410     
411     /**
412      * This method initializes the migration delegate
413      * @throws CoreException
414      */

415     private synchronized void initializeMigrationDelegate() throws CoreException {
416         if(fElement.getAttribute(IConfigurationElementConstants.MIGRATION_DELEGATE) != null && fMigrationDelegate == null) {
417             fMigrationDelegate = (ILaunchConfigurationMigrationDelegate) fElement.createExecutableExtension(IConfigurationElementConstants.MIGRATION_DELEGATE);
418         }
419     }
420     
421     /* (non-Javadoc)
422      * @see org.eclipse.debug.core.ILaunchConfigurationType#isPublic()
423      */

424     public boolean isPublic() {
425         String JavaDoc publicString = fElement.getAttribute(IConfigurationElementConstants.PUBLIC);
426         if (publicString != null) {
427             if (publicString.equalsIgnoreCase("false")) { //$NON-NLS-1$
428
return false;
429             }
430         }
431         return true;
432     }
433
434     /**
435      * Migrates the specified launch configuration by loading its delegate.
436      * In the event the migration delegate has not been provided do nothing.
437      * @param candidate the candidate launch configuration to migrate
438      * @throws CoreException
439      *
440      * @since 3.2
441      */

442     public void migrate(ILaunchConfiguration candidate) throws CoreException {
443         initializeMigrationDelegate();
444         if(fMigrationDelegate != null) {
445             fMigrationDelegate.migrate(candidate);
446         }
447     }
448     
449     /* (non-Javadoc)
450      * @see org.eclipse.debug.core.ILaunchConfigurationType#newInstance(org.eclipse.core.resources.IContainer, java.lang.String)
451      */

452     public ILaunchConfigurationWorkingCopy newInstance(IContainer container, String JavaDoc name) {
453         return new LaunchConfigurationWorkingCopy(container, name, this);
454     }
455
456     /**
457      * @see org.eclipse.debug.core.ILaunchConfigurationType#supportsMode(java.lang.String)
458      */

459     public boolean supportsMode(String JavaDoc mode) {
460         if(fModeCombinations == null) {
461             getSupportedModeCombinations();
462         }
463         Set JavaDoc modes = null;
464         for(Iterator JavaDoc iter = fModeCombinations.iterator(); iter.hasNext();) {
465             modes = (Set JavaDoc) iter.next();
466             if(modes.size() == 1 && modes.contains(mode)) {
467                 return true;
468             }
469         }
470         return false;
471     }
472     
473     /* (non-Javadoc)
474      * @see org.eclipse.debug.core.ILaunchConfigurationType#getContributorName()
475      */

476     public String JavaDoc getContributorName() {
477         return fElement.getContributor().getName();
478     }
479
480     /* (non-Javadoc)
481      * @see org.eclipse.debug.core.ILaunchConfigurationType#supportsModeCombination(java.util.Set)
482      */

483     public boolean supportsModeCombination(Set JavaDoc modes) {
484         if(fModeCombinations == null) {
485             getSupportedModeCombinations();
486         }
487         return fModeCombinations.contains(modes);
488     }
489
490 }
491
492
Popular Tags