KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Sascha Radike - bug 56642
11  *******************************************************************************/

12 package org.eclipse.debug.internal.core;
13
14  
15 import java.io.File JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.StringReader JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import javax.xml.parsers.DocumentBuilder JavaDoc;
25 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
26 import javax.xml.parsers.ParserConfigurationException JavaDoc;
27 import javax.xml.transform.TransformerException JavaDoc;
28
29 import org.eclipse.core.resources.IContainer;
30 import org.eclipse.core.resources.IFile;
31 import org.eclipse.core.resources.IResource;
32 import org.eclipse.core.resources.IWorkspaceRoot;
33 import org.eclipse.core.resources.IncrementalProjectBuilder;
34 import org.eclipse.core.resources.ResourcesPlugin;
35 import org.eclipse.core.runtime.CoreException;
36 import org.eclipse.core.runtime.IPath;
37 import org.eclipse.core.runtime.IProgressMonitor;
38 import org.eclipse.core.runtime.IStatus;
39 import org.eclipse.core.runtime.NullProgressMonitor;
40 import org.eclipse.core.runtime.Path;
41 import org.eclipse.core.runtime.PlatformObject;
42 import org.eclipse.core.runtime.Status;
43 import org.eclipse.core.runtime.SubProgressMonitor;
44 import org.eclipse.debug.core.DebugException;
45 import org.eclipse.debug.core.DebugPlugin;
46 import org.eclipse.debug.core.ILaunch;
47 import org.eclipse.debug.core.ILaunchConfiguration;
48 import org.eclipse.debug.core.ILaunchConfigurationType;
49 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
50 import org.eclipse.debug.core.ILaunchDelegate;
51 import org.eclipse.debug.core.IStatusHandler;
52 import org.eclipse.debug.core.Launch;
53 import org.eclipse.debug.core.model.ILaunchConfigurationDelegate;
54 import org.eclipse.debug.core.model.ILaunchConfigurationDelegate2;
55 import org.eclipse.debug.core.model.IPersistableSourceLocator;
56 import org.eclipse.debug.core.sourcelookup.IPersistableSourceLocator2;
57 import org.w3c.dom.Document JavaDoc;
58 import org.w3c.dom.Element JavaDoc;
59 import org.xml.sax.InputSource JavaDoc;
60 import org.xml.sax.SAXException JavaDoc;
61 import org.xml.sax.helpers.DefaultHandler JavaDoc;
62
63 import com.ibm.icu.text.MessageFormat;
64
65 /**
66  * Launch configuration handle.
67  *
68  * @see ILaunchConfiguration
69  */

