1 11 package org.eclipse.pde.internal.core.bundle; 12 13 import java.io.PrintWriter ; 14 import java.io.Serializable ; 15 import java.util.ArrayList ; 16 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.PlatformObject; 19 import org.eclipse.osgi.service.resolver.BundleDescription; 20 import org.eclipse.osgi.service.resolver.BundleSpecification; 21 import org.eclipse.osgi.util.ManifestElement; 22 import org.eclipse.pde.core.IIdentifiable; 23 import org.eclipse.pde.core.IModelChangedEvent; 24 import org.eclipse.pde.core.ModelChangedEvent; 25 import org.eclipse.pde.core.plugin.IExtensions; 26 import org.eclipse.pde.core.plugin.IPluginBase; 27 import org.eclipse.pde.core.plugin.IPluginExtension; 28 import org.eclipse.pde.core.plugin.IPluginExtensionPoint; 29 import org.eclipse.pde.core.plugin.IPluginImport; 30 import org.eclipse.pde.core.plugin.IPluginLibrary; 31 import org.eclipse.pde.core.plugin.IPluginModelBase; 32 import org.eclipse.pde.core.plugin.IPluginObject; 33 import org.eclipse.pde.core.plugin.ISharedExtensionsModel; 34 import org.eclipse.pde.core.plugin.ISharedPluginModel; 35 import org.eclipse.pde.internal.core.ICoreConstants; 36 import org.eclipse.pde.internal.core.PDEStateHelper; 37 import org.eclipse.pde.internal.core.TargetPlatformHelper; 38 import org.eclipse.pde.internal.core.ibundle.IBundle; 39 import org.eclipse.pde.internal.core.ibundle.IBundleModel; 40 import org.eclipse.pde.internal.core.ibundle.IBundlePluginBase; 41 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase; 42 import org.eclipse.pde.internal.core.ibundle.IManifestHeader; 43 import org.eclipse.pde.internal.core.plugin.AbstractExtensions; 44 import org.eclipse.pde.internal.core.plugin.PluginImport; 45 import org.eclipse.pde.internal.core.plugin.PluginLibrary; 46 import org.eclipse.pde.internal.core.text.bundle.BundleClasspathHeader; 47 import org.eclipse.pde.internal.core.text.bundle.BundleNameHeader; 48 import org.eclipse.pde.internal.core.text.bundle.BundleSymbolicNameHeader; 49 import org.eclipse.pde.internal.core.text.bundle.BundleVendorHeader; 50 import org.eclipse.pde.internal.core.text.bundle.BundleVersionHeader; 51 import org.eclipse.pde.internal.core.text.bundle.RequireBundleHeader; 52 import org.osgi.framework.BundleException; 53 import org.osgi.framework.Constants; 54 import org.osgi.framework.Version; 55 56 public class BundlePluginBase extends PlatformObject implements IBundlePluginBase, Serializable { 57 58 private static final long serialVersionUID = 1L; 59 protected IBundlePluginModelBase model; 60 private ArrayList libraries; 61 private ArrayList imports; 62 private String fTarget; 63 64 public void reset() { 65 libraries = null; 66 imports = null; 67 } 68 69 public String getSchemaVersion() { 70 IExtensions root = getExtensionsRoot(); 71 if (root instanceof AbstractExtensions) 72 return ((AbstractExtensions)root).getSchemaVersion(); 73 return (root instanceof IPluginBase) ? ((IPluginBase)root).getSchemaVersion() : null; 74 } 75 76 public void setSchemaVersion(String value) throws CoreException { 77 IExtensions root = getExtensionsRoot(); 78 if (root == null) 79 return; 80 if (root instanceof AbstractExtensions) 81 ((AbstractExtensions)root).setSchemaVersion(value); 82 if (root instanceof IPluginBase) 83 ((IPluginBase)root).setSchemaVersion(value); 84 } 85 86 public void modelChanged(IModelChangedEvent event) { 87 if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 88 if (event.getChangeProvider().equals(model.getBundleModel())) { 89 reset(); 90 } 91 getModel().fireModelChanged(event); 92 } else if (!event.getChangeProvider().equals(model.getBundleModel())) { 93 getModel().fireModelChanged(event); 94 } 95 } 96 97 102 public IBundle getBundle() { 103 if (model != null) { 104 IBundleModel bmodel = model.getBundleModel(); 105 return bmodel != null ? bmodel.getBundle() : null; 106 } 107 return null; 108 } 109 110 protected IManifestHeader getManifestHeader(String key) { 111 IBundle bundle = getBundle(); 112 return (bundle != null) ? bundle.getManifestHeader(key) : null; 113 } 114 115 public ISharedPluginModel getModel() { 116 return model; 117 } 118 119 void setModel(IBundlePluginModelBase model) { 120 this.model = model; 121 } 122 123 128 public IExtensions getExtensionsRoot() { 129 if (model != null) { 130 ISharedExtensionsModel emodel = model.getExtensionsModel(); 131 return emodel != null ? emodel.getExtensions() : null; 132 } 133 return null; 134 } 135 136 141 public void add(IPluginLibrary library) throws CoreException { 142 if (libraries == null) 143 getLibraries(); 145 libraries.add(library); 146 Object header = getManifestHeader(Constants.BUNDLE_CLASSPATH); 147 if (header instanceof BundleClasspathHeader) { 148 ((BundleClasspathHeader)header).addLibrary(library.getName()); 149 } else { 150 String value = header == null ? null : ((IManifestHeader)header).getValue(); 151 StringBuffer buffer = new StringBuffer (value == null ? "" : value); if (value != null) 153 buffer.append(",\n "); buffer.append(library.getName()); 155 getBundle().setHeader(Constants.BUNDLE_CLASSPATH, buffer.toString()); 156 } 157 fireStructureChanged(library, true); 158 } 159 160 165 public void remove(IPluginLibrary library) throws CoreException { 166 if (libraries != null) { 167 libraries.remove(library); 168 Object header = getManifestHeader(Constants.BUNDLE_CLASSPATH); 169 if (header instanceof BundleClasspathHeader) { 170 ((BundleClasspathHeader)header).removeLibrary(library.getName()); 171 } 172 fireStructureChanged(library, false); 173 } 174 } 175 176 181 public void add(IPluginImport iimport) throws CoreException { 182 if (iimport == null) 183 return; 184 if (imports == null) 185 getImports(); 187 addImport(iimport); 188 fireStructureChanged(iimport, true); 189 } 190 191 public void add(IPluginImport[] iimports) throws CoreException { 192 if (iimports != null && iimports.length > 0) { 193 if (imports == null) 194 getImports(); 196 for (int i = 0; i < iimports.length; i++) { 197 if (iimports[i] != null) 198 addImport(iimports[i]); 199 } 200 fireStructureChanged(iimports, true); 201 } 202 } 203 204 private void addImport(IPluginImport iimport) { 205 imports.add(iimport); 206 Object header = getManifestHeader(Constants.REQUIRE_BUNDLE); 207 if (header instanceof RequireBundleHeader) { 208 ((RequireBundleHeader)header).addBundle(iimport); 209 } else { 210 String value = header == null ? null : ((IManifestHeader)header).getValue(); 211 StringBuffer buffer = new StringBuffer (value == null ? "" : value); if (value != null) 213 buffer.append(",\n "); buffer.append(iimport.getId()); 215 int bundleManifestVersion = getBundleManifestVersion(getBundle()); 216 if (iimport.isOptional()) 217 if (bundleManifestVersion > 1) 218 buffer.append(";" + Constants.RESOLUTION_DIRECTIVE + ":=" + Constants.RESOLUTION_OPTIONAL); else 220 buffer.append(";" + ICoreConstants.OPTIONAL_ATTRIBUTE + "=true"); if (iimport.isReexported()) 222 if (bundleManifestVersion > 1) 223 buffer.append(";" + Constants.VISIBILITY_DIRECTIVE + ":=" + Constants.VISIBILITY_REEXPORT); else 225 buffer.append(";" + ICoreConstants.REPROVIDE_ATTRIBUTE + "=true"); String version = iimport.getVersion(); 227 if (version != null && version.trim().length() > 0) 228 buffer.append(";" + Constants.BUNDLE_VERSION_ATTRIBUTE + "=\"" + version.trim() + "\""); getBundle().setHeader(Constants.REQUIRE_BUNDLE, buffer.toString()); 230 } 231 } 232 233 238 public void remove(IPluginImport pluginImport) throws CoreException { 239 if (imports != null) { 240 imports.remove(pluginImport); 241 Object header = getManifestHeader(Constants.REQUIRE_BUNDLE); 242 if (header instanceof RequireBundleHeader) { 243 ((RequireBundleHeader)header).removeBundle(pluginImport.getId()); 244 } 245 fireStructureChanged(pluginImport, false); 246 } 247 } 248 249 public void remove(IPluginImport[] pluginImports) throws CoreException { 250 if (imports != null) { 251 for (int i = 0; i < pluginImports.length; i++) { 252 imports.remove(pluginImports[i]); 253 Object header = getManifestHeader(Constants.REQUIRE_BUNDLE); 254 if (header instanceof RequireBundleHeader) { 255 ((RequireBundleHeader)header).removeBundle(pluginImports[i].getId()); 256 } 257 } 258 fireStructureChanged(pluginImports, false); 259 } 260 } 261 262 267 public IPluginLibrary[] getLibraries() { 268 IBundle bundle = getBundle(); 269 if (bundle == null) 270 return new IPluginLibrary[0]; 271 if (libraries == null) { 272 libraries = new ArrayList (); 273 String value = bundle.getHeader(Constants.BUNDLE_CLASSPATH); 274 if (value != null) { 275 try { 276 ManifestElement[] elements = ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, value); 277 for (int i = 0; i < elements.length; i++) { 278 PluginLibrary library = new PluginLibrary(); 279 library.setModel(getModel()); 280 library.setInTheModel(true); 281 library.setParent(this); 282 library.load(elements[i].getValue()); 283 libraries.add(library); 284 } 285 } catch (BundleException e) { 286 } 287 } 288 } 289 return (IPluginLibrary[]) libraries.toArray(new IPluginLibrary[libraries.size()]); 290 } 291 292 297 public IPluginImport[] getImports() { 298 if (imports == null) { 299 imports = new ArrayList (); 300 BundleDescription description = model.getBundleDescription(); 301 if (description != null) { 302 BundleSpecification[] required = description.getRequiredBundles(); 303 for (int i = 0; i < required.length; i++) { 304 PluginImport importElement = new PluginImport(); 305 importElement.setModel(getModel()); 306 importElement.setInTheModel(true); 307 importElement.setParent(this); 308 imports.add(importElement); 309 importElement.load(required[i]); 310 } 311 BundleDescription[] imported = PDEStateHelper.getImportedBundles(description); 312 for (int i = 0; i < imported.length; i++) { 313 PluginImport importElement = new PluginImport(); 314 importElement.setModel(getModel()); 315 importElement.setInTheModel(true); 316 importElement.setParent(this); 317 imports.add(importElement); 318 importElement.load(imported[i]); 319 } 320 } else { 321 IBundle bundle = getBundle(); 322 if (bundle != null) { 323 try { 324 String value = bundle.getHeader(Constants.REQUIRE_BUNDLE); 325 int bundleManifestVersion = getBundleManifestVersion(bundle); 326 if (value != null) { 327 ManifestElement[] elements = ManifestElement.parseHeader(Constants.REQUIRE_BUNDLE, value); 328 for (int i = 0; i < elements.length; i++) { 329 PluginImport importElement = new PluginImport(); 330 importElement.setModel(getModel()); 331 importElement.setInTheModel(true); 332 importElement.setParent(this); 333 imports.add(importElement); 334 importElement.load(elements[i], bundleManifestVersion); 335 } 336 } 337 } catch (BundleException e) { 338 } 339 } 340 } 341 } 342 return (IPluginImport[])imports.toArray(new IPluginImport[imports.size()]); 343 } 344 345 350 public String getProviderName() { 351 IBundle bundle = getBundle(); 352 return bundle == null ? null : bundle.getHeader(Constants.BUNDLE_VENDOR); 353 } 354 355 360 public void setProviderName(String providerName) throws CoreException { 361 IBundle bundle = getBundle(); 362 if (bundle != null) { 363 String old = getProviderName(); 364 IManifestHeader header = bundle.getManifestHeader(Constants.BUNDLE_VENDOR); 365 if (header instanceof BundleVendorHeader) 366 ((BundleVendorHeader)header).setVendor(providerName); 367 else 368 bundle.setHeader(Constants.BUNDLE_VENDOR, providerName); 369 model.fireModelObjectChanged(this, IPluginBase.P_PROVIDER, old, providerName); 370 } 371 } 372 373 378 public String getVersion() { 379 BundleDescription desc = model.getBundleDescription(); 380 if (desc != null) { 381 Version version = desc.getVersion(); 382 return (version != null) ? version.toString() : null; 383 } 384 return getValue(Constants.BUNDLE_VERSION, false); 385 } 386 387 392 public void setVersion(String version) throws CoreException { 393 IBundle bundle = getBundle(); 394 if (bundle != null) { 395 String old = getVersion(); 396 IManifestHeader header = bundle.getManifestHeader(Constants.BUNDLE_VERSION); 397 if (header instanceof BundleVersionHeader) 398 ((BundleVersionHeader)header).setVersionRange(version); 399 else 400 bundle.setHeader(Constants.BUNDLE_VERSION, version); 401 model.fireModelObjectChanged(this, IPluginBase.P_VERSION, old, version); 402 } 403 } 404 405 411 public void swap(IPluginLibrary l1, IPluginLibrary l2) throws CoreException { 412 if (libraries != null) { 413 int index1 = libraries.indexOf(l1); 414 int index2 = libraries.indexOf(l2); 415 libraries.set(index1, l2); 416 libraries.set(index2, l1); 417 Object header = getManifestHeader(Constants.BUNDLE_CLASSPATH); 418 if (header instanceof BundleClasspathHeader) { 419 ((BundleClasspathHeader)header).swap(index1, index2); 420 } 421 model.fireModelObjectChanged(this, P_IMPORT_ORDER, l1, l2); 422 } 423 } 424 425 protected void fireStructureChanged(Object object, boolean added) { 426 int type = (added)?IModelChangedEvent.INSERT:IModelChangedEvent.REMOVE; 427 model.fireModelChanged(new ModelChangedEvent(model, type, new Object []{object}, null )); 428 } 429 430 protected void fireStructureChanged(Object [] objects, boolean added) { 431 int type = (added)?IModelChangedEvent.INSERT:IModelChangedEvent.REMOVE; 432 model.fireModelChanged(new ModelChangedEvent(model, type, objects, null )); 433 } 434 435 440 public void add(IPluginExtension extension) throws CoreException { 441 IExtensions extensions = getExtensionsRoot(); 442 if (extensions == null) 443 return; 444 extensions.add(extension); 445 446 if (getExtensions().length == 1 && getExtensionPoints().length == 0) 448 updateSingleton(true); 449 } 450 451 456 public void add(IPluginExtensionPoint point) throws CoreException { 457 IExtensions extensions = getExtensionsRoot(); 458 if (extensions == null) 459 return; 460 extensions.add(point); 461 462 if (getExtensions().length == 0 && getExtensionPoints().length == 1) 464 updateSingleton(true); 465 } 466 467 public String getResourceString(String key) { 468 return model.getResourceString(key); 469 } 470 471 476 public IPluginExtensionPoint[] getExtensionPoints() { 477 IExtensions extensions = getExtensionsRoot(); 478 if (extensions == null) 479 return new IPluginExtensionPoint[0]; 480 return extensions.getExtensionPoints(); 481 } 482 483 488 public IPluginExtension[] getExtensions() { 489 IExtensions extensions = getExtensionsRoot(); 490 if (extensions == null) 491 return new IPluginExtension[0]; 492 return extensions.getExtensions(); 493 } 494 495 public int getIndexOf(IPluginExtension e) { 496 IExtensions extensions = getExtensionsRoot(); 497 if (extensions == null) 498 return -1; 499 return extensions.getIndexOf(e); 500 } 501 502 507 public void remove(IPluginExtension extension) throws CoreException { 508 IExtensions extensions = getExtensionsRoot(); 509 if (extensions != null) { 510 extensions.remove(extension); 511 if (getExtensions().length == 0 && getExtensionPoints().length == 0) 513 updateSingleton(false); 514 } 515 } 516 517 522 public void remove(IPluginExtensionPoint extensionPoint) 523 throws CoreException { 524 IExtensions extensions = getExtensionsRoot(); 525 if (extensions != null) { 526 extensions.remove(extensionPoint); 527 if (getExtensions().length == 0 && getExtensionPoints().length == 0) 529 updateSingleton(false); 530 } 531 } 532 533 protected void updateSingleton(boolean singleton) { 534 IManifestHeader header = getManifestHeader(Constants.BUNDLE_SYMBOLICNAME); 535 if (header instanceof BundleSymbolicNameHeader) 536 ((BundleSymbolicNameHeader)header).setSingleton(singleton); 537 else { 538 if (singleton) { 539 String version = getBundle().getHeader(Constants.BUNDLE_MANIFESTVERSION); 540 if (version == null) 541 version = "1"; String value = header.getValue(); 543 String singletonValue = null; 544 if (version != null && Integer.parseInt(version) >= 2) 545 singletonValue = Constants.SINGLETON_DIRECTIVE + ":=true"; else 547 singletonValue = Constants.SINGLETON_DIRECTIVE + "=true"; if (value.indexOf(singletonValue) != -1) 549 return; 550 getBundle().setHeader(Constants.BUNDLE_SYMBOLICNAME, value + "; " + singletonValue); } 552 } 554 } 555 556 562 public void swap(IPluginExtension e1, IPluginExtension e2) 563 throws CoreException { 564 IExtensions extensions = getExtensionsRoot(); 565 if (extensions != null) { 566 extensions.swap(e1, e2); 567 } 568 } 569 570 573 public void swap(IPluginImport import1, IPluginImport import2) 574 throws CoreException { 575 if (imports != null) { 576 int index1 = imports.indexOf(import1); 577 int index2 = imports.indexOf(import2); 578 imports.set(index1, import2); 579 imports.set(index2, import1); 580 Object header = getManifestHeader(Constants.REQUIRE_BUNDLE); 581 if (header instanceof RequireBundleHeader) { 582 ((RequireBundleHeader)header).swap(index1, index2); 583 } 584 model.fireModelObjectChanged(this, P_IMPORT_ORDER, import1, import2); 585 } 586 } 587 588 593 public String getId() { 594 return getValue(Constants.BUNDLE_SYMBOLICNAME, true); 595 } 596 597 protected String getValue(String key, boolean parse) { 599 IBundle bundle = getBundle(); 600 if (bundle == null) 601 return null; 602 String value = bundle.getHeader(key); 603 if (value == null || !parse) 604 return value; 605 try { 606 ManifestElement[] elements = ManifestElement.parseHeader(key, value); 607 if (elements.length > 0) 608 return elements[0].getValue(); 609 } catch (BundleException e) { 610 } 611 return null; 612 } 613 614 619 public void setId(String id) throws CoreException { 620 IBundle bundle = getBundle(); 621 if (bundle != null) { 622 String old = getId(); 623 IManifestHeader header = bundle.getManifestHeader(Constants.BUNDLE_SYMBOLICNAME); 624 if (header instanceof BundleSymbolicNameHeader) 625 ((BundleSymbolicNameHeader)header).setId(id); 626 else 627 bundle.setHeader(Constants.BUNDLE_SYMBOLICNAME, id); 628 model.fireModelObjectChanged(this, IIdentifiable.P_ID, old, id); 629 } 630 } 631 632 637 public IPluginModelBase getPluginModel() { 638 return model; 639 } 640 641 646 public String getName() { 647 return getValue(Constants.BUNDLE_NAME, false); 648 } 649 650 655 public void setName(String name) throws CoreException { 656 IBundle bundle = getBundle(); 657 if (bundle != null) { 658 String old = getName(); 659 IManifestHeader header = bundle.getManifestHeader(Constants.BUNDLE_NAME); 660 if (header instanceof BundleNameHeader) 661 ((BundleNameHeader)header).setBundleName(name); 662 else 663 bundle.setHeader(Constants.BUNDLE_NAME, name); 664 model.fireModelObjectChanged(this, IPluginObject.P_NAME, old, name); 665 } 666 } 667 668 673 public boolean isInTheModel() { 674 return model != null; 675 } 676 677 682 public String getTranslatedName() { 683 return getResourceString(getName()); 684 } 685 686 691 public IPluginObject getParent() { 692 return null; 693 } 694 695 700 public IPluginBase getPluginBase() { 701 return this; 702 } 703 704 709 public boolean isValid() { 710 IExtensions extensions = getExtensionsRoot(); 711 return getBundle() != null 712 && getBundle().getHeader(Constants.BUNDLE_SYMBOLICNAME) != null 713 && (extensions == null || extensions.isValid()); 714 } 715 716 722 public void write(String indent, PrintWriter writer) { 723 } 724 725 728 public void setInTheModel(boolean inModel) { 729 } 730 731 static public int getBundleManifestVersion(IBundle bundle) { 732 String version = bundle.getHeader(Constants.BUNDLE_MANIFESTVERSION); 733 if (version == null) 734 return 1; try { 736 return Integer.parseInt(version); 737 } catch (NumberFormatException e) { 738 return 1; } 740 } 741 742 public void updateImport(IPluginImport iimport) { 743 Object header = getManifestHeader(Constants.REQUIRE_BUNDLE); 744 if (header instanceof RequireBundleHeader && imports != null) 745 ((RequireBundleHeader)header).updateBundle(imports.indexOf(iimport), iimport); 746 } 747 748 public String getTargetVersion() { 749 return fTarget != null ? fTarget : TargetPlatformHelper.getTargetVersionString(); 750 } 751 752 public void setTargetVersion(String target) { 753 fTarget = target; 754 } 755 756 } 757 | Popular Tags |