1 11 package org.eclipse.pde.internal.ui.launcher; 12 13 import java.io.BufferedReader ; 14 import java.io.File ; 15 import java.io.FileReader ; 16 import java.io.IOException ; 17 import java.net.URL ; 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.HashSet ; 21 import java.util.Map ; 22 import java.util.StringTokenizer ; 23 24 import org.eclipse.core.resources.IProject; 25 import org.eclipse.core.resources.IResource; 26 import org.eclipse.core.runtime.CoreException; 27 import org.eclipse.core.runtime.FileLocator; 28 import org.eclipse.core.runtime.IPath; 29 import org.eclipse.core.runtime.Path; 30 import org.eclipse.core.runtime.Platform; 31 import org.eclipse.core.runtime.Preferences; 32 import org.eclipse.core.variables.IStringVariableManager; 33 import org.eclipse.core.variables.VariablesPlugin; 34 import org.eclipse.debug.core.ILaunchConfiguration; 35 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 36 import org.eclipse.jdt.core.IClasspathEntry; 37 import org.eclipse.jdt.core.IJavaProject; 38 import org.eclipse.jdt.core.IPackageFragmentRoot; 39 import org.eclipse.jdt.core.JavaCore; 40 import org.eclipse.jdt.launching.ExecutionArguments; 41 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 42 import org.eclipse.pde.core.plugin.IPluginModelBase; 43 import org.eclipse.pde.core.plugin.ModelEntry; 44 import org.eclipse.pde.core.plugin.PluginRegistry; 45 import org.eclipse.pde.core.plugin.TargetPlatform; 46 import org.eclipse.pde.internal.core.ICoreConstants; 47 import org.eclipse.pde.internal.core.PDECore; 48 import org.eclipse.pde.internal.core.TargetPlatformHelper; 49 import org.eclipse.pde.internal.core.TracingOptionsManager; 50 import org.eclipse.pde.internal.ui.PDEPlugin; 51 import org.eclipse.pde.ui.launcher.IPDELauncherConstants; 52 import org.osgi.framework.Bundle; 53 54 public class LaunchArgumentsHelper { 55 56 public static String getWorkspaceLocation(ILaunchConfiguration configuration) 57 throws CoreException { 58 String location = configuration.getAttribute(IPDELauncherConstants.LOCATION, (String )null); 59 if (location == null) { 60 location = configuration.getAttribute(IPDELauncherConstants.LOCATION + "0", (String )null); if (location != null) { 63 ILaunchConfigurationWorkingCopy wc = null; 64 if (configuration.isWorkingCopy()) { 65 wc = (ILaunchConfigurationWorkingCopy) configuration; 66 } else { 67 wc = configuration.getWorkingCopy(); 68 } 69 wc.setAttribute(IPDELauncherConstants.LOCATION + "0", (String )null); wc.setAttribute(IPDELauncherConstants.LOCATION, location); 71 wc.doSave(); 72 } 73 } 74 return getSubstitutedString(location); 75 } 76 77 public static String [] getUserProgramArgumentArray(ILaunchConfiguration configuration) throws CoreException { 78 String args = getUserProgramArguments(configuration); 79 return new ExecutionArguments("", args).getProgramArgumentsArray(); } 81 82 public static String getUserProgramArguments(ILaunchConfiguration configuration) throws CoreException { 83 String args = configuration.getAttribute( 84 IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, 85 (String )null); 86 if (args == null) { 87 args = configuration.getAttribute("progargs", (String )null); if (args != null) { 90 ILaunchConfigurationWorkingCopy wc = null; 91 if (configuration.isWorkingCopy()) { 92 wc = (ILaunchConfigurationWorkingCopy) configuration; 93 } else { 94 wc = configuration.getWorkingCopy(); 95 } 96 wc.setAttribute("progargs", (String )null); wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, args); 98 wc.doSave(); 99 } 100 } 101 return args == null ? "" : getSubstitutedString(args); } 103 104 public static String getUserVMArguments(ILaunchConfiguration configuration) throws CoreException { 105 String args = configuration.getAttribute( 106 IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, 107 (String )null); 108 if (args == null) { 109 args = configuration.getAttribute("vmargs", (String )null); if (args != null) { 112 ILaunchConfigurationWorkingCopy wc = null; 113 if (configuration.isWorkingCopy()) { 114 wc = (ILaunchConfigurationWorkingCopy) configuration; 115 } else { 116 wc = configuration.getWorkingCopy(); 117 } 118 wc.setAttribute("vmargs", (String )null); wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, args); 120 wc.doSave(); 121 } 122 } 123 return args == null ? "" : getSubstitutedString(args); } 125 126 public static String getInitialVMArguments() { 127 Preferences preferences = PDECore.getDefault().getPluginPreferences(); 128 StringBuffer result = new StringBuffer (preferences 129 .getString(ICoreConstants.VM_ARGS)); 130 131 if (preferences.getBoolean(ICoreConstants.VM_LAUNCHER_INI)) { 132 File installDirectory = new File (Platform.getInstallLocation().getURL() 134 .getFile()); 135 if (Platform.getOS().equals(Platform.OS_MACOSX)) 136 installDirectory = new File (installDirectory, 137 "Eclipse.app/Contents/MacOS"); File eclipseIniFile = new File (installDirectory, "eclipse.ini"); BufferedReader in = null; 140 if (eclipseIniFile.exists()) { 141 try { 142 in = new BufferedReader (new FileReader (eclipseIniFile)); 143 String str; 144 boolean vmargs = false; 145 while ((str = in.readLine()) != null) { 146 if (vmargs) { 147 if (result.length() > 0) 148 result.append(" "); result.append(str); 150 } 151 if (vmargs == false && str.equals("-vmargs")) vmargs = true; 154 } 155 } catch (IOException e) { 156 PDEPlugin.log(e); 157 } finally { 158 if (in != null) 159 try { 160 in.close(); 161 } catch (IOException e) { 162 PDEPlugin.log(e); 163 } 164 } 165 } 166 } 167 return result.toString(); 168 } 169 170 public static String getInitialProgramArguments() { 171 StringBuffer buffer = new StringBuffer ("-os ${target.os} -ws ${target.ws} -arch ${target.arch} -nl ${target.nl}"); 173 Preferences preferences = PDECore.getDefault().getPluginPreferences(); 174 String programArgs = preferences.getString(ICoreConstants.PROGRAM_ARGS); 175 if (programArgs.length() > 0) { 176 buffer.append(" "); buffer.append(programArgs); 178 } 179 return buffer.toString(); 180 } 181 182 public static File getWorkingDirectory(ILaunchConfiguration configuration) throws CoreException { 183 String working; 184 try { 185 working = configuration.getAttribute( 186 IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, 187 new File (".").getCanonicalPath()); } catch (IOException e) { 189 working = "${workspace_loc}/../"; } 191 File dir = new File (getSubstitutedString(working)); 192 if (!dir.exists()) 193 dir.mkdirs(); 194 return dir; 195 } 196 197 public static Map getVMSpecificAttributesMap(ILaunchConfiguration config) throws CoreException { 198 Map map = new HashMap (2); 199 String javaCommand = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_JAVA_COMMAND, (String )null); 200 map.put(IJavaLaunchConfigurationConstants.ATTR_JAVA_COMMAND, javaCommand); 201 if (TargetPlatform.getOS().equals("macosx")) { ModelEntry entry = PluginRegistry.findEntry("org.eclipse.jdt.debug"); if (entry != null) { 204 IPluginModelBase[] models = entry.getExternalModels(); 205 for (int i = 0; i < models.length; i++) { 206 File file = new File (models[i].getInstallLocation()); 207 if (!file.isFile()) 208 file = new File (file, "jdi.jar"); if (file.exists()) { 210 map.put(IJavaLaunchConfigurationConstants.ATTR_BOOTPATH_PREPEND, new String [] {file.getAbsolutePath()}); 211 break; 212 } 213 } 214 } 215 } 216 return map; 217 } 218 219 public static String getTracingFileArgument( 220 ILaunchConfiguration config, 221 String optionsFileName) 222 throws CoreException { 223 try { 224 TracingOptionsManager mng = PDECore.getDefault().getTracingOptionsManager(); 225 Map options = 226 config.getAttribute(IPDELauncherConstants.TRACING_OPTIONS, (Map ) null); 227 String selected = config.getAttribute(IPDELauncherConstants.TRACING_CHECKED, (String )null); 228 if (selected == null) { 229 mng.save(optionsFileName, options); 230 } else if (!selected.equals(IPDELauncherConstants.TRACING_NONE)) { 231 HashSet result = new HashSet (); 232 StringTokenizer tokenizer = new StringTokenizer (selected, ","); while (tokenizer.hasMoreTokens()) { 234 result.add(tokenizer.nextToken()); 235 } 236 mng.save(optionsFileName, options, result); 237 } 238 } catch (CoreException e) { 239 return ""; } 241 return optionsFileName; 242 } 243 244 public static String [] constructClasspath(ILaunchConfiguration configuration) throws CoreException { 245 double targetVersion = TargetPlatformHelper.getTargetVersion(); 246 String jarPath = targetVersion >= 3.3 247 ? getEquinoxStartupPath("org.eclipse.equinox.launcher") : getStartupJarPath(); 249 if (jarPath == null && targetVersion < 3.3) 250 jarPath = getEquinoxStartupPath("org.eclipse.core.launcher"); 252 if (jarPath == null) 253 return null; 254 255 ArrayList entries = new ArrayList (); 256 entries.add(jarPath); 257 258 String bootstrap = configuration.getAttribute(IPDELauncherConstants.BOOTSTRAP_ENTRIES, ""); StringTokenizer tok = new StringTokenizer (getSubstitutedString(bootstrap), ","); while (tok.hasMoreTokens()) 261 entries.add(tok.nextToken().trim()); 262 return (String [])entries.toArray(new String [entries.size()]); 263 } 264 265 private static String getEquinoxStartupPath(String packageName) throws CoreException { 266 IPluginModelBase model = PluginRegistry.findModel("org.eclipse.equinox.launcher"); if (model != null) { 268 IResource resource = model.getUnderlyingResource(); 269 if (resource == null) 271 return model.getInstallLocation(); 272 273 IProject project = resource.getProject(); 275 if (project.hasNature(JavaCore.NATURE_ID)) { 276 IJavaProject jProject = JavaCore.create(project); 277 IClasspathEntry[] entries = jProject.getRawClasspath(); 278 for (int i = 0; i < entries.length; i++) { 279 int kind = entries[i].getEntryKind(); 280 if (kind == IClasspathEntry.CPE_SOURCE || kind == IClasspathEntry.CPE_LIBRARY) { 281 IPackageFragmentRoot[] roots = jProject.findPackageFragmentRoots(entries[i]); 282 for (int j = 0; j < roots.length; j++) { 283 if (roots[j].getPackageFragment(packageName).exists()) { if (kind == IClasspathEntry.CPE_SOURCE) { 286 IPath path = entries[i].getOutputLocation(); 287 if (path == null) 288 path = jProject.getOutputLocation(); 289 path = path.removeFirstSegments(1); 290 return project.getLocation().append(path).toOSString(); 291 } 292 IResource jar = roots[j].getResource(); 294 if (jar != null) { 295 return jar.getLocation().toOSString(); 296 } 297 } 298 } 299 } 300 } 301 } 302 } 303 Bundle bundle = Platform.getBundle("org.eclipse.equinox.launcher"); if (bundle != null) { 305 try { 306 URL url = FileLocator.resolve(bundle.getEntry("/")); url = FileLocator.toFileURL(url); 308 String path = url.getFile(); 309 if (path.startsWith("file:")) path = path.substring(5); 311 path = new File (path).getAbsolutePath(); 312 if (path.endsWith("!")) path = path.substring(0, path.length() - 1); 314 return path; 315 } catch (IOException e) { 316 } 317 } 318 return null; 319 } 320 321 private static String getStartupJarPath() throws CoreException { 322 IPluginModelBase model = PluginRegistry.findModel("org.eclipse.platform"); if (model != null && model.getUnderlyingResource() != null) { 324 IProject project = model.getUnderlyingResource().getProject(); 325 if (project.hasNature(JavaCore.NATURE_ID)) { 326 IJavaProject jProject = JavaCore.create(project); 327 IPackageFragmentRoot[] roots = jProject.getPackageFragmentRoots(); 328 for (int i = 0; i < roots.length; i++) { 329 if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE && 330 roots[i].getPackageFragment("org.eclipse.core.launcher").exists()){ IPath path = jProject.getOutputLocation().removeFirstSegments(1); 332 return project.getLocation().append(path).toOSString(); 333 } 334 } 335 } 336 if (project.getFile("startup.jar").exists()) return project.getFile("startup.jar").getLocation().toOSString(); } 339 File startupJar = 340 new Path(TargetPlatform.getLocation()).append("startup.jar").toFile(); 342 if (!startupJar.exists()) 345 startupJar = new Path(TargetPlatform.getDefaultLocation()).append("startup.jar").toFile(); 347 return startupJar.exists() ? startupJar.getAbsolutePath() : null; 348 } 349 350 351 private static String getSubstitutedString(String text) throws CoreException { 352 if (text == null) 353 return ""; IStringVariableManager mgr = VariablesPlugin.getDefault().getStringVariableManager(); 355 return mgr.performStringSubstitution(text); 356 } 357 358 public static String getDefaultWorkspaceLocation(String uniqueName) { 359 return "${workspace_loc}/../runtime-" + uniqueName.replaceAll("\\s", ""); } 361 362 public static String getDefaultJUnitWorkspaceLocation() { 363 return "${workspace_loc}/../junit-workspace"; } 365 366 public static String getDefaultJUnitConfigurationLocation() { 367 return "${workspace_loc}/.metadata/.plugins/org.eclipse.pde.core/pde-junit"; } 369 370 371 } 372 | Popular Tags |