KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > launching > AbstractJavaLaunchConfigurationDelegate


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.jdt.launching;
12 import java.io.File JavaDoc;
13 import com.ibm.icu.text.MessageFormat;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.eclipse.core.resources.IContainer;
22 import org.eclipse.core.resources.IMarker;
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.core.resources.ResourcesPlugin;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IPath;
28 import org.eclipse.core.runtime.IProgressMonitor;
29 import org.eclipse.core.runtime.IStatus;
30 import org.eclipse.core.runtime.Path;
31 import org.eclipse.core.runtime.Status;
32 import org.eclipse.core.variables.VariablesPlugin;
33 import org.eclipse.debug.core.DebugEvent;
34 import org.eclipse.debug.core.DebugPlugin;
35 import org.eclipse.debug.core.IBreakpointManager;
36 import org.eclipse.debug.core.IDebugEventSetListener;
37 import org.eclipse.debug.core.ILaunch;
38 import org.eclipse.debug.core.ILaunchConfiguration;
39 import org.eclipse.debug.core.ILaunchManager;
40 import org.eclipse.debug.core.model.IBreakpoint;
41 import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
42 import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
43 import org.eclipse.jdt.core.IJavaModelMarker;
44 import org.eclipse.jdt.core.IJavaProject;
45 import org.eclipse.jdt.core.JavaCore;
46 import org.eclipse.jdt.debug.core.IJavaDebugTarget;
47 import org.eclipse.jdt.debug.core.IJavaMethodBreakpoint;
48 import org.eclipse.jdt.debug.core.JDIDebugModel;
49 import org.eclipse.jdt.internal.launching.JRERuntimeClasspathEntryResolver;
50 import org.eclipse.jdt.internal.launching.JavaSourceLookupDirector;
51 import org.eclipse.jdt.internal.launching.LaunchingMessages;
52 import org.eclipse.jdt.internal.launching.LaunchingPlugin;
53 /**
54  * Abstract implementation of a Java launch configuration delegate. Provides
55  * convenience methods for accessing and verifying launch configuration
56  * attributes.
57  * <p>
58  * Clients implementing Java launch configuration delegates should subclass this
59  * class.
60  * </p>
61  *
62  * @since 2.0
63  */

