KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > ui > launcher > AbstractPDELaunchConfiguration


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.pde.ui.launcher;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.SubProgressMonitor;
23 import org.eclipse.debug.core.DebugPlugin;
24 import org.eclipse.debug.core.ILaunch;
25 import org.eclipse.debug.core.ILaunchConfiguration;
26 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
27 import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
28 import org.eclipse.jdt.launching.ExecutionArguments;
29 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
30 import org.eclipse.jdt.launching.IVMInstall;
31 import org.eclipse.jdt.launching.IVMRunner;
32 import org.eclipse.jdt.launching.VMRunnerConfiguration;
33 import org.eclipse.jface.dialogs.MessageDialog;
34 import org.eclipse.pde.core.plugin.TargetPlatform;
35 import org.eclipse.pde.internal.core.TargetPlatformHelper;
36 import org.eclipse.pde.internal.ui.IPDEUIConstants;
37 import org.eclipse.pde.internal.ui.PDEPlugin;
38 import org.eclipse.pde.internal.ui.PDEUIMessages;
39 import org.eclipse.pde.internal.ui.launcher.EclipsePluginValidationOperation;
40 import org.eclipse.pde.internal.ui.launcher.LaunchArgumentsHelper;
41 import org.eclipse.pde.internal.ui.launcher.LaunchConfigurationHelper;
42 import org.eclipse.pde.internal.ui.launcher.LaunchPluginValidator;
43 import org.eclipse.pde.internal.ui.launcher.LauncherUtils;
44 import org.eclipse.pde.internal.ui.launcher.VMHelper;
45
46 /**
47  * An abstract launch delegate for PDE-based launch configurations
48  * <p>
49  * Clients may subclass this class.
50  * </p>
51  * @since 3.2
52  */

53 public abstract class AbstractPDELaunchConfiguration extends LaunchConfigurationDelegate {
54
55     protected File JavaDoc fConfigDir = null;
56
57     /*
58      * (non-Javadoc)
59      * @see org.eclipse.debug.core.model.ILaunchConfigurationDelegate#launch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor)
60      */

61     public void launch(ILaunchConfiguration configuration, String JavaDoc mode, ILaunch launch,
62             IProgressMonitor monitor) throws CoreException {
63         try {
64             fConfigDir = null;
65             monitor.beginTask("", 4); //$NON-NLS-1$
66
try {
67                 preLaunchCheck(configuration, launch, new SubProgressMonitor(monitor, 2));
68             } catch (CoreException e) {
69                 if (e.getStatus().getSeverity() == IStatus.CANCEL) {
70                     monitor.setCanceled(true);
71                     return;
72                 }
73                 throw e;
74             }
75             VMRunnerConfiguration runnerConfig = new VMRunnerConfiguration(
76                                                 getMainClass(), getClasspath(configuration));
77             runnerConfig.setVMArguments(getVMArguments(configuration));
78             runnerConfig.setProgramArguments(getProgramArguments(configuration));
79             runnerConfig.setWorkingDirectory(getWorkingDirectory(configuration).getAbsolutePath());
80             runnerConfig.setEnvironment(getEnvironment(configuration));
81             runnerConfig.setVMSpecificAttributesMap(getVMSpecificAttributesMap(configuration));
82
83             monitor.worked(1);
84                     
85             setDefaultSourceLocator(configuration);
86             manageLaunch(launch);
87             IVMRunner runner = getVMRunner(configuration, mode);
88             if (runner != null)
89                 runner.run(runnerConfig, launch, monitor);
90             else
91                 monitor.setCanceled(true);
92             monitor.done();
93         } catch (final CoreException e) {
94             monitor.setCanceled(true);
95             LauncherUtils.getDisplay().syncExec(new Runnable JavaDoc() {
96                 public void run() {
97                     MessageDialog.openError(LauncherUtils.getActiveShell(),
98                                             PDEUIMessages.Launcher_error_title,
99                                             e.getMessage());
100                 }
101             });
102         }
103     }
104     
105     /**
106      * Returns the VM runner for the given launch mode to use when launching the
107      * given configuration.
108      *
109      * @param configuration launch configuration
110      * @param mode launch node
111      * @return VM runner to use when launching the given configuration in the given mode
112      * @throws CoreException if a VM runner cannot be determined
113      */

114     public IVMRunner getVMRunner(ILaunchConfiguration configuration, String JavaDoc mode) throws CoreException {
115         IVMInstall launcher = VMHelper.createLauncher(configuration);
116         return launcher.getVMRunner(mode);
117     }
118     
119     /**
120      * Assigns a default source locator to the given launch if a source locator
121      * has not yet been assigned to it, and the associated launch configuration
122      * does not specify a source locator.
123      *
124      * @param configuration
125      * configuration being launched
126      * @exception CoreException
127      * if unable to set the source locator
128      */

129     protected void setDefaultSourceLocator(ILaunchConfiguration configuration) throws CoreException {
130         ILaunchConfigurationWorkingCopy wc = null;
131         if (configuration.isWorkingCopy()) {
132             wc = (ILaunchConfigurationWorkingCopy) configuration;
133         } else {
134             wc = configuration.getWorkingCopy();
135         }
136         String JavaDoc id = configuration.getAttribute(
137                 IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH_PROVIDER,
138                 (String JavaDoc) null);
139         if (!PDESourcePathProvider.ID.equals(id)) {
140             wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH_PROVIDER,
141                             PDESourcePathProvider.ID);
142             wc.doSave();
143         }
144     }
145     
146     /**
147      * Returns the entries that should appear on boot classpath.
148      *
149      * @param configuration
150      * launch configuration
151      * @return the location of startup.jar and
152      * the bootstrap classpath specified by the given launch configuration
153      *
154      * @exception CoreException
155      * if unable to find startup.jar
156      */

