KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Properties JavaDoc;
17
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.Path;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.variables.IStringVariableManager;
24 import org.eclipse.core.variables.VariablesPlugin;
25 import org.eclipse.debug.core.ILaunch;
26 import org.eclipse.debug.core.ILaunchConfiguration;
27 import org.eclipse.pde.core.plugin.IPluginModelBase;
28 import org.eclipse.pde.core.plugin.TargetPlatform;
29 import org.eclipse.pde.internal.core.ClasspathHelper;
30 import org.eclipse.pde.internal.core.PDECore;
31 import org.eclipse.pde.internal.core.TargetPlatformHelper;
32 import org.eclipse.pde.internal.core.util.CoreUtility;
33 import org.eclipse.pde.internal.core.util.VersionUtil;
34 import org.eclipse.pde.internal.ui.PDEPlugin;
35 import org.eclipse.pde.internal.ui.PDEUIMessages;
36 import org.eclipse.pde.internal.ui.launcher.LaunchArgumentsHelper;
37 import org.eclipse.pde.internal.ui.launcher.LaunchConfigurationHelper;
38 import org.eclipse.pde.internal.ui.launcher.LaunchPluginValidator;
39 import org.eclipse.pde.internal.ui.launcher.LauncherUtils;
40 import org.osgi.framework.Version;
41
42 /**
43  * A launch delegate for launching Eclipse applications
44  * <p>
45  * Clients may subclass and instantiate this class.
46  * </p>
47  * @since 3.2
48  */

