1 11 package org.eclipse.pde.internal.core; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.util.ArrayList ; 18 import java.util.Dictionary ; 19 import java.util.HashMap ; 20 import java.util.Hashtable ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import java.util.Properties ; 24 import java.util.Set ; 25 import java.util.StringTokenizer ; 26 import java.util.TreeSet ; 27 import java.util.zip.ZipEntry ; 28 import java.util.zip.ZipFile ; 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 SYSTEM_BUNDLE = "org.eclipse.osgi"; 56 private static String REFERENCE_PREFIX = "reference:"; private static String FILE_URL_PREFIX = "file:"; 59 private static Map fCachedLocations; 60 61 public static Properties getConfigIniProperties() { 62 File iniFile = new File (TargetPlatform.getLocation(), "configuration/config.ini"); if (!iniFile.exists()) 64 return null; 65 Properties pini = new Properties (); 66 try { 67 FileInputStream fis = new FileInputStream (iniFile); 68 pini.load(fis); 69 fis.close(); 70 return pini; 71 } catch (IOException e) { 72 } 73 return null; 74 } 75 76 public static String getBundleList() { 77 Properties properties = getConfigIniProperties(); 78 String osgiBundles = properties == null ? null : properties.getProperty("osgi.bundles"); if (osgiBundles == null) { 80 StringBuffer buffer = new StringBuffer (); 81 if (getTargetVersion() > 3.1) { 82 buffer.append("org.eclipse.equinox.common@2:start,"); buffer.append("org.eclipse.update.configurator@3:start,"); buffer.append("org.eclipse.core.runtime@start"); } else { 86 buffer.append("org.eclipse.core.runtime@2:start,"); buffer.append("org.eclipse.update.configurator@3:start"); } 89 osgiBundles = buffer.toString(); 90 } else { 91 osgiBundles = stripPathInformation(osgiBundles); 92 } 93 return osgiBundles; 94 } 95 96 public static String stripPathInformation(String osgiBundles) { 97 StringBuffer result = new StringBuffer (); 98 StringTokenizer tokenizer = new StringTokenizer (osgiBundles, ","); while (tokenizer.hasMoreElements()) { 100 String token = tokenizer.nextToken(); 101 int index = token.indexOf('@'); 102 103 String bundle = index > 0 ? token.substring(0, index) : token; 105 bundle = bundle.trim(); 106 107 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 IPath path = new Path(bundle); 116 String id = path.isAbsolute() ? getSymbolicName(bundle) : path.lastSegment(); 117 if (result.length() > 0) 118 result.append(","); 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 getSymbolicName(String path) { 127 if (fCachedLocations == null) 128 fCachedLocations = new HashMap (); 129 130 File file = new File (path); 131 if (file.exists() && !fCachedLocations.containsKey(path)) { 132 try { 133 Dictionary dictionary = MinimalState.loadManifest(file); 134 String value = (String )dictionary.get(Constants.BUNDLE_SYMBOLICNAME); 135 if (value != null) { 136 ManifestElement[] elements = ManifestElement.parseHeader(Constants.BUNDLE_SYMBOLICNAME, value); 137 String id = elements.length > 0 ? elements[0].getValue() : null; 138 if (id != null) 139 fCachedLocations.put(path, elements[0].getValue()); 140 } 141 } catch (IOException e) { 142 } catch (BundleException e) { 143 } 144 } 145 return (String )fCachedLocations.get(path); 146 } 147 148 149 public static void checkPluginPropertiesConsistency(Map map, File configDir) { 150 File runtimeDir = new File (configDir, "org.eclipse.core.runtime"); if (runtimeDir.exists() && runtimeDir.isDirectory()) { 152 long timestamp = runtimeDir.lastModified(); 153 Iterator 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 [] files = new File (model.getInstallLocation()).listFiles(); 166 for (int i = 0; i < files.length; i++) { 167 if (files[i].isDirectory()) 168 continue; 169 String name = files[i].getName(); 170 if (name.startsWith("plugin") && name.endsWith(".properties") && files[i].lastModified() > timestamp) { 172 return true; 173 } 174 } 175 } 176 return false; 177 } 178 179 public static Set getApplicationNameSet() { 180 TreeSet result = new TreeSet (); 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())) { if (extensions[j].getId() != null) { 187 String id = IdUtil.getFullId(extensions[j]); 188 if (!id.startsWith("org.eclipse.pde.junit.runtime")) result.add(IdUtil.getFullId(extensions[j])); 190 } 191 } 192 } 193 } 194 result.add("org.eclipse.ui.ide.workbench"); return result; 196 } 197 198 public static String [] getApplicationNames() { 199 Set result = getApplicationNameSet(); 200 return (String [])result.toArray(new String [result.size()]); 201 } 202 203 public static TreeSet getProductNameSet() { 204 TreeSet result = new TreeSet (); 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())) { IPluginObject[] children = extensions[j].getChildren(); 211 if (children.length != 1) 212 continue; 213 if (!"product".equals(children[0].getName())) continue; 215 String 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 [] getProductNames() { 226 TreeSet result = getProductNameSet(); 227 return (String [])result.toArray(new String [result.size()]); 228 } 229 230 public static Dictionary getTargetEnvironment() { 231 Dictionary result = new Hashtable (); 232 result.put ("osgi.os", TargetPlatform.getOS()); result.put ("osgi.ws", TargetPlatform.getWS()); result.put ("osgi.nl", TargetPlatform.getNL()); result.put ("osgi.arch", TargetPlatform.getOSArch()); result.put("osgi.resolveOptional", "true"); result.put("osgi.resolverMode", "development"); return result; 239 } 240 241 public static Dictionary [] getPlatformProperties(String [] profiles, MinimalState state) { 242 if (profiles == null || profiles.length == 0) 243 return new Dictionary [] {getTargetEnvironment()}; 244 245 ArrayList result = new ArrayList (profiles.length); 247 for (int i = 0; i < profiles.length; i++) { 248 Properties profileProps = getJavaProfileProperties(profiles[i], state.getState()); 249 if (profileProps != null) { 250 Dictionary props = TargetPlatformHelper.getTargetEnvironment(); 251 String systemPackages = profileProps.getProperty(Constants.FRAMEWORK_SYSTEMPACKAGES); 252 if (systemPackages != null) 253 props.put(Constants.FRAMEWORK_SYSTEMPACKAGES, systemPackages); 254 String 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 [])result.toArray(new Dictionary [result.size()]); 262 return new Dictionary [] {TargetPlatformHelper.getTargetEnvironment()}; 263 } 264 265 private static Properties getJavaProfileProperties(String ee, State state) { 266 BundleDescription osgiBundle = state.getBundle(SYSTEM_BUNDLE, null); 267 if (osgiBundle == null) 268 return null; 269 270 File location = new File (osgiBundle.getLocation()); 271 String filename = ee.replace('/', '_') + ".profile"; InputStream is = null; 273 ZipFile zipFile = null; 274 try { 275 if (location.isDirectory()) { 277 File file = new File (location, filename); 278 if (file.exists()) 279 is = new FileInputStream (file); 280 } else { 281 zipFile = null; 282 try { 283 zipFile = new ZipFile (location, ZipFile.OPEN_READ); 284 ZipEntry entry = zipFile.getEntry(filename); 285 if (entry != null) 286 is = zipFile.getInputStream(entry); 287 } catch (IOException e) { 288 } 290 } 291 if (is != null) { 292 Properties profile = new Properties (); 293 profile.load(is); 294 return profile; 295 } 296 } catch (IOException e) { 297 } finally { 299 if (is != null) 300 try { 301 is.close(); 302 } catch (IOException e) { 303 } 305 if (zipFile != null) 306 try { 307 zipFile.close(); 308 } catch (IOException e) { 309 } 311 } 312 return null; 313 } 314 315 public static String [] getKnownExecutionEnvironments() { 316 String jreProfile = System.getProperty("pde.jreProfile"); if (jreProfile != null && jreProfile.length() > 0) { 318 if ("none".equals(jreProfile)) return new String [0]; 320 return new String [] {jreProfile}; 321 } 322 return ExecutionEnvironmentAnalyzer.getKnownExecutionEnvironments(); 323 } 324 325 public static String getTargetVersionString() { 326 IPluginModelBase model = PluginRegistry.findModel("org.eclipse.osgi"); if (model == null) 328 return ICoreConstants.TARGET33; 329 330 String 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 getPatchMap(PDEState state) { 358 HashMap properties = new HashMap (); 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 id = new Long (desc.getBundleId()); 365 if (ClasspathUtilCore.hasExtensibleAPI(models[i])) { 366 properties.put(id, ICoreConstants.EXTENSIBLE_API + ": true"); } else if (ClasspathUtilCore.isPatchFragment(models[i])) { 368 properties.put(id, ICoreConstants.PATCH_FRAGMENT + ": true"); } 370 } 371 return properties; 372 } 373 374 public static HashMap getBundleClasspaths(PDEState state) { 375 HashMap properties = new HashMap (); 376 BundleDescription[] bundles = state.getState().getBundles(); 377 for (int i = 0; i < bundles.length; i++) { 378 properties.put(new Long (bundles[i].getBundleId()), getValue(bundles[i], state)); 379 } 380 return properties; 381 } 382 383 private static String [] getValue(BundleDescription bundle, PDEState state) { 384 IPluginModelBase model = PluginRegistry.findModel(bundle); 385 String [] result = null; 386 if (model != null) { 387 IPluginLibrary[] libs = model.getPluginBase().getLibraries(); 388 result = new String [libs.length]; 389 for (int i = 0; i < libs.length; i++) { 390 result[i] = libs[i].getName(); 391 } 392 } else { 393 String [] libs = state.getLibraryNames(bundle.getBundleId()); 394 result = new String [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 [] {"."}; return result; 402 } 403 404 public static String [] getFeaturePaths() { 405 IFeatureModel[] models = PDECore.getDefault().getFeatureModelManager().getModels(); 406 ArrayList list = new ArrayList (); 407 for (int i = 0; i < models.length; i++) { 408 String location = models[i].getInstallLocation(); 409 if (location != null) 410 list.add(location + IPath.SEPARATOR + "feature.xml"); } 412 return (String []) list.toArray(new String [list.size()]); 413 } 414 415 public static boolean matchesCurrentEnvironment(IPluginModelBase model) { 416 BundleContext context = PDECore.getDefault().getBundleContext(); 417 Dictionary environment = getTargetEnvironment(); 418 BundleDescription bundle = model.getBundleDescription(); 419 String 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; } 430 431 } 432 | Popular Tags |