Bug 411773 - Remove uses of Equinox PlatformAdmin

Change-Id: I1807f80039e126f18d333d18e419633fc20e94c4
Signed-off-by: Thomas Watson <tjwatson@us.ibm.com>
diff --git a/ant/org.eclipse.ant.core/src/org/eclipse/ant/core/AntCorePreferences.java b/ant/org.eclipse.ant.core/src/org/eclipse/ant/core/AntCorePreferences.java
index 9a7fa78..35191b2 100644
--- a/ant/org.eclipse.ant.core/src/org/eclipse/ant/core/AntCorePreferences.java
+++ b/ant/org.eclipse.ant.core/src/org/eclipse/ant/core/AntCorePreferences.java
@@ -48,13 +48,17 @@
 import org.eclipse.core.runtime.preferences.InstanceScope;
 import org.eclipse.core.variables.IDynamicVariable;
 import org.eclipse.core.variables.VariablesPlugin;
-import org.eclipse.osgi.service.resolver.BundleDescription;
-import org.eclipse.osgi.service.resolver.ExportPackageDescription;
 import org.eclipse.osgi.util.ManifestElement;
 import org.eclipse.osgi.util.NLS;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
+import org.osgi.framework.namespace.BundleNamespace;
+import org.osgi.framework.namespace.HostNamespace;
+import org.osgi.framework.namespace.PackageNamespace;
+import org.osgi.framework.wiring.BundleRevision;
+import org.osgi.framework.wiring.BundleWire;
+import org.osgi.framework.wiring.BundleWiring;
 import org.osgi.service.packageadmin.ExportedPackage;
 import org.osgi.service.packageadmin.PackageAdmin;
 import org.osgi.service.prefs.BackingStoreException;
@@ -139,10 +143,10 @@
 	}
 
 	static private class Relation {
-		Object from;
-		Object to;
+		BundleRevision from;
+		BundleRevision to;
 
-		Relation(Object from, Object to) {
+		Relation(BundleRevision from, BundleRevision to) {
 			this.from = from;
 			this.to = to;
 		}
@@ -827,7 +831,7 @@
 				property.setName(name);
 				property.setPluginLabel(element.getContributor().getName());
 				String className = element.getAttribute(AntCorePlugin.CLASS);
-				property.setValueProvider(className, new WrappedClassLoader(bundle));
+				property.setValueProvider(className, getClassLoader(bundle));
 			}
 			defaultProperties.add(property);
 			String runtime = element.getAttribute(AntCorePlugin.ECLIPSE_RUNTIME);
@@ -837,6 +841,10 @@
 		}
 	}
 
+	private WrappedClassLoader getClassLoader(Bundle b) {
+		return new WrappedClassLoader(b);
+	}
+
 	/**
 	 * Returns the IAntClasspathEntry for the tools.jar associated with the path supplied. May return <code>null</code> if no tools.jar is found (e.g.
 	 * the path points to a JRE install).
@@ -1020,7 +1028,7 @@
 	}
 
 	protected void addPluginClassLoader(Bundle bundle) {
-		WrappedClassLoader loader = new WrappedClassLoader(bundle);
+		WrappedClassLoader loader = getClassLoader(bundle);
 		if (!pluginClassLoaders.contains(loader)) {
 			pluginClassLoaders.add(loader);
 		}
@@ -1099,19 +1107,22 @@
 		if (orderedPluginClassLoaders == null) {
 			Iterator<WrappedClassLoader> classLoaders = pluginClassLoaders.iterator();
 			Map<String, WrappedClassLoader> idToLoader = new HashMap<String, WrappedClassLoader>(pluginClassLoaders.size());
-			List<BundleDescription> bundles = new ArrayList<BundleDescription>(pluginClassLoaders.size());
+			List<BundleRevision> bundles = new ArrayList<BundleRevision>(pluginClassLoaders.size());
 			while (classLoaders.hasNext()) {
 				WrappedClassLoader loader = classLoaders.next();
 				idToLoader.put(loader.bundle.getSymbolicName(), loader);
-				bundles.add(Platform.getPlatformAdmin().getState(false).getBundle(loader.bundle.getBundleId()));
+				BundleRevision revision = loader.bundle.adapt(BundleRevision.class);
+				if (revision != null) {
+					bundles.add(revision);
+				}
 			}
-			List<Object> descriptions = computePrerequisiteOrder(bundles);
-			List<WrappedClassLoader> loaders = new ArrayList<WrappedClassLoader>(descriptions.size());
-			for (Iterator<Object> iter = descriptions.iterator(); iter.hasNext();) {
-				String id = ((BundleDescription) iter.next()).getSymbolicName();
+			List<BundleRevision> sorted = computePrerequisiteOrder(bundles);
+			List<WrappedClassLoader> loaders = new ArrayList<WrappedClassLoader>(sorted.size());
+			for (BundleRevision revision : sorted) {
+				String id = revision.getSymbolicName();
 				loaders.add(idToLoader.get(id));
 			}
-			orderedPluginClassLoaders = loaders.toArray(new WrappedClassLoader[loaders.size()]);
+			orderedPluginClassLoaders = loaders.toArray(new ClassLoader[loaders.size()]);
 		}
 		return orderedPluginClassLoaders;
 	}
@@ -1119,25 +1130,24 @@
 	/*
 	 * Copied from org.eclipse.pde.internal.build.Utils
 	 */