64 public abstract class AbstractJavaLaunchConfigurationDelegate
65         extends
66             LaunchConfigurationDelegate implements IDebugEventSetListener {
67     /**
68      * A list of prerequisite projects ordered by their build order.
69      */

70     private IProject[] fOrderedProjects;
71     /**
72      * Convenience method to get the launch manager.
73      *
74      * @return the launch manager
75      */

76     protected ILaunchManager getLaunchManager() {
77         return DebugPlugin.getDefault().getLaunchManager();
78     }
79     /**
80      * Throws a core exception with an error status object built from the given
81      * message, lower level exception, and error code.
82      *
83      * @param message
84      * the status message
85      * @param exception
86      * lower level exception associated with the error, or
87      * <code>null</code> if none
88      * @param code
89      * error code
90      * @throws CoreException
91      * the "abort" core exception
92      */

93     protected void abort(String JavaDoc message, Throwable JavaDoc exception, int code)
94             throws CoreException {
95         throw new CoreException(new Status(IStatus.ERROR, LaunchingPlugin
96                 .getUniqueIdentifier(), code, message, exception));
97     }
98     /**
99      * Returns the VM install specified by the given launch configuration, or
100      * <code>null</code> if none.
101      *
102      * @param configuration
103      * launch configuration
104      * @return the VM install specified by the given launch configuration, or
105      * <code>null</code> if none
106      * @exception CoreException
107      * if unable to retrieve the attribute
108      */

109     public IVMInstall getVMInstall(ILaunchConfiguration configuration)
110             throws CoreException {
111         return JavaRuntime.computeVMInstall(configuration);
112     }
113     /**
114      * Returns the VM install name specified by the given launch configuration,
115      * or <code>null</code> if none.
116      *
117      * @param configuration
118      * launch configuration
119      * @return the VM install name specified by the given launch configuration,
120      * or <code>null</code> if none
121      * @exception CoreException
122      * if unable to retrieve the attribute
123      */

124     public String JavaDoc getVMInstallName(ILaunchConfiguration configuration)
125             throws CoreException {
126         return configuration.getAttribute(
127                 IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_NAME,
128                 (String JavaDoc) null);
129     }
130     /**
131      * Returns the VM install type specified by the given launch configuration,
132      * or <code>null</code> if none.
133      *
134      * @param configuration
135      * launch configuration
136      * @return the VM install type specified by the given launch configuration,
137      * or <code>null</code> if none
138      * @exception CoreException
139      * if unable to retrieve the attribute
140      */

141     public IVMInstallType getVMInstallType(ILaunchConfiguration configuration)
142             throws CoreException {
143         String JavaDoc id = getVMInstallTypeId(configuration);
144         if (id != null) {
145             IVMInstallType type = JavaRuntime.getVMInstallType(id);
146             if (type != null) {
147                 return type;
148             }
149         }
150         return null;
151     }
152     /**
153      * Returns the VM install type identifier specified by the given launch
154      * configuration, or <code>null</code> if none.
155      *
156      * @param configuration
157      * launch configuration
158      * @return the VM install type identifier specified by the given launch
159      * configuration, or <code>null</code> if none
160      * @exception CoreException
161      * if unable to retrieve the attribute
162      */

163     public String JavaDoc getVMInstallTypeId(ILaunchConfiguration configuration)
164             throws CoreException {
165         return configuration.getAttribute(
166                 IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE,
167                 (String JavaDoc) null);
168     }
169     /**
170      * Verifies the VM install specified by the given launch configuration
171      * exists and returns the VM install.
172      *
173      * @param configuration
174      * launch configuration
175      * @return the VM install specified by the given launch configuration
176      * @exception CoreException
177      * if unable to retrieve the attribute, the attribute is
178      * unspecified, or if the home location is unspecified or
179      * does not exist
180      */

181     public IVMInstall verifyVMInstall(ILaunchConfiguration configuration)
182             throws CoreException {
183         IVMInstall vm = getVMInstall(configuration);
184         if (vm == null) {
185             abort(
186                     LaunchingMessages.AbstractJavaLaunchConfigurationDelegate_The_specified_JRE_installation_does_not_exist_4,
187                     null,
188                     IJavaLaunchConfigurationConstants.ERR_VM_INSTALL_DOES_NOT_EXIST);
189         }
190         File JavaDoc location = vm.getInstallLocation();
191         if (location == null) {
192             abort(
193                     MessageFormat
194                             .format(
195                                     LaunchingMessages.AbstractJavaLaunchConfigurationDelegate_JRE_home_directory_not_specified_for__0__5,
196                                     new String JavaDoc[]{vm.getName()}),
197                     null,
198                     IJavaLaunchConfigurationConstants.ERR_VM_INSTALL_DOES_NOT_EXIST);
199         }
200         if (!location.exists()) {
201             abort(
202                     MessageFormat
203                             .format(
204                                     LaunchingMessages.AbstractJavaLaunchConfigurationDelegate_JRE_home_directory_for__0__does_not_exist___1__6,
205                                     new String JavaDoc[]{vm.getName(),
206                                             location.getAbsolutePath()}),
207                     null,
208                     IJavaLaunchConfigurationConstants.ERR_VM_INSTALL_DOES_NOT_EXIST);
209         }
210         return vm;
211     }
212     /**
213      * Returns the VM connector identifier specified by the given launch
214      * configuration, or <code>null</code> if none.
215      *
216      * @param configuration
217      * launch configuration
218      * @return the VM connector identifier specified by the given launch
219      * configuration, or <code>null</code> if none
220      * @exception CoreException
221      * if unable to retrieve the attribute
222      */

223     public String JavaDoc getVMConnectorId(ILaunchConfiguration configuration)
224             throws CoreException {
225         return configuration.getAttribute(
226                 IJavaLaunchConfigurationConstants.ATTR_VM_CONNECTOR,
227                 (String JavaDoc) null);
228     }
229     /**
230      * Returns entries that should appear on the bootstrap portion of the
231      * classpath as specified by the given launch configuration, as an array of
232      * resolved strings. The returned array is <code>null</code> if all
233      * entries are standard (i.e. appear by default), or empty to represent an
234      * empty bootpath.
235      *
236      * @param configuration
237      * launch configuration
238      * @return the bootpath specified by the given launch configuration. An
239      * empty bootpath is specified by an empty array, and
240      * <code>null</code> represents a default bootpath.
241      * @exception CoreException
242      * if unable to retrieve the attribute
243      */

244     public String JavaDoc[] getBootpath(ILaunchConfiguration configuration)
245             throws CoreException {
246         String JavaDoc[][] paths = getBootpathExt(configuration);
247         String JavaDoc[] pre = paths[0];
248         String JavaDoc[] main = paths[1];
249         String JavaDoc[] app = paths[2];
250         if (pre == null && main == null && app == null) {
251             // default
252
return null;
253         }
254         IRuntimeClasspathEntry[] entries = JavaRuntime
255                 .computeUnresolvedRuntimeClasspath(configuration);
256         entries = JavaRuntime.resolveRuntimeClasspath(entries, configuration);
257         List JavaDoc bootEntries = new ArrayList JavaDoc(entries.length);
258         boolean empty = true;
259         boolean allStandard = true;
260         for (int i = 0; i < entries.length; i++) {
261             if (entries[i].getClasspathProperty() != IRuntimeClasspathEntry.USER_CLASSES) {
262                 String JavaDoc location = entries[i].getLocation();
263                 if (location != null) {
264                     empty = false;
265                     bootEntries.add(location);
266                     allStandard = allStandard
267                             && entries[i].getClasspathProperty() == IRuntimeClasspathEntry.STANDARD_CLASSES;
268                 }
269             }
270         }
271         if (empty) {
272             return new String JavaDoc[0];
273         } else if (allStandard) {
274             return null;
275         } else {
276             return (String JavaDoc[]) bootEntries
277                     .toArray(new String JavaDoc[bootEntries.size()]);
278         }
279     }
280     /**
281      * Returns three sets of entries which represent the boot classpath
282      * specified in the launch configuration, as an array of three arrays of
283      * resolved strings. The first array represents the classpath that should be
284      * prepended to the boot classpath. The second array represents the main
285      * part of the boot classpath -<code>null</code> represents the default
286      * bootclasspath. The third array represents the classpath that should be
287      * appended to the boot classpath.
288      *
289      * @param configuration
290      * launch configuration
291      * @return a description of the boot classpath specified by the given launch
292      * configuration.
293      * @exception CoreException
294      * if unable to retrieve the attribute
295      * @since 3.0
296      */

297     public String JavaDoc[][] getBootpathExt(ILaunchConfiguration configuration)
298             throws CoreException {
299         String JavaDoc[][] bootpathInfo = new String JavaDoc[3][];
300         IRuntimeClasspathEntry[] entries = JavaRuntime
301                 .computeUnresolvedRuntimeClasspath(configuration);
302         List JavaDoc bootEntriesPrepend = new ArrayList JavaDoc();
303         int index = 0;
304         IRuntimeClasspathEntry jreEntry = null;
305         while (jreEntry == null && index < entries.length) {
306             IRuntimeClasspathEntry entry = entries[index++];
307             if (entry.getClasspathProperty() == IRuntimeClasspathEntry.BOOTSTRAP_CLASSES
308                     || entry.getClasspathProperty() == IRuntimeClasspathEntry.STANDARD_CLASSES) {
309                 if (JavaRuntime.isVMInstallReference(entry)) {
310                     jreEntry = entry;
311                 } else {
312                     bootEntriesPrepend.add(entry);
313                 }
314             }
315         }
316         IRuntimeClasspathEntry[] bootEntriesPrep = JavaRuntime
317                 .resolveRuntimeClasspath(
318                         (IRuntimeClasspathEntry[]) bootEntriesPrepend
319                                 .toArray(new IRuntimeClasspathEntry[bootEntriesPrepend
320                                         .size()]), configuration);
321         String JavaDoc[] entriesPrep = null;
322         if (bootEntriesPrep.length > 0) {
323             entriesPrep = new String JavaDoc[bootEntriesPrep.length];
324             for (int i = 0; i < bootEntriesPrep.length; i++) {
325                 entriesPrep[i] = bootEntriesPrep[i].getLocation();
326             }
327         }
328         if (jreEntry != null) {
329             List JavaDoc bootEntriesAppend = new ArrayList JavaDoc();
330             for (; index < entries.length; index++) {
331                 IRuntimeClasspathEntry entry = entries[index];
332                 if (entry.getClasspathProperty() == IRuntimeClasspathEntry.BOOTSTRAP_CLASSES) {
333                     bootEntriesAppend.add(entry);
334                 }
335             }
336             bootpathInfo[0] = entriesPrep;
337             IRuntimeClasspathEntry[] bootEntriesApp = JavaRuntime
338                     .resolveRuntimeClasspath(
339                             (IRuntimeClasspathEntry[]) bootEntriesAppend
340                                     .toArray(new IRuntimeClasspathEntry[bootEntriesAppend
341                                             .size()]), configuration);
342             if (bootEntriesApp.length > 0) {
343                 bootpathInfo[2] = new String JavaDoc[bootEntriesApp.length];
344                 for (int i = 0; i < bootEntriesApp.length; i++) {
345                     bootpathInfo[2][i] = bootEntriesApp[i].getLocation();
346                 }
347             }
348             IVMInstall install = getVMInstall(configuration);
349             LibraryLocation[] libraryLocations = install.getLibraryLocations();
350             if (libraryLocations != null) {
351                 // determine if explicit bootpath should be used
352
// TODO: this test does not tell us if the bootpath entries are different (could still be
353
// the same, as a non-bootpath entry on the JRE may have been removed/added)
354
// We really need a way to ask a VM type for its default bootpath library locations and
355
// compare that to the resolved entries for the "jreEntry" to see if they
356
// are different (requires explicit bootpath)
357
if (!JRERuntimeClasspathEntryResolver.isSameArchives(libraryLocations, install.getVMInstallType().getDefaultLibraryLocations(install.getInstallLocation()))) {
358                     // resolve bootpath entries in JRE entry
359
IRuntimeClasspathEntry[] bootEntries = null;
360                     if (jreEntry.getType() == IRuntimeClasspathEntry.CONTAINER) {
361                         IRuntimeClasspathEntry bootEntry = JavaRuntime.newRuntimeContainerClasspathEntry(
362                                 jreEntry.getPath(),
363                                 IRuntimeClasspathEntry.BOOTSTRAP_CLASSES,
364                                 getJavaProject(configuration));
365                         bootEntries = JavaRuntime.resolveRuntimeClasspathEntry(bootEntry, configuration);
366                     } else {
367                         bootEntries = JavaRuntime.resolveRuntimeClasspathEntry(jreEntry, configuration);
368                     }
369                     
370                     // non-default JRE libraries - use explicit bootpath only
371
String JavaDoc[] bootpath = new String JavaDoc[bootEntriesPrep.length
372                             + bootEntries.length + bootEntriesApp.length];
373                     if (bootEntriesPrep.length > 0) {
374                         System.arraycopy(bootpathInfo[0], 0, bootpath, 0,
375                                 bootEntriesPrep.length);
376                     }
377                     int dest = bootEntriesPrep.length;
378                     for (int i = 0; i < bootEntries.length; i++) {
379                         bootpath[dest] = bootEntries[i].getLocation();
380                         dest++;
381                     }
382                     if (bootEntriesApp.length > 0) {
383                         System.arraycopy(bootpathInfo[2], 0, bootpath, dest,
384                                 bootEntriesApp.length);
385                     }
386                     bootpathInfo[0] = null;
387                     bootpathInfo[1] = bootpath;
388                     bootpathInfo[2] = null;
389                 }
390             }
391         } else {
392             if (entriesPrep == null) {
393                 bootpathInfo[1] = new String JavaDoc[0];
394             } else {
395                 bootpathInfo[1] = entriesPrep;
396             }
397         }
398         return bootpathInfo;
399     }
400     /**
401      * Returns the entries that should appear on the user portion of the
402      * classpath as specified by the given launch configuration, as an array of
403      * resolved strings. The returned array is empty if no classpath is
404      * specified.
405      *
406      * @param configuration
407      * launch configuration
408      * @return the classpath specified by the given launch configuration,
409      * possibly an empty array
410      * @exception CoreException
411      * if unable to retrieve the attribute
412      */

413     public String JavaDoc[] getClasspath(ILaunchConfiguration configuration)
414             throws CoreException {
415         IRuntimeClasspathEntry[] entries = JavaRuntime
416                 .computeUnresolvedRuntimeClasspath(configuration);
417         entries = JavaRuntime.resolveRuntimeClasspath(entries, configuration);
418         List JavaDoc userEntries = new ArrayList JavaDoc(entries.length);
419         Set JavaDoc set = new HashSet JavaDoc(entries.length);
420         for (int i = 0; i < entries.length; i++) {
421             if (entries[i].getClasspathProperty() == IRuntimeClasspathEntry.USER_CLASSES) {
422                 String JavaDoc location = entries[i].getLocation();
423                 if (location != null) {
424                     if (!set.contains(location)) {
425                         userEntries.add(location);
426                         set.add(location);
427                     }
428                 }
429             }
430         }
431         return (String JavaDoc[]) userEntries.toArray(new String JavaDoc[userEntries.size()]);
432     }
433     /**
434      * Returns the Java project specified by the given launch configuration, or
435      * <code>null</code> if none.
436      *
437      * @param configuration
438      * launch configuration
439      * @return the Java project specified by the given launch configuration, or
440      * <code>null</code> if none
441      * @exception CoreException
442      * if unable to retrieve the attribute
443      */

444     public IJavaProject getJavaProject(ILaunchConfiguration configuration)
445             throws CoreException {
446         String JavaDoc projectName = getJavaProjectName(configuration);
447         if (projectName != null) {
448             projectName = projectName.trim();
449             if (projectName.length() > 0) {
450                 IProject project = ResourcesPlugin.getWorkspace().getRoot()
451                         .getProject(projectName);
452                 IJavaProject javaProject = JavaCore.create(project);
453                 if (javaProject != null && javaProject.exists()) {
454                     return javaProject;
455                 }
456             }
457         }
458         return null;
459     }
460     /**
461      * Returns the Java project name specified by the given launch
462      * configuration, or <code>null</code> if none.
463      *
464      * @param configuration
465      * launch configuration
466      * @return the Java project name specified by the given launch
467      * configuration, or <code>null</code> if none
468      * @exception CoreException
469      * if unable to retrieve the attribute
470      */

471     public String JavaDoc getJavaProjectName(ILaunchConfiguration configuration)
472             throws CoreException {
473         return configuration.getAttribute(
474                 IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
475                 (String JavaDoc) null);
476     }
477     /**
478      * Returns the main type name specified by the given launch configuration,
479      * or <code>null</code> if none.
480      *
481      * @param configuration
482      * launch configuration
483      * @return the main type name specified by the given launch configuration,
484      * or <code>null</code> if none
485      * @exception CoreException
486      * if unable to retrieve the attribute
487      */

488     public String JavaDoc getMainTypeName(ILaunchConfiguration configuration)
489             throws CoreException {
490         String JavaDoc mainType = configuration.getAttribute(
491                 IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,
492                 (String JavaDoc) null);
493         if (mainType == null) {
494             return null;
495         }
496         return VariablesPlugin.getDefault().getStringVariableManager()
497                 .performStringSubstitution(mainType);
498     }
499     /**
500      * Returns the program arguments specified by the given launch
501      * configuration, as a string. The returned string is empty if no program
502      * arguments are specified.
503      *
504      * @param configuration
505      * launch configuration
506      * @return the program arguments specified by the given launch
507      * configuration, possibly an empty string
508      * @exception CoreException
509      * if unable to retrieve the attribute
510      */

511     public String JavaDoc getProgramArguments(ILaunchConfiguration configuration)
512             throws CoreException {
513         String JavaDoc arguments = configuration.getAttribute(
514                 IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, ""); //$NON-NLS-1$
515
return VariablesPlugin.getDefault().getStringVariableManager()
516                 .performStringSubstitution(arguments);
517     }
518     /**
519      * Returns the VM arguments specified by the given launch configuration, as
520      * a string. The returned string is empty if no VM arguments are specified.
521      *
522      * @param configuration
523      * launch configuration
524      * @return the VM arguments specified by the given launch configuration,
525      * possibly an empty string
526      * @exception CoreException
527      * if unable to retrieve the attribute
528      */

529     public String JavaDoc getVMArguments(ILaunchConfiguration configuration) throws CoreException {
530         String JavaDoc arguments = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, ""); //$NON-NLS-1$
531
String JavaDoc args = VariablesPlugin.getDefault().getStringVariableManager().performStringSubstitution(arguments);
532         int libraryPath = args.indexOf("-Djava.library.path"); //$NON-NLS-1$
533
if (libraryPath < 0) {
534             // if a library path is already specified, do not override
535
String JavaDoc[] javaLibraryPath = getJavaLibraryPath(configuration);
536             if (javaLibraryPath != null && javaLibraryPath.length > 0) {
537                 StringBuffer JavaDoc path = new StringBuffer JavaDoc(args);
538                 path.append(" -Djava.library.path="); //$NON-NLS-1$
539
path.append("\""); //$NON-NLS-1$
540
for (int i = 0; i < javaLibraryPath.length; i++) {
541                     if (i > 0) {
542                         path.append(File.pathSeparatorChar);
543                     }
544                     path.append(javaLibraryPath[i]);
545                 }
546                 path.append("\""); //$NON-NLS-1$
547
args = path.toString();
548             }
549         }
550         return args;
551     }
552     /**
553      * Returns the Map of VM-specific attributes specified by the given launch
554      * configuration, or <code>null</code> if none.
555      *
556      * @param configuration
557      * launch configuration
558      * @return the <code>Map</code> of VM-specific attributes
559      * @exception CoreException
560      * if unable to retrieve the attribute
561      */