70 public class LaunchConfiguration extends PlatformObject implements ILaunchConfiguration {
71     
72     /**
73      * Launch configuration attribute that specifies the resources paths mapped to it.
74      * Not all launch configurations will have a mapped resource unless migrated.
75      * Value is a list of resource paths stored as portable strings, or <code>null</code>
76      * if none.
77      *
78      * @since 3.2
79      */

80     public static final String JavaDoc ATTR_MAPPED_RESOURCE_PATHS = DebugPlugin.getUniqueIdentifier() + ".MAPPED_RESOURCE_PATHS"; //$NON-NLS-1$
81

82     /**
83      * Launch configuration attribute that specifies the resources types mapped to it.
84      * Not all launch configurations will have a mapped resource unless migrated.
85      * Value is a list of resource type integers, or <code>null</code> if none.
86      *
87      * @since 3.2
88      */

89     public static final String JavaDoc ATTR_MAPPED_RESOURCE_TYPES = DebugPlugin.getUniqueIdentifier() + ".MAPPED_RESOURCE_TYPES"; //$NON-NLS-1$
90

91     /**
92      * The launch modes set on this configuration.
93      *
94      * @since 3.3
95      */

96     public static final String JavaDoc ATTR_LAUNCH_MODES = DebugPlugin.getUniqueIdentifier() + ".LAUNCH_MODES"; //$NON-NLS-1$
97

98     /**
99      * Launch configuration attribute storing a list
100      * of preferred launchers for associated mode sets.
101      * This attribute is a list of launchers stored by mode set
102      * and relating to the id of the preferred launcher, which happens to be an <code>ILaunchDelegate</code>
103      *
104      * @since 3.3
105      */

106     public static final String JavaDoc ATTR_PREFERRED_LAUNCHERS = DebugPlugin.getUniqueIdentifier() + ".preferred_launchers"; //$NON-NLS-1$
107

108     /**
109      * Status handler to prompt in the UI thread
110      *
111      * @since 3.3
112      */

113     protected static final IStatus promptStatus = new Status(IStatus.INFO, "org.eclipse.debug.ui", 200, "", null); //$NON-NLS-1$//$NON-NLS-2$
114

115     /**
116      * Status handler to prompt the user to resolve the missing launch delegate issue
117      * @since 3.3
118      */

119     protected static final IStatus delegateNotAvailable = new Status(IStatus.INFO, "org.eclipse.debug.core", 226, "", null); //$NON-NLS-1$ //$NON-NLS-2$
120

121     /**
122      * Status handle to prompt the user to resolve duplicate launch delegates being detected
123      *
124      * @since 3.3
125      */

126     protected static final IStatus duplicateDelegates = new Status(IStatus.INFO, "org.eclipse.debug.core", 227, "", null); //$NON-NLS-1$//$NON-NLS-2$
127

128     /**
129      * Location this configuration is stored in. This
130      * is the key for a launch configuration handle.
131      */

132     private IPath fLocation;
133
134     /**
135      * Constructs a launch configuration in the given location.
136      *
137      * @param location path to where this launch configuration's
138      * underlying file is located
139      */

140     protected LaunchConfiguration(IPath location) {
141         setLocation(location);
142     }
143     
144     /**
145      * Constructs a launch configuration from the given
146      * memento.
147      *
148      * @param memento launch configuration memento
149      * @exception CoreException if the memento is invalid or
150      * an exception occurs reading the memento
151      */

152     protected LaunchConfiguration(String JavaDoc memento) throws CoreException {
153         Exception JavaDoc ex = null;
154         try {
155             Element JavaDoc root = null;
156             DocumentBuilder JavaDoc parser =
157                 DocumentBuilderFactory.newInstance().newDocumentBuilder();
158             parser.setErrorHandler(new DefaultHandler JavaDoc());
159             StringReader JavaDoc reader = new StringReader JavaDoc(memento);
160             InputSource JavaDoc source = new InputSource JavaDoc(reader);
161             root = parser.parse(source).getDocumentElement();
162             
163             String JavaDoc localString = root.getAttribute(IConfigurationElementConstants.LOCAL);
164             String JavaDoc path = root.getAttribute(IConfigurationElementConstants.PATH);
165
166             String JavaDoc message = null;
167             if (path == null) {
168                 message = DebugCoreMessages.LaunchConfiguration_Invalid_launch_configuration_memento__missing_path_attribute_3;
169             } else if (localString == null) {
170                 message = DebugCoreMessages.LaunchConfiguration_Invalid_launch_configuration_memento__missing_local_attribute_4;
171             }
172             if (message != null) {
173                 IStatus s = newStatus(message, DebugException.INTERNAL_ERROR, null);
174                 throw new CoreException(s);
175             }
176             
177             IPath location = null;
178             boolean local = (Boolean.valueOf(localString)).booleanValue();
179             if (local) {
180                 location = LaunchManager.LOCAL_LAUNCH_CONFIGURATION_CONTAINER_PATH.append(path);
181             } else {
182                 location = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(path)).getLocation();
183             }
184             setLocation(location);
185             if (location == null) {
186                 IStatus s = newStatus(MessageFormat.format(DebugCoreMessages.LaunchConfiguration_Unable_to_restore_location_for_launch_configuration_from_memento___0__1, new String JavaDoc[]{path}), DebugPlugin.INTERNAL_ERROR, null);
187                 throw new CoreException(s);
188             }
189             return;
190         } catch (ParserConfigurationException JavaDoc e) {
191             ex = e;
192         } catch (SAXException JavaDoc e) {
193             ex = e;
194         } catch (IOException JavaDoc e) {
195             ex = e;
196         }
197         IStatus s = newStatus(DebugCoreMessages.LaunchConfiguration_Exception_occurred_parsing_memento_5, DebugException.INTERNAL_ERROR, ex);
198         throw new CoreException(s);
199     }
200
201     /* (non-Javadoc)
202      * @see org.eclipse.debug.core.ILaunchConfiguration#contentsEqual(org.eclipse.debug.core.ILaunchConfiguration)
203      */