157     public String JavaDoc[] getClasspath(ILaunchConfiguration configuration) throws CoreException {
158         String JavaDoc[] classpath = LaunchArgumentsHelper.constructClasspath(configuration);
159         if (classpath == null) {
160             String JavaDoc message = PDEUIMessages.WorkbenchLauncherConfigurationDelegate_noStartup;
161             throw new CoreException(LauncherUtils.createErrorStatus(message));
162         }
163         return classpath;
164     }
165     
166     /**
167      * Returns an array of environment variables to be used when
168      * launching the given configuration or <code>null</code> if unspecified.
169      *
170      * @param configuration launch configuration
171      * @throws CoreException if unable to access associated attribute or if
172      * unable to resolve a variable in an environment variable's value
173      */

174     public String JavaDoc[] getEnvironment(ILaunchConfiguration configuration) throws CoreException {
175         return DebugPlugin.getDefault().getLaunchManager().getEnvironment(configuration);
176     }
177     
178     /**
179      * Returns the working directory path specified by the given launch
180      * configuration, or <code>null</code> if none.
181      *
182      * @param configuration
183      * launch configuration
184      * @return the working directory path specified by the given launch
185      * configuration, or <code>null</code> if none
186      * @exception CoreException
187      * if unable to retrieve the attribute
188      */

189     public File JavaDoc getWorkingDirectory(ILaunchConfiguration configuration) throws CoreException {
190         return LaunchArgumentsHelper.getWorkingDirectory(configuration);
191     }
192     
193     /**
194      * Returns the Map of VM-specific attributes specified by the given launch
195      * configuration, or <code>null</code> if none.
196      *
197      * @param configuration
198      * launch configuration
199      * @return the <code>Map</code> of VM-specific attributes
200      * @exception CoreException
201      * if unable to retrieve the attribute
202      */

203     public Map JavaDoc getVMSpecificAttributesMap(ILaunchConfiguration configuration) throws CoreException {
204         return LaunchArgumentsHelper.getVMSpecificAttributesMap(configuration);
205     }
206     
207     /**
208      * Returns the VM arguments specified by the given launch configuration, as
209      * an array of strings.
210      *
211      * @param configuration
212      * launch configuration
213      * @return the VM arguments specified by the given launch configuration,
214      * possibly an empty array
215      * @exception CoreException
216      * if unable to retrieve the attribute
217      */

