KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > launcher > LaunchArgumentsHelper


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.internal.ui.launcher;
12
13 import java.io.BufferedReader JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.FileReader JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.net.URL JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
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 JavaDoc getWorkspaceLocation(ILaunchConfiguration configuration)
57     throws CoreException {
58         String JavaDoc location = configuration.getAttribute(IPDELauncherConstants.LOCATION, (String JavaDoc)null);
59         if (location == null) {
60             // backward compatibility
61
location = configuration.getAttribute(IPDELauncherConstants.LOCATION + "0", (String JavaDoc)null); //$NON-NLS-1$
62
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 JavaDoc)null); //$NON-NLS-1$
70
wc.setAttribute(IPDELauncherConstants.LOCATION, location);
71                 wc.doSave();
72             }
73         }
74         return getSubstitutedString(location);
75     }
76
77     public static String JavaDoc[] getUserProgramArgumentArray(ILaunchConfiguration configuration) throws CoreException {
78         String JavaDoc args = getUserProgramArguments(configuration);
79         return new ExecutionArguments("", args).getProgramArgumentsArray(); //$NON-NLS-1$
80
}
81
82     public static String JavaDoc getUserProgramArguments(ILaunchConfiguration configuration) throws CoreException {
83         String JavaDoc args = configuration.getAttribute(
84                 IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
85                 (String JavaDoc)null);
86         if (args == null) {
87             // backward compatibility
88
args = configuration.getAttribute("progargs", (String JavaDoc)null); //$NON-NLS-1$
89
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 JavaDoc)null); //$NON-NLS-1$
97
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, args);
98                 wc.doSave();
99             }
100         }
101         return args == null ? "" : getSubstitutedString(args); //$NON-NLS-1$
102
}
103
104     public static String JavaDoc getUserVMArguments(ILaunchConfiguration configuration) throws CoreException {
105         String JavaDoc args = configuration.getAttribute(
106                 IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS,
107                 (String JavaDoc)null);
108         if (args == null) {
109             // backward compatibility
110
args = configuration.getAttribute("vmargs", (String JavaDoc)null); //$NON-NLS-1$
111
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 JavaDoc)null); //$NON-NLS-1$
119
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, args);
120                 wc.doSave();
121             }
122         }
123         return args == null ? "" : getSubstitutedString(args); //$NON-NLS-1$
124
}
125     
126     public static String JavaDoc getInitialVMArguments() {
127         Preferences preferences = PDECore.getDefault().getPluginPreferences();
128         StringBuffer JavaDoc result = new StringBuffer JavaDoc(preferences
129                 .getString(ICoreConstants.VM_ARGS));
130
131         if (preferences.getBoolean(ICoreConstants.VM_LAUNCHER_INI)) {
132             // hack on the args from eclipse.ini
133
File JavaDoc installDirectory = new File JavaDoc(Platform.getInstallLocation().getURL()
134                     .getFile());
135             if (Platform.getOS().equals(Platform.OS_MACOSX))
136                 installDirectory = new File JavaDoc(installDirectory,
137                         "Eclipse.app/Contents/MacOS"); //$NON-NLS-1$
138
File JavaDoc eclipseIniFile = new File JavaDoc(installDirectory, "eclipse.ini"); //$NON-NLS-1$
139
BufferedReader JavaDoc in = null;
140             if (eclipseIniFile.exists()) {
141                 try {
142                     in = new BufferedReader JavaDoc(new FileReader JavaDoc(eclipseIniFile));
143                     String JavaDoc str;
144                     boolean vmargs = false;
145                     while ((str = in.readLine()) != null) {
146                         if (vmargs) {
147                             if (result.length() > 0)
148                                 result.append(" "); //$NON-NLS-1$
149
result.append(str);
150                         }
151                         // start concat'ng if we have vmargs
152
if (vmargs == false && str.equals("-vmargs")) //$NON-NLS-1$
153
vmargs = true;
154                     }
155                 } catch (IOException JavaDoc e) {
156                     PDEPlugin.log(e);
157                 } finally {
158                     if (in != null)
159                         try {
160                             in.close();
161                         } catch (IOException JavaDoc e) {
162                             PDEPlugin.log(e);
163                         }
164                 }
165             }
166         }
167         return result.toString();
168     }
169     
170     public static String JavaDoc getInitialProgramArguments() {
171         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("-os ${target.os} -ws ${target.ws} -arch ${target.arch} -nl ${target.nl}"); //$NON-NLS-1$
172

173         Preferences preferences = PDECore.getDefault().getPluginPreferences();
174         String JavaDoc programArgs = preferences.getString(ICoreConstants.PROGRAM_ARGS);
175         if (programArgs.length() > 0) {
176             buffer.append(" "); //$NON-NLS-1$
177
buffer.append(programArgs);
178         }
179         return buffer.toString();
180     }
181
182     public static File JavaDoc getWorkingDirectory(ILaunchConfiguration configuration) throws CoreException {
183         String JavaDoc working;
184         try {
185             working = configuration.getAttribute(
186                     IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY,
187                     new File JavaDoc(".").getCanonicalPath()); //$NON-NLS-1$
188
} catch (IOException JavaDoc e) {
189             working = "${workspace_loc}/../"; //$NON-NLS-1$
190
}
191         File JavaDoc dir = new File JavaDoc(getSubstitutedString(working));
192         if (!dir.exists())
193             dir.mkdirs();
194         return dir;
195     }
196
197     public static Map JavaDoc getVMSpecificAttributesMap(ILaunchConfiguration config) throws CoreException {
198         Map JavaDoc map = new HashMap JavaDoc(2);
199         String JavaDoc javaCommand = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_JAVA_COMMAND, (String JavaDoc)null);
200         map.put(IJavaLaunchConfigurationConstants.ATTR_JAVA_COMMAND, javaCommand);
201         if (TargetPlatform.getOS().equals("macosx")) { //$NON-NLS-1$
202
ModelEntry entry = PluginRegistry.findEntry("org.eclipse.jdt.debug"); //$NON-NLS-1$
203
if (entry != null) {
204                 IPluginModelBase[] models = entry.getExternalModels();
205                 for (int i = 0; i < models.length; i++) {
206                     File JavaDoc file = new File JavaDoc(models[i].getInstallLocation());
207                     if (!file.isFile())
208                         file = new File JavaDoc(file, "jdi.jar"); //$NON-NLS-1$
209
if (file.exists()) {
210                         map.put(IJavaLaunchConfigurationConstants.ATTR_BOOTPATH_PREPEND, new String JavaDoc[] {file.getAbsolutePath()});
211                         break;
212                     }
213                 }
214             }
215         }
216         return map;
217     }
218
219     public static String JavaDoc getTracingFileArgument(
220             ILaunchConfiguration config,
221             String JavaDoc optionsFileName)
222     throws CoreException {
223         try {
224             TracingOptionsManager mng = PDECore.getDefault().getTracingOptionsManager();
225             Map JavaDoc options =
226                 config.getAttribute(IPDELauncherConstants.TRACING_OPTIONS, (Map JavaDoc) null);
227             String JavaDoc selected = config.getAttribute(IPDELauncherConstants.TRACING_CHECKED, (String JavaDoc)null);
228             if (selected == null) {
229                 mng.save(optionsFileName, options);
230             } else if (!selected.equals(IPDELauncherConstants.TRACING_NONE)) {
231                 HashSet JavaDoc result = new HashSet JavaDoc();
232                 StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(selected, ","); //$NON-NLS-1$
233
while (tokenizer.hasMoreTokens()) {
234                     result.add(tokenizer.nextToken());
235                 }
236                 mng.save(optionsFileName, options, result);
237             }
238         } catch (CoreException e) {
239             return ""; //$NON-NLS-1$
240
}
241         return optionsFileName;
242     }
243
244     public static String JavaDoc[] constructClasspath(ILaunchConfiguration configuration) throws CoreException {
245         double targetVersion = TargetPlatformHelper.getTargetVersion();
246         String JavaDoc jarPath = targetVersion >= 3.3
247                                 ? getEquinoxStartupPath("org.eclipse.equinox.launcher") //$NON-NLS-1$
248
: getStartupJarPath();
249         if (jarPath == null && targetVersion < 3.3)
250             jarPath = getEquinoxStartupPath("org.eclipse.core.launcher"); //$NON-NLS-1$
251

252         if (jarPath == null)
253             return null;
254
255         ArrayList JavaDoc entries = new ArrayList JavaDoc();
256         entries.add(jarPath);
257
258         String JavaDoc bootstrap = configuration.getAttribute(IPDELauncherConstants.BOOTSTRAP_ENTRIES, ""); //$NON-NLS-1$
259
StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(getSubstitutedString(bootstrap), ","); //$NON-NLS-1$
260
while (tok.hasMoreTokens())
261             entries.add(tok.nextToken().trim());
262         return (String JavaDoc[])entries.toArray(new String JavaDoc[entries.size()]);
263     }
264
265     private static String JavaDoc getEquinoxStartupPath(String JavaDoc packageName) throws CoreException {
266         IPluginModelBase model = PluginRegistry.findModel("org.eclipse.equinox.launcher"); //$NON-NLS-1$
267
if (model != null) {
268             IResource resource = model.getUnderlyingResource();
269             // found in the target
270
if (resource == null)
271                 return model.getInstallLocation();
272             
273             // find it in the workspace
274
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()) { //$NON-NLS-1$
284
// if source folder, find the output folder
285
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                                 // else if is a library jar, then get the location of the jar itself
293
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"); //$NON-NLS-1$
304
if (bundle != null) {
305             try {
306                 URL JavaDoc url = FileLocator.resolve(bundle.getEntry("/")); //$NON-NLS-1$
307
url = FileLocator.toFileURL(url);
308                 String JavaDoc path = url.getFile();
309                 if (path.startsWith("file:")) //$NON-NLS-1$
310
path = path.substring(5);
311                 path = new File JavaDoc(path).getAbsolutePath();
312                 if (path.endsWith("!")) //$NON-NLS-1$
313
path = path.substring(0, path.length() - 1);
314                 return path;
315             } catch (IOException JavaDoc e) {
316             }
317         }
318         return null;
319     }
320
321     private static String JavaDoc getStartupJarPath() throws CoreException {
322         IPluginModelBase model = PluginRegistry.findModel("org.eclipse.platform"); //$NON-NLS-1$
323
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()){ //$NON-NLS-1$
331
IPath path = jProject.getOutputLocation().removeFirstSegments(1);
332                         return project.getLocation().append(path).toOSString();
333                     }
334                 }
335             }
336             if (project.getFile("startup.jar").exists()) //$NON-NLS-1$
337
return project.getFile("startup.jar").getLocation().toOSString(); //$NON-NLS-1$
338
}
339         File JavaDoc startupJar =
340             new Path(TargetPlatform.getLocation()).append("startup.jar").toFile(); //$NON-NLS-1$
341