204     public boolean contentsEqual(ILaunchConfiguration object) {
205         try {
206             if (object instanceof LaunchConfiguration) {
207                 LaunchConfiguration otherConfig = (LaunchConfiguration) object;
208                 return getName().equals(otherConfig.getName())
209                      && getType().equals(otherConfig.getType())
210                      && getLocation().equals(otherConfig.getLocation())
211                      && getInfo().equals(otherConfig.getInfo());
212             }
213             return false;
214         } catch (CoreException ce) {
215             return false;
216         }
217     }
218
219     /* (non-Javadoc)
220      * @see org.eclipse.debug.core.ILaunchConfiguration#copy(java.lang.String)
221      */

222     public ILaunchConfigurationWorkingCopy copy(String JavaDoc name) throws CoreException {
223         ILaunchConfigurationWorkingCopy copy = new LaunchConfigurationWorkingCopy(this, name);
224         return copy;
225     }
226     
227     /* (non-Javadoc)
228      * @see org.eclipse.debug.core.ILaunchConfiguration#delete()
229      */

230     public void delete() throws CoreException {
231         if (exists()) {
232             if (isLocal()) {
233                 if (!(getLocation().toFile().delete())) {
234                     throw new DebugException(
235                         new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(),
236                          DebugException.REQUEST_FAILED, DebugCoreMessages.LaunchConfiguration_Failed_to_delete_launch_configuration__1, null)
237                     );
238                 }
239                 // manually update the launch manager cache since there
240
// will be no resource delta
241
getLaunchManager().launchConfigurationDeleted(this);
242             } else {
243                 // delete the resource using IFile API such that
244
// resource deltas are fired.
245
IFile file = getFile();
246                 if (file != null) {
247                     // validate edit
248
if (file.isReadOnly()) {
249                         IStatus status = ResourcesPlugin.getWorkspace().validateEdit(new IFile[] {file}, null);
250                         if (!status.isOK()) {
251                             throw new CoreException(status);
252                         }
253                     }
254                     file.delete(true, null);
255                 } else {
256                     // Error - the exists test passed, but could not locate file
257
}
258             }
259         }
260     }
261
262     /**
263      * Returns whether this configuration is equal to the
264      * given configuration. Two configurations are equal if
265      * they are stored in the same location (and neither one
266      * is a working copy).
267      *
268      * @return whether this configuration is equal to the
269      * given configuration
270      * @see Object#equals(Object)
271      */

272     public boolean equals(Object JavaDoc object) {
273         if (object instanceof ILaunchConfiguration) {
274             if (isWorkingCopy()) {
275                 return this == object;
276             }
277             ILaunchConfiguration config = (ILaunchConfiguration) object;
278             if (!config.isWorkingCopy()) {
279                 return config.getLocation().equals(getLocation());
280             }
281         }
282         return false;
283     }
284
285     /* (non-Javadoc)
286      * @see org.eclipse.debug.core.ILaunchConfiguration#exists()
287      */

288     public boolean exists() {
289         if (isLocal()) {
290             return getLocation().toFile().exists();
291         }
292
293         IFile file = getFile();
294         return file != null && file.exists();
295     }
296
297     /* (non-Javadoc)
298      * @see org.eclipse.debug.core.ILaunchConfiguration#getAttribute(java.lang.String, boolean)
299      */

300     public boolean getAttribute(String JavaDoc attributeName, boolean defaultValue) throws CoreException {
301         return getInfo().getBooleanAttribute(attributeName, defaultValue);
302     }
303
304     /* (non-Javadoc)
305      * @see org.eclipse.debug.core.ILaunchConfiguration#getAttribute(java.lang.String, int)
306      */

307     public int getAttribute(String JavaDoc attributeName, int defaultValue) throws CoreException {
308         return getInfo().getIntAttribute(attributeName, defaultValue);
309     }
310
311     /* (non-Javadoc)
312      * @see org.eclipse.debug.core.ILaunchConfiguration#getAttribute(java.lang.String, java.util.List)
313      */

314     public List JavaDoc getAttribute(String JavaDoc attributeName, List JavaDoc defaultValue) throws CoreException {
315         return getInfo().getListAttribute(attributeName, defaultValue);
316     }
317
318     /**
319      * @see org.eclipse.debug.core.ILaunchConfiguration#getAttribute(java.lang.String, java.util.Set)
320      */

