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.net.MalformedURLException ; 17 import java.net.URL ; 18 import java.util.ArrayList ; 19 import java.util.Dictionary ; 20 import java.util.HashMap ; 21 import java.util.Hashtable ; 22 import java.util.Iterator ; 23 import java.util.Locale ; 24 import java.util.Map ; 25 import java.util.Properties ; 26 import java.util.Set ; 27 import java.util.StringTokenizer ; 28 import java.util.TreeSet ; 29 30 import org.eclipse.core.runtime.CoreException; 31 import org.eclipse.core.runtime.IPath; 32 import org.eclipse.core.runtime.IStatus; 33 import org.eclipse.core.runtime.Path; 34 import org.eclipse.core.runtime.Platform; 35 import org.eclipse.core.runtime.Status; 36 import org.eclipse.osgi.service.resolver.BundleDescription; 37 import org.eclipse.osgi.service.resolver.State; 38 import org.eclipse.osgi.util.ManifestElement; 39 import org.eclipse.pde.core.plugin.IPluginExtension; 40 import org.eclipse.pde.core.plugin.IPluginLibrary; 41 import org.eclipse.pde.core.plugin.IPluginModelBase; 42 import org.eclipse.pde.core.plugin.IPluginObject; 43 import org.eclipse.pde.internal.core.ifeature.IFeature; 44 import org.eclipse.pde.internal.core.ifeature.IFeatureModel; 45 import org.eclipse.pde.internal.core.util.CoreUtility; 46 import org.eclipse.update.configurator.ConfiguratorUtils; 47 import org.eclipse.update.configurator.IPlatformConfiguration; 48 import org.osgi.framework.BundleContext; 49 import org.osgi.framework.BundleException; 50 import org.osgi.framework.Constants; 51 import org.osgi.framework.InvalidSyntaxException; 52 53 public class TargetPlatform implements IEnvironmentVariables { 54 55 private static String REFERENCE_PREFIX = "reference:"; private static String FILE_URL_PREFIX = "file:"; 58 static class LocalSite { 59 private ArrayList plugins; 60 private IPath path; 61 62 public LocalSite(IPath path) { 63 if (path.getDevice() != null) 64 this.path = path.setDevice(path.getDevice().toUpperCase(Locale.ENGLISH)); 65 else 66 this.path = path; 67 plugins = new ArrayList (); 68 } 69 70 public IPath getPath() { 71 return path; 72 } 73 74 public URL getURL() throws MalformedURLException { 75 return new URL ("file:" + path.removeTrailingSeparator()); } 77 78 public void add(IPluginModelBase model) { 79 plugins.add(model); 80 } 81 82 public String [] getRelativePluginList() { 83 String [] list = new String [plugins.size()]; 84 for (int i = 0; i < plugins.size(); i++) { 85 IPluginModelBase model = (IPluginModelBase) plugins.get(i); 86 IPath location = new Path(model.getInstallLocation()); 87 if (location.segmentCount() > 2) 89 location = location.removeFirstSegments(location.segmentCount() - 2); 90 list[i] = location.setDevice(null).makeRelative().toString(); 92 } 93 return list; 94 } 95 } 96 97 private static Map fCachedLocations; 98 99 public static Properties getConfigIniProperties() { 100 File iniFile = new File (ExternalModelManager.getEclipseHome().toOSString(), "configuration/config.ini"); if (!iniFile.exists()) 102 return null; 103 Properties pini = new Properties (); 104 try { 105 FileInputStream fis = new FileInputStream (iniFile); 106 pini.load(fis); 107 fis.close(); 108 return pini; 109 } catch (IOException e) { 110 } 111 return null; 112 } 113 114 public static String getBundleList() { 115 Properties properties = getConfigIniProperties(); 116 String osgiBundles = properties == null ? null : properties.getProperty("osgi.bundles"); if (osgiBundles == null) { 118 StringBuffer buffer = new StringBuffer (); 119 if (getTargetVersion() > 3.1) { 120 buffer.append("org.eclipse.equinox.common@2:start,"); buffer.append("org.eclipse.update.configurator@3:start,"); buffer.append("org.eclipse.core.runtime@start"); } else { 124 buffer.append("org.eclipse.core.runtime@2:start,"); buffer.append("org.eclipse.update.configurator@3:start"); } 127 osgiBundles = buffer.toString(); 128 } else { 129 osgiBundles = stripPathInformation(osgiBundles); 130 } 131 return osgiBundles; 132 } 133 134 public static String stripPathInformation(String osgiBundles) { 135 osgiBundles = osgiBundles.replaceAll("\\s", ""); StringBuffer result = new StringBuffer (); 137 StringTokenizer tokenizer = new StringTokenizer (osgiBundles, ","); while (tokenizer.hasMoreElements()) { 139 String token = tokenizer.nextToken(); 140 int index = token.indexOf('@'); 141 142 String bundle = index > 0 ? token.substring(0, index) : token; 144 145 if (bundle.startsWith(REFERENCE_PREFIX) && bundle.length() > REFERENCE_PREFIX.length()) 147 bundle = bundle.substring(REFERENCE_PREFIX.length()); 148 if (bundle.startsWith(FILE_URL_PREFIX) && bundle.length() > FILE_URL_PREFIX.length()) 149 bundle = bundle.substring(FILE_URL_PREFIX.length()); 150 151 IPath path = new Path(bundle); 154 String id = path.isAbsolute() ? getSymbolicName(bundle) : path.lastSegment(); 155 if (result.length() > 0) 156 result.append(","); result.append(id != null ? id : bundle); 158 if (index > -1) 159 result.append(token.substring(index)); 160 } 161 return result.toString(); 162 } 163 164 private static synchronized String getSymbolicName(String path) { 165 if (fCachedLocations == null) 166 fCachedLocations = new HashMap (); 167 168 File file = new File (path); 169 if (file.exists() && !fCachedLocations.containsKey(path)) { 170 try { 171 Dictionary dictionary = MinimalState.loadManifest(file); 172 String value = (String )dictionary.get(Constants.BUNDLE_SYMBOLICNAME); 173 if (value != null) { 174 ManifestElement[] elements = ManifestElement.parseHeader(Constants.BUNDLE_SYMBOLICNAME, value); 175 String id = elements.length > 0 ? elements[0].getValue() : null; 176 if (id != null) 177 fCachedLocations.put(path, elements[0].getValue()); 178 } 179 } catch (IOException e) { 180 } catch (BundleException e) { 181 } 182 } 183 return (String )fCachedLocations.get(path); 184 } 185 186 187 public static void createPlatformConfigurationArea( 188 Map pluginMap, 189 File configDir, 190 String brandingPluginID) 191 throws CoreException { 192 try { 193 if (pluginMap.containsKey("org.eclipse.update.configurator")) savePlatformConfiguration(ConfiguratorUtils.getPlatformConfiguration(null),configDir, pluginMap, brandingPluginID); 195 checkPluginPropertiesConsistency(pluginMap, configDir); 196 } catch (CoreException e) { 197 throw e; 199 } catch (Exception e) { 200 String message = e.getMessage(); 202 if (message==null || message.length() == 0) 203 message = PDECoreMessages.TargetPlatform_exceptionThrown; 204 throw new CoreException( 205 new Status( 206 IStatus.ERROR, 207 PDECore.getPluginId(), 208 IStatus.ERROR, 209 message, 210 e)); 211 } 212 } 213 214 private static void checkPluginPropertiesConsistency(Map map, File configDir) { 215 File runtimeDir = new File (configDir, "org.eclipse.core.runtime"); if (runtimeDir.exists() && runtimeDir.isDirectory()) { 217 long timestamp = runtimeDir.lastModified(); 218 Iterator iter = map.values().iterator(); 219 while (iter.hasNext()) { 220 if (hasChanged((IPluginModelBase)iter.next(), timestamp)) { 221 CoreUtility.deleteContent(runtimeDir); 222 break; 223 } 224 } 225 } 226 } 227 228 private static boolean hasChanged(IPluginModelBase model, long timestamp) { 229 if (model.getUnderlyingResource() != null) { 230 File [] files = new File (model.getInstallLocation()).listFiles(); 231 for (int i = 0; i < files.length; i++) { 232 if (files[i].isDirectory()) 233 continue; 234 String name = files[i].getName(); 235 if (name.startsWith("plugin") && name.endsWith(".properties") && files[i].lastModified() > timestamp) { 237 return true; 238 } 239 } 240 } 241 return false; 242 } 243 244 private static void savePlatformConfiguration( 245 IPlatformConfiguration platformConfiguration, 246 File configFile, 247 Map pluginMap, 248 String primaryFeatureId) 249 throws IOException , CoreException, MalformedURLException { 250 ArrayList sites = new ArrayList (); 251 252 Iterator iter = pluginMap.values().iterator(); 254 while(iter.hasNext()) { 255 IPluginModelBase model = (IPluginModelBase)iter.next(); 256 IPath sitePath = getTransientSitePath(model); 257 addToSite(sitePath, model, sites); 258 } 259 260 createConfigurationEntries(platformConfiguration,sites); 261 if (primaryFeatureId != null) 262 createFeatureEntries(platformConfiguration, pluginMap, primaryFeatureId); 263 platformConfiguration.refresh(); 264 platformConfiguration.save(new URL ("file:" + configFile.getPath())); } 266 267 private static IPath getTransientSitePath(IPluginModelBase model) { 268 return new Path(model.getInstallLocation()).removeLastSegments(2); 269 } 270 271 private static void addToSite( 272 IPath path, 273 IPluginModelBase model, 274 ArrayList sites) { 275 if (path.getDevice() != null) 276 path = path.setDevice(path.getDevice().toUpperCase(Locale.ENGLISH)); 277 for (int i = 0; i < sites.size(); i++) { 278 LocalSite localSite = (LocalSite) sites.get(i); 279 if (localSite.getPath().equals(path)) { 280 localSite.add(model); 281 return; 282 } 283 } 284 LocalSite localSite = new LocalSite(path); 286 localSite.add(model); 287 sites.add(localSite); 288 } 289 290 private static void createConfigurationEntries( 291 IPlatformConfiguration config, 292 ArrayList sites) 293 throws CoreException, MalformedURLException { 294 295 for (int i = 0; i < sites.size(); i++) { 296 LocalSite localSite = (LocalSite) sites.get(i); 297 String [] plugins = localSite.getRelativePluginList(); 298 299 int policy = IPlatformConfiguration.ISitePolicy.USER_INCLUDE; 300 IPlatformConfiguration.ISitePolicy sitePolicy = 301 config.createSitePolicy(policy, plugins); 302 IPlatformConfiguration.ISiteEntry siteEntry = 303 config.createSiteEntry(localSite.getURL(), sitePolicy); 304 config.configureSite(siteEntry); 305 } 306 config.isTransient(true); 307 } 308 309 310 private static void createFeatureEntries( 311 IPlatformConfiguration config, 312 Map pluginMap, 313 String brandingPluginID) 314 throws MalformedURLException { 315 316 IFeatureModel featureModel = PDECore.getDefault().getFeatureModelManager().findFeatureModel(brandingPluginID); 318 if (featureModel == null) 319 return; 320 321 IFeature feature = featureModel.getFeature(); 322 String featureVersion = feature.getVersion(); 323 IPluginModelBase primaryPlugin = (IPluginModelBase)pluginMap.get(brandingPluginID); 324 if (primaryPlugin == null) 325 return; 326 327 URL pluginURL = new URL ("file:" + primaryPlugin.getInstallLocation()); URL [] root = new URL [] { pluginURL }; 329 IPlatformConfiguration.IFeatureEntry featureEntry = 330 config.createFeatureEntry( 331 brandingPluginID, 332 featureVersion, 333 brandingPluginID, 334 primaryPlugin.getPluginBase().getVersion(), 335 true, 336 null, 337 root); 338 config.configureFeatureEntry(featureEntry); 339 } 340 341 public static String getOS() { 342 String value = getProperty(OS); 343 return value.equals("") ? Platform.getOS() : value; } 345 346 public static String getWS() { 347 String value = getProperty(WS); 348 return value.equals("") ? Platform.getWS() : value; } 350 351 public static String getNL() { 352 String value = getProperty(NL); 353 return value.equals("") ? Platform.getNL() : value; } 355 356 public static String getOSArch() { 357 String value = getProperty(ARCH); 358 return value.equals("") ? Platform.getOSArch() : value; } 360 361 private static String getProperty(String key) { 362 return PDECore.getDefault().getPluginPreferences().getString(key); 363 } 364 365 public static String [] getApplicationNames() { 366 TreeSet result = new TreeSet (); 367 IPluginModelBase[] plugins = PDECore.getDefault().getModelManager().getPlugins(); 368 for (int i = 0; i < plugins.length; i++) { 369 IPluginExtension[] extensions = plugins[i].getPluginBase().getExtensions(); 370 for (int j = 0; j < extensions.length; j++) { 371 String point = extensions[j].getPoint(); 372 if (point != null && point.equals("org.eclipse.core.runtime.applications")) { String id = extensions[j].getPluginBase().getId(); 374 if (id == null || id.trim().length() == 0 || id.startsWith("org.eclipse.pde.junit.runtime")) continue; 376 if (extensions[j].getId() != null) 377 result.add(id+ "." + extensions[j].getId()); } 379 } 380 } 381 return (String [])result.toArray(new String [result.size()]); 382 } 383 384 public static TreeSet getProductNameSet() { 385 TreeSet result = new TreeSet (); 386 IPluginModelBase[] plugins = PDECore.getDefault().getModelManager().getPlugins(); 387 for (int i = 0; i < plugins.length; i++) { 388 IPluginExtension[] extensions = plugins[i].getPluginBase().getExtensions(); 389 for (int j = 0; j < extensions.length; j++) { 390 String point = extensions[j].getPoint(); 391 if (point != null && point.equals("org.eclipse.core.runtime.products")) { IPluginObject[] children = extensions[j].getChildren(); 393 if (children.length != 1) 394 continue; 395 if (!"product".equals(children[0].getName())) continue; 397 String id = extensions[j].getPluginBase().getId(); 398 if (id == null || id.trim().length() == 0) 399 continue; 400 if (extensions[j].getId() != null) 401 result.add(id+ "." + extensions[j].getId()); } 403 } 404 } 405 return result; 406 } 407 408 public static String [] getProductNames() { 409 TreeSet result = getProductNameSet(); 410 return (String [])result.toArray(new String [result.size()]); 411 } 412 413 public static Dictionary getTargetEnvironment() { 414 Dictionary result = new Hashtable (); 415 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"); return result; 421 } 422 423 public static String getTargetVersionString() { 424 return PDECore.getDefault().getModelManager().getTargetVersion(); 425 } 426 427 public static double getTargetVersion() { 428 return Double.parseDouble(getTargetVersionString()); 429 } 430 431 public static PDEState getPDEState() { 432 return PDECore.getDefault().getModelManager().getState(); 433 } 434 435 public static State getState() { 436 return getPDEState().getState(); 437 } 438 439 public static Map getPatchMap(PDEState state) { 440 HashMap properties = new HashMap (); 441 PluginModelManager manager = PDECore.getDefault().getModelManager(); 442 IPluginModelBase[] models = manager.getAllPlugins(); 443 for (int i = 0; i < models.length; i++) { 444 BundleDescription desc = models[i].getBundleDescription(); 445 if (desc == null) 446 continue; 447 Long id = new Long (desc.getBundleId()); 448 if (ClasspathUtilCore.hasExtensibleAPI(models[i])) { 449 properties.put(id, ICoreConstants.EXTENSIBLE_API + ": true"); } else if (ClasspathUtilCore.isPatchFragment(models[i])) { 451 properties.put(id, ICoreConstants.PATCH_FRAGMENT + ": true"); } 453 } 454 return properties; 455 } 456 457 public static HashMap getBundleClasspaths(PDEState state) { 458 HashMap properties = new HashMap (); 459 BundleDescription[] bundles = state.getState().getBundles(); 460 for (int i = 0; i < bundles.length; i++) { 461 properties.put(new Long (bundles[i].getBundleId()), getValue(bundles[i], state)); 462 } 463 return properties; 464 } 465 466 private static String [] getValue(BundleDescription bundle, PDEState state) { 467 IPluginModelBase model = PDECore.getDefault().getModelManager().findModel(bundle); 468 String [] result = null; 469 if (model != null) { 470 IPluginLibrary[] libs = model.getPluginBase().getLibraries(); 471 result = new String [libs.length]; 472 for (int i = 0; i < libs.length; i++) { 473 result[i] = libs[i].getName(); 474 } 475 } else { 476 String [] libs = state.getLibraryNames(bundle.getBundleId()); 477 result = new String [libs.length]; 478 for (int i = 0; i < libs.length; i++) { 479 result[i] = libs[i]; 480 } 481 } 482 if (result.length == 0) 483 return new String [] {"."}; return result; 485 } 486 487 public static String [] getFeaturePaths() { 488 IFeatureModel[] models = PDECore.getDefault().getFeatureModelManager().getModels(); 489 ArrayList list = new ArrayList (); 490 for (int i = 0; i < models.length; i++) { 491 String location = models[i].getInstallLocation(); 492 if (location != null) 493 list.add(location + IPath.SEPARATOR + "feature.xml"); } 495 return (String []) list.toArray(new String [list.size()]); 496 } 497 498 503 public static String getDefaultProduct() { 504 Properties config = getConfigIniProperties(); 505 if (config != null) { 506 String product = (String ) config.get("eclipse.product"); if (product != null && getProductNameSet().contains(product)) 508 return product; 509 } 510 Set set = getProductNameSet(); 511 if (set.contains("org.eclipse.sdk.ide")) return "org.eclipse.sdk.ide"; 514 return set.contains("org.eclipse.platform.ide") ? "org.eclipse.platform.ide" : null; } 516 517 public static boolean isRuntimeRefactored1() { 518 PluginModelManager manager = PDECore.getDefault().getModelManager(); 519 return manager.findEntry("org.eclipse.equinox.common") != null; } 521 522 public static boolean isRuntimeRefactored2() { 523 PluginModelManager manager = PDECore.getDefault().getModelManager(); 524 return manager.findEntry("org.eclipse.core.runtime.compatibility.registry") != null; } 526 527 public static boolean matchesCurrentEnvironment(IPluginModelBase model) { 528 BundleContext context = PDECore.getDefault().getBundleContext(); 529 Dictionary environment = getTargetEnvironment(); 530 BundleDescription bundle = model.getBundleDescription(); 531 String filterSpec = bundle != null ? bundle.getPlatformFilter() : null; 532 try { 533 return filterSpec == null|| context.createFilter(filterSpec).match(environment); 534 } catch (InvalidSyntaxException e) { 535 return false; 536 } 537 } 538 539 } 540 | Popular Tags |