342         // if something goes wrong with the preferences, fall back on the startup.jar
343
// in the running eclipse.
344
if (!startupJar.exists())
345             startupJar = new Path(TargetPlatform.getDefaultLocation()).append("startup.jar").toFile(); //$NON-NLS-1$
346

347         return startupJar.exists() ? startupJar.getAbsolutePath() : null;
348     }
349
350
351     private static String JavaDoc getSubstitutedString(String JavaDoc text) throws CoreException {
352         if (text == null)
353             return ""; //$NON-NLS-1$
354
IStringVariableManager mgr = VariablesPlugin.getDefault().getStringVariableManager();
355         return mgr.performStringSubstitution(text);
356     }
357
358     public static String JavaDoc getDefaultWorkspaceLocation(String JavaDoc uniqueName) {
359         return "${workspace_loc}/../runtime-" + uniqueName.replaceAll("\\s", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
360
}
361
362     public static String JavaDoc getDefaultJUnitWorkspaceLocation() {
363         return "${workspace_loc}/../junit-workspace"; //$NON-NLS-1$
364
}
365
366     public static String JavaDoc getDefaultJUnitConfigurationLocation() {
367         return "${workspace_loc}/.metadata/.plugins/org.eclipse.pde.core/pde-junit"; //$NON-NLS-1$
368
}
369
370
371 }
372
Popular Tags