321     public Set JavaDoc getAttribute(String JavaDoc attributeName, Set JavaDoc defaultValue) throws CoreException {
322         return getInfo().getSetAttribute(attributeName, defaultValue);
323     }
324     
325     /* (non-Javadoc)
326      * @see org.eclipse.debug.core.ILaunchConfiguration#getAttribute(java.lang.String, java.util.Map)
327      */

328     public Map JavaDoc getAttribute(String JavaDoc attributeName, Map JavaDoc defaultValue) throws CoreException {
329         return getInfo().getMapAttribute(attributeName, defaultValue);
330     }
331
332     /* (non-Javadoc)
333      * @see org.eclipse.debug.core.ILaunchConfiguration#getAttribute(java.lang.String, java.lang.String)
334      */

335     public String JavaDoc getAttribute(String JavaDoc attributeName, String JavaDoc defaultValue) throws CoreException {
336         return getInfo().getStringAttribute(attributeName, defaultValue);
337     }
338
339     /* (non-Javadoc)
340      * @see org.eclipse.debug.core.ILaunchConfiguration#getAttributes()
341      */

342     public Map JavaDoc getAttributes() throws CoreException {
343         LaunchConfigurationInfo info = getInfo();
344         return info.getAttributes();
345     }
346
347     /* (non-Javadoc)
348      * @see org.eclipse.debug.core.ILaunchConfiguration#getCategory()
349      */

350     public String JavaDoc getCategory() throws CoreException {
351         return getType().getCategory();
352     }
353
354     /**
355      * Returns the container this launch configuration is
356      * stored in, or <code>null</code> if this launch configuration
357      * is stored locally.
358      *
359      * @return the container this launch configuration is
360      * stored in, or <code>null</code> if this launch configuration
361      * is stored locally
362      */

363     protected IContainer getContainer() {
364         IFile file = getFile();
365         if (file != null) {
366             return file.getParent();
367         }
368         return null;
369     }
370
371     /* (non-Javadoc)
372      * @see org.eclipse.debug.core.ILaunchConfiguration#getFile()
373      */

374     public IFile getFile() {
375         if (isLocal()) {
376             return null;
377         }
378         IFile[] files= ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(getLocation());
379         if (files.length > 0) {
380             return files[0];
381         }
382         return null;
383     }
384
385     /**
386      * Returns the info object containing the attributes
387      * of this configuration
388      *
389      * @return info for this handle
390      * @exception CoreException if unable to retrieve the
391      * info object
392      */

393     protected LaunchConfigurationInfo getInfo() throws CoreException {
394         return getLaunchManager().getInfo(this);
395     }
396
397     /**
398      * Returns the last segment from the location
399      * @return the last segment from the location
400      */

401     private String JavaDoc getLastLocationSegment() {
402         String JavaDoc name = getLocation().lastSegment();
403         if (name.length() > LAUNCH_CONFIGURATION_FILE_EXTENSION.length()) {
404             name = name.substring(0, name.length() - (LAUNCH_CONFIGURATION_FILE_EXTENSION.length() + 1));
405         }
406         return name;
407     }
408     
409     /**
410      * Returns the launch manager
411      *
412      * @return launch manager
413      */

414     protected LaunchManager getLaunchManager() {
415         return (LaunchManager)DebugPlugin.getDefault().getLaunchManager();
416     }
417
418     /* (non-Javadoc)
419      * @see org.eclipse.debug.core.ILaunchConfiguration#getLocation()
420      */

421     public IPath getLocation() {
422         return fLocation;
423     }
424
425     /* (non-Javadoc)
426      * @see org.eclipse.debug.core.ILaunchConfiguration#getResource()
427      */

