1 11 package org.eclipse.osgi.internal.resolver; 12 13 import java.util.*; 14 import org.eclipse.core.internal.dependencies.*; 15 import org.eclipse.osgi.service.resolver.*; 16 17 public class ResolverHelper { 18 19 private final static Version NULL_VERSION = new Version(0, 0, 0); 20 private final static IMatchRule GENERAL_MATCHRULE = new GeneralMatchRule(); 21 22 static class BundleVersionComparator implements Comparator { 23 public int compare(Object arg0, Object arg1) { 24 Version v1 = (Version) arg0; 25 Version v2 = (Version) arg1; 26 return v1.isGreaterThan(v2) ? 1 : v1.equals(v2) ? 0 : -1; 27 } 28 } 29 30 private final static class GeneralMatchRule implements IMatchRule { 31 public boolean isSatisfied(Object constraint, Object available) { 32 return ((VersionConstraint) constraint).isSatisfiedBy((Version) available); 33 } 34 35 public String toString() { 36 return "general"; } 38 } 39 40 public static Element createElement(BundleDescription bundleDescription, DependencySystem system) { 41 String name = getSymbolicName(bundleDescription); 42 Version version = getVersion(bundleDescription); 43 return system.createElement(name, version, createPrerequisites(bundleDescription, system), bundleDescription.isSingleton(), bundleDescription); 44 } 45 46 private static Version getVersion(BundleDescription bundleDescription) { 47 Version version = bundleDescription.getVersion(); 48 if (version == null) 49 version = Version.emptyVersion; 50 return version; 51 } 52 53 private static String getSymbolicName(BundleDescription bundleDescription) { 54 String name = bundleDescription.getSymbolicName(); 55 if (name == null) 57 name = Long.toString(bundleDescription.getBundleId()); 59 return name; 60 } 61 62 private static Dependency[] createPrerequisites(BundleDescription bundleDesc, DependencySystem system) { 63 BundleSpecification[] required = bundleDesc.getRequiredBundles(); 64 HostSpecification host = bundleDesc.getHost(); 65 int dependencyCount = required == null ? 0 : required.length; 66 if (host != null) 67 dependencyCount++; 68 if (dependencyCount == 0) 69 return new Dependency[0]; 70 List prereqs = new ArrayList(dependencyCount); 71 for (int i = 0; i < required.length; i++) 72 if (!required[i].getName().equals(bundleDesc.getSymbolicName())) 74 prereqs.add(createPrerequisite(system, required[i])); 75 if (host != null) 76 prereqs.add(createPrerequisite(system, host)); 77 return (Dependency[]) prereqs.toArray(new Dependency[prereqs.size()]); 78 } 79 80 private static Dependency createPrerequisite(DependencySystem system, VersionConstraint constraint) { 81 boolean optional = (constraint instanceof BundleSpecification) && ((BundleSpecification) constraint).isOptional(); 82 return system.createDependency(constraint.getName(), GENERAL_MATCHRULE, optional, constraint); 83 } 84 85 public static DependencySystem createDependencySystem(ISelectionPolicy policy) { 86 return new DependencySystem(new ResolverHelper.BundleVersionComparator(), policy); 87 } 88 89 public static DependencySystem buildDependencySystem(State state, ISelectionPolicy selectionPolicy) { 90 DependencySystem dependencySystem = createDependencySystem(selectionPolicy); 91 BundleDescription[] bundles = state.getBundles(); 92 for (int i = 0; i < bundles.length; i++) 93 dependencySystem.addElement(ResolverHelper.createElement(bundles[i], dependencySystem)); 94 return dependencySystem; 95 } 96 97 public static void remove(BundleDescription description, DependencySystem system) { 98 system.removeElement(getSymbolicName(description), getVersion(description)); 99 } 100 101 public static void add(BundleDescription description, DependencySystem system) { 102 system.addElement(createElement(description, system)); 103 } 104 105 public static void unresolve(BundleDescription bundle, DependencySystem system) { 106 Element element = system.getElement(getSymbolicName(bundle), getVersion(bundle)); 107 if (element != null) 108 system.unresolve(new Element[] {element}); 109 } 110 111 public static void update(BundleDescription newDescription, BundleDescription existing, DependencySystem system) { 112 system.removeElement(getSymbolicName(existing), getVersion(existing)); 113 system.addElement(createElement(newDescription, system)); 114 } 115 } | Popular Tags |