562     public Map JavaDoc getVMSpecificAttributesMap(ILaunchConfiguration configuration)
563             throws CoreException {
564         Map JavaDoc map = configuration
565                 .getAttribute(
566                         IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE_SPECIFIC_ATTRS_MAP,
567                         (Map JavaDoc) null);
568         String JavaDoc[][] paths = getBootpathExt(configuration);
569         String JavaDoc[] pre = paths[0];
570         String JavaDoc[] boot = paths[1];
571         String JavaDoc[] app = paths[2];
572         if (pre != null || app != null || boot != null) {
573             if (map == null) {
574                 map = new HashMap JavaDoc(3);
575             }
576             if (pre != null) {
577                 map
578                         .put(
579                                 IJavaLaunchConfigurationConstants.ATTR_BOOTPATH_PREPEND,
580                                 pre);
581             }
582             if (app != null) {
583                 map.put(IJavaLaunchConfigurationConstants.ATTR_BOOTPATH_APPEND,
584                         app);
585             }
586             if (boot != null) {
587                 map.put(IJavaLaunchConfigurationConstants.ATTR_BOOTPATH, boot);
588             }
589         }
590         return map;
591     }
592     /**
593      * Returns the working directory specified by the given launch
594      * configuration, or <code>null</code> if none.
595      *
596      * @param configuration
597      * launch configuration
598      * @return the working directory specified by the given launch
599      * configuration, or <code>null</code> if none
600      * @exception CoreException
601      * if unable to retrieve the attribute
602      */