428     public IResource[] getMappedResources() throws CoreException {
429         List JavaDoc paths = getAttribute(ATTR_MAPPED_RESOURCE_PATHS, (List JavaDoc)null);
430         if (paths == null || paths.size() == 0) {
431             return null;
432         }
433         List JavaDoc types = getAttribute(ATTR_MAPPED_RESOURCE_TYPES, (List JavaDoc)null);
434         if (types == null || types.size() != paths.size()) {
435             throw new CoreException(newStatus(DebugCoreMessages.LaunchConfiguration_10, DebugPlugin.INTERNAL_ERROR, null));
436         }
437         ArrayList JavaDoc list = new ArrayList JavaDoc();
438         IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
439         for(int i = 0; i < paths.size(); i++) {
440             String JavaDoc pathStr = (String JavaDoc) paths.get(i);
441             String JavaDoc typeStr= (String JavaDoc) types.get(i);
442             int type = -1;
443             try {
444                 type = Integer.decode(typeStr).intValue();
445             } catch (NumberFormatException JavaDoc e) {
446                 throw new CoreException(newStatus(DebugCoreMessages.LaunchConfiguration_10, DebugPlugin.INTERNAL_ERROR, e));
447             }
448             IPath path = Path.fromPortableString(pathStr);
449             IResource res = null;
450             switch (type) {
451             case IResource.FILE:
452                 res = root.getFile(path);
453                 break;
454             case IResource.PROJECT:
455                 res = root.getProject(pathStr);
456                 break;
457             case IResource.FOLDER:
458                 res = root.getFolder(path);
459                 break;
460             case IResource.ROOT:
461                 res = root;
462                 break;
463             default:
464                 throw new CoreException(newStatus(DebugCoreMessages.LaunchConfiguration_10, DebugPlugin.INTERNAL_ERROR, null));
465             }
466             if(res != null) {
467                 list.add(res);
468             }
469         }
470         if (list.isEmpty()) {
471             return null;
472         }
473         return (IResource[])list.toArray(new IResource[list.size()]);
474     }
475
476     /* (non-Javadoc)
477      * @see org.eclipse.debug.core.ILaunchConfiguration#getMemento()
478      */

479     public String JavaDoc getMemento() throws CoreException {
480         IPath relativePath = null;
481         if (isLocal()) {
482             IPath rootPath = LaunchManager.LOCAL_LAUNCH_CONFIGURATION_CONTAINER_PATH;
483             IPath configPath = getLocation();
484             relativePath = configPath.removeFirstSegments(rootPath.segmentCount());
485             relativePath = relativePath.setDevice(null);
486         } else {
487             IFile file = getFile();
488             if (file == null) {
489                 // cannot generate memento - missing file
490
IStatus status = newStatus(MessageFormat.format(DebugCoreMessages.LaunchConfiguration_Unable_to_generate_memento_for__0___shared_file_does_not_exist__1, new String JavaDoc[]{getName()}), DebugException.INTERNAL_ERROR, null);
491                 throw new CoreException(status);
492             }
493             relativePath = file.getFullPath();
494         }
495         Exception JavaDoc e= null;
496         try {
497             Document JavaDoc doc = LaunchManager.getDocument();
498             Element JavaDoc node = doc.createElement(IConfigurationElementConstants.LAUNCH_CONFIGURATION);
499             doc.appendChild(node);
500             node.setAttribute(IConfigurationElementConstants.LOCAL, (Boolean.valueOf(isLocal())).toString());
501             node.setAttribute(IConfigurationElementConstants.PATH, relativePath.toString());
502             return LaunchManager.serializeDocument(doc);
503         } catch (IOException JavaDoc ioe) {
504             e= ioe;
505         } catch (ParserConfigurationException JavaDoc pce) {
506             e= pce;
507         } catch (TransformerException JavaDoc te) {
508             e= te;
509         }
510         IStatus status = newStatus(DebugCoreMessages.LaunchConfiguration_Exception_occurred_creating_launch_configuration_memento_9, DebugException.INTERNAL_ERROR, e);
511         throw new CoreException(status);
512     }
513
514     /* (non-Javadoc)
515      * @see org.eclipse.debug.core.ILaunchConfiguration#getName()
516      */

517     public String JavaDoc getName() {
518         return getLastLocationSegment();
519     }
520
521     public Set JavaDoc getModes() throws CoreException {
522         Set JavaDoc options = getAttribute(ATTR_LAUNCH_MODES, (Set JavaDoc)null);
523         return (options != null ? new HashSet JavaDoc(options) : new HashSet JavaDoc(0));
524     }
525     
526     /* (non-Javadoc)
527      * @see org.eclipse.debug.core.ILaunchConfiguration#getType()
528      */

529     public ILaunchConfigurationType getType() throws CoreException {
530         return getInfo().getType();
531     }
532
533     /* (non-Javadoc)
534      * @see org.eclipse.debug.core.ILaunchConfiguration#getWorkingCopy()
535      */

536     public ILaunchConfigurationWorkingCopy getWorkingCopy() throws CoreException {
537         return new LaunchConfigurationWorkingCopy(this);
538     }
539
540     /* (non-Javadoc)
541      * @see java.lang.Object#hashCode()
542      */

