KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.FileOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Properties JavaDoc;
22 import java.util.Set JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.Path;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.core.variables.IStringVariableManager;
30 import org.eclipse.core.variables.VariablesPlugin;
31 import org.eclipse.debug.core.ILaunchConfiguration;
32 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
33 import org.eclipse.osgi.service.resolver.BundleDescription;
34 import org.eclipse.pde.core.plugin.IPluginElement;
35 import org.eclipse.pde.core.plugin.IPluginExtension;
36 import org.eclipse.pde.core.plugin.IPluginModelBase;
37 import org.eclipse.pde.core.plugin.IPluginObject;
38 import org.eclipse.pde.core.plugin.PluginRegistry;
39 import org.eclipse.pde.core.plugin.TargetPlatform;
40 import org.eclipse.pde.internal.core.PDECore;
41 import org.eclipse.pde.internal.core.TargetPlatformHelper;
42 import org.eclipse.pde.internal.ui.PDEPlugin;
43 import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
44
45 public class LaunchConfigurationHelper {
46     
47     public static void synchronizeManifests(ILaunchConfiguration config, File JavaDoc configDir) {
48         try {
49             String JavaDoc programArgs = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
50                                                         ""); //$NON-NLS-1$
51
if (programArgs.indexOf("-clean") != -1) //$NON-NLS-1$
52
return;
53         } catch (CoreException e) {
54         }
55         File JavaDoc dir = new File JavaDoc(configDir, "org.eclipse.osgi/manifests"); //$NON-NLS-1$
56
if (dir.exists() && dir.isDirectory()) {
57             PDECore.getDefault().getJavaElementChangeListener().synchronizeManifests(dir);
58         }
59     }
60
61     public static File JavaDoc getConfigurationArea(ILaunchConfiguration config) {
62         File JavaDoc dir = getConfigurationLocation(config);
63         if (!dir.exists())
64             dir.mkdirs();
65         return dir;
66     }
67     
68     public static File JavaDoc getConfigurationLocation(ILaunchConfiguration config) {
69         File JavaDoc dir = new File JavaDoc(PDECore.getDefault().getStateLocation().toOSString(), config.getName());
70         try {
71             if (!config.getAttribute(IPDELauncherConstants.CONFIG_USE_DEFAULT_AREA, true)) {
72                 String JavaDoc userPath = config.getAttribute(IPDELauncherConstants.CONFIG_LOCATION, (String JavaDoc)null);
73                 if (userPath != null) {
74                     userPath = getSubstitutedString(userPath);
75                     dir = new File JavaDoc(userPath).getAbsoluteFile();
76                 }
77             }
78         } catch (CoreException e) {
79         }
80         return dir;
81     }
82
83     private static String JavaDoc getSubstitutedString(String JavaDoc text) throws CoreException {
84         if (text == null)
85             return ""; //$NON-NLS-1$
86
IStringVariableManager mgr = VariablesPlugin.getDefault().getStringVariableManager();
87         return mgr.performStringSubstitution(text);
88     }
89     
90     public static Properties JavaDoc createConfigIniFile(ILaunchConfiguration configuration, String JavaDoc productID, Map JavaDoc map, File JavaDoc directory) throws CoreException {
91         Properties JavaDoc properties = null;
92         // if we are to generate a config.ini, start with the values in the target platform's config.ini - bug 141918
93
if (configuration.getAttribute(IPDELauncherConstants.CONFIG_GENERATE_DEFAULT, true)) {
94             properties = TargetPlatformHelper.getConfigIniProperties();
95             // if target's config.ini does not exist, lets try to fill in default values
96
if (properties == null)
97                 properties = new Properties JavaDoc();
98             // keep properties only if we are launching the default product (bug 175437)
99
else if (productID == null || !productID.equals(properties.get("eclipse.product"))) //$NON-NLS-1$
100
properties.clear();
101             // if target's config.ini has the osgi.bundles header, then parse and compute the proper osgi.bundles value
102
String JavaDoc bundleList = properties.getProperty("osgi.bundles"); //$NON-NLS-1$
103
if (bundleList != null)
104                 properties.setProperty("osgi.bundles", computeOSGiBundles(TargetPlatformHelper.stripPathInformation(bundleList), map)); //$NON-NLS-1$
105
} else {
106             String JavaDoc templateLoc = configuration.getAttribute(IPDELauncherConstants.CONFIG_TEMPLATE_LOCATION, (String JavaDoc)null);
107             if (templateLoc != null) {
108                 properties = loadFromTemplate(getSubstitutedString(templateLoc));
109                 // if template contains osgi.bundles, then only strip the path, do not compute the value
110
String JavaDoc osgiBundles = properties.getProperty("osgi.bundles"); //$NON-NLS-1$
111
if (osgiBundles != null)
112                     properties.setProperty("osgi.bundles", TargetPlatformHelper.stripPathInformation(osgiBundles)); //$NON-NLS-1$
113
}
114         }
115         // whether we create a new config.ini or read from one as a template, we should add the required properties - bug 161265
116
if (properties != null)
117             addRequiredProperties(properties, productID, map);
118         else
119             properties = new Properties JavaDoc();
120         setBundleLocations(map, properties);
121         if (!directory.exists())
122             directory.mkdirs();
123         save(new File JavaDoc(directory, "config.ini"), properties); //$NON-NLS-1$
124
return properties;
125     }
126     
127     private static void addRequiredProperties(Properties JavaDoc properties, String JavaDoc productID, Map JavaDoc map) {
128         if (!properties.containsKey("osgi.install.area")) //$NON-NLS-1$
129
properties.setProperty("osgi.install.area", "file:" + TargetPlatform.getLocation()); //$NON-NLS-1$ //$NON-NLS-2$
130
if (!properties.containsKey("osgi.configuration.cascaded")) //$NON-NLS-1$
131
properties.setProperty("osgi.configuration.cascaded", "false"); //$NON-NLS-1$ //$NON-NLS-2$
132
if (!properties.containsKey("osgi.framework")) //$NON-NLS-1$
133
properties.setProperty("osgi.framework", "org.eclipse.osgi"); //$NON-NLS-1$ //$NON-NLS-2$
134
if (!properties.containsKey("osgi.splashPath") && productID != null) //$NON-NLS-1$
135
addSplashLocation(properties, productID, map);
136         // if osgi.splashPath is set, try to resolve relative paths to absolute paths
137
if (properties.containsKey("osgi.splashPath")) //$NON-NLS-1$
138
resolveLocationPath(properties.getProperty("osgi.splashPath"), properties, map); //$NON-NLS-1$
139
if (!properties.containsKey("osgi.bundles")) //$NON-NLS-1$
140
properties.setProperty("osgi.bundles", computeOSGiBundles(TargetPlatform.getBundleList(), map)); //$NON-NLS-1$
141
if (!properties.containsKey("osgi.bundles.defaultStartLevel")) //$NON-NLS-1$
142
properties.setProperty("osgi.bundles.defaultStartLevel", "4"); //$NON-NLS-1$ //$NON-NLS-2$
143
}
144     
145     private static String JavaDoc computeOSGiBundles(String JavaDoc bundleList, Map JavaDoc map) {
146         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
147         Set JavaDoc initialBundleSet = new HashSet JavaDoc();
148         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(bundleList, ","); //$NON-NLS-1$
149
while (tokenizer.hasMoreTokens()) {
150             String JavaDoc token = tokenizer.nextToken();
151             int index = token.indexOf('@');
152             String JavaDoc id = index != -1 ? token.substring(0, index) : token;
153             if (map.containsKey(id)) {
154                 if (buffer.length() > 0)
155                     buffer.append(',');
156                 buffer.append(id);
157                 if (index != -1 && index < token.length() -1)
158                     buffer.append(token.substring(index));
159                 initialBundleSet.add(id);
160             }
161         }
162         // if org.eclipse.update.configurator is not included (LIKE IN BASIC RCP APPLICATION), then write out all bundles in osgi.bundles - bug 170772
163
if (!initialBundleSet.contains("org.eclipse.update.configurator")) { //$NON-NLS-1$
164
initialBundleSet.add("org.eclipse.osgi"); //$NON-NLS-1$
165
Iterator JavaDoc iter = map.keySet().iterator();
166             while (iter.hasNext()) {
167                 String JavaDoc id = iter.next().toString();
168                 if (!initialBundleSet.contains(id)) {
169                     if (buffer.length() > 0)
170                         buffer.append(',');
171                     buffer.append(id);
172                 }
173             }
174         }
175         return buffer.toString();
176     }
177     
178     private static Properties JavaDoc loadFromTemplate(String JavaDoc templateLoc) throws CoreException {
179         Properties JavaDoc properties = new Properties JavaDoc();
180         File JavaDoc templateFile = new File JavaDoc(templateLoc);
181         if (templateFile.exists() && templateFile.isFile()) {
182             FileInputStream JavaDoc stream = null;
183             try {
184                 stream = new FileInputStream JavaDoc(templateFile);
185                 properties.load(stream);
186             } catch (Exception JavaDoc e) {
187                 String JavaDoc message = e.getMessage();
188                 if (message != null)
189                     throw new CoreException(
190                         new Status(
191                             IStatus.ERROR,
192                             PDEPlugin.getPluginId(),
193                             IStatus.ERROR,
194                             message,
195                             e));
196             } finally {
197                 if (stream != null) {
198                     try {
199                         stream.close();
200                     } catch (IOException JavaDoc e) {
201                     }
202                 }
203             }
204         }
205         return properties;
206     }
207
208     private static void addSplashLocation(Properties JavaDoc properties, String JavaDoc productID, Map JavaDoc map) {
209         Properties JavaDoc targetConfig = TargetPlatformHelper.getConfigIniProperties();
210         String JavaDoc targetProduct = targetConfig == null ? null : targetConfig.getProperty("eclipse.product"); //$NON-NLS-1$
211
String JavaDoc targetSplash = targetConfig == null ? null : targetConfig.getProperty("osgi.splashPath"); //$NON-NLS-1$
212
if (!productID.equals(targetProduct) || targetSplash == null) {
213             ArrayList JavaDoc locations = new ArrayList JavaDoc();
214             String JavaDoc plugin = getContributingPlugin(productID);
215             locations.add(plugin);
216             IPluginModelBase model = (IPluginModelBase)map.get(plugin);
217             if (model != null) {
218                 BundleDescription desc = model.getBundleDescription();
219                 if (desc != null) {
220                     BundleDescription[] fragments = desc.getFragments();
221                     for (int i = 0; i < fragments.length; i++)
222                         locations.add(fragments[i].getSymbolicName());
223                 }
224             }
225             resolveLocationPath(locations, properties, map);
226         } else
227             resolveLocationPath(targetSplash, properties, map);
228     }
229     
230     private static void resolveLocationPath(String JavaDoc splashPath, Properties JavaDoc properties, Map JavaDoc map) {
231         ArrayList JavaDoc locations = new ArrayList JavaDoc();
232         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(splashPath, ","); //$NON-NLS-1$
233
while (tok.hasMoreTokens())
234             locations.add(tok.nextToken());
235         resolveLocationPath(locations, properties, map);
236     }
237         
238     private static void resolveLocationPath(ArrayList JavaDoc locations, Properties JavaDoc properties, Map JavaDoc map) {
239         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
240         for (int i = 0; i < locations.size(); i++) {
241             String JavaDoc location = (String JavaDoc)locations.get(i);
242             if (location.startsWith("platform:/base/plugins/")) { //$NON-NLS-1$
243
location = location.replaceFirst("platform:/base/plugins/", ""); //$NON-NLS-1$ //$NON-NLS-2$
244
}
245             String JavaDoc url = getBundleURL(location, map);
246             if (url == null)
247                 continue;
248             if (buffer.length() > 0)
249                 buffer.append(","); //$NON-NLS-1$
250
buffer.append(url);
251         }
252         if (buffer.length() > 0)
253             properties.setProperty("osgi.splashPath", buffer.toString()); //$NON-NLS-1$
254
}
255     
256     public static String JavaDoc getBundleURL(String JavaDoc id, Map JavaDoc pluginMap) {
257         IPluginModelBase model = (IPluginModelBase)pluginMap.get(id.trim());
258         return getBundleURL(model);
259     }
260     
261     public static String JavaDoc getBundleURL(IPluginModelBase model) {
262         if (model == null)
263             return null;
264         return "file:" + new Path(model.getInstallLocation()).removeTrailingSeparator().toString(); //$NON-NLS-1$
265
}
266         
267     private static void setBundleLocations(Map JavaDoc map, Properties JavaDoc properties) {
268         String JavaDoc framework = properties.getProperty("osgi.framework"); //$NON-NLS-1$
269
if (framework != null) {
270             if (framework.startsWith("platform:/base/plugins/")) { //$NON-NLS-1$
271
framework.replaceFirst("platform:/base/plugins/", ""); //$NON-NLS-1$ //$NON-NLS-2$
272
}
273             String JavaDoc url = getBundleURL(framework, map);
274             if (url != null)
275                 properties.setProperty("osgi.framework", url); //$NON-NLS-1$
276
}
277         
278         String JavaDoc bundles = properties.getProperty("osgi.bundles"); //$NON-NLS-1$
279
if (bundles != null) {
280             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
281             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(bundles, ","); //$NON-NLS-1$
282
while (tokenizer.hasMoreTokens()) {
283                 String JavaDoc token = tokenizer.nextToken().trim();
284                 String JavaDoc url = getBundleURL(token, map);
285                 int index = -1;
286                 if (url == null) {
287                     index = token.indexOf('@');
288                     if (index != -1)
289                         url = getBundleURL(token.substring(0,index), map);
290                     if (url == null) {
291                         index = token.indexOf(':');
292                         if (index != -1)
293                             url = getBundleURL(token.substring(0,index), map);
294                     }
295                 }
296                 if (url != null) {
297                     if (buffer.length() > 0) {
298                         buffer.append(","); //$NON-NLS-1$
299
}
300                     buffer.append("reference:" + url); //$NON-NLS-1$
301
if (index != -1)
302                         buffer.append(token.substring(index));
303                 }
304             }
305             properties.setProperty("osgi.bundles", buffer.toString()); //$NON-NLS-1$
306
}
307     }
308     
309     public static void save(File JavaDoc file, Properties JavaDoc properties) {
310         try {
311             FileOutputStream JavaDoc stream = new FileOutputStream JavaDoc(file);
312             properties.store(stream, "Configuration File"); //$NON-NLS-1$
313
stream.flush();
314             stream.close();
315         } catch (IOException JavaDoc e) {
316             PDECore.logException(e);
317         }
318     }
319
320     public static String JavaDoc getContributingPlugin(String JavaDoc productID) {
321         if (productID == null)
322             return null;
323         int index = productID.lastIndexOf('.');
324         return index == -1 ? productID : productID.substring(0, index);
325     }
326     
327     public static String JavaDoc getProductID(ILaunchConfiguration configuration)
328             throws CoreException {
329         if (configuration.getAttribute(IPDELauncherConstants.USE_PRODUCT, false)) {
330             return configuration.getAttribute(IPDELauncherConstants.PRODUCT,
331                     (String JavaDoc) null);
332         }
333
334         // find the product associated with the application, and return its
335
// contributing plug-in
336
String JavaDoc appID = configuration.getAttribute(IPDELauncherConstants.APPLICATION,
337                 TargetPlatform.getDefaultApplication());
338         IPluginModelBase[] plugins = PluginRegistry.getActiveModels();
339         for (int i = 0; i < plugins.length; i++) {
340             String JavaDoc id = plugins[i].getPluginBase().getId();
341             IPluginExtension[] extensions = plugins[i].getPluginBase().getExtensions();
342             for (int j = 0; j < extensions.length; j++) {
343                 String JavaDoc point = extensions[j].getPoint();
344                 String JavaDoc extId = extensions[j].getId();
345                 if ("org.eclipse.core.runtime.products".equals(point) && extId != null) {//$NON-NLS-1$
346
IPluginObject[] children = extensions[j].getChildren();
347                     if (children.length != 1)
348                         continue;
349                     if (!"product".equals(children[0].getName())) //$NON-NLS-1$
350
continue;
351                     if (appID.equals(((IPluginElement) children[0]).getAttribute(
352                             "application").getValue())) { //$NON-NLS-1$
353
return id + "." + extId; //$NON-NLS-1$
354
}
355                 }
356             }
357         }
358         return null;
359
360     }
361
362 }
363
Popular Tags