603     public File JavaDoc getWorkingDirectory(ILaunchConfiguration configuration)
604             throws CoreException {
605         return verifyWorkingDirectory(configuration);
606     }
607     /**
608      * Returns the working directory path specified by the given launch
609      * configuration, or <code>null</code> if none.
610      *
611      * @param configuration
612      * launch configuration
613      * @return the working directory path specified by the given launch
614      * configuration, or <code>null</code> if none
615      * @exception CoreException
616      * if unable to retrieve the attribute
617      */

618     public IPath getWorkingDirectoryPath(ILaunchConfiguration configuration)
619             throws CoreException {
620         String JavaDoc path = configuration.getAttribute(
621                 IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY,
622                 (String JavaDoc) null);
623         if (path != null) {
624             path = VariablesPlugin.getDefault().getStringVariableManager()
625                     .performStringSubstitution(path);
626             return new Path(path);
627         }
628         return null;
629     }
630     /**
631      * Verifies a Java project is specified by the given launch configuration,
632      * and returns the Java project.
633      *
634      * @param configuration
635      * launch configuration
636      * @return the Java project specified by the given launch configuration
637      * @exception CoreException
638      * if unable to retrieve the attribute or the attribute is
639      * unspecified
640      */

641     public IJavaProject verifyJavaProject(ILaunchConfiguration configuration)
642             throws CoreException {
643         String JavaDoc name = getJavaProjectName(configuration);
644         if (name == null) {
645             abort(
646                     LaunchingMessages.AbstractJavaLaunchConfigurationDelegate_Java_project_not_specified_9,
647                     null,
648                     IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_PROJECT);
649         }
650         IJavaProject project = getJavaProject(configuration);
651         if (project == null) {
652             abort(
653                     LaunchingMessages.AbstractJavaLaunchConfigurationDelegate_Project_does_not_exist_or_is_not_a_Java_project_10,
654                     null,
655                     IJavaLaunchConfigurationConstants.ERR_NOT_A_JAVA_PROJECT);
656         }
657         return project;
658     }
659     /**
660      * Verifies a main type name is specified by the given launch configuration,
661      * and returns the main type name.
662      *
663      * @param configuration
664      * launch configuration
665      * @return the main type name specified by the given launch configuration
666      * @exception CoreException
667      * if unable to retrieve the attribute or the attribute is
668      * unspecified
669      */