543     public int hashCode() {
544         return getLocation().hashCode();
545     }
546
547     /**
548      * Set the source locator to use with the launch, if specified
549      * by this configuration.
550      *
551      * @param launch the launch on which to set the source locator
552      */

553     protected void initializeSourceLocator(ILaunch launch) throws CoreException {
554         if (launch.getSourceLocator() == null) {
555             String JavaDoc type = getAttribute(ATTR_SOURCE_LOCATOR_ID, (String JavaDoc)null);
556             if (type == null) {
557                 type = getType().getSourceLocatorId();
558             }
559             if (type != null) {
560                 IPersistableSourceLocator locator = getLaunchManager().newSourceLocator(type);
561                 String JavaDoc memento = getAttribute(ATTR_SOURCE_LOCATOR_MEMENTO, (String JavaDoc)null);
562                 if (memento == null) {
563                     locator.initializeDefaults(this);
564                 } else {
565                     if(locator instanceof IPersistableSourceLocator2)
566                         ((IPersistableSourceLocator2)locator).initializeFromMemento(memento, this);
567                     else
568                         locator.initializeFromMemento(memento);
569                 }
570                 launch.setSourceLocator(locator);
571             }
572         }
573     }
574
575     /* (non-Javadoc)
576      * @see org.eclipse.debug.core.ILaunchConfiguration#isLocal()
577      */

578     public boolean isLocal() {
579         IPath localPath = LaunchManager.LOCAL_LAUNCH_CONFIGURATION_CONTAINER_PATH;
580         return localPath.isPrefixOf(getLocation());
581     }
582     
583     /* (non-Javadoc)
584      * @see org.eclipse.debug.core.ILaunchConfiguration#isMigrationCandidate()
585      */

586     public boolean isMigrationCandidate() throws CoreException {
587         return ((LaunchConfigurationType)getType()).isMigrationCandidate(this);
588     }
589
590     /* (non-Javadoc)
591      * @see org.eclipse.debug.core.ILaunchConfiguration#isWorkingCopy()
592      */

593     public boolean isWorkingCopy() {
594         return false;
595     }
596
597     /* (non-Javadoc)
598      * @see org.eclipse.debug.core.ILaunchConfiguration#launch(java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
599      */

600     public ILaunch launch(String JavaDoc mode, IProgressMonitor monitor) throws CoreException {
601         return launch(mode, monitor, false);
602     }
603
604     /* (non-Javadoc)
605      * @see org.eclipse.debug.core.ILaunchConfiguration#launch(java.lang.String, org.eclipse.core.runtime.IProgressMonitor, boolean)
606      */

607     public ILaunch launch(String JavaDoc mode, IProgressMonitor monitor, boolean build) throws CoreException {
608         return launch(mode, monitor, build, true);
609     }
610
611     /* (non-Javadoc)
612      * @see org.eclipse.debug.core.ILaunchConfiguration#launch(java.lang.String, org.eclipse.core.runtime.IProgressMonitor, boolean, boolean)
613      */

