KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > launcher > WorkbenchLaunchConfigurationDelegate


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.internal.ui.launcher;
12
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.FileOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Properties JavaDoc;
19 import java.util.StringTokenizer JavaDoc;
20 import java.util.TreeMap JavaDoc;
21
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.core.runtime.Path;
27 import org.eclipse.core.runtime.SubProgressMonitor;
28 import org.eclipse.debug.core.DebugPlugin;
29 import org.eclipse.debug.core.ILaunch;
30 import org.eclipse.debug.core.ILaunchConfiguration;
31 import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
32 import org.eclipse.jdt.launching.ExecutionArguments;
33 import org.eclipse.jdt.launching.IVMInstall;
34 import org.eclipse.jdt.launching.VMRunnerConfiguration;
35 import org.eclipse.pde.core.plugin.IPluginModelBase;
36 import org.eclipse.pde.internal.core.ClasspathHelper;
37 import org.eclipse.pde.internal.core.ExternalModelManager;
38 import org.eclipse.pde.internal.core.ICoreConstants;
39 import org.eclipse.pde.internal.core.PDECore;
40 import org.eclipse.pde.internal.core.TargetPlatform;
41 import org.eclipse.pde.internal.ui.PDEPlugin;
42 import org.eclipse.pde.internal.ui.PDEUIMessages;
43
44 public class WorkbenchLaunchConfigurationDelegate extends LaunchConfigurationDelegate
45             implements ILauncherSettings {
46     protected File JavaDoc fConfigDir = null;
47     
48     /*
49      * @see ILaunchConfigurationDelegate#launch(ILaunchConfiguration, String)
50      */

51     public void launch(
52         ILaunchConfiguration configuration,
53         String JavaDoc mode,
54         ILaunch launch,
55         final IProgressMonitor monitor)
56         throws CoreException {
57         try {
58             fConfigDir = null;
59             monitor.beginTask("", 5); //$NON-NLS-1$
60

61             String JavaDoc workspace = configuration.getAttribute(LOCATION + "0", LauncherUtils.getDefaultPath().append("runtime-workbench-workspace").toOSString()); //$NON-NLS-1$ //$NON-NLS-2$
62
// Clear workspace and prompt, if necessary
63
if (!LauncherUtils.clearWorkspace(configuration, workspace, new SubProgressMonitor(monitor, 1))) {
64                 monitor.setCanceled(true);
65                 return;
66             }
67
68             // clear config area, if necessary
69
if (configuration.getAttribute(CONFIG_CLEAR_AREA, false))
70                 LauncherUtils.clearConfigArea(getConfigDir(configuration), new SubProgressMonitor(monitor, 1));
71             launch.setAttribute(ILauncherSettings.CONFIG_LOCATION, getConfigDir(configuration).toString());
72             
73             
74             // create launcher
75
IVMInstall launcher = LauncherUtils.createLauncher(configuration);
76             monitor.worked(1);
77             
78             // load the arguments on the launcher
79
VMRunnerConfiguration runnerConfig = createVMRunner(configuration);
80             if (runnerConfig == null) {
81                 monitor.setCanceled(true);
82                 return;
83             }
84             monitor.worked(1);
85                         
86             LauncherUtils.setDefaultSourceLocator(configuration, launch);
87             LauncherUtils.synchronizeManifests(configuration, getConfigDir(configuration));
88             PDEPlugin.getDefault().getLaunchListener().manage(launch);
89             launcher.getVMRunner(mode).run(runnerConfig, launch, monitor);
90             monitor.worked(1);
91         } catch (CoreException e) {
92             monitor.setCanceled(true);
93             throw e;
94         }
95     }
96
97     protected VMRunnerConfiguration createVMRunner(ILaunchConfiguration configuration)
98         throws CoreException {
99         String JavaDoc[] classpath = LauncherUtils.constructClasspath(configuration);
100         if (classpath == null) {
101             String JavaDoc message = PDEUIMessages.WorkbenchLauncherConfigurationDelegate_noStartup;
102             throw new CoreException(LauncherUtils.createErrorStatus(message));
103         }
104         
105         // Program arguments
106
String JavaDoc[] programArgs = getProgramArguments(configuration);
107         if (programArgs == null)
108             return null;
109
110         // Environment variables
111
String JavaDoc[] envp =
112             DebugPlugin.getDefault().getLaunchManager().getEnvironment(configuration);
113
114         VMRunnerConfiguration runnerConfig =
115             new VMRunnerConfiguration("org.eclipse.core.launcher.Main", classpath); //$NON-NLS-1$
116
runnerConfig.setVMArguments(getVMArguments(configuration));
117         runnerConfig.setProgramArguments(programArgs);
118         runnerConfig.setEnvironment(envp);
119         runnerConfig.setVMSpecificAttributesMap(LauncherUtils.getVMSpecificAttributes(configuration));
120         return runnerConfig;
121     }
122     
123     protected String JavaDoc[] getProgramArguments(ILaunchConfiguration configuration) throws CoreException {
124         ArrayList JavaDoc programArgs = new ArrayList JavaDoc();
125         
126         // If a product is specified, then add it to the program args
127
if (configuration.getAttribute(USE_PRODUCT, false)) {
128             programArgs.add("-product"); //$NON-NLS-1$
129
programArgs.add(configuration.getAttribute(PRODUCT, "")); //$NON-NLS-1$
130
} else {
131             // specify the application to launch
132
programArgs.add("-application"); //$NON-NLS-1$
133
programArgs.add(configuration.getAttribute(APPLICATION, LauncherUtils.getDefaultApplicationName()));
134         }
135         
136         // specify the workspace location for the runtime workbench
137
String JavaDoc targetWorkspace =
138             configuration.getAttribute(LOCATION + "0", LauncherUtils.getDefaultPath().append("runtime-workbench-workspace").toOSString()); //$NON-NLS-1$ //$NON-NLS-2$
139
programArgs.add("-data"); //$NON-NLS-1$
140
programArgs.add(targetWorkspace);
141         
142         boolean isOSGI = PDECore.getDefault().getModelManager().isOSGiRuntime();
143         boolean showSplash = true;
144         if (configuration.getAttribute(USEFEATURES, false)) {
145             validateFeatures();
146             IPath installPath = PDEPlugin.getWorkspace().getRoot().getLocation();
147             programArgs.add("-install"); //$NON-NLS-1$
148
programArgs.add("file:" + installPath.removeLastSegments(1).addTrailingSeparator().toString()); //$NON-NLS-1$
149
if (isOSGI && !configuration.getAttribute(CONFIG_USE_DEFAULT_AREA, true)) {
150                 programArgs.add("-configuration"); //$NON-NLS-1$
151
programArgs.add("file:" + new Path(getConfigDir(configuration).getPath()).addTrailingSeparator().toString()); //$NON-NLS-1$
152
}
153             programArgs.add("-update"); //$NON-NLS-1$
154
// add the output folder names
155
programArgs.add("-dev"); //$NON-NLS-1$
156
if (PDECore.getDefault().getModelManager().isOSGiRuntime())
157                 programArgs.add(ClasspathHelper.getDevEntriesProperties(getConfigDir(configuration).toString() + "/dev.properties", true)); //$NON-NLS-1$
158
else
159                 programArgs.add(ClasspathHelper.getDevEntries(true));
160             
161         } else {
162             TreeMap JavaDoc pluginMap = LauncherUtils.getPluginsToRun(configuration);
163             if (pluginMap == null)
164                 return null;
165                 
166             if (isOSGI) {
167                 String JavaDoc productID = LauncherUtils.getProductID(configuration);
168                 Properties JavaDoc prop = LauncherUtils.createConfigIniFile(configuration,
169                         productID, pluginMap, getConfigDir(configuration));
170                 showSplash = prop.containsKey("osgi.splashPath") || prop.containsKey("splashLocation"); //$NON-NLS-1$ //$NON-NLS-2$
171
TargetPlatform.createPlatformConfigurationArea(
172                         pluginMap,
173                         getConfigDir(configuration),
174                         LauncherUtils.getContributingPlugin(productID));
175             } else {
176                 String JavaDoc primaryPlugin = LauncherUtils.getPrimaryPlugin();
177                 TargetPlatform.createPlatformConfigurationArea(
178                         pluginMap,
179                         getConfigDir(configuration),
180                         primaryPlugin);
181                 if (primaryPlugin != null) {
182                     programArgs.add("-feature"); //$NON-NLS-1$
183
programArgs.add(primaryPlugin);
184                 }
185                 IPluginModelBase bootModel = (IPluginModelBase)pluginMap.get("org.eclipse.core.boot"); //$NON-NLS-1$
186
String JavaDoc bootPath = LauncherUtils.getBootPath(bootModel);
187                 if (bootPath != null && !bootPath.endsWith(".jar")) { //$NON-NLS-1$
188
programArgs.add("-boot"); //$NON-NLS-1$
189
programArgs.add("file:" + bootPath); //$NON-NLS-1$
190
}
191             }
192             
193             programArgs.add("-configuration"); //$NON-NLS-1$
194
if (isOSGI)
195                 programArgs.add("file:" + new Path(getConfigDir(configuration).getPath()).addTrailingSeparator().toString()); //$NON-NLS-1$
196
else
197                 programArgs.add("file:" + new Path(getConfigDir(configuration).getPath()).append("platform.cfg").toString()); //$NON-NLS-1$ //$NON-NLS-2$
198

199             // add the output folder names
200
programArgs.add("-dev"); //$NON-NLS-1$
201
if (PDECore.getDefault().getModelManager().isOSGiRuntime())
202                 programArgs.add(ClasspathHelper.getDevEntriesProperties(getConfigDir(configuration).toString() + "/dev.properties", pluginMap)); //$NON-NLS-1$
203
else
204                 programArgs.add(ClasspathHelper.getDevEntries(true));
205         }
206         
207         // necessary for PDE to know how to load plugins when target platform = host platform
208
// see PluginPathFinder.getPluginPaths()
209
programArgs.add("-pdelaunch"); //$NON-NLS-1$
210

211         // add tracing, if turned on
212
if (configuration.getAttribute(TRACING, false)
213                 && !TRACING_NONE.equals(configuration.getAttribute(TRACING_CHECKED, (String JavaDoc) null))) {
214             programArgs.add("-debug"); //$NON-NLS-1$
215
programArgs.add(
216                 LauncherUtils.getTracingFileArgument(
217                     configuration,
218                     getConfigDir(configuration).toString() + Path.SEPARATOR + ".options")); //$NON-NLS-1$
219
}
220
221         // add the program args specified by the user
222
StringTokenizer JavaDoc tokenizer =
223             new StringTokenizer JavaDoc(configuration.getAttribute(PROGARGS, "")); //$NON-NLS-1$
224
while (tokenizer.hasMoreTokens()) {
225             String JavaDoc token = tokenizer.nextToken();
226             // be forgiving if people have tracing turned on and forgot
227
// to remove the -debug from the program args field.
228
if (token.equals("-debug") && programArgs.contains("-debug")) //$NON-NLS-1$ //$NON-NLS-2$
229
continue;
230             programArgs.add(token);
231         }
232
233         if (!programArgs.contains("-nosplash") && showSplash) { //$NON-NLS-1$
234
if (PDECore.getDefault().getTargetVersion().equals(ICoreConstants.TARGET31)) {
235                 programArgs.add(0, "-launcher"); //$NON-NLS-1$
236
IPath path = ExternalModelManager.getEclipseHome().append("eclipse"); //$NON-NLS-1$
237
programArgs.add(1, path.toOSString()); //This could be the branded launcher if we want (also this does not bring much)
238
programArgs.add(2, "-name"); //$NON-NLS-1$
239
programArgs.add(3, "Eclipse"); //This should be the name of the product //$NON-NLS-1$
240
programArgs.add(4, "-showsplash"); //$NON-NLS-1$
241
programArgs.add(5, "600"); //$NON-NLS-1$
242
} else {
243                 programArgs.add(0, "-showsplash"); //$NON-NLS-1$
244
programArgs.add(1, computeShowsplashArgument());
245             }
246         }
247         if (!programArgs.contains("-os")) { //$NON-NLS-1$
248
programArgs.add("-os"); //$NON-NLS-1$
249
programArgs.add(TargetPlatform.getOS());
250         }
251         if (!programArgs.contains("-ws")) { //$NON-NLS-1$
252
programArgs.add("-ws"); //$NON-NLS-1$
253
programArgs.add(TargetPlatform.getWS());
254         }
255         if (!programArgs.contains("-arch")) { //$NON-NLS-1$
256
programArgs.add("-arch"); //$NON-NLS-1$
257
programArgs.add(TargetPlatform.getOSArch());
258         }
259         return (String JavaDoc[])programArgs.toArray(new String JavaDoc[programArgs.size()]);
260     }
261     
262     protected String JavaDoc[] getVMArguments(ILaunchConfiguration configuration) throws CoreException {
263         return new ExecutionArguments(configuration.getAttribute(VMARGS,""),"").getVMArgumentsArray(); //$NON-NLS-1$ //$NON-NLS-2$
264
}
265             
266     protected void validateFeatures() throws CoreException {
267         IPath installPath = PDEPlugin.getWorkspace().getRoot().getLocation();
268         String JavaDoc lastSegment = installPath.lastSegment();
269         boolean badStructure = lastSegment == null;
270         if (!badStructure) {
271             IPath featuresPath = installPath.removeLastSegments(1).append("features"); //$NON-NLS-1$
272
badStructure = !lastSegment.equalsIgnoreCase("plugins") //$NON-NLS-1$
273
|| !featuresPath.toFile().exists();
274         }
275         if (badStructure) {
276             throw new CoreException(LauncherUtils.createErrorStatus(PDEUIMessages.WorkbenchLauncherConfigurationDelegate_badFeatureSetup));
277         }
278         // Ensure important files are present
279
ensureProductFilesExist(getProductPath());
280     }
281     
282     protected IPath getInstallPath() {
283         return PDEPlugin.getWorkspace().getRoot().getLocation();
284     }
285     
286     protected IPath getProductPath() {
287         return getInstallPath().removeLastSegments(1);
288     }
289
290     protected String JavaDoc computeShowsplashArgument() {
291         IPath eclipseHome = ExternalModelManager.getEclipseHome();
292         IPath fullPath = eclipseHome.append("eclipse"); //$NON-NLS-1$
293
return fullPath.toOSString() + " -showsplash 600"; //$NON-NLS-1$
294
}
295
296     protected void ensureProductFilesExist(IPath productArea) {
297         File JavaDoc productDir = productArea.toFile();
298         File JavaDoc marker = new File JavaDoc(productDir, ".eclipseproduct"); //$NON-NLS-1$
299
IPath eclipsePath = ExternalModelManager.getEclipseHome();
300         if (!marker.exists())
301             copyFile(eclipsePath, ".eclipseproduct", marker); //$NON-NLS-1$
302

303         if (PDECore.getDefault().getModelManager().isOSGiRuntime()) {
304             File JavaDoc configDir = new File JavaDoc(productDir, "configuration"); //$NON-NLS-1$
305
if (!configDir.exists())
306                 configDir.mkdirs();
307             File JavaDoc ini = new File JavaDoc(configDir, "config.ini"); //$NON-NLS-1$
308
if (!ini.exists())
309                 copyFile(eclipsePath.append("configuration"), "config.ini", ini); //$NON-NLS-1$ //$NON-NLS-2$
310
} else {
311             File JavaDoc ini = new File JavaDoc(productDir, "install.ini"); //$NON-NLS-1$
312
if (!ini.exists())
313                 copyFile(eclipsePath, "install.ini", ini); //$NON-NLS-1$
314
}
315     }
316
317     protected void copyFile(IPath eclipsePath, String JavaDoc name, File JavaDoc target) {
318         File JavaDoc source = new File JavaDoc(eclipsePath.toFile(), name);
319         if (source.exists() == false)
320             return;
321         FileInputStream JavaDoc is = null;
322         FileOutputStream JavaDoc os = null;
323         try {
324             is = new FileInputStream JavaDoc(source);
325             os = new FileOutputStream JavaDoc(target);
326             byte[] buf = new byte[1024];
327             long currentLen = 0;
328             int len = is.read(buf);
329             while (len != -1) {
330                 currentLen += len;
331                 os.write(buf, 0, len);
332                 len = is.read(buf);
333             }
334         } catch (IOException JavaDoc e) {
335         } finally {
336             try {
337                 if (is != null)
338                     is.close();
339                 if (os != null)
340                     os.close();
341             } catch (IOException JavaDoc e) {
342             }
343         }
344     }
345     
346     /* (non-Javadoc)
347      * @see org.eclipse.debug.core.model.LaunchConfigurationDelegate#getBuildOrder(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String)
348      */

349     protected IProject[] getBuildOrder(ILaunchConfiguration configuration,
350             String JavaDoc mode) throws CoreException {
351         return computeBuildOrder(LauncherUtils.getAffectedProjects(configuration));
352     }
353     
354     /* (non-Javadoc)
355      * @see org.eclipse.debug.core.model.LaunchConfigurationDelegate#getProjectsForProblemSearch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String)
356      */

357     protected IProject[] getProjectsForProblemSearch(
358             ILaunchConfiguration configuration, String JavaDoc mode)
359             throws CoreException {
360         return LauncherUtils.getAffectedProjects(configuration);
361     }
362     
363     protected File JavaDoc getConfigDir(ILaunchConfiguration config) {
364         if (fConfigDir == null) {
365             try {
366                 if (config.getAttribute(USEFEATURES, false) && config.getAttribute(CONFIG_USE_DEFAULT_AREA, true)) {
367                     String JavaDoc root = getProductPath().toString();
368                     if (PDECore.getDefault().getModelManager().isOSGiRuntime())
369                         root += "/configuration"; //$NON-NLS-1$
370
fConfigDir = new File JavaDoc(root);
371                 } else {
372                     fConfigDir = LauncherUtils.createConfigArea(config);
373                 }
374             } catch (CoreException e) {
375                 fConfigDir = LauncherUtils.createConfigArea(config);
376             }
377         }
378         if (!fConfigDir.exists())
379             fConfigDir.mkdirs();
380         return fConfigDir;
381     }
382 }
383
Popular Tags