218     public String JavaDoc[] getVMArguments(ILaunchConfiguration configuration) throws CoreException {
219         return new ExecutionArguments(LaunchArgumentsHelper.getUserVMArguments(configuration), "").getVMArgumentsArray(); //$NON-NLS-1$
220
}
221
222     /**
223      * Returns the program arguments to launch with.
224      * This list is a combination of arguments computed by PDE based on attributes
225      * specified in the given launch configuration, followed by the program arguments
226      * that the entered directly into the launch configuration.
227      *
228      * @param configuration
229      * launch configuration
230      * @return the program arguments necessary for launching
231      *
232      * @exception CoreException
233      * if unable to retrieve the attribute or create the
234      * necessary configuration files
235      */

236     public String JavaDoc[] getProgramArguments(ILaunchConfiguration configuration) throws CoreException {
237         ArrayList JavaDoc programArgs = new ArrayList JavaDoc();
238  
239         // add tracing, if turned on
240
if (configuration.getAttribute(IPDELauncherConstants.TRACING, false)
241                 && !IPDELauncherConstants.TRACING_NONE.equals(configuration.getAttribute(IPDELauncherConstants.TRACING_CHECKED, (String JavaDoc) null))) {
242             programArgs.add("-debug"); //$NON-NLS-1$
243
programArgs.add(
244                     LaunchArgumentsHelper.getTracingFileArgument(
245                     configuration,
246                     getConfigDir(configuration).toString() + IPath.SEPARATOR + ".options")); //$NON-NLS-1$
247
}
248
249         // add the program args specified by the user
250
String JavaDoc[] userArgs = LaunchArgumentsHelper.getUserProgramArgumentArray(configuration);
251         ArrayList JavaDoc userDefined = new ArrayList JavaDoc();
252         for (int i = 0; i < userArgs.length; i++) {
253             // be forgiving if people have tracing turned on and forgot
254
// to remove the -debug from the program args field.
255
if (userArgs[i].equals("-debug") && programArgs.contains("-debug")) //$NON-NLS-1$ //$NON-NLS-2$
256
continue;
257             userDefined.add(userArgs[i]);
258         }
259         
260         if (!configuration.getAttribute(IPDEUIConstants.APPEND_ARGS_EXPLICITLY, false)) {
261
262             if (!userDefined.contains("-os")) { //$NON-NLS-1$
263
programArgs.add("-os"); //$NON-NLS-1$
264
programArgs.add(TargetPlatform.getOS());
265             }
266             if (!userDefined.contains("-ws")) { //$NON-NLS-1$
267
programArgs.add("-ws"); //$NON-NLS-1$
268
programArgs.add(TargetPlatform.getWS());
269             }
270             if (!userDefined.contains("-arch")) { //$NON-NLS-1$
271
programArgs.add("-arch"); //$NON-NLS-1$
272
programArgs.add(TargetPlatform.getOSArch());
273             }
274         }
275         
276         if (userDefined.size() > 0) {
277             programArgs.addAll(userDefined);
278         }
279         
280         return (String JavaDoc[])programArgs.toArray(new String JavaDoc[programArgs.size()]);
281     }
282     
283     /**
284      * Does sanity checking before launching. The criteria whether the launch should
285      * proceed or not is specific to the launch configuration type.
286      *
287      * @param configuration launch configuration
288      * @param launch the launch object to contribute processes and debug targets to
289      * @param monitor a progress monitor
290      *
291      * @throws CoreException exception thrown if launch fails or canceled or if unable to retrieve attributes
292      * from the launch configuration
293      *
294      */

295     protected void preLaunchCheck(ILaunchConfiguration configuration, ILaunch launch, IProgressMonitor monitor)
296             throws CoreException {
297         boolean autoValidate = configuration.getAttribute(IPDELauncherConstants.AUTOMATIC_VALIDATE, false);
298         monitor.beginTask("", autoValidate ? 3 : 4); //$NON-NLS-1$
299
if (autoValidate)
300             validatePluginDependencies(configuration, new SubProgressMonitor(monitor, 1));
301         validateProjectDependencies(configuration, new SubProgressMonitor(monitor, 1));
302         clear(configuration, new SubProgressMonitor(monitor, 1));
303         launch.setAttribute(IPDELauncherConstants.CONFIG_LOCATION, getConfigDir(configuration).toString());
304         synchronizeManifests(configuration, new SubProgressMonitor(monitor, 1));
305     }
306
307     /**
308      * Returns the configuration area specified by the given launch
309      * configuration.
310      *
311      * @param configuration
312      * launch configuration
313      * @return the directory path specified by the given launch
314      * configuration
315      */