614     public ILaunch launch(String JavaDoc mode, IProgressMonitor monitor, boolean build, boolean register) throws CoreException {
615         if (monitor == null) {
616             monitor = new NullProgressMonitor();
617         }
618         /* Setup progress monitor
619          * - Prepare delegate (0)
620          * - Pre-launch check (1)
621          * - [Build before launch (7)] if build
622          * - [Incremental build before launch (3)] if build
623          * - Final launch validation (1)
624          * - Initialize source locator (1)
625          * - Launch delegate (10) */

626         if (build) {
627             monitor.beginTask("", 23); //$NON-NLS-1$
628
}
629         else {
630             monitor.beginTask("", 13); //$NON-NLS-1$
631
}
632         monitor.subTask(DebugCoreMessages.LaunchConfiguration_9);
633         try {
634             // bug 28245 - force the delegate to load in case it is interested in launch notifications
635
Set JavaDoc modes = getModes();
636             modes.add(mode);
637             ILaunchDelegate[] delegates = getType().getDelegates(modes);
638             ILaunchConfigurationDelegate delegate = null;
639             if (delegates.length == 1) {
640                 delegate = delegates[0].getDelegate();
641             } else if (delegates.length == 0) {
642                 monitor.setCanceled(true);
643                 IStatusHandler handler = DebugPlugin.getDefault().getStatusHandler(promptStatus);
644                 if (handler != null) {
645                     handler.handleStatus(delegateNotAvailable, new Object JavaDoc[] {this, mode});
646                 }
647                 IStatus status = new Status(IStatus.CANCEL, DebugPlugin.getUniqueIdentifier(), DebugPlugin.INTERNAL_ERROR, "No launch delegate found - launch canceled", null); //$NON-NLS-1$
648
throw new CoreException(status);
649             } else {
650                 ILaunchDelegate del = getPreferredDelegate(modes);
651                 if(del == null) {
652                     del = getType().getPreferredDelegate(modes);
653                 }
654                 if(del == null) {
655                     IStatusHandler handler = DebugPlugin.getDefault().getStatusHandler(promptStatus);
656                     IStatus status = null;
657                     if (handler != null) {
658                         status = (IStatus) handler.handleStatus(duplicateDelegates, new Object JavaDoc[] {this, mode});
659                     }
660                     if(status != null && status.isOK()) {
661                         del = getPreferredDelegate(modes);
662                         if(del == null) {
663                             del = getType().getPreferredDelegate(modes);
664                         }
665                         if(del != null) {
666                             delegate = del.getDelegate();
667                         }
668                         else {
669                             monitor.setCanceled(true);
670                             status = new Status(IStatus.CANCEL, DebugPlugin.getUniqueIdentifier(), DebugPlugin.INTERNAL_ERROR, "Duplicate launcher detected - launch canceled", null); //$NON-NLS-1$
671
throw new CoreException(status);
672                         }
673                     }
674                     else {
675                         monitor.setCanceled(true);
676                         status = new Status(IStatus.CANCEL, DebugPlugin.getUniqueIdentifier(), DebugPlugin.INTERNAL_ERROR, "Duplicate launcher detected - launch canceled", null); //$NON-NLS-1$
677
throw new CoreException(status);
678                     }
679                 }
680                 else {
681                     delegate = del.getDelegate();
682                 }
683             }
684             
685             ILaunchConfigurationDelegate2 delegate2 = null;
686             if (delegate instanceof ILaunchConfigurationDelegate2) {
687                 delegate2 = (ILaunchConfigurationDelegate2) delegate;
688             }
689             // allow the delegate to provide a launch implementation
690
ILaunch launch = null;
691             if (delegate2 != null) {
692                 launch = delegate2.getLaunch(this, mode);
693             }
694             if (launch == null) {
695                 launch = new Launch(this, mode, null);
696             } else {
697                 // ensure the launch mode is valid
698
if (!mode.equals(launch.getLaunchMode())) {
699                     IStatus status = new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), DebugPlugin.INTERNAL_ERROR,
700                             MessageFormat.format(DebugCoreMessages.LaunchConfiguration_13, new String JavaDoc[]{mode, launch.getLaunchMode()}), null);
701                     throw new CoreException(status);
702                 }
703             }
704             boolean captureOutput = getAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT, true);
705             if(!captureOutput) {
706                 launch.setAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT, "false"); //$NON-NLS-1$
707
} else {
708                 launch.setAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT, null);
709             }
710             String JavaDoc attribute = getAttribute(DebugPlugin.ATTR_CONSOLE_ENCODING, (String JavaDoc)null);
711             launch.setAttribute(DebugPlugin.ATTR_CONSOLE_ENCODING, attribute);
712             
713         // perform initial pre-launch sanity checks
714
monitor.subTask(DebugCoreMessages.LaunchConfiguration_8);
715             
716             if (delegate2 != null) {
717                 if (!(delegate2.preLaunchCheck(this, mode, new SubProgressMonitor(monitor, 1)))) {
718                     // canceled
719
monitor.setCanceled(true);
720                     return launch;
721                 }
722             }
723             else {
724                 monitor.worked(1); /* No pre-launch-check */
725             }
726         // preform pre-launch build
727
if (build) {
728                 IProgressMonitor buildMonitor = new SubProgressMonitor(monitor, 10, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK);
729                 buildMonitor.beginTask(DebugCoreMessages.LaunchConfiguration_7, 10);
730                 buildMonitor.subTask(DebugCoreMessages.LaunchConfiguration_6);
731                 if (delegate2 != null) {
732                     build = delegate2.buildForLaunch(this, mode, new SubProgressMonitor(buildMonitor, 7));
733                 }
734                 if (build) {
735                     buildMonitor.subTask(DebugCoreMessages.LaunchConfiguration_5);
736                     ResourcesPlugin.getWorkspace().build(IncrementalProjectBuilder.INCREMENTAL_BUILD, new SubProgressMonitor(buildMonitor, 3));
737                 }
738                 else {
739                     buildMonitor.worked(3); /* No incremental build required */
740                 }
741             }
742         // final validation
743
monitor.subTask(DebugCoreMessages.LaunchConfiguration_4);
744             if (delegate2 != null) {
745                 if (!(delegate2.finalLaunchCheck(this, mode, new SubProgressMonitor(monitor, 1)))) {
746                     // canceled
747
monitor.setCanceled(true);
748                     return launch;
749                 }
750             }
751             else {
752                 monitor.worked(1); /* No validation */
753             }
754             if (register) {
755                 getLaunchManager().addLaunch(launch);
756             }
757             
758             try {
759                 //initialize the source locator
760
monitor.subTask(DebugCoreMessages.LaunchConfiguration_3);
761                 initializeSourceLocator(launch);
762                 monitor.worked(1);
763
764                 /* Launch the delegate */
765                 monitor.subTask(DebugCoreMessages.LaunchConfiguration_2);
766                 delegate.launch(this, mode, launch, new SubProgressMonitor(monitor, 10));
767             } catch (CoreException e) {
768                 // if there was an exception, and the launch is empty, remove it
769
if (!launch.hasChildren()) {
770                     getLaunchManager().removeLaunch(launch);
771                 }
772                 monitor.setCanceled(true);
773                 throw e;
774             }
775             if (monitor.isCanceled()) {
776                 getLaunchManager().removeLaunch(launch);
777             }
778             return launch;
779         }
780         finally {
781             monitor.done();
782         }
783     }
784     
785     /* (non-Javadoc)
786      * @see org.eclipse.debug.core.ILaunchConfiguration#migrate()
787      */

