1 11 package org.eclipse.pde.internal.ui.search.dependencies; 12 13 import java.util.HashMap ; 14 import java.util.Set ; 15 16 import org.eclipse.osgi.service.resolver.BaseDescription; 17 import org.eclipse.osgi.service.resolver.BundleDescription; 18 import org.eclipse.osgi.service.resolver.BundleSpecification; 19 import org.eclipse.osgi.service.resolver.ExportPackageDescription; 20 import org.eclipse.osgi.service.resolver.HostSpecification; 21 import org.eclipse.osgi.service.resolver.ImportPackageSpecification; 22 import org.eclipse.pde.core.plugin.IPluginModelBase; 23 import org.eclipse.pde.core.plugin.PluginRegistry; 24 import org.osgi.framework.Constants; 25 26 public class DependencyCalculator { 27 28 boolean fIncludeOptional; 29 protected HashMap fDependencies; 30 31 34 public DependencyCalculator(boolean includeOptional) { 35 super(); 36 fIncludeOptional = includeOptional; 37 } 38 39 40 public void findDependencies(Object [] includedBundles) { 41 if (fDependencies == null) 42 fDependencies = new HashMap (); 43 for (int i = 0; i < includedBundles.length; i++) { 44 findObjectDependencies(includedBundles[i]); 45 } 46 } 47 48 public void findDependency(Object bundle) { 49 if (fDependencies == null) 50 fDependencies = new HashMap (); 51 findObjectDependencies(bundle); 52 } 53 54 private void findObjectDependencies(Object obj) { 55 if (obj instanceof IPluginModelBase) { 56 IPluginModelBase base = ((IPluginModelBase)obj); 57 BundleDescription desc = base.getBundleDescription(); 58 if (desc != null) 59 obj = desc; 60 } 61 if (obj instanceof BundleDescription) 62 findDependencies((BundleDescription)obj); 63 } 64 65 68 public Set getBundleIDs() { 69 Set temp = fDependencies.keySet(); 70 fDependencies = null; 71 return temp; 72 } 73 74 protected void findDependencies(BundleDescription desc) { 75 if (desc == null) 76 return; 77 String id = desc.getSymbolicName(); 78 if (fDependencies.containsKey(id)) 79 return; 80 IPluginModelBase model = PluginRegistry.findModel(desc); 81 if (model == null) 82 return; 83 fDependencies.put(id, model); 84 85 addRequiredBundles(desc.getRequiredBundles()); 86 addImportedPackages(desc.getImportPackages()); 87 88 HostSpecification host = desc.getHost(); 89 if (host != null) { 90 BaseDescription bd = host.getSupplier(); 92 if (bd != null && bd instanceof BundleDescription) 93 findDependencies((BundleDescription)bd); 94 } else { 95 addFragments(desc); 97 } 98 } 99 100 protected void addRequiredBundles(BundleSpecification[] requiredBundles) { 101 for (int i = 0; i < requiredBundles.length; i++) { 102 if (requiredBundles[i].isOptional() && !fIncludeOptional) 103 continue; 104 BaseDescription bd = requiredBundles[i].getSupplier(); 105 if (bd != null && bd instanceof BundleDescription) 107 findDependencies((BundleDescription)bd); 108 } 109 } 110 111 protected void addImportedPackages(ImportPackageSpecification[] packages) { 112 for (int i = 0; i < packages.length; i++) { 113 if (!fIncludeOptional) 114 if (Constants.RESOLUTION_OPTIONAL.equals(packages[i].getDirective(Constants.RESOLUTION_DIRECTIVE))) { 115 continue; 116 } 117 BaseDescription bd = packages[i].getSupplier(); 118 if (bd != null && bd instanceof ExportPackageDescription) { 120 BundleDescription exporter = ((ExportPackageDescription)bd).getExporter(); 121 if (exporter != null) 122 findDependencies(exporter); 123 } 124 } 125 } 126 127 protected void addFragments(BundleDescription desc) { 128 BundleDescription[] fragments = desc.getFragments(); 129 for (int i = 0; i < fragments.length; i++) 130 if (fragments[i].isResolved() && !fragments[i].getSymbolicName().equals("org.eclipse.ui.workbench.compatibility")) { findDependencies(fragments[i]); 132 } 133 } 134 135 public boolean containsPluginId(String id) { 136 return fDependencies.containsKey(id); 137 } 138 139 } 140 | Popular Tags |