KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > TargetPlatformHelper


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.core;
12
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Dictionary JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Hashtable JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Properties JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26 import java.util.TreeSet JavaDoc;
27 import java.util.zip.ZipEntry JavaDoc;
28 import java.util.zip.ZipFile JavaDoc;
29
30 import org.eclipse.core.runtime.IPath;
31 import org.eclipse.core.runtime.IStatus;
32 import org.eclipse.core.runtime.Path;
33 import org.eclipse.osgi.service.resolver.BundleDescription;
34 import org.eclipse.osgi.service.resolver.State;
35 import org.eclipse.osgi.util.ManifestElement;
36 import org.eclipse.pde.core.plugin.IPluginExtension;
37 import org.eclipse.pde.core.plugin.IPluginLibrary;
38 import org.eclipse.pde.core.plugin.IPluginModelBase;
39 import org.eclipse.pde.core.plugin.IPluginObject;
40 import org.eclipse.pde.core.plugin.PluginRegistry;
41 import org.eclipse.pde.core.plugin.TargetPlatform;
42 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
43 import org.eclipse.pde.internal.core.util.CoreUtility;
44 import org.eclipse.pde.internal.core.util.IdUtil;
45 import org.eclipse.pde.internal.core.util.VersionUtil;
46 import org.osgi.framework.BundleContext;
47 import org.osgi.framework.BundleException;
48 import org.osgi.framework.Constants;
49 import org.osgi.framework.InvalidSyntaxException;
50 import org.osgi.framework.Version;
51
52 public class TargetPlatformHelper {
53     
54     private static final String JavaDoc SYSTEM_BUNDLE = "org.eclipse.osgi"; //$NON-NLS-1$
55

56     private static String JavaDoc REFERENCE_PREFIX = "reference:"; //$NON-NLS-1$
57
private static String JavaDoc FILE_URL_PREFIX = "file:"; //$NON-NLS-1$
58

59     private static Map JavaDoc fCachedLocations;
60     
61     public static Properties JavaDoc getConfigIniProperties() {
62         File JavaDoc iniFile = new File JavaDoc(TargetPlatform.getLocation(), "configuration/config.ini"); //$NON-NLS-1$
63
if (!iniFile.exists())
64             return null;
65         Properties JavaDoc pini = new Properties JavaDoc();
66         try {
67             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(iniFile);
68             pini.load(fis);
69             fis.close();
70             return pini;
71         } catch (IOException JavaDoc e) {
72         }
73         return null;
74     }
75     
76     public static String JavaDoc getBundleList() {
77         Properties JavaDoc properties = getConfigIniProperties();
78         String JavaDoc osgiBundles = properties == null ? null : properties.getProperty("osgi.bundles"); //$NON-NLS-1$
79
if (osgiBundles == null) {
80             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
81             if (getTargetVersion() > 3.1) {
82                 buffer.append("org.eclipse.equinox.common@2:start,"); //$NON-NLS-1$
83
buffer.append("org.eclipse.update.configurator@3:start,"); //$NON-NLS-1$
84
buffer.append("org.eclipse.core.runtime@start"); //$NON-NLS-1$
85
} else {
86                 buffer.append("org.eclipse.core.runtime@2:start,"); //$NON-NLS-1$
87
buffer.append("org.eclipse.update.configurator@3:start"); //$NON-NLS-1$
88
}
89             osgiBundles = buffer.toString();
90         } else {
91             osgiBundles = stripPathInformation(osgiBundles);
92         }
93         return osgiBundles;
94     }
95     
96     public static String JavaDoc stripPathInformation(String JavaDoc osgiBundles) {
97         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
98         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(osgiBundles, ","); //$NON-NLS-1$
99
while (tokenizer.hasMoreElements()) {
100             String JavaDoc token = tokenizer.nextToken();
101             int index = token.indexOf('@');
102             
103             // read up until the first @, if there
104
String JavaDoc bundle = index > 0 ? token.substring(0, index) : token;
105             bundle = bundle.trim();
106             
107             // strip [reference:][file:/] prefixes if any
108
if (bundle.startsWith(REFERENCE_PREFIX) && bundle.length() > REFERENCE_PREFIX.length())
109                 bundle = bundle.substring(REFERENCE_PREFIX.length());
110             if (bundle.startsWith(FILE_URL_PREFIX) && bundle.length() > FILE_URL_PREFIX.length())
111                 bundle = bundle.substring(FILE_URL_PREFIX.length());
112             
113             // if the path is relative, the last segment is the bundle symbolic name
114
// Otherwise, we need to retrieve the bundle symbolic name ourselves
115
IPath path = new Path(bundle);
116             String JavaDoc id = path.isAbsolute() ? getSymbolicName(bundle) : path.lastSegment();
117             if (result.length() > 0)
118                 result.append(","); //$NON-NLS-1$
119
result.append(id != null ? id : bundle);
120             if (index > -1)
121                 result.append(token.substring(index).trim());
122         }
123         return result.toString();
124     }
125     
126     private static synchronized String JavaDoc getSymbolicName(String JavaDoc path) {
127         if (fCachedLocations == null)
128             fCachedLocations = new HashMap JavaDoc();
129         
130         File JavaDoc file = new File JavaDoc(path);
131         if (file.exists() && !fCachedLocations.containsKey(path)) {
132             try {
133                 Dictionary JavaDoc dictionary = MinimalState.loadManifest(file);
134                 String JavaDoc value = (String JavaDoc)dictionary.get(Constants.BUNDLE_SYMBOLICNAME);
135                 if (value != null) {
136                     ManifestElement[] elements = ManifestElement.parseHeader(Constants.BUNDLE_SYMBOLICNAME, value);
137                     String JavaDoc id = elements.length > 0 ? elements[0].getValue() : null;
138                     if (id != null)
139                         fCachedLocations.put(path, elements[0].getValue());
140                 }
141             } catch (IOException JavaDoc e) {
142             } catch (BundleException e) {
143             }
144         }
145         return (String JavaDoc)fCachedLocations.get(path);
146     }
147
148     
149     public static void checkPluginPropertiesConsistency(Map JavaDoc map, File JavaDoc configDir) {
150         File JavaDoc runtimeDir = new File JavaDoc(configDir, "org.eclipse.core.runtime"); //$NON-NLS-1$
151
if (runtimeDir.exists() && runtimeDir.isDirectory()) {
152             long timestamp = runtimeDir.lastModified();
153             Iterator JavaDoc iter = map.values().iterator();
154             while (iter.hasNext()) {
155                 if (hasChanged((IPluginModelBase)iter.next(), timestamp)) {
156                     CoreUtility.deleteContent(runtimeDir);
157                     break;
158                 }
159             }
160         }
161     }
162     
163     private static boolean hasChanged(IPluginModelBase model, long timestamp) {
164         if (model.getUnderlyingResource() != null) {
165             File JavaDoc[] files = new File JavaDoc(model.getInstallLocation()).listFiles();
166             for (int i = 0; i < files.length; i++) {
167                 if (files[i].isDirectory())
168                     continue;
169                 String JavaDoc name = files[i].getName();
170                 if (name.startsWith("plugin") && name.endsWith(".properties") //$NON-NLS-1$ //$NON-NLS-2$
171
&& files[i].lastModified() > timestamp) {
172                      return true;
173                 }
174             }
175         }
176         return false;
177     }
178     
179     public static Set JavaDoc getApplicationNameSet() {
180         TreeSet JavaDoc result = new TreeSet JavaDoc();
181         IPluginModelBase[] plugins = PluginRegistry.getActiveModels();
182         for (int i = 0; i < plugins.length; i++) {
183             IPluginExtension[] extensions = plugins[i].getPluginBase().getExtensions();
184             for (int j = 0; j < extensions.length; j++) {
185                 if ("org.eclipse.core.runtime.applications".equals(extensions[j].getPoint())) { //$NON-NLS-1$
186
if (extensions[j].getId() != null) {
187                         String JavaDoc id = IdUtil.getFullId(extensions[j]);
188                         if (!id.startsWith("org.eclipse.pde.junit.runtime")) //$NON-NLS-1$
189
result.add(IdUtil.getFullId(extensions[j]));
190                     }
191                 }
192             }
193         }
194         result.add("org.eclipse.ui.ide.workbench"); //$NON-NLS-1$
195
return result;
196     }
197
198     public static String JavaDoc[] getApplicationNames() {
199         Set JavaDoc result = getApplicationNameSet();
200         return (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
201     }
202     
203     public static TreeSet JavaDoc getProductNameSet() {
204         TreeSet JavaDoc result = new TreeSet JavaDoc();
205         IPluginModelBase[] plugins = PluginRegistry.getActiveModels();
206         for (int i = 0; i < plugins.length; i++) {
207             IPluginExtension[] extensions = plugins[i].getPluginBase().getExtensions();
208             for (int j = 0; j < extensions.length; j++) {
209                 if ("org.eclipse.core.runtime.products".equals(extensions[j].getPoint())) {//$NON-NLS-1$
210
IPluginObject[] children = extensions[j].getChildren();
211                     if (children.length != 1)
212                         continue;
213                     if (!"product".equals(children[0].getName())) //$NON-NLS-1$
214
continue;
215                     String JavaDoc id = extensions[j].getId();
216                     if (id != null && id.trim().length() > 0) {
217                         result.add(IdUtil.getFullId(extensions[j]));
218                     }
219                 }
220             }
221         }
222         return result;
223     }
224     
225     public static String JavaDoc[] getProductNames() {
226         TreeSet JavaDoc result = getProductNameSet();
227         return (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
228     }
229     
230     public static Dictionary JavaDoc getTargetEnvironment() {
231         Dictionary JavaDoc result = new Hashtable JavaDoc();
232         result.put ("osgi.os", TargetPlatform.getOS()); //$NON-NLS-1$
233
result.put ("osgi.ws", TargetPlatform.getWS()); //$NON-NLS-1$
234
result.put ("osgi.nl", TargetPlatform.getNL()); //$NON-NLS-1$
235
result.put ("osgi.arch", TargetPlatform.getOSArch()); //$NON-NLS-1$
236
result.put("osgi.resolveOptional", "true"); //$NON-NLS-1$ //$NON-NLS-2$
237
result.put("osgi.resolverMode", "development"); //$NON-NLS-1$ //$NON-NLS-2$
238
return result;
239     }
240     
241     public static Dictionary JavaDoc[] getPlatformProperties(String JavaDoc[] profiles, MinimalState state) {
242         if (profiles == null || profiles.length == 0)
243             return new Dictionary JavaDoc[] {getTargetEnvironment()};
244         
245         // add java profiles for those EE's that have a .profile file in the current system bundle
246
ArrayList JavaDoc result = new ArrayList JavaDoc(profiles.length);
247         for (int i = 0; i < profiles.length; i++) {
248             Properties JavaDoc profileProps = getJavaProfileProperties(profiles[i], state.getState());
249             if (profileProps != null) {
250                 Dictionary JavaDoc props = TargetPlatformHelper.getTargetEnvironment();
251                 String JavaDoc systemPackages = profileProps.getProperty(Constants.FRAMEWORK_SYSTEMPACKAGES);
252                 if (systemPackages != null)
253                     props.put(Constants.FRAMEWORK_SYSTEMPACKAGES, systemPackages);
254                 String JavaDoc ee = profileProps.getProperty(Constants.FRAMEWORK_EXECUTIONENVIRONMENT);
255                 if (ee != null)
256                     props.put(Constants.FRAMEWORK_EXECUTIONENVIRONMENT, ee);
257                 result.add(props);
258             }
259         }
260         if (result.size() > 0)
261             return (Dictionary JavaDoc[])result.toArray(new Dictionary JavaDoc[result.size()]);
262         return new Dictionary JavaDoc[] {TargetPlatformHelper.getTargetEnvironment()};
263     }
264     
265     private static Properties JavaDoc getJavaProfileProperties(String JavaDoc ee, State state) {
266         BundleDescription osgiBundle = state.getBundle(SYSTEM_BUNDLE, null);
267         if (osgiBundle == null)
268             return null;
269         
270         File JavaDoc location = new File JavaDoc(osgiBundle.getLocation());
271         String JavaDoc filename = ee.replace('/', '_') + ".profile"; //$NON-NLS-1$
272
InputStream JavaDoc is = null;
273         ZipFile JavaDoc zipFile = null;
274         try {
275             // find the input stream to the profile properties file
276
if (location.isDirectory()) {
277                 File JavaDoc file = new File JavaDoc(location, filename);
278                 if (file.exists())
279                     is = new FileInputStream JavaDoc(file);
280             } else {
281                 zipFile = null;
282                 try {
283                     zipFile = new ZipFile JavaDoc(location, ZipFile.OPEN_READ);
284                     ZipEntry JavaDoc entry = zipFile.getEntry(filename);
285                     if (entry != null)
286                         is = zipFile.getInputStream(entry);
287                 } catch (IOException JavaDoc e) {
288                     // nothing to do
289
}
290             }
291             if (is != null) {
292                 Properties JavaDoc profile = new Properties JavaDoc();
293                 profile.load(is);
294                 return profile;
295             }
296         } catch (IOException JavaDoc e) {
297             // nothing to do
298
} finally {
299             if (is != null)
300                 try {
301                     is.close();
302                 } catch (IOException JavaDoc e) {
303                     // nothing to do
304
}
305             if (zipFile != null)
306                 try {
307                     zipFile.close();
308                 } catch (IOException JavaDoc e) {
309                     // nothing to do
310
}
311         }
312         return null;
313     }
314     
315     public static String JavaDoc[] getKnownExecutionEnvironments() {
316         String JavaDoc jreProfile = System.getProperty("pde.jreProfile"); //$NON-NLS-1$
317
if (jreProfile != null && jreProfile.length() > 0) {
318             if ("none".equals(jreProfile)) //$NON-NLS-1$
319
return new String JavaDoc[0];
320             return new String JavaDoc[] {jreProfile};
321         }
322         return ExecutionEnvironmentAnalyzer.getKnownExecutionEnvironments();
323     }
324
325     public static String JavaDoc getTargetVersionString() {
326         IPluginModelBase model = PluginRegistry.findModel("org.eclipse.osgi"); //$NON-NLS-1$
327
if (model == null)
328             return ICoreConstants.TARGET33;
329         
330         String JavaDoc version = model.getPluginBase().getVersion();
331         if (VersionUtil.validateVersion(version).getSeverity() == IStatus.OK) {
332             Version vid = new Version(version);
333             int major = vid.getMajor();
334             int minor = vid.getMinor();
335             if (major == 3 && minor == 0)
336                 return ICoreConstants.TARGET30;
337             if (major == 3 && minor == 1)
338                 return ICoreConstants.TARGET31;
339             if (major == 3 && minor == 2)
340                 return ICoreConstants.TARGET32;
341         }
342         return ICoreConstants.TARGET33;
343     }
344     
345     public static double getTargetVersion() {
346         return Double.parseDouble(getTargetVersionString());
347     }
348     
349     public static PDEState getPDEState() {
350         return PDECore.getDefault().getModelManager().getState();
351     }
352     
353     public static State getState() {
354         return getPDEState().getState();
355     }
356     
357     public static Map JavaDoc getPatchMap(PDEState state) {
358         HashMap JavaDoc properties = new HashMap JavaDoc();
359         IPluginModelBase[] models = PluginRegistry.getActiveModels();
360         for (int i = 0; i < models.length; i++) {
361             BundleDescription desc = models[i].getBundleDescription();
362             if (desc == null)
363                 continue;
364             Long JavaDoc id = new Long JavaDoc(desc.getBundleId());
365             if (ClasspathUtilCore.hasExtensibleAPI(models[i])) {
366                 properties.put(id, ICoreConstants.EXTENSIBLE_API + ": true"); //$NON-NLS-1$
367
} else if (ClasspathUtilCore.isPatchFragment(models[i])) {
368                 properties.put(id, ICoreConstants.PATCH_FRAGMENT + ": true"); //$NON-NLS-1$
369
}
370         }
371         return properties;
372     }
373     
374     public static HashMap JavaDoc getBundleClasspaths(PDEState state) {
375         HashMap JavaDoc properties = new HashMap JavaDoc();
376         BundleDescription[] bundles = state.getState().getBundles();
377         for (int i = 0; i < bundles.length; i++) {
378             properties.put(new Long JavaDoc(bundles[i].getBundleId()), getValue(bundles[i], state));
379         }
380         return properties;
381     }
382     
383     private static String JavaDoc[] getValue(BundleDescription bundle, PDEState state) {
384         IPluginModelBase model = PluginRegistry.findModel(bundle);
385         String JavaDoc[] result = null;
386         if (model != null) {
387             IPluginLibrary[] libs = model.getPluginBase().getLibraries();
388             result = new String JavaDoc[libs.length];
389             for (int i = 0; i < libs.length; i++) {
390                 result[i] = libs[i].getName();
391             }
392         } else {
393             String JavaDoc[] libs = state.getLibraryNames(bundle.getBundleId());
394             result = new String JavaDoc[libs.length];
395             for (int i = 0; i < libs.length; i++) {
396                 result[i] = libs[i];
397             }
398         }
399         if (result.length == 0)
400             return new String JavaDoc[] {"."}; //$NON-NLS-1$
401
return result;
402     }
403     
404     public static String JavaDoc[] getFeaturePaths() {
405         IFeatureModel[] models = PDECore.getDefault().getFeatureModelManager().getModels();
406         ArrayList JavaDoc list = new ArrayList JavaDoc();
407         for (int i = 0; i < models.length; i++) {
408             String JavaDoc location = models[i].getInstallLocation();
409             if (location != null)
410                 list.add(location + IPath.SEPARATOR + "feature.xml"); //$NON-NLS-1$
411
}
412         return (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
413     }
414
415     public static boolean matchesCurrentEnvironment(IPluginModelBase model) {
416         BundleContext context = PDECore.getDefault().getBundleContext();
417         Dictionary JavaDoc environment = getTargetEnvironment();
418         BundleDescription bundle = model.getBundleDescription();
419         String JavaDoc filterSpec = bundle != null ? bundle.getPlatformFilter() : null;
420         try {
421             return filterSpec == null|| context.createFilter(filterSpec).match(environment);
422         } catch (InvalidSyntaxException e) {
423             return false;
424         }
425     }
426     
427     public static boolean usesNewApplicationModel() {
428         return PluginRegistry.findModel("org.eclipse.equinox.app") != null; //$NON-NLS-1$
429
}
430         
431 }
432
Popular Tags