49 public class EclipseApplicationLaunchConfiguration extends AbstractPDELaunchConfiguration {
50     
51     /*
52      * (non-Javadoc)
53      * @see org.eclipse.pde.ui.launcher.AbstractPDELaunchConfiguration#getProgramArguments(org.eclipse.debug.core.ILaunchConfiguration)
54      */

55     public String JavaDoc[] getProgramArguments(ILaunchConfiguration configuration) throws CoreException {
56         ArrayList JavaDoc programArgs = new ArrayList JavaDoc();
57         
58         // If a product is specified, then add it to the program args
59
if (configuration.getAttribute(IPDELauncherConstants.USE_PRODUCT, false)) {
60             programArgs.add("-product"); //$NON-NLS-1$
61
programArgs.add(configuration.getAttribute(IPDELauncherConstants.PRODUCT, "")); //$NON-NLS-1$
62
} else {
63             // specify the application to launch
64
programArgs.add("-application"); //$NON-NLS-1$
65
programArgs.add(configuration.getAttribute(IPDELauncherConstants.APPLICATION, TargetPlatform.getDefaultApplication()));
66         }
67         
68         // specify the workspace location for the runtime workbench
69
String JavaDoc targetWorkspace = LaunchArgumentsHelper.getWorkspaceLocation(configuration);
70         if (targetWorkspace.length() > 0) {
71             programArgs.add("-data"); //$NON-NLS-1$
72
programArgs.add(targetWorkspace);
73         }
74         
75         boolean showSplash = true;
76         Map JavaDoc pluginMap = LaunchPluginValidator.getPluginsToRun(configuration);
77         if (configuration.getAttribute(IPDELauncherConstants.USEFEATURES, false)) {
78             validateFeatures();
79             IPath installPath = PDEPlugin.getWorkspace().getRoot().getLocation();
80             programArgs.add("-install"); //$NON-NLS-1$
81
programArgs.add("file:" + installPath.removeLastSegments(1).addTrailingSeparator().toString()); //$NON-NLS-1$
82
if (!configuration.getAttribute(IPDELauncherConstants.CONFIG_USE_DEFAULT_AREA, true)) {
83                 programArgs.add("-configuration"); //$NON-NLS-1$
84
programArgs.add("file:" + new Path(getConfigDir(configuration).getPath()).addTrailingSeparator().toString()); //$NON-NLS-1$
85
}
86             programArgs.add("-update"); //$NON-NLS-1$
87
// add the output folder names
88
programArgs.add("-dev"); //$NON-NLS-1$
89
programArgs.add(ClasspathHelper.getDevEntriesProperties(getConfigDir(configuration).toString() + "/dev.properties", true)); //$NON-NLS-1$
90
} else {
91             String JavaDoc productID = LaunchConfigurationHelper.getProductID(configuration);
92             Properties JavaDoc prop = LaunchConfigurationHelper.createConfigIniFile(configuration,
93                     productID, pluginMap, getConfigDir(configuration));
94             showSplash = prop.containsKey("osgi.splashPath") || prop.containsKey("splashLocation"); //$NON-NLS-1$ //$NON-NLS-2$
95
String JavaDoc brandingId = LaunchConfigurationHelper.getContributingPlugin(productID);
96             TargetPlatform.createPlatformConfiguration(
97                     getConfigDir(configuration),
98                     (IPluginModelBase[])pluginMap.values().toArray(new IPluginModelBase[pluginMap.size()]),
99                     brandingId != null ? (IPluginModelBase) pluginMap.get(brandingId) : null);
100             TargetPlatformHelper.checkPluginPropertiesConsistency(pluginMap, getConfigDir(configuration));
101             programArgs.add("-configuration"); //$NON-NLS-1$
102
programArgs.add("file:" + new Path(getConfigDir(configuration).getPath()).addTrailingSeparator().toString()); //$NON-NLS-1$
103

104             // add the output folder names
105
programArgs.add("-dev"); //$NON-NLS-1$
106
programArgs.add(ClasspathHelper.getDevEntriesProperties(getConfigDir(configuration).toString() + "/dev.properties", pluginMap)); //$NON-NLS-1$
107
}
108         // necessary for PDE to know how to load plugins when target platform = host platform
109
// see PluginPathFinder.getPluginPaths() and PluginPathFinder.isDevLaunchMode()
110
IPluginModelBase base = (IPluginModelBase)pluginMap.get(PDECore.PLUGIN_ID);
111         if (base != null && VersionUtil.compareMacroMinorMicro(base.getBundleDescription().getVersion(), new Version("3.3.1")) < 0) //$NON-NLS-1$
112
programArgs.add("-pdelaunch"); //$NON-NLS-1$
113

114         String JavaDoc[] args = super.getProgramArguments(configuration);
115         for (int i = 0; i < args.length; i++) {
116             programArgs.add(args[i]);
117         }
118         
119         if (!programArgs.contains("-nosplash") && showSplash) { //$NON-NLS-1$
120
if (TargetPlatformHelper.getTargetVersion() >= 3.1) {
121                 programArgs.add(0, "-launcher"); //$NON-NLS-1$
122

123                 IPath path = null;
124                 if (TargetPlatform.getOS().equals("macosx") ) { //$NON-NLS-1$
125
path = new Path(TargetPlatform.getLocation()).append("Eclipse.app/Contents/Resources/Splash.app/Contents/MacOS/eclipse"); //$NON-NLS-1$
126
} else {
127                     path = new Path(TargetPlatform.getLocation()).append("eclipse"); //$NON-NLS-1$
128
}
129                 
130                 programArgs.add(1, path.toOSString()); //This could be the branded launcher if we want (also this does not bring much)
131
programArgs.add(2, "-name"); //$NON-NLS-1$
132
programArgs.add(3, "Eclipse"); //This should be the name of the product //$NON-NLS-1$
133
programArgs.add(4, "-showsplash"); //$NON-NLS-1$
134
programArgs.add(5, "600"); //$NON-NLS-1$
135
} else {
136                 programArgs.add(0, "-showsplash"); //$NON-NLS-1$
137
programArgs.add(1, computeShowsplashArgument());
138             }
139         }
140         return (String JavaDoc[])programArgs.toArray(new String JavaDoc[programArgs.size()]);
141     }
142     
143     private void validateFeatures() throws CoreException {
144         IPath installPath = PDEPlugin.getWorkspace().getRoot().getLocation();
145         String JavaDoc lastSegment = installPath.lastSegment();
146         boolean badStructure = lastSegment == null;
147         if (!badStructure) {
148             IPath featuresPath = installPath.removeLastSegments(1).append("features"); //$NON-NLS-1$
149
badStructure = !lastSegment.equalsIgnoreCase("plugins") //$NON-NLS-1$
150
|| !featuresPath.toFile().exists();
151         }
152         if (badStructure) {
153             throw new CoreException(LauncherUtils.createErrorStatus(PDEUIMessages.WorkbenchLauncherConfigurationDelegate_badFeatureSetup));
154         }
155         // Ensure important files are present
156
ensureProductFilesExist(getProductPath());
157     }
158     
159     private IPath getProductPath() {
160         return PDEPlugin.getWorkspace().getRoot().getLocation().removeLastSegments(1);
161     }
162
163     private String JavaDoc computeShowsplashArgument() {
164         IPath eclipseHome = new Path(TargetPlatform.getLocation());
165         IPath fullPath = eclipseHome.append("eclipse"); //$NON-NLS-1$
166
return fullPath.toOSString() + " -showsplash 600"; //$NON-NLS-1$
167
}
168
169     private void ensureProductFilesExist(IPath productArea) {
170         File JavaDoc productDir = productArea.toFile();
171         File JavaDoc marker = new File JavaDoc(productDir, ".eclipseproduct"); //$NON-NLS-1$
172
IPath eclipsePath = new Path(TargetPlatform.getLocation());
173         if (!marker.exists())
174             CoreUtility.copyFile(eclipsePath, ".eclipseproduct", marker); //$NON-NLS-1$
175

176         File JavaDoc configDir = new File JavaDoc(productDir, "configuration"); //$NON-NLS-1$
177
if (!configDir.exists())
178             configDir.mkdirs();
179         File JavaDoc ini = new File JavaDoc(configDir, "config.ini"); //$NON-NLS-1$
180
if (!ini.exists())
181             CoreUtility.copyFile(eclipsePath.append("configuration"), "config.ini", ini); //$NON-NLS-1$ //$NON-NLS-2$
182
}
183
184     /*
185      * (non-Javadoc)
186      * @see org.eclipse.pde.ui.launcher.AbstractPDELaunchConfiguration#getConfigDir(org.eclipse.debug.core.ILaunchConfiguration)
187      */

188     protected File JavaDoc getConfigDir(ILaunchConfiguration config) {
189         if (fConfigDir == null) {
190             try {
191                 if (config.getAttribute(IPDELauncherConstants.USEFEATURES, false)
192                         && config.getAttribute(IPDELauncherConstants.CONFIG_USE_DEFAULT_AREA, true)) {
193                     String JavaDoc root = getProductPath().toString();
194                     root += "/configuration"; //$NON-NLS-1$
195
fConfigDir = new File JavaDoc(root);
196                     if (!fConfigDir.exists())
197                         fConfigDir.mkdirs();
198                 } else {
199                     fConfigDir = LaunchConfigurationHelper.getConfigurationArea(config);
200                 }
201             } catch (CoreException e) {
202                 fConfigDir = LaunchConfigurationHelper.getConfigurationArea(config);
203             }
204         }
205         return fConfigDir;
206     }
207     
208     /**
209      * Clears the workspace prior to launching if the workspace exists and the option to
210      * clear it is turned on. Also clears the configuration area if that option is chosen.
211      *
212      * @param configuration
213      * the launch configuration
214      * @param monitor
215      * the progress monitor
216      * @throws CoreException
217      * if unable to retrieve launch attribute values
218      * @since 3.3
219      */

220     protected void clear(ILaunchConfiguration configuration, IProgressMonitor monitor) throws CoreException {
221         String JavaDoc workspace = LaunchArgumentsHelper.getWorkspaceLocation(configuration);
222         // Clear workspace and prompt, if necessary
223
if (!LauncherUtils.clearWorkspace(configuration, workspace, monitor))
224             throw new CoreException(Status.CANCEL_STATUS);
225
226         // clear config area, if necessary
227
if (configuration.getAttribute(IPDELauncherConstants.CONFIG_CLEAR_AREA, false))
228             CoreUtility.deleteContent(getConfigDir(configuration));
229     }
230
231     /* (non-Javadoc)
232      * @see org.eclipse.pde.ui.launcher.AbstractPDELaunchConfiguration#preLaunchCheck(org.eclipse.debug.core.ILaunchConfiguration, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor)
233      */

234     protected void preLaunchCheck(ILaunchConfiguration configuration,
235             ILaunch launch, IProgressMonitor monitor) throws CoreException {
236         validateConfigIni(configuration);
237         super.preLaunchCheck(configuration, launch, monitor);
238     }
239     
240     private void validateConfigIni(ILaunchConfiguration configuration) throws CoreException {
241         if (!configuration.getAttribute(IPDELauncherConstants.CONFIG_GENERATE_DEFAULT, true)) {
242             String JavaDoc templateLoc = configuration.getAttribute(IPDELauncherConstants.CONFIG_TEMPLATE_LOCATION, ""); //$NON-NLS-1$
243
IStringVariableManager mgr = VariablesPlugin.getDefault().getStringVariableManager();
244             templateLoc = mgr.performStringSubstitution(templateLoc);
245             
246             File JavaDoc templateFile = new File JavaDoc(templateLoc);
247             if (!templateFile.exists()) {
248                 if (!LauncherUtils.generateConfigIni())
249                     throw new CoreException(Status.CANCEL_STATUS);
250                 // with the way the launcher works, if a config.ini file is not found one will be generated automatically.
251
// This check was to warn the user a config.ini needs to be generated. - bug 161265, comment #7
252
}
253         }
254     }
255
256     /* (non-Javadoc)
257      * @see org.eclipse.pde.ui.launcher.AbstractPDELaunchConfiguration#getVMArguments(org.eclipse.debug.core.ILaunchConfiguration)
258      */

259     public String JavaDoc[] getVMArguments(ILaunchConfiguration configuration)
260             throws CoreException {
261         String JavaDoc[] vmArgs = super.getVMArguments(configuration);
262         IPluginModelBase base = (IPluginModelBase)LaunchPluginValidator.getPluginsToRun(configuration).get(PDECore.PLUGIN_ID);
263         if (base != null && VersionUtil.compareMacroMinorMicro(base.getBundleDescription().getVersion(), new Version("3.3.1")) >= 0) { //$NON-NLS-1$
264
// necessary for PDE to know how to load plugins when target platform = host platform
265
// see PluginPathFinder.getPluginPaths() and PluginPathFinder.isDevLaunchMode()
266
String JavaDoc[] result = new String JavaDoc[vmArgs.length + 1];
267             System.arraycopy(vmArgs, 0, result, 0, vmArgs.length);
268             result[vmArgs.length] = "-Declipse.pde.launch=true"; //$NON-NLS-1$
269
return result;
270         }
271         return vmArgs;
272     }
273     
274 }
275
Popular Tags