670     public String JavaDoc verifyMainTypeName(ILaunchConfiguration configuration)
671             throws CoreException {
672         String JavaDoc name = getMainTypeName(configuration);
673         if (name == null) {
674             abort(
675                     LaunchingMessages.AbstractJavaLaunchConfigurationDelegate_Main_type_not_specified_11,
676                     null,
677                     IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_MAIN_TYPE);
678         }
679         return name;
680     }
681     /**
682      * Verifies the working directory specified by the given launch
683      * configuration exists, and returns the working directory, or
684      * <code>null</code> if none is specified.
685      *
686      * @param configuration
687      * launch configuration
688      * @return the working directory specified by the given launch
689      * configuration, or <code>null</code> if none
690      * @exception CoreException
691      * if unable to retrieve the attribute
692      */

693     public File JavaDoc verifyWorkingDirectory(ILaunchConfiguration configuration)
694             throws CoreException {
695         IPath path = getWorkingDirectoryPath(configuration);
696         if (path == null) {
697             File JavaDoc dir = getDefaultWorkingDirectory(configuration);
698             if (dir != null) {
699                 if (!dir.isDirectory()) {
700                     abort(
701                             MessageFormat.format(
702                                     LaunchingMessages.AbstractJavaLaunchConfigurationDelegate_Working_directory_does_not_exist___0__12,
703                                     new String JavaDoc[]{dir.toString()}),
704                                     null,
705                                     IJavaLaunchConfigurationConstants.ERR_WORKING_DIRECTORY_DOES_NOT_EXIST);
706                 }
707                 return dir;
708             }
709         } else {
710             if (path.isAbsolute()) {
711                 File JavaDoc dir = new File JavaDoc(path.toOSString());
712                 if (dir.isDirectory()) {
713                     return dir;
714                 }
715                 // This may be a workspace relative path returned by a variable.
716
// However variable paths start with a slash and thus are thought to
717
// be absolute
718
IResource res = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
719                 if (res instanceof IContainer && res.exists()) {
720                     return res.getLocation().toFile();
721                 }
722                 abort(
723                     MessageFormat
724                             .format(
725                                     LaunchingMessages.AbstractJavaLaunchConfigurationDelegate_Working_directory_does_not_exist___0__12,
726                                     new String JavaDoc[]{path.toString()}),
727                     null,
728                     IJavaLaunchConfigurationConstants.ERR_WORKING_DIRECTORY_DOES_NOT_EXIST);
729             } else {
730                 IResource res = ResourcesPlugin.getWorkspace().getRoot()
731                         .findMember(path);
732                 if (res instanceof IContainer && res.exists()) {
733                     return res.getLocation().toFile();
734                 }
735                 abort(
736                     MessageFormat
737                             .format(
738                                     LaunchingMessages.AbstractJavaLaunchConfigurationDelegate_Working_directory_does_not_exist___0__12,
739                                     new String JavaDoc[]{path.toString()}),
740                     null,
741                     IJavaLaunchConfigurationConstants.ERR_WORKING_DIRECTORY_DOES_NOT_EXIST);
742             }
743         }
744         return null;
745     }
746     /**
747      * Returns whether the given launch configuration specifies that termination
748      * is allowed.
749      *
750      * @param configuration
751      * launch configuration
752      * @return whether termination is allowed
753      * @exception CoreException
754      * if unable to retrieve the attribute
755      */

