[FELIX-4942] Extract the consistency check into its own method

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1690704 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/resolver/src/main/java/org/apache/felix/resolver/Candidates.java b/resolver/src/main/java/org/apache/felix/resolver/Candidates.java
index 89bc5c4..96bed4d 100644
--- a/resolver/src/main/java/org/apache/felix/resolver/Candidates.java
+++ b/resolver/src/main/java/org/apache/felix/resolver/Candidates.java
@@ -113,6 +113,11 @@
         m_delta = new OpenHashMapSet<Requirement, Capability>(3);
     }
 
+    public int getNbResources()
+    {
+        return m_populateResultCache.size();
+    }
+
     /**
      * Returns the delta which is the differences in the candidates from the
      * original Candidates permutation.
diff --git a/resolver/src/main/java/org/apache/felix/resolver/ResolverImpl.java b/resolver/src/main/java/org/apache/felix/resolver/ResolverImpl.java
index 24281ab..ab6e352 100644
--- a/resolver/src/main/java/org/apache/felix/resolver/ResolverImpl.java
+++ b/resolver/src/main/java/org/apache/felix/resolver/ResolverImpl.java
@@ -123,8 +123,6 @@
         ResolveSession session = new ResolveSession(rc);
         Map<Resource, List<Wire>> wireMap =
             new HashMap<Resource, List<Wire>>();
-        Map<Resource, Packages> resourcePkgMap =
-            new HashMap<Resource, Packages>();
 
         // Make copies of arguments in case we want to modify them.
         Collection<Resource> mandatoryResources = new ArrayList<Resource>(rc.getMandatoryResources());
@@ -239,7 +237,6 @@
                         continue;
                     }
 
-                    resourcePkgMap.clear();
                     session.getPackageSourcesCache().clear();
                     // Null out each time a new permutation is attempted.
                     // We only use this to store a valid permutation which is a
@@ -248,22 +245,16 @@
 
 //allCandidates.dump();
 
-                    Map<Resource, ResolutionError> currentFaultyResources = null;
                     rethrow = allCandidates.checkSubstitutes(importPermutations);
                     if (rethrow != null)
                     {
                         continue;
                     }
 
-                    // Reuse a resultCache map for checking package consistency
-                    // for all resources.
-                    Map<Resource, Object> resultCache =
-                        new HashMap<Resource, Object>(allResources.size());
-                    // Check the package space consistency for all 'root' resources.
+                    // Compute the list of hosts
+                    Map<Resource, Resource> hosts = new LinkedHashMap<Resource, Resource>();
                     for (Resource resource : allResources)
                     {
-                        Resource target = resource;
-
                         // If we are resolving a fragment, then get its
                         // host candidate and verify it instead.
                         Requirement hostReq = hostReqs.get(resource);
@@ -277,42 +268,15 @@
                             {
                                 continue;
                             }
-                            target = hostCap.getResource();
+                            resource = hostCap.getResource();
                         }
-
-                        calculatePackageSpaces(
-                            session, allCandidates.getWrappedHost(target), allCandidates,
-                            resourcePkgMap, new HashMap<Capability, Set<Resource>>(256),
-                            new HashSet<Resource>(64));
-//System.out.println("+++ PACKAGE SPACES START +++");
-//dumpResourcePkgMap(resourcePkgMap);
-//System.out.println("+++ PACKAGE SPACES END +++");
-
-                        rethrow = checkPackageSpaceConsistency(
-                                session, allCandidates.getWrappedHost(target),
-                                allCandidates, resourcePkgMap, resultCache);
-                        if (rethrow != null)
-                        {
-                            if (currentFaultyResources == null)
-                            {
-                                currentFaultyResources = new HashMap<Resource, ResolutionError>();
-                            }
-                            Resource faultyResource = resource;
-                            // check that the faulty requirement is not from a fragment
-                            for (Requirement faultyReq : rethrow.getUnresolvedRequirements())
-                            {
-                                if (faultyReq instanceof WrappedRequirement)
-                                {
-                                    faultyResource =
-                                        ((WrappedRequirement) faultyReq)
-                                        .getDeclaredRequirement().getResource();
-                                    break;
-                                }
-                            }
-                            currentFaultyResources.put(faultyResource, rethrow);
-                        }
+                        hosts.put(resource, allCandidates.getWrappedHost(resource));
                     }
-                    if (currentFaultyResources != null)
+
+                    Map<Resource, ResolutionError> currentFaultyResources = new HashMap<Resource, ResolutionError>();
+                    rethrow = checkConsistency(session, allCandidates, currentFaultyResources, hosts);
+
+                    if (!currentFaultyResources.isEmpty())
                     {
                         if (faultyResources == null)
                         {
@@ -415,6 +379,54 @@
         return wireMap;
     }
 
+    private ResolutionError checkConsistency(
+        ResolveSession session,
+        Candidates allCandidates,
+        Map<Resource, ResolutionError> currentFaultyResources,
+        Map<Resource, Resource> hosts)
+    {
+        Map<Resource, Packages> resourcePkgMap =
+                new HashMap<Resource, Packages>(allCandidates.getNbResources());
+        // Reuse a resultCache map for checking package consistency
+        // for all resources.
+        Map<Resource, Object> resultCache =
+                new HashMap<Resource, Object>();
+        // Check the package space consistency for all 'root' resources.
+        ResolutionError error = null;
+        for (Entry<Resource, Resource> entry : hosts.entrySet())
+        {
+            calculatePackageSpaces(
+                    session, entry.getValue(), allCandidates,
+                    resourcePkgMap, new HashMap<Capability, Set<Resource>>(256),
+                    new HashSet<Resource>(64));
+//System.out.println("+++ PACKAGE SPACES START +++");
+//dumpResourcePkgMap(resourcePkgMap);
+//System.out.println("+++ PACKAGE SPACES END +++");
+
+            ResolutionError rethrow = checkPackageSpaceConsistency(
+                    session, entry.getValue(),
+                    allCandidates, resourcePkgMap, resultCache);
+            if (rethrow != null)
+            {
+                Resource faultyResource = entry.getKey();
+                // check that the faulty requirement is not from a fragment
+                for (Requirement faultyReq : rethrow.getUnresolvedRequirements())
+                {
+                    if (faultyReq instanceof WrappedRequirement)
+                    {
+                        faultyResource =
+                                ((WrappedRequirement) faultyReq)
+                                        .getDeclaredRequirement().getResource();
+                        break;
+                    }
+                }
+                currentFaultyResources.put(faultyResource, rethrow);
+                error = rethrow;
+            }
+        }
+        return error;
+    }
+
     /**
      * Resolves a dynamic requirement for the specified host resource using the
      * specified {@link ResolveContext}. The dynamic requirement may contain