Bug 201489 [osgi R5] multiple versions of a fragment should not attach to the same host
diff --git a/bundles/org.eclipse.osgi/resolver/src/org/eclipse/osgi/internal/module/ResolverBundle.java b/bundles/org.eclipse.osgi/resolver/src/org/eclipse/osgi/internal/module/ResolverBundle.java
index 7e94894..a065e0f 100644
--- a/bundles/org.eclipse.osgi/resolver/src/org/eclipse/osgi/internal/module/ResolverBundle.java
+++ b/bundles/org.eclipse.osgi/resolver/src/org/eclipse/osgi/internal/module/ResolverBundle.java
@@ -324,6 +324,14 @@
 			fragment.setNewFragmentExports(true);
 
 		initFragments();
+		// need to make sure there is not already another version of this fragment 
+		// already attached to this host
+		for (Iterator iFragments = fragments.iterator(); iFragments.hasNext();) {
+			ResolverBundle existingFragment = (ResolverBundle) iFragments.next();
+			String bsn = existingFragment.getName();
+			if (bsn != null && bsn.equals(fragment.getName()))
+				return new ResolverExport[0];
+		}
 		if (fragments.contains(fragment))
 			return new ResolverExport[0];
 		fragments.add(fragment);
diff --git a/bundles/org.eclipse.osgi/resolver/src/org/eclipse/osgi/internal/module/ResolverImpl.java b/bundles/org.eclipse.osgi/resolver/src/org/eclipse/osgi/internal/module/ResolverImpl.java
index fa7cbc5..77e6d07 100644
--- a/bundles/org.eclipse.osgi/resolver/src/org/eclipse/osgi/internal/module/ResolverImpl.java
+++ b/bundles/org.eclipse.osgi/resolver/src/org/eclipse/osgi/internal/module/ResolverImpl.java
@@ -287,7 +287,22 @@
 	}
 
 	// Attach fragment to its host
-	private void attachFragment(ResolverBundle bundle, ArrayList rejectedSingletons) {
+	private void attachFragment(ResolverBundle bundle, ArrayList rejectedSingletons, ArrayList processedFragments) {
+		if (processedFragments.contains(bundle.getName()))
+			return;
+		processedFragments.add(bundle.getName());
+		// we want to attach multiple versions of the same fragment
+		// from highest version to lowest to give the higher versions first pick
+		// of the available host bundles.
+		Object[] fragments = resolverBundles.get(bundle.getName());
+		for (int i = 0; i < fragments.length; i++) {
+			ResolverBundle fragment = (ResolverBundle) fragments[i];
+			if (!fragment.isResolved())
+				attachFragment0(fragment, rejectedSingletons);
+		}
+	}
+
+	private void attachFragment0(ResolverBundle bundle, ArrayList rejectedSingletons) {
 		if (!bundle.isFragment() || !bundle.isResolvable() || rejectedSingletons.contains(bundle.getBundle()))
 			return;
 		// no need to select singletons now; it will be done when we select the rest of the singleton bundles (bug 152042)
@@ -403,8 +418,9 @@
 		}
 
 		// First attach all fragments to the matching hosts
+		ArrayList processedFragments = new ArrayList(bundles.length);
 		for (int i = 0; i < bundles.length; i++)
-			attachFragment(bundles[i], rejectedSingletons);
+			attachFragment(bundles[i], rejectedSingletons, processedFragments);
 
 		// add initial grouping constraints after fragments have been attached
 		for (int i = 0; i < bundles.length; i++)