756     public boolean isAllowTerminate(ILaunchConfiguration configuration)
757             throws CoreException {
758         return configuration.getAttribute(
759                 IJavaLaunchConfigurationConstants.ATTR_ALLOW_TERMINATE, false);
760     }
761     /**
762      * Returns whether the given launch configuration specifies that execution
763      * should suspend on entry of the main method.
764      *
765      * @param configuration
766      * launch configuration
767      * @return whether execution should suspend in main
768      * @exception CoreException
769      * if unable to retrieve the attribute
770      * @since 2.1
771      */

772     public boolean isStopInMain(ILaunchConfiguration configuration)
773             throws CoreException {
774         return configuration.getAttribute(
775                 IJavaLaunchConfigurationConstants.ATTR_STOP_IN_MAIN, false);
776     }
777     /**
778      * Assigns a default source locator to the given launch if a source locator
779      * has not yet been assigned to it, and the associated launch configuration
780      * does not specify a source locator.
781      *
782      * @param launch
783      * launch object
784      * @param configuration
785      * configuration being launched
786      * @exception CoreException
787      * if unable to set the source locator
788      */

789     protected void setDefaultSourceLocator(ILaunch launch,
790             ILaunchConfiguration configuration) throws CoreException {
791         // set default source locator if none specified
792
if (launch.getSourceLocator() == null) {
793             ISourceLookupDirector sourceLocator = new JavaSourceLookupDirector();
794             sourceLocator
795                     .setSourcePathComputer(getLaunchManager()
796                             .getSourcePathComputer(
797                                     "org.eclipse.jdt.launching.sourceLookup.javaSourcePathComputer")); //$NON-NLS-1$
798
sourceLocator.initializeDefaults(configuration);
799             launch.setSourceLocator(sourceLocator);
800         }
801     }
802     /**
803      * Determines if the given launch configuration specifies the "stop-in-main"
804      * attribute, and sets up an event listener to handle the option if
805      * required.
806      *
807      * @param configuration
808      * configuration being launched
809      * @exception CoreException
810      * if unable to access the attribute
811      * @since 2.1
812      */