-	private List<Object> computePrerequisiteOrder(List<BundleDescription> plugins) {
+	private List<BundleRevision> computePrerequisiteOrder(List<BundleRevision> plugins) {
 		List<Relation> prereqs = new ArrayList<Relation>(plugins.size());
-		List<BundleDescription> fragments = new ArrayList<BundleDescription>();
+		List<BundleRevision> fragments = new ArrayList<BundleRevision>();
 
 		// create a collection of directed edges from plugin to prereq
-		for (Iterator<BundleDescription> iter = plugins.iterator(); iter.hasNext();) {
-			BundleDescription current = iter.next();
-			if (current.getHost() != null) {
+		for (BundleRevision current : plugins) {
+			if ((current.getTypes() & BundleRevision.TYPE_FRAGMENT) != 0) {
 				fragments.add(current);
 				continue;
 			}
 			boolean found = false;
 
-			BundleDescription[] prereqList = getDependentBundles(current);
-			for (int j = 0; j < prereqList.length; j++) {
+			BundleRevision[] prereqList = getDependentBundles(current);
+			for (BundleRevision prereq : prereqList) {
 				// ensure that we only include values from the original set.
-				if (plugins.contains(prereqList[j])) {
+				if (plugins.contains(prereq)) {
 					found = true;
-					prereqs.add(new Relation(current, prereqList[j]));
+					prereqs.add(new Relation(current, prereq));
 				}
 			}
 
@@ -1150,21 +1160,17 @@
 
 		// The fragments needs to added relatively to their host and to their
 		// own prerequisite (bug #43244)
-		for (Iterator<BundleDescription> iter = fragments.iterator(); iter.hasNext();) {
-			BundleDescription current = iter.next();
+		for (BundleRevision currentFrag : fragments) {
 
-			if (plugins.contains(current.getHost().getBundle())) {
-				prereqs.add(new Relation(current, current.getHost().getSupplier()));
-			} else {
-				AntCorePlugin.getPlugin().getLog().log(new Status(IStatus.ERROR, AntCorePlugin.PI_ANTCORE, AntCorePlugin.ERROR_MALFORMED_URL, NLS.bind(InternalCoreAntMessages.AntCorePreferences_1, new String[] { current.getSymbolicName() }), null));
-			}
-
-			BundleDescription[] prereqList = getDependentBundles(current);
-			for (int j = 0; j < prereqList.length; j++) {
-				// ensure that we only include values from the original set.
-				if (plugins.contains(prereqList[j])) {
-					prereqs.add(new Relation(current, prereqList[j]));
+			if (plugins.contains(currentFrag)) {
+				BundleWiring wiring = currentFrag.getWiring();
+				List<BundleWire> hostWires = wiring == null ? Collections.<BundleWire> emptyList()
+						: wiring.getRequiredWires(HostNamespace.HOST_NAMESPACE);
+				if (!hostWires.isEmpty()) {
+					prereqs.add(new Relation(currentFrag, hostWires.get(0).getProvider()));
 				}
+			} else {
+				AntCorePlugin.getPlugin().getLog().log(new Status(IStatus.ERROR, AntCorePlugin.PI_ANTCORE, AntCorePlugin.ERROR_MALFORMED_URL, NLS.bind(InternalCoreAntMessages.AntCorePreferences_1, new String[] { currentFrag.getSymbolicName() }), null));
 			}
 		}
 
@@ -1175,10 +1181,10 @@
 	/*
 	 * Copied from org.eclipse.pde.internal.build.site.PDEState.
 	 */
-	private BundleDescription[] getDependentBundles(BundleDescription root) {
-		BundleDescription[] imported = getImportedBundles(root);
-		BundleDescription[] required = getRequiredBundles(root);
-		BundleDescription[] dependents = new BundleDescription[imported.length + required.length];
+	private BundleRevision[] getDependentBundles(BundleRevision root) {
+		BundleRevision[] imported = getImportedBundles(root);
+		BundleRevision[] required = getRequiredBundles(root);
+		BundleRevision[] dependents = new BundleRevision[imported.length + required.length];
 		System.arraycopy(imported, 0, dependents, 0, imported.length);
 		System.arraycopy(required, 0, dependents, imported.length, required.length);
 		return dependents;
@@ -1187,39 +1193,42 @@
 	/*
 	 * Copied from org.eclipse.pde.internal.build.site.PDEState.
 	 */
-	private BundleDescription[] getRequiredBundles(BundleDescription root) {
-		if (root == null) {
-			return new BundleDescription[0];
-		}
-		return root.getResolvedRequires();
+	private BundleRevision[] getRequiredBundles(BundleRevision root) {
+		return getDependantRequirements(root, BundleNamespace.BUNDLE_NAMESPACE);
 	}
 
 	/*
 	 * Copied from org.eclipse.pde.internal.build.site.PDEState.
 	 */
-	private BundleDescription[] getImportedBundles(BundleDescription root) {
+	private BundleRevision[] getImportedBundles(BundleRevision root) {
+		return getDependantRequirements(root, PackageNamespace.PACKAGE_NAMESPACE);
+	}
+
+	private BundleRevision[] getDependantRequirements(BundleRevision root, String namespace) {
 		if (root == null) {
-			return new BundleDescription[0];
+			return new BundleRevision[0];
 		}
-		ExportPackageDescription[] packages = root.getResolvedImports();
-		ArrayList<BundleDescription> resolvedImports = new ArrayList<BundleDescription>(packages.length);
-		for (int i = 0; i < packages.length; i++) {
-			if (!root.getLocation().equals(packages[i].getExporter().getLocation()) && !resolvedImports.contains(packages[i].getExporter())) {
-				resolvedImports.add(packages[i].getExporter());
+		BundleWiring wiring = root.getWiring();
+		List<BundleWire> requiredWires = wiring == null ? Collections.<BundleWire> emptyList() : wiring.getRequiredWires(namespace);
+		ArrayList<BundleRevision> requirementProviders = new ArrayList<BundleRevision>(requiredWires.size());
+		for (BundleWire requiredWire : requiredWires) {
+			BundleRevision provider = requiredWire.getProvider();
+			if (!provider.equals(root) && !requirementProviders.contains(provider)) {
+				requirementProviders.add(provider);
 			}
 		}
-		return resolvedImports.toArray(new BundleDescription[resolvedImports.size()]);
+		return requirementProviders.toArray(new BundleRevision[requirementProviders.size()]);
 	}
 
 	/*
 	 * Copied from org.eclipse.pde.internal.build.Utils
 	 */
-	private void removeArcs(List<Relation> edges, List<Object> roots, Map<Object, Integer> counts) {
-		for (Iterator<Object> j = roots.iterator(); j.hasNext();) {
+	private void removeArcs(List<Relation> edges, List<BundleRevision> roots, Map<BundleRevision, Integer> counts) {
+		for (Iterator<BundleRevision> j = roots.iterator(); j.hasNext();) {
 			Object root = j.next();
 			for (int i = 0; i < edges.size(); i++) {
 				if (root.equals(edges.get(i).to)) {
-					Object input = edges.get(i).from;
+					BundleRevision input = edges.get(i).from;
 					Integer count = counts.get(input);
 					if (count != null) {
 						counts.put(input, new Integer(count.intValue() - 1));
@@ -1232,15 +1241,15 @@
 	/*
 	 * Copied from org.eclipse.pde.internal.build.Utils
 	 */
-	private List<Object> computeNodeOrder(List<Relation> edges) {
-		Map<Object, Integer> counts = computeCounts(edges);
-		List<Object> nodes = new ArrayList<Object>(counts.size());
+	private List<BundleRevision> computeNodeOrder(List<Relation> edges) {
+		Map<BundleRevision, Integer> counts = computeCounts(edges);
+		List<BundleRevision> nodes = new ArrayList<BundleRevision>(counts.size());
 		while (!counts.isEmpty()) {
-			List<Object> roots = findRootNodes(counts);
+			List<BundleRevision> roots = findRootNodes(counts);
 			if (roots.isEmpty()) {
 				break;
 			}
-			for (Iterator<Object> i = roots.iterator(); i.hasNext();) {
+			for (Iterator<BundleRevision> i = roots.iterator(); i.hasNext();) {
 				counts.remove(i.next());
 			}
 			nodes.addAll(roots);
@@ -1252,12 +1261,12 @@
 	/*
 	 * Copied from org.eclipse.pde.internal.build.Utils
 	 */
-	private Map<Object, Integer> computeCounts(List<Relation> mappings) {
-		Map<Object, Integer> counts = new HashMap<Object, Integer>(5);
+	private Map<BundleRevision, Integer> computeCounts(List<Relation> mappings) {
+		Map<BundleRevision, Integer> counts = new HashMap<BundleRevision, Integer>(5);
 		for (int i = 0; i < mappings.size(); i++) {
-			Object from = mappings.get(i).from;
+			BundleRevision from = mappings.get(i).from;
 			Integer fromCount = counts.get(from);
-			Object to = mappings.get(i).to;
+			BundleRevision to = mappings.get(i).to;
 			if (to == null)
 				counts.put(from, new Integer(0));
 			else {
@@ -1273,10 +1282,10 @@
 	/*
 	 * Copied from org.eclipse.pde.internal.build.Utils
 	 */
-	private List<Object> findRootNodes(Map<Object, Integer> counts) {
-		List<Object> result = new ArrayList<Object>(5);
-		for (Iterator<Object> i = counts.keySet().iterator(); i.hasNext();) {
-			Object node = i.next();
+	private List<BundleRevision> findRootNodes(Map<BundleRevision, Integer> counts) {
+		List<BundleRevision> result = new ArrayList<BundleRevision>(5);
+		for (Iterator<BundleRevision> i = counts.keySet().iterator(); i.hasNext();) {
+			BundleRevision node = i.next();
 			int count = counts.get(node).intValue();
 			if (count == 0) {
 				result.add(node);
@@ -1288,7 +1297,7 @@
 	private void initializePluginClassLoaders() {
 		pluginClassLoaders = new ArrayList<WrappedClassLoader>(10);
 		// ant.core should always be present
-		pluginClassLoaders.add(new WrappedClassLoader(AntCorePlugin.getPlugin().getBundle()));
+		pluginClassLoaders.add(getClassLoader(AntCorePlugin.getPlugin().getBundle()));
 	}
 
 	/**
diff --git a/ant/org.eclipse.ant.launching/src/org/eclipse/ant/internal/launching/launchConfigurations/AntLaunchDelegate.java b/ant/org.eclipse.ant.launching/src/org/eclipse/ant/internal/launching/launchConfigurations/AntLaunchDelegate.java
index 4c83c46..1b3dc70 100644
--- a/ant/org.eclipse.ant.launching/src/org/eclipse/ant/internal/launching/launchConfigurations/AntLaunchDelegate.java
+++ b/ant/org.eclipse.ant.launching/src/org/eclipse/ant/internal/launching/launchConfigurations/AntLaunchDelegate.java
@@ -15,6 +15,7 @@
 import java.io.File;
 import java.io.IOException;
 import java.net.URL;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -63,8 +64,10 @@
 import org.eclipse.jdt.launching.IVMInstall;
 import org.eclipse.jdt.launching.JavaRuntime;
 import org.eclipse.jdt.launching.SocketUtil;
-import org.eclipse.osgi.service.resolver.BundleDescription;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.namespace.HostNamespace;
+import org.osgi.framework.wiring.BundleWire;
+import org.osgi.framework.wiring.BundleWiring;
 
 import com.ibm.icu.text.MessageFormat;
 
@@ -706,12 +709,13 @@
 	private String getSWTLibraryLocation() {
 		if (fgSWTLibraryLocation == null) {
 			Bundle bundle = Platform.getBundle("org.eclipse.swt"); //$NON-NLS-1$
-			BundleDescription description = Platform.getPlatformAdmin().getState(false).getBundle(bundle.getBundleId());
-			BundleDescription[] fragments = description.getFragments();
-			if (fragments == null || fragments.length == 0) {
+			BundleWiring wiring = bundle.adapt(BundleWiring.class);
+			List<BundleWire> fragmentWires = wiring == null ? Collections.<BundleWire> emptyList()
+					: wiring.getProvidedWires(HostNamespace.HOST_NAMESPACE);
+			if (fragmentWires.isEmpty()) {
 				return null;
 			}
-			Bundle fragBundle = Platform.getBundle(fragments[0].getSymbolicName());
+			Bundle fragBundle = fragmentWires.get(0).getRequirer().getBundle();
 			try {
 				URL url = FileLocator.toFileURL(fragBundle.getEntry("/")); //$NON-NLS-1$
 				IPath path = new Path(url.getPath());
diff --git a/ant/org.eclipse.ant.launching/src/org/eclipse/ant/internal/launching/launchConfigurations/ContributedClasspathEntriesEntry.java b/ant/org.eclipse.ant.launching/src/org/eclipse/ant/internal/launching/launchConfigurations/ContributedClasspathEntriesEntry.java
index cbf81c0..ec7a0f5 100644
--- a/ant/org.eclipse.ant.launching/src/org/eclipse/ant/internal/launching/launchConfigurations/ContributedClasspathEntriesEntry.java
+++ b/ant/org.eclipse.ant.launching/src/org/eclipse/ant/internal/launching/launchConfigurations/ContributedClasspathEntriesEntry.java
@@ -16,6 +16,7 @@
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import org.eclipse.ant.core.AntCorePlugin;
@@ -34,8 +35,10 @@
 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
 import org.eclipse.jdt.launching.IVMInstall;
 import org.eclipse.jdt.launching.JavaRuntime;
-import org.eclipse.osgi.service.resolver.BundleDescription;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.namespace.HostNamespace;
+import org.osgi.framework.wiring.BundleWire;
+import org.osgi.framework.wiring.BundleWiring;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
@@ -158,10 +161,11 @@
 		if (fgSWTEntries == null) {
 			fgSWTEntries = new ArrayList<IRuntimeClasspathEntry>();
 			Bundle bundle = Platform.getBundle("org.eclipse.swt"); //$NON-NLS-1$
-			BundleDescription description = Platform.getPlatformAdmin().getState(false).getBundle(bundle.getBundleId());
-			BundleDescription[] fragments = description.getFragments();
-			for (int i = 0; i < fragments.length; i++) {
-				Bundle fragmentBundle = Platform.getBundle(fragments[i].getName());
+			BundleWiring wiring = bundle.adapt(BundleWiring.class);
+			List<BundleWire> fragmentWires = wiring == null ? Collections.<BundleWire> emptyList()
+					: wiring.getProvidedWires(HostNamespace.HOST_NAMESPACE);
+			for (BundleWire fragmentWire : fragmentWires) {
+				Bundle fragmentBundle = fragmentWire.getRequirer().getBundle();
 				URL bundleURL;
 				try {
 					bundleURL = FileLocator.resolve(fragmentBundle.getEntry("/")); //$NON-NLS-1$