1 11 package org.eclipse.pde.internal.ui.launcher; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.FileOutputStream ; 16 import java.io.IOException ; 17 import java.util.ArrayList ; 18 import java.util.Properties ; 19 import java.util.StringTokenizer ; 20 import java.util.TreeMap ; 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 fConfigDir = null; 47 48 51 public void launch( 52 ILaunchConfiguration configuration, 53 String mode, 54 ILaunch launch, 55 final IProgressMonitor monitor) 56 throws CoreException { 57 try { 58 fConfigDir = null; 59 monitor.beginTask("", 5); 61 String workspace = configuration.getAttribute(LOCATION + "0", LauncherUtils.getDefaultPath().append("runtime-workbench-workspace").toOSString()); if (!LauncherUtils.clearWorkspace(configuration, workspace, new SubProgressMonitor(monitor, 1))) { 64 monitor.setCanceled(true); 65 return; 66 } 67 68 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 IVMInstall launcher = LauncherUtils.createLauncher(configuration); 76 monitor.worked(1); 77 78 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 [] classpath = LauncherUtils.constructClasspath(configuration); 100 if (classpath == null) { 101 String message = PDEUIMessages.WorkbenchLauncherConfigurationDelegate_noStartup; 102 throw new CoreException(LauncherUtils.createErrorStatus(message)); 103 } 104 105 String [] programArgs = getProgramArguments(configuration); 107 if (programArgs == null) 108 return null; 109 110 String [] envp = 112 DebugPlugin.getDefault().getLaunchManager().getEnvironment(configuration); 113 114 VMRunnerConfiguration runnerConfig = 115 new VMRunnerConfiguration("org.eclipse.core.launcher.Main", classpath); 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 [] getProgramArguments(ILaunchConfiguration configuration) throws CoreException { 124 ArrayList programArgs = new ArrayList (); 125 126 if (configuration.getAttribute(USE_PRODUCT, false)) { 128 programArgs.add("-product"); programArgs.add(configuration.getAttribute(PRODUCT, "")); } else { 131 programArgs.add("-application"); programArgs.add(configuration.getAttribute(APPLICATION, LauncherUtils.getDefaultApplicationName())); 134 } 135 136 String targetWorkspace = 138 configuration.getAttribute(LOCATION + "0", LauncherUtils.getDefaultPath().append("runtime-workbench-workspace").toOSString()); programArgs.add("-data"); 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"); programArgs.add("file:" + installPath.removeLastSegments(1).addTrailingSeparator().toString()); if (isOSGI && !configuration.getAttribute(CONFIG_USE_DEFAULT_AREA, true)) { 150 programArgs.add("-configuration"); programArgs.add("file:" + new Path(getConfigDir(configuration).getPath()).addTrailingSeparator().toString()); } 153 programArgs.add("-update"); programArgs.add("-dev"); if (PDECore.getDefault().getModelManager().isOSGiRuntime()) 157 programArgs.add(ClasspathHelper.getDevEntriesProperties(getConfigDir(configuration).toString() + "/dev.properties", true)); else 159 programArgs.add(ClasspathHelper.getDevEntries(true)); 160 161 } else { 162 TreeMap pluginMap = LauncherUtils.getPluginsToRun(configuration); 163 if (pluginMap == null) 164 return null; 165 166 if (isOSGI) { 167 String productID = LauncherUtils.getProductID(configuration); 168 Properties prop = LauncherUtils.createConfigIniFile(configuration, 169 productID, pluginMap, getConfigDir(configuration)); 170 showSplash = prop.containsKey("osgi.splashPath") || prop.containsKey("splashLocation"); TargetPlatform.createPlatformConfigurationArea( 172 pluginMap, 173 getConfigDir(configuration), 174 LauncherUtils.getContributingPlugin(productID)); 175 } else { 176 String primaryPlugin = LauncherUtils.getPrimaryPlugin(); 177 TargetPlatform.createPlatformConfigurationArea( 178 pluginMap, 179 getConfigDir(configuration), 180 primaryPlugin); 181 if (primaryPlugin != null) { 182 programArgs.add("-feature"); programArgs.add(primaryPlugin); 184 } 185 IPluginModelBase bootModel = (IPluginModelBase)pluginMap.get("org.eclipse.core.boot"); String bootPath = LauncherUtils.getBootPath(bootModel); 187 if (bootPath != null && !bootPath.endsWith(".jar")) { programArgs.add("-boot"); programArgs.add("file:" + bootPath); } 191 } 192 193 programArgs.add("-configuration"); if (isOSGI) 195 programArgs.add("file:" + new Path(getConfigDir(configuration).getPath()).addTrailingSeparator().toString()); else 197 programArgs.add("file:" + new Path(getConfigDir(configuration).getPath()).append("platform.cfg").toString()); 199 programArgs.add("-dev"); if (PDECore.getDefault().getModelManager().isOSGiRuntime()) 202 programArgs.add(ClasspathHelper.getDevEntriesProperties(getConfigDir(configuration).toString() + "/dev.properties", pluginMap)); else 204 programArgs.add(ClasspathHelper.getDevEntries(true)); 205 } 206 207 programArgs.add("-pdelaunch"); 211 if (configuration.getAttribute(TRACING, false) 213 && !TRACING_NONE.equals(configuration.getAttribute(TRACING_CHECKED, (String ) null))) { 214 programArgs.add("-debug"); programArgs.add( 216 LauncherUtils.getTracingFileArgument( 217 configuration, 218 getConfigDir(configuration).toString() + Path.SEPARATOR + ".options")); } 220 221 StringTokenizer tokenizer = 223 new StringTokenizer (configuration.getAttribute(PROGARGS, "")); while (tokenizer.hasMoreTokens()) { 225 String token = tokenizer.nextToken(); 226 if (token.equals("-debug") && programArgs.contains("-debug")) continue; 230 programArgs.add(token); 231 } 232 233 if (!programArgs.contains("-nosplash") && showSplash) { if (PDECore.getDefault().getTargetVersion().equals(ICoreConstants.TARGET31)) { 235 programArgs.add(0, "-launcher"); IPath path = ExternalModelManager.getEclipseHome().append("eclipse"); programArgs.add(1, path.toOSString()); programArgs.add(2, "-name"); programArgs.add(3, "Eclipse"); programArgs.add(4, "-showsplash"); programArgs.add(5, "600"); } else { 243 programArgs.add(0, "-showsplash"); programArgs.add(1, computeShowsplashArgument()); 245 } 246 } 247 if (!programArgs.contains("-os")) { programArgs.add("-os"); programArgs.add(TargetPlatform.getOS()); 250 } 251 if (!programArgs.contains("-ws")) { programArgs.add("-ws"); programArgs.add(TargetPlatform.getWS()); 254 } 255 if (!programArgs.contains("-arch")) { programArgs.add("-arch"); programArgs.add(TargetPlatform.getOSArch()); 258 } 259 return (String [])programArgs.toArray(new String [programArgs.size()]); 260 } 261 262 protected String [] getVMArguments(ILaunchConfiguration configuration) throws CoreException { 263 return new ExecutionArguments(configuration.getAttribute(VMARGS,""),"").getVMArgumentsArray(); } 265 266 protected void validateFeatures() throws CoreException { 267 IPath installPath = PDEPlugin.getWorkspace().getRoot().getLocation(); 268 String lastSegment = installPath.lastSegment(); 269 boolean badStructure = lastSegment == null; 270 if (!badStructure) { 271 IPath featuresPath = installPath.removeLastSegments(1).append("features"); badStructure = !lastSegment.equalsIgnoreCase("plugins") || !featuresPath.toFile().exists(); 274 } 275 if (badStructure) { 276 throw new CoreException(LauncherUtils.createErrorStatus(PDEUIMessages.WorkbenchLauncherConfigurationDelegate_badFeatureSetup)); 277 } 278 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 computeShowsplashArgument() { 291 IPath eclipseHome = ExternalModelManager.getEclipseHome(); 292 IPath fullPath = eclipseHome.append("eclipse"); return fullPath.toOSString() + " -showsplash 600"; } 295 296 protected void ensureProductFilesExist(IPath productArea) { 297 File productDir = productArea.toFile(); 298 File marker = new File (productDir, ".eclipseproduct"); IPath eclipsePath = ExternalModelManager.getEclipseHome(); 300 if (!marker.exists()) 301 copyFile(eclipsePath, ".eclipseproduct", marker); 303 if (PDECore.getDefault().getModelManager().isOSGiRuntime()) { 304 File configDir = new File (productDir, "configuration"); if (!configDir.exists()) 306 configDir.mkdirs(); 307 File ini = new File (configDir, "config.ini"); if (!ini.exists()) 309 copyFile(eclipsePath.append("configuration"), "config.ini", ini); } else { 311 File ini = new File (productDir, "install.ini"); if (!ini.exists()) 313 copyFile(eclipsePath, "install.ini", ini); } 315 } 316 317 protected void copyFile(IPath eclipsePath, String name, File target) { 318 File source = new File (eclipsePath.toFile(), name); 319 if (source.exists() == false) 320 return; 321 FileInputStream is = null; 322 FileOutputStream os = null; 323 try { 324 is = new FileInputStream (source); 325 os = new FileOutputStream (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 e) { 335 } finally { 336 try { 337 if (is != null) 338 is.close(); 339 if (os != null) 340 os.close(); 341 } catch (IOException e) { 342 } 343 } 344 } 345 346 349 protected IProject[] getBuildOrder(ILaunchConfiguration configuration, 350 String mode) throws CoreException { 351 return computeBuildOrder(LauncherUtils.getAffectedProjects(configuration)); 352 } 353 354 357 protected IProject[] getProjectsForProblemSearch( 358 ILaunchConfiguration configuration, String mode) 359 throws CoreException { 360 return LauncherUtils.getAffectedProjects(configuration); 361 } 362 363 protected File getConfigDir(ILaunchConfiguration config) { 364 if (fConfigDir == null) { 365 try { 366 if (config.getAttribute(USEFEATURES, false) && config.getAttribute(CONFIG_USE_DEFAULT_AREA, true)) { 367 String root = getProductPath().toString(); 368 if (PDECore.getDefault().getModelManager().isOSGiRuntime()) 369 root += "/configuration"; fConfigDir = new File (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 |