001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.launcher;
011:
012: import java.io.BufferedReader;
013: import java.io.File;
014: import java.io.FileReader;
015: import java.io.IOException;
016: import java.net.URL;
017: import java.util.ArrayList;
018: import java.util.HashMap;
019: import java.util.HashSet;
020: import java.util.Map;
021: import java.util.StringTokenizer;
022:
023: import org.eclipse.core.resources.IProject;
024: import org.eclipse.core.resources.IResource;
025: import org.eclipse.core.runtime.CoreException;
026: import org.eclipse.core.runtime.FileLocator;
027: import org.eclipse.core.runtime.IPath;
028: import org.eclipse.core.runtime.Path;
029: import org.eclipse.core.runtime.Platform;
030: import org.eclipse.core.runtime.Preferences;
031: import org.eclipse.core.variables.IStringVariableManager;
032: import org.eclipse.core.variables.VariablesPlugin;
033: import org.eclipse.debug.core.ILaunchConfiguration;
034: import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
035: import org.eclipse.jdt.core.IClasspathEntry;
036: import org.eclipse.jdt.core.IJavaProject;
037: import org.eclipse.jdt.core.IPackageFragmentRoot;
038: import org.eclipse.jdt.core.JavaCore;
039: import org.eclipse.jdt.launching.ExecutionArguments;
040: import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
041: import org.eclipse.pde.core.plugin.IPluginModelBase;
042: import org.eclipse.pde.core.plugin.ModelEntry;
043: import org.eclipse.pde.core.plugin.PluginRegistry;
044: import org.eclipse.pde.core.plugin.TargetPlatform;
045: import org.eclipse.pde.internal.core.ICoreConstants;
046: import org.eclipse.pde.internal.core.PDECore;
047: import org.eclipse.pde.internal.core.TargetPlatformHelper;
048: import org.eclipse.pde.internal.core.TracingOptionsManager;
049: import org.eclipse.pde.internal.ui.PDEPlugin;
050: import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
051: import org.osgi.framework.Bundle;
052:
053: public class LaunchArgumentsHelper {
054:
055: public static String getWorkspaceLocation(
056: ILaunchConfiguration configuration) throws CoreException {
057: String location = configuration.getAttribute(
058: IPDELauncherConstants.LOCATION, (String) null);
059: if (location == null) {
060: // backward compatibility
061: location = configuration
062: .getAttribute(
063: IPDELauncherConstants.LOCATION + "0", (String) null); //$NON-NLS-1$
064: if (location != null) {
065: ILaunchConfigurationWorkingCopy wc = null;
066: if (configuration.isWorkingCopy()) {
067: wc = (ILaunchConfigurationWorkingCopy) configuration;
068: } else {
069: wc = configuration.getWorkingCopy();
070: }
071: wc
072: .setAttribute(IPDELauncherConstants.LOCATION
073: + "0", (String) null); //$NON-NLS-1$
074: wc.setAttribute(IPDELauncherConstants.LOCATION,
075: location);
076: wc.doSave();
077: }
078: }
079: return getSubstitutedString(location);
080: }
081:
082: public static String[] getUserProgramArgumentArray(
083: ILaunchConfiguration configuration) throws CoreException {
084: String args = getUserProgramArguments(configuration);
085: return new ExecutionArguments("", args).getProgramArgumentsArray(); //$NON-NLS-1$
086: }
087:
088: public static String getUserProgramArguments(
089: ILaunchConfiguration configuration) throws CoreException {
090: String args = configuration
091: .getAttribute(
092: IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
093: (String) null);
094: if (args == null) {
095: // backward compatibility
096: args = configuration
097: .getAttribute("progargs", (String) null); //$NON-NLS-1$
098: if (args != null) {
099: ILaunchConfigurationWorkingCopy wc = null;
100: if (configuration.isWorkingCopy()) {
101: wc = (ILaunchConfigurationWorkingCopy) configuration;
102: } else {
103: wc = configuration.getWorkingCopy();
104: }
105: wc.setAttribute("progargs", (String) null); //$NON-NLS-1$
106: wc
107: .setAttribute(
108: IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
109: args);
110: wc.doSave();
111: }
112: }
113: return args == null ? "" : getSubstitutedString(args); //$NON-NLS-1$
114: }
115:
116: public static String getUserVMArguments(
117: ILaunchConfiguration configuration) throws CoreException {
118: String args = configuration.getAttribute(
119: IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS,
120: (String) null);
121: if (args == null) {
122: // backward compatibility
123: args = configuration.getAttribute("vmargs", (String) null); //$NON-NLS-1$
124: if (args != null) {
125: ILaunchConfigurationWorkingCopy wc = null;
126: if (configuration.isWorkingCopy()) {
127: wc = (ILaunchConfigurationWorkingCopy) configuration;
128: } else {
129: wc = configuration.getWorkingCopy();
130: }
131: wc.setAttribute("vmargs", (String) null); //$NON-NLS-1$
132: wc
133: .setAttribute(
134: IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS,
135: args);
136: wc.doSave();
137: }
138: }
139: return args == null ? "" : getSubstitutedString(args); //$NON-NLS-1$
140: }
141:
142: public static String getInitialVMArguments() {
143: Preferences preferences = PDECore.getDefault()
144: .getPluginPreferences();
145: StringBuffer result = new StringBuffer(preferences
146: .getString(ICoreConstants.VM_ARGS));
147:
148: if (preferences.getBoolean(ICoreConstants.VM_LAUNCHER_INI)) {
149: // hack on the args from eclipse.ini
150: File installDirectory = new File(Platform
151: .getInstallLocation().getURL().getFile());
152: if (Platform.getOS().equals(Platform.OS_MACOSX))
153: installDirectory = new File(installDirectory,
154: "Eclipse.app/Contents/MacOS"); //$NON-NLS-1$
155: File eclipseIniFile = new File(installDirectory,
156: "eclipse.ini"); //$NON-NLS-1$
157: BufferedReader in = null;
158: if (eclipseIniFile.exists()) {
159: try {
160: in = new BufferedReader(new FileReader(
161: eclipseIniFile));
162: String str;
163: boolean vmargs = false;
164: while ((str = in.readLine()) != null) {
165: if (vmargs) {
166: if (result.length() > 0)
167: result.append(" "); //$NON-NLS-1$
168: result.append(str);
169: }
170: // start concat'ng if we have vmargs
171: if (vmargs == false && str.equals("-vmargs")) //$NON-NLS-1$
172: vmargs = true;
173: }
174: } catch (IOException e) {
175: PDEPlugin.log(e);
176: } finally {
177: if (in != null)
178: try {
179: in.close();
180: } catch (IOException e) {
181: PDEPlugin.log(e);
182: }
183: }
184: }
185: }
186: return result.toString();
187: }
188:
189: public static String getInitialProgramArguments() {
190: StringBuffer buffer = new StringBuffer(
191: "-os ${target.os} -ws ${target.ws} -arch ${target.arch} -nl ${target.nl}"); //$NON-NLS-1$
192:
193: Preferences preferences = PDECore.getDefault()
194: .getPluginPreferences();
195: String programArgs = preferences
196: .getString(ICoreConstants.PROGRAM_ARGS);
197: if (programArgs.length() > 0) {
198: buffer.append(" "); //$NON-NLS-1$
199: buffer.append(programArgs);
200: }
201: return buffer.toString();
202: }
203:
204: public static File getWorkingDirectory(
205: ILaunchConfiguration configuration) throws CoreException {
206: String working;
207: try {
208: working = configuration
209: .getAttribute(
210: IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY,
211: new File(".").getCanonicalPath()); //$NON-NLS-1$
212: } catch (IOException e) {
213: working = "${workspace_loc}/../"; //$NON-NLS-1$
214: }
215: File dir = new File(getSubstitutedString(working));
216: if (!dir.exists())
217: dir.mkdirs();
218: return dir;
219: }
220:
221: public static Map getVMSpecificAttributesMap(
222: ILaunchConfiguration config) throws CoreException {
223: Map map = new HashMap(2);
224: String javaCommand = config.getAttribute(
225: IJavaLaunchConfigurationConstants.ATTR_JAVA_COMMAND,
226: (String) null);
227: map.put(IJavaLaunchConfigurationConstants.ATTR_JAVA_COMMAND,
228: javaCommand);
229: if (TargetPlatform.getOS().equals("macosx")) { //$NON-NLS-1$
230: ModelEntry entry = PluginRegistry
231: .findEntry("org.eclipse.jdt.debug"); //$NON-NLS-1$
232: if (entry != null) {
233: IPluginModelBase[] models = entry.getExternalModels();
234: for (int i = 0; i < models.length; i++) {
235: File file = new File(models[i].getInstallLocation());
236: if (!file.isFile())
237: file = new File(file, "jdi.jar"); //$NON-NLS-1$
238: if (file.exists()) {
239: map
240: .put(
241: IJavaLaunchConfigurationConstants.ATTR_BOOTPATH_PREPEND,
242: new String[] { file
243: .getAbsolutePath() });
244: break;
245: }
246: }
247: }
248: }
249: return map;
250: }
251:
252: public static String getTracingFileArgument(
253: ILaunchConfiguration config, String optionsFileName)
254: throws CoreException {
255: try {
256: TracingOptionsManager mng = PDECore.getDefault()
257: .getTracingOptionsManager();
258: Map options = config.getAttribute(
259: IPDELauncherConstants.TRACING_OPTIONS, (Map) null);
260: String selected = config.getAttribute(
261: IPDELauncherConstants.TRACING_CHECKED,
262: (String) null);
263: if (selected == null) {
264: mng.save(optionsFileName, options);
265: } else if (!selected
266: .equals(IPDELauncherConstants.TRACING_NONE)) {
267: HashSet result = new HashSet();
268: StringTokenizer tokenizer = new StringTokenizer(
269: selected, ","); //$NON-NLS-1$
270: while (tokenizer.hasMoreTokens()) {
271: result.add(tokenizer.nextToken());
272: }
273: mng.save(optionsFileName, options, result);
274: }
275: } catch (CoreException e) {
276: return ""; //$NON-NLS-1$
277: }
278: return optionsFileName;
279: }
280:
281: public static String[] constructClasspath(
282: ILaunchConfiguration configuration) throws CoreException {
283: double targetVersion = TargetPlatformHelper.getTargetVersion();
284: String jarPath = targetVersion >= 3.3 ? getEquinoxStartupPath("org.eclipse.equinox.launcher") //$NON-NLS-1$
285: : getStartupJarPath();
286: if (jarPath == null && targetVersion < 3.3)
287: jarPath = getEquinoxStartupPath("org.eclipse.core.launcher"); //$NON-NLS-1$
288:
289: if (jarPath == null)
290: return null;
291:
292: ArrayList entries = new ArrayList();
293: entries.add(jarPath);
294:
295: String bootstrap = configuration.getAttribute(
296: IPDELauncherConstants.BOOTSTRAP_ENTRIES, ""); //$NON-NLS-1$
297: StringTokenizer tok = new StringTokenizer(
298: getSubstitutedString(bootstrap), ","); //$NON-NLS-1$
299: while (tok.hasMoreTokens())
300: entries.add(tok.nextToken().trim());
301: return (String[]) entries.toArray(new String[entries.size()]);
302: }
303:
304: private static String getEquinoxStartupPath(String packageName)
305: throws CoreException {
306: IPluginModelBase model = PluginRegistry
307: .findModel("org.eclipse.equinox.launcher"); //$NON-NLS-1$
308: if (model != null) {
309: IResource resource = model.getUnderlyingResource();
310: // found in the target
311: if (resource == null)
312: return model.getInstallLocation();
313:
314: // find it in the workspace
315: IProject project = resource.getProject();
316: if (project.hasNature(JavaCore.NATURE_ID)) {
317: IJavaProject jProject = JavaCore.create(project);
318: IClasspathEntry[] entries = jProject.getRawClasspath();
319: for (int i = 0; i < entries.length; i++) {
320: int kind = entries[i].getEntryKind();
321: if (kind == IClasspathEntry.CPE_SOURCE
322: || kind == IClasspathEntry.CPE_LIBRARY) {
323: IPackageFragmentRoot[] roots = jProject
324: .findPackageFragmentRoots(entries[i]);
325: for (int j = 0; j < roots.length; j++) {
326: if (roots[j]
327: .getPackageFragment(packageName)
328: .exists()) { //$NON-NLS-1$
329: // if source folder, find the output folder
330: if (kind == IClasspathEntry.CPE_SOURCE) {
331: IPath path = entries[i]
332: .getOutputLocation();
333: if (path == null)
334: path = jProject
335: .getOutputLocation();
336: path = path.removeFirstSegments(1);
337: return project.getLocation()
338: .append(path).toOSString();
339: }
340: // else if is a library jar, then get the location of the jar itself
341: IResource jar = roots[j].getResource();
342: if (jar != null) {
343: return jar.getLocation()
344: .toOSString();
345: }
346: }
347: }
348: }
349: }
350: }
351: }
352: Bundle bundle = Platform
353: .getBundle("org.eclipse.equinox.launcher"); //$NON-NLS-1$
354: if (bundle != null) {
355: try {
356: URL url = FileLocator.resolve(bundle.getEntry("/")); //$NON-NLS-1$
357: url = FileLocator.toFileURL(url);
358: String path = url.getFile();
359: if (path.startsWith("file:")) //$NON-NLS-1$
360: path = path.substring(5);
361: path = new File(path).getAbsolutePath();
362: if (path.endsWith("!")) //$NON-NLS-1$
363: path = path.substring(0, path.length() - 1);
364: return path;
365: } catch (IOException e) {
366: }
367: }
368: return null;
369: }
370:
371: private static String getStartupJarPath() throws CoreException {
372: IPluginModelBase model = PluginRegistry
373: .findModel("org.eclipse.platform"); //$NON-NLS-1$
374: if (model != null && model.getUnderlyingResource() != null) {
375: IProject project = model.getUnderlyingResource()
376: .getProject();
377: if (project.hasNature(JavaCore.NATURE_ID)) {
378: IJavaProject jProject = JavaCore.create(project);
379: IPackageFragmentRoot[] roots = jProject
380: .getPackageFragmentRoots();
381: for (int i = 0; i < roots.length; i++) {
382: if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE
383: && roots[i]
384: .getPackageFragment(
385: "org.eclipse.core.launcher").exists()) { //$NON-NLS-1$
386: IPath path = jProject.getOutputLocation()
387: .removeFirstSegments(1);
388: return project.getLocation().append(path)
389: .toOSString();
390: }
391: }
392: }
393: if (project.getFile("startup.jar").exists()) //$NON-NLS-1$
394: return project
395: .getFile("startup.jar").getLocation().toOSString(); //$NON-NLS-1$
396: }
397: File startupJar = new Path(TargetPlatform.getLocation())
398: .append("startup.jar").toFile(); //$NON-NLS-1$
399:
400: // if something goes wrong with the preferences, fall back on the startup.jar
401: // in the running eclipse.
402: if (!startupJar.exists())
403: startupJar = new Path(TargetPlatform.getDefaultLocation())
404: .append("startup.jar").toFile(); //$NON-NLS-1$
405:
406: return startupJar.exists() ? startupJar.getAbsolutePath()
407: : null;
408: }
409:
410: private static String getSubstitutedString(String text)
411: throws CoreException {
412: if (text == null)
413: return ""; //$NON-NLS-1$
414: IStringVariableManager mgr = VariablesPlugin.getDefault()
415: .getStringVariableManager();
416: return mgr.performStringSubstitution(text);
417: }
418:
419: public static String getDefaultWorkspaceLocation(String uniqueName) {
420: return "${workspace_loc}/../runtime-" + uniqueName.replaceAll("\\s", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
421: }
422:
423: public static String getDefaultJUnitWorkspaceLocation() {
424: return "${workspace_loc}/../junit-workspace"; //$NON-NLS-1$
425: }
426:
427: public static String getDefaultJUnitConfigurationLocation() {
428: return "${workspace_loc}/.metadata/.plugins/org.eclipse.pde.core/pde-junit"; //$NON-NLS-1$
429: }
430:
431: }
|