316     protected File JavaDoc getConfigDir(ILaunchConfiguration configuration) {
317         if (fConfigDir == null)
318             fConfigDir = LaunchConfigurationHelper.getConfigurationArea(configuration);
319         return fConfigDir;
320     }
321
322     /*
323     /* (non-Javadoc)
324      * @see org.eclipse.debug.core.model.LaunchConfigurationDelegate#getBuildOrder(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String)
325      */

326     protected IProject[] getBuildOrder(ILaunchConfiguration configuration,
327             String JavaDoc mode) throws CoreException {
328         return computeBuildOrder(LaunchPluginValidator.getAffectedProjects(configuration));
329     }
330
331     /*
332      * (non-Javadoc)
333      * @see org.eclipse.debug.core.model.LaunchConfigurationDelegate#getProjectsForProblemSearch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String)
334      */

335     protected IProject[] getProjectsForProblemSearch(
336             ILaunchConfiguration configuration, String JavaDoc mode)
337             throws CoreException {
338         return LaunchPluginValidator.getAffectedProjects(configuration);
339     }
340     
341     /**
342      * Returns the fully-qualified name of the class to launch.
343      *
344      * @return the fully-qualified name of the class to launch. Must not return <code>null</code>.
345      * @since 3.3
346      */

347     public String JavaDoc getMainClass() {
348         if (TargetPlatformHelper.getTargetVersion() >= 3.3)
349             return "org.eclipse.equinox.launcher.Main"; //$NON-NLS-1$
350
return "org.eclipse.core.launcher.Main"; //$NON-NLS-1$
351
}
352     
353     /**
354      * Adds a listener to the launch to be notified at interesting launch lifecycle
355      * events such as when the launch terminates.
356      *
357      * @param launch
358      * the launch
359      *
360      * @since 3.3
361      */

362     protected void manageLaunch(ILaunch launch) {
363         PDEPlugin.getDefault().getLaunchListener().manage(launch);
364     }
365     
366     /**
367      * Checks for old-style plugin.xml files that have become stale since the last launch.
368      * For any stale plugin.xml files found, the corresponding MANIFEST.MF is deleted
369      * from the runtime configuration area so that it gets regenerated upon startup.
370      *
371      * @param configuration
372      * the launch configuration
373      * @param monitor
374      * a progress monitor
375      *
376      * @since 3.3
377      */

378     protected void synchronizeManifests(ILaunchConfiguration configuration, IProgressMonitor monitor) {
379         LaunchConfigurationHelper.synchronizeManifests(configuration, getConfigDir(configuration));
380         monitor.done();
381     }
382     
383     /**
384      * Checks if the Automated Management of Dependencies option is turned on.
385      * If so, it makes aure all manifests are updated with the correct dependencies.
386      *
387      * @param configuration
388      * the launch configuration
389      * @param monitor
390      * a progress monitor
391      *
392      * @since 3.3
393      */

394     protected void validateProjectDependencies(ILaunchConfiguration configuration, IProgressMonitor monitor) {
395         LauncherUtils.validateProjectDependencies(configuration, monitor);
396     }
397     
398     /**
399      * By default, this method does nothing. Clients should override, if appropriate.
400      *
401      * @param configuration
402      * the launch configuration
403      * @param monitor
404      * the progress monitor
405      * @throws CoreException
406      * if unable to retrieve launch attribute values
407      * @since 3.3
408      */

409     protected void clear(ILaunchConfiguration configuration, IProgressMonitor monitor) throws CoreException {
410     }
411     
412     /**
413      * Validates inter-bundle dependencies automatically prior to launching
414      * if that option is turned on.
415      *
416      * @param configuration
417      * the launch configuration
418      * @param monitor
419      * a progress monitor
420      * @since 3.3
421      */

422     protected void validatePluginDependencies(ILaunchConfiguration configuration, IProgressMonitor monitor) throws CoreException {
423         EclipsePluginValidationOperation op = new EclipsePluginValidationOperation(configuration);
424         LaunchPluginValidator.runValidationOperation(op, monitor);
425     }
426     
427 }
428
Popular Tags