813     protected void prepareStopInMain(ILaunchConfiguration configuration)
814             throws CoreException {
815         if (isStopInMain(configuration)) {
816             // This listener does not remove itself from the debug plug-in
817
// as an event listener (there is no dispose notification for
818
// launch delegates). However, since there is only one delegate
819
// instantiated per config type, this is tolerable.
820
DebugPlugin.getDefault().addDebugEventListener(this);
821         }
822     }
823     /**
824      * Handles the "stop-in-main" option.
825      *
826      * @param events
827      * the debug events.
828      * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(DebugEvent[])
829      */

830     public void handleDebugEvents(DebugEvent[] events) {
831         for (int i = 0; i < events.length; i++) {
832             DebugEvent event = events[i];
833             if (event.getKind() == DebugEvent.CREATE
834                     && event.getSource() instanceof IJavaDebugTarget) {
835                 IJavaDebugTarget target = (IJavaDebugTarget) event.getSource();
836                 ILaunch launch = target.getLaunch();
837                 if (launch != null) {
838                     ILaunchConfiguration configuration = launch
839                             .getLaunchConfiguration();
840                     if (configuration != null) {
841                         try {
842                             if (isStopInMain(configuration)) {
843                                 String JavaDoc mainType = getMainTypeName(configuration);
844                                 if (mainType != null) {
845                                     Map JavaDoc map = new HashMap JavaDoc();
846                                     map
847                                             .put(
848                                                     IJavaLaunchConfigurationConstants.ATTR_STOP_IN_MAIN,
849                                                     IJavaLaunchConfigurationConstants.ATTR_STOP_IN_MAIN);
850                                     IJavaMethodBreakpoint bp = JDIDebugModel
851                                             .createMethodBreakpoint(
852                                                     ResourcesPlugin
853                                                             .getWorkspace()
854                                                             .getRoot(),
855                                                     mainType, "main", //$NON-NLS-1$
856
"([Ljava/lang/String;)V", //$NON-NLS-1$
857
true, false, false, -1, -1,
858                                                     -1, 1, false, map);
859                                     bp.setPersisted(false);
860                                     target.breakpointAdded(bp);
861                                     DebugPlugin.getDefault()
862                                             .removeDebugEventListener(this);
863                                 }
864                             }
865                         } catch (CoreException e) {
866                             LaunchingPlugin.log(e);
867                         }
868                     }
869                 }
870             }
871         }
872     }
873     
874     /*
875      * (non-Javadoc)
876      *
877      * @see org.eclipse.debug.core.model.LaunchConfigurationDelegate#getBuildOrder(org.eclipse.debug.core.ILaunchConfiguration,
878      * java.lang.String)
879      */

880     protected IProject[] getBuildOrder(ILaunchConfiguration configuration,
881             String JavaDoc mode) throws CoreException {
882         return fOrderedProjects;
883     }
884     /*
885      * (non-Javadoc)
886      *
887      * @see org.eclipse.debug.core.model.LaunchConfigurationDelegate#getProjectsForProblemSearch(org.eclipse.debug.core.ILaunchConfiguration,
888      * java.lang.String)
889      */

