1 12 package org.eclipse.pde.ui.launcher; 13 14 import java.io.File ; 15 import java.util.List ; 16 import java.util.Map ; 17 18 import org.eclipse.core.resources.IProject; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.core.runtime.IProgressMonitor; 22 import org.eclipse.core.runtime.IStatus; 23 import org.eclipse.core.runtime.Path; 24 import org.eclipse.core.runtime.Status; 25 import org.eclipse.core.runtime.SubProgressMonitor; 26 import org.eclipse.debug.core.DebugPlugin; 27 import org.eclipse.debug.core.ILaunch; 28 import org.eclipse.debug.core.ILaunchConfiguration; 29 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 30 import org.eclipse.jdt.core.IJavaProject; 31 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 32 import org.eclipse.jdt.launching.IVMInstall; 33 import org.eclipse.jdt.launching.IVMRunner; 34 import org.eclipse.osgi.service.resolver.BundleDescription; 35 import org.eclipse.osgi.util.NLS; 36 import org.eclipse.pde.core.plugin.IFragmentModel; 37 import org.eclipse.pde.core.plugin.IPluginModelBase; 38 import org.eclipse.pde.core.plugin.PluginRegistry; 39 import org.eclipse.pde.core.plugin.TargetPlatform; 40 import org.eclipse.pde.internal.core.ClasspathHelper; 41 import org.eclipse.pde.internal.core.PDECore; 42 import org.eclipse.pde.internal.core.TargetPlatformHelper; 43 import org.eclipse.pde.internal.core.util.CoreUtility; 44 import org.eclipse.pde.internal.core.util.VersionUtil; 45 import org.eclipse.pde.internal.ui.IPDEUIConstants; 46 import org.eclipse.pde.internal.ui.PDEPlugin; 47 import org.eclipse.pde.internal.ui.PDEUIMessages; 48 import org.eclipse.pde.internal.ui.launcher.EclipsePluginValidationOperation; 49 import org.eclipse.pde.internal.ui.launcher.LaunchArgumentsHelper; 50 import org.eclipse.pde.internal.ui.launcher.LaunchConfigurationHelper; 51 import org.eclipse.pde.internal.ui.launcher.LaunchPluginValidator; 52 import org.eclipse.pde.internal.ui.launcher.LauncherUtils; 53 import org.eclipse.pde.internal.ui.launcher.VMHelper; 54 import org.osgi.framework.Version; 55 56 61 public class JUnitLaunchConfigurationDelegate extends org.eclipse.jdt.junit.launcher.JUnitLaunchConfigurationDelegate { 62 63 private static String [] REQUIRED_PLUGINS = {"org.junit", "org.eclipse.jdt.junit.runtime", "org.eclipse.pde.junit.runtime"}; 65 protected File fConfigDir = null; 66 67 private Map fPluginMap; 68 69 73 public IVMRunner getVMRunner(ILaunchConfiguration configuration, String mode) throws CoreException { 74 IVMInstall launcher = VMHelper.createLauncher(configuration); 75 return launcher.getVMRunner(mode); 76 } 77 78 81 public String verifyMainTypeName(ILaunchConfiguration configuration) throws CoreException { 82 if (TargetPlatformHelper.getTargetVersion() >= 3.3) 83 return "org.eclipse.equinox.launcher.Main"; return "org.eclipse.core.launcher.Main"; } 86 87 private String getTestPluginId(ILaunchConfiguration configuration) 88 throws CoreException { 89 IJavaProject javaProject = getJavaProject(configuration); 90 IPluginModelBase model = 91 PluginRegistry.findModel(javaProject.getProject()); 92 if (model == null) 93 abort(NLS.bind(PDEUIMessages.JUnitLaunchConfiguration_error_notaplugin, 94 javaProject.getProject().getName()), 95 null, IStatus.OK); 96 if (model instanceof IFragmentModel) 97 return ((IFragmentModel)model).getFragment().getPluginId(); 98 99 return model.getPluginBase().getId(); 100 } 101 102 106 protected void abort(String message, Throwable exception, int code) 107 throws CoreException { 108 throw new CoreException(new Status(IStatus.ERROR, IPDEUIConstants.PLUGIN_ID, code, message, exception)); 109 } 110 111 114 protected void collectExecutionArguments(ILaunchConfiguration configuration, List vmArguments, List programArgs) throws CoreException { 115 super.collectExecutionArguments(configuration, vmArguments, programArgs); 116 117 programArgs.add("-application"); String application = null; 120 try { 121 application = configuration.getAttribute(IPDELauncherConstants.APPLICATION, (String )null); 123 } catch (CoreException e) { 124 } 125 126 if (application == null) { 129 IPluginModelBase model = (IPluginModelBase)fPluginMap.get("org.eclipse.pde.junit.runtime"); BundleDescription desc = model != null ? model.getBundleDescription() : null; 131 if (desc != null) { 132 Version version = desc.getVersion(); 133 int major = version.getMajor(); 134 if (major >= 3 && version.getMinor() >= 3 && !TargetPlatformHelper.usesNewApplicationModel()) { 138 application = IPDEUIConstants.LEGACY_UI_TEST_APPLICATION; 139 } 140 } 141 } 142 143 if (application == null) 145 application = IPDEUIConstants.UI_TEST_APPLICATION; 146 147 programArgs.add(application); 148 149 if (configuration.getAttribute(IPDELauncherConstants.USE_PRODUCT, false)) { 151 programArgs.add("-product"); programArgs.add(configuration.getAttribute(IPDELauncherConstants.PRODUCT, "")); } else { 154 String defaultApplication = IPDEUIConstants.CORE_TEST_APPLICATION.equals(application) ? null : TargetPlatform.getDefaultApplication(); 156 String testApplication = configuration.getAttribute(IPDELauncherConstants.APP_TO_TEST, defaultApplication); 157 if (testApplication != null) { 158 programArgs.add("-testApplication"); programArgs.add(testApplication); 160 } 161 } 162 163 String targetWorkspace = LaunchArgumentsHelper.getWorkspaceLocation(configuration); 165 if (targetWorkspace.length() > 0) { 166 programArgs.add("-data"); programArgs.add(targetWorkspace); 168 } 169 170 String productID = LaunchConfigurationHelper.getProductID(configuration); 172 LaunchConfigurationHelper.createConfigIniFile(configuration, 173 productID, fPluginMap, getConfigurationDirectory(configuration)); 174 String brandingId = LaunchConfigurationHelper.getContributingPlugin(productID); 175 TargetPlatform.createPlatformConfiguration( 176 getConfigurationDirectory(configuration), 177 (IPluginModelBase[])fPluginMap.values().toArray(new IPluginModelBase[fPluginMap.size()]), 178 brandingId != null ? (IPluginModelBase) fPluginMap.get(brandingId) : null); 179 TargetPlatformHelper.checkPluginPropertiesConsistency(fPluginMap, getConfigurationDirectory(configuration)); 180 181 programArgs.add("-configuration"); programArgs.add("file:" + new Path(getConfigurationDirectory(configuration).getPath()).addTrailingSeparator().toString()); 184 programArgs.add("-dev"); programArgs.add(ClasspathHelper.getDevEntriesProperties(getConfigurationDirectory(configuration).toString() + "/dev.properties", fPluginMap)); 188 IPluginModelBase base = findPlugin(PDECore.PLUGIN_ID); 191 if (base != null && VersionUtil.compareMacroMinorMicro(base.getBundleDescription().getVersion(), new Version("3.3.1")) < 0) programArgs.add("-pdelaunch"); 194 if (configuration.getAttribute(IPDELauncherConstants.TRACING, false) 196 && !IPDELauncherConstants.TRACING_NONE.equals(configuration.getAttribute( 197 IPDELauncherConstants.TRACING_CHECKED, (String ) null))) { 198 programArgs.add("-debug"); String path = getConfigurationDirectory(configuration).getPath() + IPath.SEPARATOR + ".options"; programArgs.add(LaunchArgumentsHelper.getTracingFileArgument(configuration, path)); 201 } 202 203 String [] userArgs = LaunchArgumentsHelper.getUserProgramArgumentArray(configuration); 205 for (int i = 0; i < userArgs.length; i++) { 206 if (userArgs[i].equals("-debug") && programArgs.contains("-debug")) continue; 210 programArgs.add(userArgs[i]); 211 } 212 213 if (!configuration.getAttribute(IPDEUIConstants.APPEND_ARGS_EXPLICITLY, false)) { 214 if (!programArgs.contains("-os")) { programArgs.add("-os"); programArgs.add(TargetPlatform.getOS()); 217 } 218 if (!programArgs.contains("-ws")) { programArgs.add("-ws"); programArgs.add(TargetPlatform.getWS()); 221 } 222 if (!programArgs.contains("-arch")) { programArgs.add("-arch"); programArgs.add(TargetPlatform.getOSArch()); 225 } 226 } 227 228 programArgs.add("-testpluginname"); programArgs.add(getTestPluginId(configuration)); 230 } 231 232 private IPluginModelBase findPlugin(String id) throws CoreException { 233 IPluginModelBase model = PluginRegistry.findModel(id); 234 if (model == null) 235 model = PDECore.getDefault().findPluginInHost(id); 236 if (model == null) 237 abort( 238 NLS.bind(PDEUIMessages.JUnitLaunchConfiguration_error_missingPlugin, id), 239 null, 240 IStatus.OK); 241 return model; 242 } 243 244 248 public String getProgramArguments(ILaunchConfiguration configuration) 249 throws CoreException { 250 return LaunchArgumentsHelper.getUserProgramArguments(configuration); 251 } 252 253 257 public String getVMArguments(ILaunchConfiguration configuration) 258 throws CoreException { 259 String vmArgs = LaunchArgumentsHelper.getUserVMArguments(configuration); 260 261 IPluginModelBase base = (IPluginModelBase)LaunchPluginValidator.getPluginsToRun(configuration).get(PDECore.PLUGIN_ID); 264 if (base != null && VersionUtil.compareMacroMinorMicro(base.getBundleDescription().getVersion(), new Version("3.3.1")) >= 0) { if (vmArgs.length() > 0 && !vmArgs.endsWith(" ")) vmArgs = vmArgs.concat(" "); vmArgs = vmArgs.concat("-Declipse.pde.launch=true"); } 269 return vmArgs; 270 } 271 272 276 public String [] getEnvironment(ILaunchConfiguration configuration) throws CoreException { 277 return DebugPlugin.getDefault().getLaunchManager().getEnvironment(configuration); 278 } 279 280 284 public String [] getClasspath(ILaunchConfiguration configuration) throws CoreException { 285 String [] classpath = LaunchArgumentsHelper.constructClasspath(configuration); 286 if (classpath == null) { 287 abort(PDEUIMessages.WorkbenchLauncherConfigurationDelegate_noStartup, null, IStatus.OK); 288 } 289 return classpath; 290 } 291 292 296 public File getWorkingDirectory(ILaunchConfiguration configuration) throws CoreException { 297 return LaunchArgumentsHelper.getWorkingDirectory(configuration); 298 } 299 300 304 public Map getVMSpecificAttributesMap(ILaunchConfiguration configuration) throws CoreException { 305 return LaunchArgumentsHelper.getVMSpecificAttributesMap(configuration); 306 } 307 308 312 protected void setDefaultSourceLocator(ILaunch launch, ILaunchConfiguration configuration) throws CoreException { 313 ILaunchConfigurationWorkingCopy wc = null; 314 if (configuration.isWorkingCopy()) { 315 wc = (ILaunchConfigurationWorkingCopy) configuration; 316 } else { 317 wc = configuration.getWorkingCopy(); 318 } 319 String id = configuration.getAttribute( 320 IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH_PROVIDER, 321 (String ) null); 322 if (!PDESourcePathProvider.ID.equals(id)) { 323 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH_PROVIDER, 324 PDESourcePathProvider.ID); 325 wc.doSave(); 326 } 327 328 manageLaunch(launch); 329 } 330 331 338 protected File getConfigurationDirectory(ILaunchConfiguration configuration) { 339 if (fConfigDir == null) 340 fConfigDir = LaunchConfigurationHelper.getConfigurationArea(configuration); 341 return fConfigDir; 342 } 343 344 348 protected IProject[] getBuildOrder(ILaunchConfiguration configuration, 349 String mode) throws CoreException { 350 return computeBuildOrder(LaunchPluginValidator.getAffectedProjects(configuration)); 351 } 352 353 357 protected IProject[] getProjectsForProblemSearch( 358 ILaunchConfiguration configuration, String mode) 359 throws CoreException { 360 return LaunchPluginValidator.getAffectedProjects(configuration); 361 } 362 363 370 protected void manageLaunch(ILaunch launch) { 371 PDEPlugin.getDefault().getLaunchListener().manage(launch); 372 } 373 374 377 protected void preLaunchCheck(ILaunchConfiguration configuration, ILaunch launch, IProgressMonitor monitor) throws CoreException { 378 fPluginMap = LaunchPluginValidator.getPluginsToRun(configuration); 380 381 for (int i = 0; i < REQUIRED_PLUGINS.length; i++) { 383 String id = REQUIRED_PLUGINS[i]; 384 if (!fPluginMap.containsKey(id)) { 385 fPluginMap.put(id, findPlugin(id)); 386 } 387 } 388 389 boolean autoValidate = configuration.getAttribute(IPDELauncherConstants.AUTOMATIC_VALIDATE, false); 390 monitor.beginTask("", autoValidate ? 3 : 4); if (autoValidate) 392 validatePluginDependencies(configuration, new SubProgressMonitor(monitor, 1)); 393 validateProjectDependencies(configuration, new SubProgressMonitor(monitor, 1)); 394 clear(configuration, new SubProgressMonitor(monitor, 1)); 395 launch.setAttribute(IPDELauncherConstants.CONFIG_LOCATION, getConfigurationDirectory(configuration).toString()); 396 synchronizeManifests(configuration, new SubProgressMonitor(monitor, 1)); 397 } 398 408 protected void synchronizeManifests(ILaunchConfiguration configuration, IProgressMonitor monitor) { 409 LaunchConfigurationHelper.synchronizeManifests(configuration, getConfigurationDirectory(configuration)); 410 monitor.done(); 411 } 412 413 425 protected void clear(ILaunchConfiguration configuration, IProgressMonitor monitor) throws CoreException { 426 String workspace = LaunchArgumentsHelper.getWorkspaceLocation(configuration); 427 if (!LauncherUtils.clearWorkspace(configuration, workspace, new SubProgressMonitor(monitor, 1))) { 429 monitor.setCanceled(true); 430 return; 431 } 432 433 if (configuration.getAttribute(IPDELauncherConstants.CONFIG_CLEAR_AREA, false)) 435 CoreUtility.deleteContent(getConfigurationDirectory(configuration)); 436 } 437 438 447 protected void validateProjectDependencies(ILaunchConfiguration configuration, IProgressMonitor monitor) { 448 LauncherUtils.validateProjectDependencies(configuration, monitor); 449 } 450 451 460 protected void validatePluginDependencies(ILaunchConfiguration configuration, IProgressMonitor monitor) throws CoreException { 461 EclipsePluginValidationOperation op = new EclipsePluginValidationOperation(configuration); 462 LaunchPluginValidator.runValidationOperation(op, monitor); 463 } 464 } 465 | Popular Tags |