788     public void migrate() throws CoreException {
789         ((LaunchConfigurationType)getType()).migrate(this);
790     }
791
792     /**
793      * Creates and returns a new error status based on
794      * the given message, code, and exception.
795      *
796      * @param message error message
797      * @param code error code
798      * @param e exception or <code>null</code>
799      * @return status
800      */

801     protected IStatus newStatus(String JavaDoc message, int code, Throwable JavaDoc e) {
802         return new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), code, message, e);
803     }
804
805     /**
806      * Sets the location of this configuration's underlying
807      * file.
808      *
809      * @param location the location of this configuration's underlying
810      * file
811      */

812     private void setLocation(IPath location) {
813         fLocation = location;
814     }
815
816     /* (non-Javadoc)
817      * @see org.eclipse.debug.core.ILaunchConfiguration#supportsMode(java.lang.String)
818      */

819     public boolean supportsMode(String JavaDoc mode) throws CoreException {
820         return getType().supportsMode(mode);
821     }
822
823     /* (non-Javadoc)
824      * @see org.eclipse.debug.core.ILaunchConfiguration#isReadOnly()
825      */

826     public boolean isReadOnly() {
827         if(!isLocal()) {
828             IFile file = getFile();
829             if(file != null) {
830                 return file.isReadOnly();
831             }
832         }
833         else {
834             File JavaDoc file = getLocation().toFile();
835             if(file != null) {
836                 return file.exists() && !file.canWrite();
837             }
838         }
839         return false;
840     }
841
842     /**
843      * @see org.eclipse.debug.core.ILaunchConfiguration#getPreferredDelegate(java.util.Set)
844      */

845     public ILaunchDelegate getPreferredDelegate(Set JavaDoc modes) throws CoreException {
846         Map JavaDoc delegates = getAttribute(LaunchConfiguration.ATTR_PREFERRED_LAUNCHERS, (Map JavaDoc)null);
847         if(delegates != null) {
848             String JavaDoc id = (String JavaDoc) delegates.get(modes.toString());
849             if(id != null) {
850                 return getLaunchManager().getLaunchDelegate(id);
851             }
852         }
853         return null;
854     }
855     
856     /**
857      * @see java.lang.Object#toString()
858      */

859     public String JavaDoc toString() {
860         return getName();
861     }
862     
863 }
864
865
Popular Tags