890     protected IProject[] getProjectsForProblemSearch(
891             ILaunchConfiguration configuration, String JavaDoc mode)
892             throws CoreException {
893         return fOrderedProjects;
894     }
895     
896     /* (non-Javadoc)
897      * @see org.eclipse.debug.core.model.LaunchConfigurationDelegate#isLaunchProblem(org.eclipse.core.resources.IMarker)
898      */

899     protected boolean isLaunchProblem(IMarker problemMarker) throws CoreException {
900         return super.isLaunchProblem(problemMarker) && problemMarker.getType().equals(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER);
901     }
902     /*
903      * (non-Javadoc)
904      *
905      * @see org.eclipse.debug.core.model.ILaunchConfigurationDelegate2#preLaunchCheck(org.eclipse.debug.core.ILaunchConfiguration,
906      * java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
907      */

908     public boolean preLaunchCheck(ILaunchConfiguration configuration,
909             String JavaDoc mode, IProgressMonitor monitor) throws CoreException {
910         // build project list
911
if (monitor != null) {
912             monitor.subTask(LaunchingMessages.AbstractJavaLaunchConfigurationDelegate_20);
913         }
914         fOrderedProjects = null;
915         IJavaProject javaProject = JavaRuntime.getJavaProject(configuration);
916         if (javaProject != null) {
917             fOrderedProjects = computeReferencedBuildOrder(new IProject[]{javaProject
918                     .getProject()});
919         }
920         // do generic launch checks
921
return super.preLaunchCheck(configuration, mode, monitor);
922     }
923     
924      /* (non-Javadoc)
925      * @see org.eclipse.debug.core.model.LaunchConfigurationDelegate#getBreakpoints(org.eclipse.debug.core.ILaunchConfiguration)
926      */

927     protected IBreakpoint[] getBreakpoints(ILaunchConfiguration configuration) {
928          IBreakpointManager breakpointManager = DebugPlugin.getDefault().getBreakpointManager();
929          if (!breakpointManager.isEnabled()) {
930              // no need to check breakpoints individually.
931
return null;
932          }
933          return breakpointManager.getBreakpoints(JDIDebugModel.getPluginIdentifier());
934      }
935     
936     /**
937      * Returns the VM runner for the given launch mode to use when launching the
938      * given configuration.
939      *
940      * @param configuration launch configuration
941      * @param mode launch node
942      * @return VM runner to use when launching the given configuration in the given mode
943      * @throws CoreException if a VM runner cannot be determined
944      * @since 3.1
945      */

946     public IVMRunner getVMRunner(ILaunchConfiguration configuration, String JavaDoc mode) throws CoreException {
947         IVMInstall vm = verifyVMInstall(configuration);
948         IVMRunner runner = vm.getVMRunner(mode);
949         if (runner == null) {
950             abort(MessageFormat.format(LaunchingMessages.JavaLocalApplicationLaunchConfigurationDelegate_0, new String JavaDoc[]{vm.getName(), mode}), null, IJavaLaunchConfigurationConstants.ERR_VM_RUNNER_DOES_NOT_EXIST);
951         }
952         return runner;
953     }
954     
955     /**
956      * Returns an array of environment variables to be used when
957      * launching the given configuration or <code>null</code> if unspecified.
958      *
959      * @param configuration launch configuration
960      * @throws CoreException if unable to access associated attribute or if
961      * unable to resolve a variable in an environment variable's value
962      * @since 3.1
963      */

964     public String JavaDoc[] getEnvironment(ILaunchConfiguration configuration) throws CoreException {
965         return DebugPlugin.getDefault().getLaunchManager().getEnvironment(configuration);
966     }
967     
968     /**
969      * Returns an array of paths to be used for the <code>java.library.path</code>
970      * system property, or <code>null</code> if unspecified.
971      *
972      * @param configuration
973      * @return an array of paths to be used for the <code>java.library.path</code>
974      * system property, or <code>null</code>
975      * @throws CoreException if unable to determine the attribute
976      * @since 3.1
977      */

978     public String JavaDoc[] getJavaLibraryPath(ILaunchConfiguration configuration) throws CoreException {
979         IJavaProject project = getJavaProject(configuration);
980         if (project != null) {
981             String JavaDoc[] paths = JavaRuntime.computeJavaLibraryPath(project, true);
982             if (paths.length > 0) {
983                 return paths;
984             }
985         }
986         return null;
987     }
988     
989     /**
990      * Returns the default working directory for the given launch configuration,
991      * or <code>null</code> if none. Subclasses may override as necessary.
992      *
993      * @param configuration
994      * @return default working directory or <code>null</code> if none
995      * @throws CoreException if an exception occurs computing the default working
996      * directory
997      * @since 3.2
998      */

999     protected File JavaDoc getDefaultWorkingDirectory(ILaunchConfiguration configuration) throws CoreException {
1000        // default working directory is the project if this config has a project
1001
IJavaProject jp = getJavaProject(configuration);
1002        if (jp != null) {
1003            IProject p = jp.getProject();
1004            return p.getLocation().toFile();
1005        }
1006        return null;
1007    }
1008}
1009
Popular Tags