felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rickh...@apache.org
Subject svn commit: r835915 [2/2] - in /felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver: ./ cs/ felix/ manifestparser/ prototype/
Date Fri, 13 Nov 2009 16:50:35 GMT
Modified: felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/felix/FelixResolverImpl.java
URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/felix/FelixResolverImpl.java?rev=835915&r1=835914&r2=835915&view=diff
==============================================================================
--- felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/felix/FelixResolverImpl.java (original)
+++ felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/felix/FelixResolverImpl.java Fri Nov 13 16:50:35 2009
@@ -18,7 +18,16 @@
  */
 package org.apache.felix.resolver.felix;
 
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.StringTokenizer;
 import org.apache.felix.resolver.*;
 
 public class FelixResolverImpl
@@ -26,7 +35,6 @@
     // Reusable empty array.
     private static final Wire[] m_emptyWires = new Wire[0];
     private static final Module[] m_emptyModules = new Module[0];
-    private static final PackageSource[] m_emptySources = new PackageSource[0];
 
     public FelixResolverImpl()
     {
@@ -34,7 +42,7 @@
 
     // Returns a map of resolved bundles where the key is the module
     // and the value is an array of wires.
-    public Map resolve(ResolverState state, Module rootModule)
+    public Map resolve(ResolverState state, Module rootModule) throws ResolveException
     {
         // If the module is already resolved, then we can just return.
         if (rootModule.isResolved())
@@ -60,7 +68,7 @@
         // this map will be resolved, only the target module and
         // any candidates selected to resolve its requirements and the
         // transitive requirements this implies.
-        populateCandidatesMap(state, candidatesMap, new HashMap(), null, rootModule);
+        populateCandidatesMap(state, candidatesMap, rootModule);
 
         // The next step is to use the candidates map to determine if
         // the class space for the root module is consistent. This
@@ -86,8 +94,8 @@
     }
 
     private void populateCandidatesMap(
-        ResolverState state, Map candidatesMap, Map byproductMap,
-        Module instigatingModule, Module targetModule)
+        ResolverState state, Map candidatesMap, Module targetModule)
+        throws ResolveException
     {
         // Detect cycles.
         if (candidatesMap.containsKey(targetModule))
@@ -95,8 +103,6 @@
             return;
         }
 
-        // Finally, resolve any dependencies the module may have.
-
         // Add target module to the candidates map so we can detect cycles.
         candidatesMap.put(targetModule, null);
 
@@ -113,60 +119,46 @@
             // package maps. The "resolved" candidates have higher priority
             // than "unresolved" ones, so put the "resolved" candidates
             // at the front of the list of candidates.
-            PackageSource[] resolved = state.getResolvedCandidates(reqs.get(reqIdx));
-            PackageSource[] unresolved = state.getUnresolvedCandidates(reqs.get(reqIdx));
-            PackageSource[] candidates = new PackageSource[resolved.length + unresolved.length];
-            System.arraycopy(resolved, 0, candidates, 0, resolved.length);
-            System.arraycopy(unresolved, 0, candidates, resolved.length, unresolved.length);
+            List<ExportedPackage> candidates = state.getResolvedCandidates(reqs.get(reqIdx));
+            candidates.addAll(state.getUnresolvedCandidates(reqs.get(reqIdx)));
 
             // If we have candidates, then we need to recursively populate
             // the resolver map with each of them.
-            RuntimeException rethrow = null;
-            if (candidates.length > 0)
+            ResolveException rethrow = null;
+            if (candidates.size() > 0)
             {
-                for (int candIdx = 0; candIdx < candidates.length; candIdx++)
+                for (Iterator it = candidates.iterator(); it.hasNext(); )
                 {
+                    ExportedPackage candidate = (ExportedPackage) it.next();
+
                     try
                     {
                         // Only populate the resolver map with modules that
                         // are not already resolved.
-                        if (!candidates[candIdx].m_module.isResolved())
+                        if (!candidate.getModule().isResolved())
                         {
                             populateCandidatesMap(
-                                state, candidatesMap, byproductMap,
-                                targetModule, candidates[candIdx].m_module);
+                                state, candidatesMap, candidate.getModule());
                         }
                     }
-                    catch (RuntimeException ex)
+                    catch (ResolveException ex)
                     {
                         // If we received a resolve exception, then the
                         // current candidate is not resolvable for some
                         // reason and should be removed from the list of
                         // candidates. For now, just null it.
-                        candidates[candIdx] = null;
+                        it.remove();
                         rethrow = ex;
                     }
                 }
-
-                // Remove any nulled candidates to create the final list
-                // of available candidates.
-                candidates = shrinkCandidateArray(candidates);
             }
 
             // If no candidates exist at this point, then throw a
             // resolve exception unless the import is optional.
-            if (candidates.length == 0)
+            if ((candidates.size() == 0))
             {
-                // Since the target module cannot resolve, remove its
-                // candidates set list from the candidates map, since
-                // it is invalid.
-                candidatesMap.remove(targetModule);
-
-                // Also remove any byproduct resolved modules.
-// TODO: FELIX-1422 - Maybe this goes too far and should only discard the
-//       the failing module as a candidate, since some modules may have
-//       resolved correctly because they didn't depend on the failing module.
-                removeByproductModules(candidatesMap, byproductMap, targetModule);
+                // Remove invalid candidate and any cycle byproduct resolved modules.
+                removeInvalidCandidate(targetModule, candidatesMap, new ArrayList());
 
                 // If we have received an exception while trying to populate
                 // the candidates map, rethrow that exception since it might
@@ -181,13 +173,13 @@
                 else
                 {
                     throw new ResolveException(
-                        "Unable to resolve: " + targetModule + " - " + reqs.get(reqIdx));
+                        "Unable to resolve " + targetModule + " - " + reqs.get(reqIdx));
                 }
             }
-            else if (candidates.length > 0)
+            else if (candidates.size() > 0)
             {
                 candSetList.add(
-                    new CandidateSet(CandidateSet.NORMAL, targetModule, reqs.get(reqIdx), candidates));
+                    new CandidateSet(targetModule, reqs.get(reqIdx), candidates));
             }
         }
 
@@ -195,38 +187,72 @@
         // candidate set list to the candidates map to be used for calculating
         // uses constraints and ultimately wires.
         candidatesMap.put(targetModule, candSetList);
+    }
 
-        // Also add this module as a byproduct of the instigating module,
-        // since it caused it to be resolved; this is necessary if we have
-        // a failure to resolver somewhere higher up.
-        if (instigatingModule != null)
-        {
-            Module[] modules = (Module[]) byproductMap.get(instigatingModule);
-            if (modules == null)
-            {
-                modules = new Module[] { targetModule };
-            }
-            else
-            {
-                Module[] tmp = new Module[modules.length + 1];
-                System.arraycopy(modules, 0, tmp, 0, modules.length);
-                tmp[modules.length] = targetModule;
-                modules = tmp;
+    private static void removeInvalidCandidate(
+        Module invalidModule, Map candidatesMap, List invalidList)
+    {
+// TODO: PERFORMANCE - This could be quicker if we kept track of who depended on whom,
+//       or only those modules used as candidates or those in a cycle.
+
+        // Remove the invalid module's  candidates set list from the candidates map,
+        // since it should only contain entries for validly resolved modules.
+        candidatesMap.remove(invalidModule);
+
+        // Loop through each candidate set list in the candidates map to try
+        // to find references to the invalid module.
+        for (Iterator itCandidatesMap = candidatesMap.entrySet().iterator();
+            itCandidatesMap.hasNext(); )
+        {
+            Map.Entry entry = (Map.Entry) itCandidatesMap.next();
+            Module module = (Module) entry.getKey();
+            List candSetList = (List) entry.getValue();
+            if (candSetList != null)
+            {
+                // Loop through each candidate set in the candidate set list
+                // to search for the invalid module.
+                for (Iterator itCandSetList = candSetList.iterator(); itCandSetList.hasNext(); )
+                {
+                    // Loop through the candidate in the candidate set and remove
+                    // the invalid module if it is found.
+                    CandidateSet cs = (CandidateSet) itCandSetList.next();
+                    for (Iterator itCandidates = cs.m_candidates.iterator();
+                        itCandidates.hasNext(); )
+                    {
+                        // If the invalid module is a candidate, then remove it from
+                        // the candidate set.
+                        ExportedPackage candCap = (ExportedPackage) itCandidates.next();
+                        if (candCap.getModule().equals(invalidModule))
+                        {
+                            itCandidates.remove();
+
+                            // If there are no more candidates in the candidate set, then
+                            // remove it from the candidate set list.
+                            if (cs.m_candidates.size() == 0)
+                            {
+                                itCandSetList.remove();
+
+                                // If the requirement is not optional, then add the module
+                                // to a list which will be removed after removing the current
+                                // invalid module.
+                                if ((module != invalidModule) && !invalidList.contains(module))
+                                {
+                                    invalidList.add(module);
+                                }
+                            }
+                            break;
+                        }
+                    }
+                }
             }
-            byproductMap.put(instigatingModule, modules);
         }
-    }
 
-    private static void removeByproductModules(
-        Map candidatesMap, Map byproductMap, Module targetModule)
-    {
-        Module[] modules = (Module[]) byproductMap.remove(targetModule);
-        if (modules != null)
+        if (!invalidList.isEmpty())
         {
-            for (int i = 0; i < modules.length; i++)
+            while (!invalidList.isEmpty())
             {
-                candidatesMap.remove(modules[i]);
-                removeByproductModules(candidatesMap, byproductMap, modules[i]);
+                Module m = (Module) invalidList.remove(0);
+                removeInvalidCandidate(m, candidatesMap, invalidList);
             }
         }
     }
@@ -298,9 +324,9 @@
                         for (int csIdx = 0; csIdx < candSetList.size(); csIdx++)
                         {
                             CandidateSet cs = (CandidateSet) candSetList.get(csIdx);
-                            if ((cs.m_candidates != null) && (cs.m_candidates.length > 1))
+                            if ((cs.m_candidates != null) && (cs.m_candidates.size() > 1))
                             {
-                                weight += cs.m_candidates.length;
+                                weight += cs.m_candidates.size();
                             }
                         }
                         return -weight;
@@ -344,7 +370,7 @@
 
         // Get the package map for the target module, which is a
         // map of all packages accessible to the module and their
-        // associated package sources.
+        // associated capabilities.
         Map pkgMap = null;
         try
         {
@@ -353,24 +379,23 @@
         catch (ResolveException ex)
         {
             System.out.println(
-                "Constraint violation for " + targetModule + " detected." +
-                ex);
+                "Constraint violation for " + targetModule + " detected - " + ex);
             return false;
         }
 
         // Loop through all of the target module's accessible packages and
-        // verify that all package sources are consistent.
+        // verify that all packages are consistent.
         for (Iterator iter = pkgMap.entrySet().iterator(); iter.hasNext(); )
         {
             Map.Entry entry = (Map.Entry) iter.next();
             // Get the resolved package, which contains the set of all
-            // package sources for the given package.
+            // capabilities for the given package.
             ResolvedPackage rp = (ResolvedPackage) entry.getValue();
-            // Loop through each package source and test if it is consistent.
-            for (int srcIdx = 0; srcIdx < rp.m_sourceList.size(); srcIdx++)
+            // Loop through each capability and test if it is consistent.
+            for (int capIdx = 0; capIdx < rp.m_capList.size(); capIdx++)
             {
-                PackageSource ps = (PackageSource) rp.m_sourceList.get(srcIdx);
-                if (!isClassSpaceConsistent(ps.m_module, moduleMap, cycleMap, candidatesMap))
+                ExportedPackage cap = (ExportedPackage) rp.m_capList.get(capIdx);
+                if (!isClassSpaceConsistent(cap.getModule(), moduleMap, cycleMap, candidatesMap))
                 {
                     return false;
                 }
@@ -387,8 +412,7 @@
         catch (ResolveException ex)
         {
             System.out.println(
-                "Constraint violation for " + targetModule + " detected." +
-                ex);
+                "Constraint violation for " + targetModule + " detected - " + ex);
             return false;
         }
 
@@ -411,8 +435,8 @@
                 rp = (ResolvedPackage) rp.clone();
 
                 // Loop through all implied "uses" constraints for the current
-                // "used" package and verify that all package sources are
-                // compatible with the package source of the root module's
+                // "used" package and verify that all packages are
+                // compatible with the packages of the root module's
                 // package map.
                 List constraintList = (List) entry.getValue();
                 for (int constIdx = 0; constIdx < constraintList.size(); constIdx++)
@@ -421,7 +445,7 @@
                     // package.
                     ResolvedPackage rpUses = (ResolvedPackage) constraintList.get(constIdx);
                     // Determine if the implied "uses" constraint is compatible with
-                    // the target module's package sources for the given "used"
+                    // the target module's packages for the given "used"
                     // package. They are compatible if one is the subset of the other.
                     // Retain the union of the two sets if they are compatible.
                     if (rpUses.isSubset(rp))
@@ -431,15 +455,15 @@
                     else if (rp.isSubset(rpUses))
                     {
                         // Keep the superset, i.e., the union.
-                        rp.m_sourceList.clear();
-                        rp.m_sourceList.addAll(rpUses.m_sourceList);
+                        rp.m_capList.clear();
+                        rp.m_capList.addAll(rpUses.m_capList);
                     }
                     else
                     {
-//                        System.out.println(
-//                            "Constraint violation for " + targetModule
-//                            + " detected; module can see "
-//                            + rp + " and " + rpUses);
+                        System.out.println(
+                            "Constraint violation for " + targetModule
+                            + " detected; module can see "
+                            + rp + " and " + rpUses);
 
                         // If the resolved package has a candidate set, then
                         // attempt to directly rotate the candidates to fix the
@@ -449,16 +473,16 @@
                         // conflict, which in some cases will be smarter. Only
                         // rotate the candidates if we have more than one and we
                         // haven't already rotated them completely.
-                        if ((rp.m_cs != null) && (rp.m_cs.m_candidates.length > 1)
-                            && (rp.m_cs.m_rotated < rp.m_cs.m_candidates.length))
+                        if ((rp.m_cs != null) && (rp.m_cs.m_candidates.size() > 1)
+                            && (rp.m_cs.m_rotated < rp.m_cs.m_candidates.size()))
                         {
                             // Rotate candidates.
-                            PackageSource first = rp.m_cs.m_candidates[0];
-                            for (int i = 1; i < rp.m_cs.m_candidates.length; i++)
+                            ExportedPackage first = (ExportedPackage) rp.m_cs.m_candidates.get(0);
+                            for (int i = 1; i < rp.m_cs.m_candidates.size(); i++)
                             {
-                                rp.m_cs.m_candidates[i - 1] = rp.m_cs.m_candidates[i];
+                                rp.m_cs.m_candidates.set(i - 1, rp.m_cs.m_candidates.get(i));
                             }
-                            rp.m_cs.m_candidates[rp.m_cs.m_candidates.length - 1] = first;
+                            rp.m_cs.m_candidates.set(rp.m_cs.m_candidates.size() - 1, first);
                             rp.m_cs.m_rotated++;
                             m_candidatesRotated = true;
                         }
@@ -491,18 +515,18 @@
         Map pkgMap = getModulePackages(moduleMap, targetModule, candidatesMap);
 
         // Each package accessible from the target module is potentially
-        // comprised of one or more modules, called package sources. The
-        // "uses" constraints implied by all package sources must be
-        // calculated and combined to determine the complete set of implied
-        // "uses" constraints for each package accessible by the target module.
+        // comprised of one or more capabilities. The "uses" constraints
+        // implied by all capabilities must be calculated and combined to
+        // determine the complete set of implied "uses" constraints for
+        // each package accessible by the target module.
         for (Iterator iter = pkgMap.entrySet().iterator(); iter.hasNext(); )
         {
             Map.Entry entry = (Map.Entry) iter.next();
             ResolvedPackage rp = (ResolvedPackage) entry.getValue();
-            for (int srcIdx = 0; srcIdx < rp.m_sourceList.size(); srcIdx++)
+            for (int capIdx = 0; capIdx < rp.m_capList.size(); capIdx++)
             {
                 usesMap = calculateUsesConstraints(
-                    (PackageSource) rp.m_sourceList.get(srcIdx),
+                    (ExportedPackage) rp.m_capList.get(capIdx),
                     moduleMap, usesMap, cycleMap, candidatesMap);
             }
         }
@@ -510,31 +534,31 @@
     }
 
     private static Map calculateUsesConstraints(
-        PackageSource psTarget, Map moduleMap, Map usesMap,
+        ExportedPackage capTarget, Map moduleMap, Map usesMap,
         Map cycleMap, Map candidatesMap)
         throws ResolveException
     {
 //System.out.println("calculateUsesConstraints2("+psTarget.m_module+")");
         // If we are in a cycle, then return for now.
-        if (cycleMap.get(psTarget) != null)
+        if (cycleMap.get(capTarget) != null)
         {
             return usesMap;
         }
 
-        // Record the target package source in the cycle map.
-        cycleMap.put(psTarget, psTarget);
+        // Record the target capability in the cycle map.
+        cycleMap.put(capTarget, capTarget);
 
         // Get all packages accessible from the module of the
-        // target package source.
-        Map pkgMap = getModulePackages(moduleMap, psTarget.m_module, candidatesMap);
+        // target capability.
+        Map pkgMap = getModulePackages(moduleMap, capTarget.getModule(), candidatesMap);
 
-        // Get capability (i.e., package) of the target package source.
-        ExportedPackage cap = psTarget.m_capability;
+        // Cast to implementation class to get access to cached data.
+        ExportedPackage cap = (ExportedPackage) capTarget;
 
         // Loop through all "used" packages of the capability.
         for (int i = 0; i < cap.getUses().size(); i++)
         {
-            // The target package source module should have a resolved package
+            // The target capability's module should have a resolved package
             // for the "used" package in its set of accessible packages,
             // since it claims to use it, so get the associated resolved
             // package.
@@ -544,13 +568,13 @@
             // but check for safety.
             if (rp != null)
             {
-                // First, iterate through all package sources for the resolved
+                // First, iterate through all capabilities for the resolved
                 // package associated with the current "used" package and calculate
-                // and combine the "uses" constraints for each package source.
-                for (int srcIdx = 0; srcIdx < rp.m_sourceList.size(); srcIdx++)
+                // and combine the "uses" constraints for each package.
+                for (int srcIdx = 0; srcIdx < rp.m_capList.size(); srcIdx++)
                 {
                     usesMap = calculateUsesConstraints(
-                        (PackageSource) rp.m_sourceList.get(srcIdx),
+                        (ExportedPackage) rp.m_capList.get(srcIdx),
                         moduleMap, usesMap, cycleMap, candidatesMap);
                 }
 
@@ -586,7 +610,7 @@
     /**
      * <p>
      * Calculates the module's set of accessible packages and their
-     * assocaited package sources. This method uses the current candidates
+     * assocaited package capabilities. This method uses the current candidates
      * for resolving the module's requirements from the candidate map
      * to calculate the module's accessible packages.
      * </p>
@@ -603,37 +627,16 @@
 //System.out.println("calculateModulePackages("+module+")");
         Map importedPackages = calculateImportedPackages(module, candidatesMap);
         Map exportedPackages = calculateExportedPackages(module);
-        Map requiredPackages = new HashMap();
 
-        // Merge exported packages into required packages. If a package is both
-        // exported and required, then append the exported source to the end of
-        // the require package sources; otherwise just add it to the package map.
-        for (Iterator i = exportedPackages.entrySet().iterator(); i.hasNext(); )
-        {
-            Map.Entry entry = (Map.Entry) i.next();
-            ResolvedPackage rpReq = (ResolvedPackage) requiredPackages.get(entry.getKey());
-            if (rpReq != null)
-            {
-                // Merge exported and required packages, avoiding duplicate
-                // package sources and maintaining ordering.
-                ResolvedPackage rpExport = (ResolvedPackage) entry.getValue();
-                rpReq.merge(rpExport);
-            }
-            else
-            {
-                requiredPackages.put(entry.getKey(), entry.getValue());
-            }
-        }
-
-        // Merge imported packages into required packages. Imports overwrite
+        // Merge imported packages into exported packages. Imports overwrite
         // any required and/or exported package.
         for (Iterator i = importedPackages.entrySet().iterator(); i.hasNext(); )
         {
             Map.Entry entry = (Map.Entry) i.next();
-            requiredPackages.put(entry.getKey(), entry.getValue());
+            exportedPackages.put(entry.getKey(), entry.getValue());
         }
 
-        return requiredPackages;
+        return exportedPackages;
     }
 
     private static Map calculateImportedPackages(Module targetModule, Map candidatesMap)
@@ -653,19 +656,20 @@
         List candSetList = (List) candidatesMap.get(targetModule);
 
         // Loop through all candidate sets that represent import dependencies
-        // for the target module and add the current candidate's package source
+        // for the target module and add the current candidate's packages
         // to the imported package map.
         for (int candSetIdx = 0;
             (candSetList != null) && (candSetIdx < candSetList.size());
             candSetIdx++)
         {
             CandidateSet cs = (CandidateSet) candSetList.get(candSetIdx);
-            PackageSource ps = cs.m_candidates[cs.m_idx];
+            ExportedPackage candCap = (ExportedPackage) cs.m_candidates.get(cs.m_idx);
 
-            String pkgName = ps.m_capability.getName();
+            String pkgName = (String)
+                candCap.getName();
 
             ResolvedPackage rp = new ResolvedPackage(pkgName, cs);
-            rp.m_sourceList.add(ps);
+            rp.m_capList.add(candCap);
             pkgMap.put(rp.m_name, rp);
         }
 
@@ -682,10 +686,11 @@
         List<ExportedPackage> caps = targetModule.getExports();
         for (int capIdx = 0; (caps != null) && (capIdx < caps.size()); capIdx++)
         {
-            String pkgName = caps.get(capIdx).getName();
+            String pkgName = (String)
+                caps.get(capIdx).getName();
             ResolvedPackage rp = (ResolvedPackage) pkgMap.get(pkgName);
             rp = (rp == null) ? new ResolvedPackage(pkgName, null) : rp;
-            rp.m_sourceList.add(new PackageSource(targetModule, caps.get(capIdx)));
+            rp.m_capList.add(caps.get(capIdx));
             pkgMap.put(rp.m_name, rp);
         }
 
@@ -703,7 +708,7 @@
                 CandidateSet cs = (CandidateSet) candSetList.get(j);
                 // See if we can increment the candidate set, without overflowing
                 // the candidate array bounds.
-                if ((cs.m_idx + 1) < cs.m_candidates.length)
+                if ((cs.m_idx + 1) < cs.m_candidates.size())
                 {
                     cs.m_idx++;
                     return;
@@ -748,23 +753,21 @@
             // Get the current candidate set.
             CandidateSet cs = (CandidateSet) candSetList.get(candSetIdx);
 
-            // Create a package wire for package dependencies.
-            // Filter out the case where a module imports from
-            // itself, since the module should simply load from
-            // its internal class path in this case.
-            if (importer != cs.m_candidates[cs.m_idx].m_module)
+            if (importer != ((ExportedPackage) cs.m_candidates.get(cs.m_idx)).getModule())
             {
                 // Add wire for imported package.
                 packageWires.add(new Wire(
                     importer,
                     cs.m_requirement,
-                    cs.m_candidates[cs.m_idx].m_module,
-                    cs.m_candidates[cs.m_idx].m_capability));
+                    ((ExportedPackage) cs.m_candidates.get(cs.m_idx)).getModule(),
+                    ((ExportedPackage) cs.m_candidates.get(cs.m_idx))));
             }
 
             // Create any necessary wires for the selected candidate module.
             wireMap = populateWireMap(
-                state, candidatesMap, cs.m_candidates[cs.m_idx].m_module, wireMap);
+                state, candidatesMap,
+                ((ExportedPackage) cs.m_candidates.get(cs.m_idx)).getModule(),
+                wireMap);
         }
 
         packageWires.addAll(moduleWires);
@@ -777,34 +780,6 @@
     // Utility methods.
     //
 
-    private static PackageSource[] shrinkCandidateArray(PackageSource[] candidates)
-    {
-        if (candidates == null)
-        {
-            return m_emptySources;
-        }
-
-        // Move all non-null values to one end of the array.
-        int lower = 0;
-        for (int i = 0; i < candidates.length; i++)
-        {
-            if (candidates[i] != null)
-            {
-                candidates[lower++] = candidates[i];
-            }
-        }
-
-        if (lower == 0)
-        {
-            return m_emptySources;
-        }
-
-        // Copy non-null values into a new array and return.
-        PackageSource[] newCandidates= new PackageSource[lower];
-        System.arraycopy(candidates, 0, newCandidates, 0, lower);
-        return newCandidates;
-    }
-
     //
     // Inner classes.
     //
@@ -812,7 +787,7 @@
     public static interface ResolverState
     {
         Module[] getModules();
-        PackageSource[] getResolvedCandidates(ImportedPackage req);
-        PackageSource[] getUnresolvedCandidates(ImportedPackage req);
+        List<ExportedPackage> getResolvedCandidates(ImportedPackage req);
+        List<ExportedPackage> getUnresolvedCandidates(ImportedPackage req);
     }
 }
\ No newline at end of file

Modified: felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/felix/ResolvedPackage.java
URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/felix/ResolvedPackage.java?rev=835915&r1=835914&r2=835915&view=diff
==============================================================================
--- felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/felix/ResolvedPackage.java (original)
+++ felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/felix/ResolvedPackage.java Fri Nov 13 16:50:35 2009
@@ -20,19 +20,20 @@
 
 import java.util.ArrayList;
 import java.util.List;
+import org.apache.felix.resolver.ExportedPackage;
 
 /**
  * This utility class is a resolved package, which is comprised of a
- * set of <tt>PackageSource</tt>s that is calculated by the resolver
- * algorithm. A given resolved package may have a single package source,
+ * set of <tt>ICapability</tt>s that is calculated by the resolver
+ * algorithm. A given resolved package may have a single capability,
  * as is the case with imported packages, or it may have multiple
- * package sources, as is the case with required bundles.
+ * capabilities, as is the case with required bundles.
  */
 class ResolvedPackage
 {
     public final String m_name;
     public final CandidateSet m_cs;
-    public final List m_sourceList = new ArrayList();
+    public final List m_capList = new ArrayList();
 
     public ResolvedPackage(String name, CandidateSet cs)
     {
@@ -43,7 +44,7 @@
 
     public boolean isSubset(ResolvedPackage rp)
     {
-        if (m_sourceList.size() > rp.m_sourceList.size())
+        if (m_capList.size() > rp.m_capList.size())
         {
             return false;
         }
@@ -52,13 +53,13 @@
             return false;
         }
         // Determine if the target set of source modules is a subset.
-        return rp.m_sourceList.containsAll(m_sourceList);
+        return rp.m_capList.containsAll(m_capList);
     }
 
     public Object clone()
     {
         ResolvedPackage rp = new ResolvedPackage(m_name, m_cs);
-        rp.m_sourceList.addAll(m_sourceList);
+        rp.m_capList.addAll(m_capList);
         return rp;
     }
 
@@ -66,11 +67,11 @@
     {
         // Merge required packages, avoiding duplicate
         // package sources and maintaining ordering.
-        for (int srcIdx = 0; srcIdx < rp.m_sourceList.size(); srcIdx++)
+        for (int capIdx = 0; capIdx < rp.m_capList.size(); capIdx++)
         {
-            if (!m_sourceList.contains(rp.m_sourceList.get(srcIdx)))
+            if (!m_capList.contains(rp.m_capList.get(capIdx)))
             {
-                m_sourceList.add(rp.m_sourceList.get(srcIdx));
+                m_capList.add(rp.m_capList.get(capIdx));
             }
         }
     }
@@ -85,11 +86,11 @@
         sb.append(padding);
         sb.append(m_name);
         sb.append(" from [");
-        for (int i = 0; i < m_sourceList.size(); i++)
+        for (int i = 0; i < m_capList.size(); i++)
         {
-            PackageSource ps = (PackageSource) m_sourceList.get(i);
-            sb.append(ps.m_module);
-            if ((i + 1) < m_sourceList.size())
+            ExportedPackage cap = (ExportedPackage) m_capList.get(i);
+            sb.append(cap.getModule());
+            if ((i + 1) < m_capList.size())
             {
                 sb.append(", ");
             }

Modified: felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/manifestparser/Main.java
URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/manifestparser/Main.java?rev=835915&r1=835914&r2=835915&view=diff
==============================================================================
--- felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/manifestparser/Main.java (original)
+++ felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/manifestparser/Main.java Fri Nov 13 16:50:35 2009
@@ -74,118 +74,125 @@
 
         List<Module> moduleList = new ArrayList<Module>();
 
-        moduleList.add(new Module("system.bundle")
-            .exporting(new ExportedPackage("javax.activation").with("version=1.1.0"))
-            .exporting(new ExportedPackage("javax.crypto"))
-            .exporting(new ExportedPackage("javax.crypto.spec"))
-            .exporting(new ExportedPackage("javax.imageio"))
-            .exporting(new ExportedPackage("javax.imageio.stream"))
-            .exporting(new ExportedPackage("javax.jws").with("version=2.0.0"))
-            .exporting(new ExportedPackage("javax.jws.soap"))
-            .exporting(new ExportedPackage("javax.management"))
-            .exporting(new ExportedPackage("javax.management.loading"))
-            .exporting(new ExportedPackage("javax.management.modelmbean"))
-            .exporting(new ExportedPackage("javax.management.openmbean"))
-            .exporting(new ExportedPackage("javax.management.remote"))
-            .exporting(new ExportedPackage("javax.naming"))
-            .exporting(new ExportedPackage("javax.naming.directory"))
-            .exporting(new ExportedPackage("javax.naming.ldap"))
-            .exporting(new ExportedPackage("javax.naming.spi"))
-            .exporting(new ExportedPackage("javax.net"))
-            .exporting(new ExportedPackage("javax.net.ssl"))
-            .exporting(new ExportedPackage("javax.rmi"))
-            .exporting(new ExportedPackage("javax.rmi.CORBA"))
-            .exporting(new ExportedPackage("javax.security.auth"))
-            .exporting(new ExportedPackage("javax.security.auth.callback"))
-            .exporting(new ExportedPackage("javax.security.auth.kerberos"))
-            .exporting(new ExportedPackage("javax.security.auth.login"))
-            .exporting(new ExportedPackage("javax.security.auth.spi"))
-            .exporting(new ExportedPackage("javax.security.auth.x500"))
-            .exporting(new ExportedPackage("javax.security.cert"))
-            .exporting(new ExportedPackage("javax.security.sasl"))
-            .exporting(new ExportedPackage("javax.sql"))
-            .exporting(new ExportedPackage("javax.swing"))
-            .exporting(new ExportedPackage("javax.swing.border"))
-            .exporting(new ExportedPackage("javax.swing.tree"))
-            .exporting(new ExportedPackage("javax.xml"))
-            .exporting(new ExportedPackage("javax.xml.bind").with("version=2.2.0"))
-            .exporting(new ExportedPackage("javax.xml.bind.annotation"))
-            .exporting(new ExportedPackage("javax.xml.bind.annotation.adapters"))
-            .exporting(new ExportedPackage("javax.xml.bind.attachment"))
-            .exporting(new ExportedPackage("javax.xml.bind.helpers"))
-            .exporting(new ExportedPackage("javax.xml.bind.util"))
-            .exporting(new ExportedPackage("javax.xml.datatype"))
-            .exporting(new ExportedPackage("javax.xml.namespace"))
-            .exporting(new ExportedPackage("javax.xml.parsers"))
-            .exporting(new ExportedPackage("javax.xml.registry"))
-            .exporting(new ExportedPackage("javax.xml.registry.infomodel"))
-            .exporting(new ExportedPackage("javax.xml.rpc").with("version=1.1.0"))
-            .exporting(new ExportedPackage("javax.xml.rpc.encoding"))
-            .exporting(new ExportedPackage("javax.xml.rpc.handler.soap").with("version=1.1.0"))
-            .exporting(new ExportedPackage("javax.xml.rpc.holders"))
-            .exporting(new ExportedPackage("javax.xml.rpc.server").with("version=1.1.0"))
-            .exporting(new ExportedPackage("javax.xml.rpc.soap").with("version=1.1.0"))
-            .exporting(new ExportedPackage("javax.xml.soap").with("version=1.3.0"))
-            .exporting(new ExportedPackage("javax.xml.stream").with("version=1.0.0"))
-            .exporting(new ExportedPackage("javax.xml.stream.events"))
-            .exporting(new ExportedPackage("javax.xml.stream.util").with("version=1.0.0"))
-            .exporting(new ExportedPackage("javax.xml.transform"))
-            .exporting(new ExportedPackage("javax.xml.transform.dom"))
-            .exporting(new ExportedPackage("javax.xml.transform.sax"))
-            .exporting(new ExportedPackage("javax.xml.transform.stream"))
-            .exporting(new ExportedPackage("javax.xml.validation"))
-            .exporting(new ExportedPackage("javax.xml.ws").with("version=2.1.0"))
-            .exporting(new ExportedPackage("javax.xml.ws.handler").with("version=2.1.0"))
-            .exporting(new ExportedPackage("javax.xml.ws.handler.soap").with("version=2.1.0"))
-            .exporting(new ExportedPackage("javax.xml.ws.http"))
-            .exporting(new ExportedPackage("javax.xml.ws.soap").with("version=2.1.0"))
-            .exporting(new ExportedPackage("javax.xml.ws.spi").with("version=2.1.0"))
-            .exporting(new ExportedPackage("javax.xml.ws.spi.http"))
-            .exporting(new ExportedPackage("javax.xml.ws.wsaddressing"))
-            .exporting(new ExportedPackage("javax.xml.xpath"))
-            .exporting(new ExportedPackage("org.ietf.jgss"))
-            .exporting(new ExportedPackage("org.omg.CORBA_2_3"))
-            .exporting(new ExportedPackage("org.omg.CORBA_2_3.portable"))
-            .exporting(new ExportedPackage("org.omg.CORBA.DynAnyPackage"))
-            .exporting(new ExportedPackage("org.omg.CORBA.ORBPackage"))
-            .exporting(new ExportedPackage("org.omg.CORBA.portable"))
-            .exporting(new ExportedPackage("org.omg.CORBA.TypeCodePackage"))
-            .exporting(new ExportedPackage("org.omg.CosNaming"))
-            .exporting(new ExportedPackage("org.omg.IOP"))
-            .exporting(new ExportedPackage("org.omg.PortableInterceptor"))
-            .exporting(new ExportedPackage("org.omg.PortableServer"))
-            .exporting(new ExportedPackage("org.osgi.framework").with("version=1.3.0"))
-            .exporting(new ExportedPackage("org.osgi.service.packageadmin").with("version=1.2.0"))
-            .exporting(new ExportedPackage("org.osgi.util.tracker"))
-            .exporting(new ExportedPackage("org.w3c.dom"))
-            .exporting(new ExportedPackage("org.w3c.dom.ls"))
-            .exporting(new ExportedPackage("org.xml.sax"))
-            .exporting(new ExportedPackage("org.xml.sax.ext"))
-            .exporting(new ExportedPackage("org.xml.sax.helpers"))
+        Module m;
+        moduleList.add((m = new Module("system.bundle"))
+            .exporting(new ExportedPackage(m, "javax.annotation").with("version=1.1.0"))
+            .exporting(new ExportedPackage(m, "javax.annotation.security").with("version=1.1.0"))
+            .exporting(new ExportedPackage(m, "javax.annotation.sql").with("version=1.1.0"))
+            .exporting(new ExportedPackage(m, "javax.activation").with("version=1.1.0"))
+            .exporting(new ExportedPackage(m, "javax.crypto"))
+            .exporting(new ExportedPackage(m, "javax.crypto.spec"))
+            .exporting(new ExportedPackage(m, "javax.imageio"))
+            .exporting(new ExportedPackage(m, "javax.imageio.stream"))
+            .exporting(new ExportedPackage(m, "javax.jws").with("version=2.0.0"))
+            .exporting(new ExportedPackage(m, "javax.jws.soap"))
+            .exporting(new ExportedPackage(m, "javax.management"))
+            .exporting(new ExportedPackage(m, "javax.management.loading"))
+            .exporting(new ExportedPackage(m, "javax.management.modelmbean"))
+            .exporting(new ExportedPackage(m, "javax.management.openmbean"))
+            .exporting(new ExportedPackage(m, "javax.management.remote"))
+            .exporting(new ExportedPackage(m, "javax.naming"))
+            .exporting(new ExportedPackage(m, "javax.naming.directory"))
+            .exporting(new ExportedPackage(m, "javax.naming.ldap"))
+            .exporting(new ExportedPackage(m, "javax.naming.spi"))
+            .exporting(new ExportedPackage(m, "javax.net"))
+            .exporting(new ExportedPackage(m, "javax.net.ssl"))
+            .exporting(new ExportedPackage(m, "javax.rmi"))
+            .exporting(new ExportedPackage(m, "javax.rmi.CORBA"))
+            .exporting(new ExportedPackage(m, "javax.security.auth"))
+            .exporting(new ExportedPackage(m, "javax.security.auth.callback"))
+            .exporting(new ExportedPackage(m, "javax.security.auth.kerberos"))
+            .exporting(new ExportedPackage(m, "javax.security.auth.login"))
+            .exporting(new ExportedPackage(m, "javax.security.auth.spi"))
+            .exporting(new ExportedPackage(m, "javax.security.auth.x500"))
+            .exporting(new ExportedPackage(m, "javax.security.cert"))
+            .exporting(new ExportedPackage(m, "javax.security.sasl"))
+            .exporting(new ExportedPackage(m, "javax.sql"))
+            .exporting(new ExportedPackage(m, "javax.swing"))
+            .exporting(new ExportedPackage(m, "javax.swing.border"))
+            .exporting(new ExportedPackage(m, "javax.swing.tree"))
+            .exporting(new ExportedPackage(m, "javax.tools"))
+            .exporting(new ExportedPackage(m, "javax.xml"))
+            .exporting(new ExportedPackage(m, "javax.xml.bind").with("version=2.2.0"))
+            .exporting(new ExportedPackage(m, "javax.xml.bind.annotation"))
+            .exporting(new ExportedPackage(m, "javax.xml.bind.annotation.adapters"))
+            .exporting(new ExportedPackage(m, "javax.xml.bind.attachment"))
+            .exporting(new ExportedPackage(m, "javax.xml.bind.helpers"))
+            .exporting(new ExportedPackage(m, "javax.xml.bind.util"))
+            .exporting(new ExportedPackage(m, "javax.xml.datatype"))
+            .exporting(new ExportedPackage(m, "javax.xml.namespace"))
+            .exporting(new ExportedPackage(m, "javax.xml.parsers"))
+            .exporting(new ExportedPackage(m, "javax.xml.registry"))
+            .exporting(new ExportedPackage(m, "javax.xml.registry.infomodel"))
+            .exporting(new ExportedPackage(m, "javax.xml.rpc").with("version=1.1.0"))
+            .exporting(new ExportedPackage(m, "javax.xml.rpc.encoding").with("version=1.1.0"))
+            .exporting(new ExportedPackage(m, "javax.xml.rpc.handler.soap").with("version=1.1.0"))
+            .exporting(new ExportedPackage(m, "javax.xml.rpc.holders").with("version=1.1.0"))
+            .exporting(new ExportedPackage(m, "javax.xml.rpc.server").with("version=1.1.0"))
+            .exporting(new ExportedPackage(m, "javax.xml.rpc.soap").with("version=1.1.0"))
+            .exporting(new ExportedPackage(m, "javax.xml.soap").with("version=1.3.0"))
+            .exporting(new ExportedPackage(m, "javax.xml.stream").with("version=1.0.0"))
+            .exporting(new ExportedPackage(m, "javax.xml.stream.events"))
+            .exporting(new ExportedPackage(m, "javax.xml.stream.util").with("version=1.0.0"))
+            .exporting(new ExportedPackage(m, "javax.xml.transform"))
+            .exporting(new ExportedPackage(m, "javax.xml.transform.dom"))
+            .exporting(new ExportedPackage(m, "javax.xml.transform.sax"))
+            .exporting(new ExportedPackage(m, "javax.xml.transform.stream"))
+            .exporting(new ExportedPackage(m, "javax.xml.validation"))
+            .exporting(new ExportedPackage(m, "javax.xml.ws").with("version=2.2.0"))
+            .exporting(new ExportedPackage(m, "javax.xml.ws.handler").with("version=2.2.0"))
+            .exporting(new ExportedPackage(m, "javax.xml.ws.handler.soap").with("version=2.2.0"))
+            .exporting(new ExportedPackage(m, "javax.xml.ws.http"))
+            .exporting(new ExportedPackage(m, "javax.xml.ws.soap").with("version=2.2.0"))
+            .exporting(new ExportedPackage(m, "javax.xml.ws.spi").with("version=2.2.0"))
+            .exporting(new ExportedPackage(m, "javax.xml.ws.spi.http"))
+            .exporting(new ExportedPackage(m, "javax.xml.ws.wsaddressing"))
+            .exporting(new ExportedPackage(m, "javax.xml.xpath"))
+            .exporting(new ExportedPackage(m, "org.ietf.jgss"))
+            .exporting(new ExportedPackage(m, "org.omg.CORBA_2_3"))
+            .exporting(new ExportedPackage(m, "org.omg.CORBA_2_3.portable"))
+            .exporting(new ExportedPackage(m, "org.omg.CORBA.DynAnyPackage"))
+            .exporting(new ExportedPackage(m, "org.omg.CORBA.ORBPackage"))
+            .exporting(new ExportedPackage(m, "org.omg.CORBA.portable"))
+            .exporting(new ExportedPackage(m, "org.omg.CORBA.TypeCodePackage"))
+            .exporting(new ExportedPackage(m, "org.omg.CosNaming"))
+            .exporting(new ExportedPackage(m, "org.omg.IOP"))
+            .exporting(new ExportedPackage(m, "org.omg.PortableInterceptor"))
+            .exporting(new ExportedPackage(m, "org.omg.PortableServer"))
+            .exporting(new ExportedPackage(m, "org.osgi.framework").with("version=1.4.0"))
+            .exporting(new ExportedPackage(m, "org.osgi.service.packageadmin").with("version=1.2.0"))
+            .exporting(new ExportedPackage(m, "org.osgi.service.url").with("version=1.0.0"))
+            .exporting(new ExportedPackage(m, "org.osgi.util.tracker"))
+            .exporting(new ExportedPackage(m, "org.w3c.dom"))
+            .exporting(new ExportedPackage(m, "org.w3c.dom.ls"))
+            .exporting(new ExportedPackage(m, "org.xml.sax"))
+            .exporting(new ExportedPackage(m, "org.xml.sax.ext"))
+            .exporting(new ExportedPackage(m, "org.xml.sax.helpers"))
 // Optional
-            .exporting(new ExportedPackage("com.sun.enterprise.security.jauth.callback"))
-            .exporting(new ExportedPackage("com.sun.enterprise.web.connector.grizzly.algorithms"))
-            .exporting(new ExportedPackage("com.sun.enterprise.web.connector.grizzly.ssl"))
-            .exporting(new ExportedPackage("com.sun.enterprise.web.portunif"))
-            .exporting(new ExportedPackage("com.sun.enterprise.web.portunif.util"))
-            .exporting(new ExportedPackage("com.sun.enterprise.webservice.monitoring"))
-            .exporting(new ExportedPackage("com.sun.msv.datatype"))
-            .exporting(new ExportedPackage("com.sun.msv.datatype.xsd"))
-            .exporting(new ExportedPackage("com.sun.msv.grammar"))
-            .exporting(new ExportedPackage("com.sun.msv.grammar.trex"))
-            .exporting(new ExportedPackage("com.sun.msv.reader"))
-            .exporting(new ExportedPackage("com.sun.msv.reader.trex.ng"))
-            .exporting(new ExportedPackage("com.sun.msv.reader.util"))
-            .exporting(new ExportedPackage("com.sun.msv.util"))
-            .exporting(new ExportedPackage("com.sun.msv.verifier"))
-            .exporting(new ExportedPackage("com.sun.msv.verifier.regexp"))
-            .exporting(new ExportedPackage("com.sun.xml.bind.serializer"))
-            .exporting(new ExportedPackage("com.sun.xml.bind.validator"))
-            .exporting(new ExportedPackage("com.sun.xml.ws.spi.runtime"))
-            .exporting(new ExportedPackage("org.apache.coyote"))
-            .exporting(new ExportedPackage("org.apache.log4j"))
-            .exporting(new ExportedPackage("org.apache.tomcat.util.buf"))
-            .exporting(new ExportedPackage("org.iso_relax.verifier.impl"))
+            .exporting(new ExportedPackage(m, "com.sun.enterprise.security.jauth.callback"))
+            .exporting(new ExportedPackage(m, "com.sun.enterprise.web.connector.grizzly.algorithms"))
+            .exporting(new ExportedPackage(m, "com.sun.enterprise.web.connector.grizzly.ssl"))
+            .exporting(new ExportedPackage(m, "com.sun.enterprise.web.portunif"))
+            .exporting(new ExportedPackage(m, "com.sun.enterprise.web.portunif.util"))
+            .exporting(new ExportedPackage(m, "com.sun.enterprise.webservice.monitoring"))
+            .exporting(new ExportedPackage(m, "com.sun.mail.util"))
+            .exporting(new ExportedPackage(m, "com.sun.msv.datatype"))
+            .exporting(new ExportedPackage(m, "com.sun.msv.datatype.xsd"))
+            .exporting(new ExportedPackage(m, "com.sun.msv.grammar"))
+            .exporting(new ExportedPackage(m, "com.sun.msv.grammar.trex"))
+            .exporting(new ExportedPackage(m, "com.sun.msv.reader"))
+            .exporting(new ExportedPackage(m, "com.sun.msv.reader.trex.ng"))
+            .exporting(new ExportedPackage(m, "com.sun.msv.reader.util"))
+            .exporting(new ExportedPackage(m, "com.sun.msv.util"))
+            .exporting(new ExportedPackage(m, "com.sun.msv.verifier"))
+            .exporting(new ExportedPackage(m, "com.sun.msv.verifier.regexp"))
+            .exporting(new ExportedPackage(m, "com.sun.xml.bind.serializer"))
+            .exporting(new ExportedPackage(m, "com.sun.xml.bind.validator"))
+            .exporting(new ExportedPackage(m, "com.sun.xml.ws.spi.runtime"))
+            .exporting(new ExportedPackage(m, "org.apache.coyote"))
+            .exporting(new ExportedPackage(m, "org.apache.log4j"))
+            .exporting(new ExportedPackage(m, "org.apache.tomcat.util.buf"))
+            .exporting(new ExportedPackage(m, "org.iso_relax.verifier.impl"))
         );
 
         Module maxImporter = null;
@@ -197,6 +204,7 @@
             {
 //                Map headerMap = new StringMap(getMainAttributes(jarFile), false);
                 Map headerMap = new StringMap(jarFile.getManifest().getMainAttributes(), false);
+//compareMaps(headerMap, manifestMap);
                 if (headerMap.containsKey("Bundle-SymbolicName"))
                 {
                     ManifestParser mp = new ManifestParser(headerMap);
@@ -221,7 +229,7 @@
         System.out.println("Generated " + moduleList.size() + " modules.");
         System.out.println("Max importer: " + maxImporter + " (" + maxImporter.getImports().size() + ")");
 
-        Resolver resolver;
+        Resolver resolver = null;
         if (choice.equals("-legacy"))
         {
             resolver = new FelixResolver(moduleList);
@@ -235,6 +243,8 @@
         {
             long starttime = System.currentTimeMillis();
             Map<Module, List<Wire>> wireMap = resolver.resolve(maxImporter);
+//            Map<Module, List<Wire>> wireMap = resolver.resolve(resolver.getModule("com.sun.enterprise.hk2-core"));
+//            Map<Module, List<Wire>> wireMap = resolver.resolve(resolver.getModule("com.sun.pkg.client"));
             long endtime = System.currentTimeMillis();
             System.out.println("Resolve time: " + (endtime - starttime));
             System.out.println("Modules resolved: " + wireMap.size());
@@ -409,7 +419,7 @@
                 throw new RuntimeException("Unsupported capability: " + caps[capIdx].getNamespace());
             }
 
-            ExportedPackage ep = new ExportedPackage(caps[capIdx].getPackageName());
+            ExportedPackage ep = new ExportedPackage(module, caps[capIdx].getPackageName());
 
             R4Directive[] dirs = caps[capIdx].getDirectives();
             for (int dirIdx = 0; (dirs != null) && (dirIdx < dirs.length); dirIdx++)

Modified: felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/prototype/ProtoResolver.java
URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/prototype/ProtoResolver.java?rev=835915&r1=835914&r2=835915&view=diff
==============================================================================
--- felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/prototype/ProtoResolver.java (original)
+++ felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/prototype/ProtoResolver.java Fri Nov 13 16:50:35 2009
@@ -21,18 +21,34 @@
 import org.apache.felix.resolver.*;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Set;
+import org.apache.felix.resolver.cs.Capability;
+import org.apache.felix.resolver.cs.CapabilitySet;
 
 public class ProtoResolver implements Resolver
 {
     private final List<Module> m_moduleList;
+    private final CapabilitySet m_pkgCapSet;
 
     public ProtoResolver(List<Module> moduleList)
     {
         m_moduleList = moduleList;
+        List indices = new ArrayList();
+        indices.add("package");
+        m_pkgCapSet = new CapabilitySet(indices);
+        for (int modIdx = 0; modIdx < m_moduleList.size(); modIdx++)
+        {
+            List<ExportedPackage> exports = m_moduleList.get(modIdx).getExports();
+            for (int expIdx = 0; expIdx < exports.size(); expIdx++)
+            {
+                m_pkgCapSet.addCapability(exports.get(expIdx));
+            }
+        }
     }
 
     public Module getModule(String name)
@@ -48,26 +64,26 @@
     }
 
     private Module m_rootModule = null;
-    private List<Map<ImportedPackage, List<Module>>> m_candidatePermutations
-        = new ArrayList<Map<ImportedPackage, List<Module>>>();
+    private List<Map<ImportedPackage, Set<Capability>>> m_candidatePermutations
+        = new ArrayList<Map<ImportedPackage, Set<Capability>>>();
 
     public Map<Module, List<Wire>> resolve(Module module)
     {
 System.out.println("+++ PROTO RESOLVER");
-        Map<ImportedPackage, List<Module>> candidateMap = resolve(
+        Map<ImportedPackage, Set<Capability>> candidateMap = resolve(
             module,
             null,
             module,
-            new HashMap<ImportedPackage, List<Module>>(),
+            new HashMap<ImportedPackage, Set<Capability>>(),
             new HashMap<String, Blame>(),
             new HashMap<Module, Boolean>());
 
         return populateWireMap(module, candidateMap, new HashMap<Module, List<Wire>>());
     }
 
-    private Map<ImportedPackage, List<Module>> resolve(
+    private Map<ImportedPackage, Set<Capability>> resolve(
         Module module, ImportedPackage ipGoal, Module blameModule,
-        Map<ImportedPackage, List<Module>> candidateMap,
+        Map<ImportedPackage, Set<Capability>> candidateMap,
         Map<String, Blame> existingConstraints,
         Map<Module, Boolean> cycleMap)
     {
@@ -110,11 +126,11 @@
                         // those instead, otherwise find the matching providers.
                         if (candidateMap.get(imports.get(impIdx)) == null)
                         {
-                            List<Module> exporters = findExporters(imports.get(impIdx));
+                            Set<Capability> exporters = findExporters(imports.get(impIdx));
                             if (exporters.size() == 0)
                             {
                                 throw new RuntimeException("Unable to resolve " + module
-                                    + ": missing requirement " + imports.get(impIdx).getName());
+                                    + ": missing requirement " + imports.get(impIdx));
                             }
                             candidateMap.put(imports.get(impIdx), exporters);
                         }
@@ -132,106 +148,112 @@
                     boolean repeat;
                     do
                     {
-                        repeat = false;
-
-                        // Loop through all of the target module's imports and v
-                        for (int impIdx = 0; impIdx < imports.size(); impIdx++)
+                        do
                         {
-                            // Get the current candidate for the import.
-                            List<Module> exporters = candidateMap.get(imports.get(impIdx));
-                            // If the current candidate is already resolved, then try
-                            // to merge packages while verifying constraints.
-                            if (exporters.get(0).isResolved())
-                            {
-                                // HERE WE WILL NEED TO TRY TO MERGE THE CANDIDATE.
-                            }
-                            // If the current candidate is not resolved, then try to resolve
-                            // it, which will also merge packages while verify constraints.
-                            else if (!exporters.get(0).isResolved())
+                            repeat = false;
+
+                            // Loop through all of the target module's imports and v
+                            for (int impIdx = 0; impIdx < imports.size(); impIdx++)
                             {
-                                try
+                                // Get the current candidate for the import.
+                                Set<Capability> exporters = candidateMap.get(imports.get(impIdx));
+                                // If the current candidate is already resolved, then try
+                                // to merge packages while verifying constraints.
+                                Iterator<Capability> itExporters = exporters.iterator();
+                                Capability cap = itExporters.next();
+                                if (cap.getModule().isResolved())
                                 {
-                                    resolve(
-                                        exporters.get(0),
-                                        imports.get(impIdx),
-                                        module,
-                                        candidateMap,
-                                        currentConstraints,
-                                        cycleMap);
+                                    // HERE WE WILL NEED TO TRY TO MERGE THE CANDIDATE.
                                 }
-                                // If we have a constraint conflict, then the current candidate
-                                // should be removed, since it conflicts with an existing choice.
-                                // If we are at the root, we should try the next permutated
-                                // candidate map if possible.
-                                catch (ResolverConflictException ex)
+                                // If the current candidate is not resolved, then try to resolve
+                                // it, which will also merge packages while verify constraints.
+                                else if (!cap.getModule().isResolved())
                                 {
+                                    try
+                                    {
+                                        resolve(
+                                            cap.getModule(),
+                                            imports.get(impIdx),
+                                            module,
+                                            candidateMap,
+                                            currentConstraints,
+                                            cycleMap);
+                                    }
+                                    // If we have a constraint conflict, then the current candidate
+                                    // should be removed, since it conflicts with an existing choice.
+                                    // If we are at the root, we should try the next permutated
+                                    // candidate map if possible.
+                                    catch (ResolverConflictException ex)
+                                    {
 //System.out.println("RCE " + ex);
 //System.out.println("Current candidate map   : " + candidateMap);
-                                    // Remove offending candidate.
-                                    exporters.remove(0);
+                                        // Remove offending candidate.
+                                        itExporters.remove();
 //System.out.println("Updated candidate map   : " + candidateMap);
-                                    if (exporters.size() == 0)
-                                    {
-                                        // TODO: PROTO RESOLVER - Maybe this should be moved.
-                                        if ((module == m_rootModule) && (m_candidatePermutations.size() > 0))
+                                        if (!itExporters.hasNext())
                                         {
-                                            currentConstraints.clear();
-                                            currentConstraints.putAll(currentConstraintsOrig);
-                                            candidateMap = m_candidatePermutations.remove(0);
-System.out.println("+++ TRYING ALTERNATIVE: " + candidateMap);
-                                            repeat = true;
+                                            // TODO: PROTO RESOLVER - Maybe this should be moved.
+                                            if ((module == m_rootModule) && (m_candidatePermutations.size() > 0))
+                                            {
+                                                currentConstraints.clear();
+                                                currentConstraints.putAll(currentConstraintsOrig);
+                                                candidateMap = m_candidatePermutations.remove(0);
+//System.out.println("+++ TRYING ALTERNATIVE: " + candidateMap);
+                                                repeat = true;
+                                            }
+                                            else
+                                            {
+                                                candidateMap.remove(imports.get(impIdx));
+                                                throw new ResolveException("Unresolved constraint "
+                                                    + imports.get(impIdx)
+                                                    + " in " + module);
+                                            }
                                         }
                                         else
                                         {
-                                            candidateMap.remove(imports.get(impIdx));
-                                            throw new ResolveException("Unresolved constraint "
-                                                + imports.get(impIdx)
-                                                + " in " + module);
+                                            repeat = true;
                                         }
+                                        break;
                                     }
-                                    else
+                                    // If we cannot resolve the candidate, then the current candidate
+                                    // should be removed. If we are at the root, we should try the
+                                    // next permutated candidate map if possible.
+                                    catch (ResolveException ex)
                                     {
-                                        repeat = true;
-                                    }
-                                    break;
-                                }
-                                // If we cannot resolve the candidate, then the current candidate
-                                // should be removed. If we are at the root, we should try the
-                                // next permutated candidate map if possible.
-                                catch (ResolveException ex)
-                                {
-System.out.println("RE " + ex);
+//System.out.println("RE " + ex);
 //System.out.println("Current candidate map   : " + candidateMap);
-                                    // Remove offending candidate.
-                                    exporters.remove(0);
+                                        // Remove offending candidate.
+                                        itExporters.remove();
 //System.out.println("Updated candidate map   : " + candidateMap);
-                                    if (exporters.size() == 0)
-                                    {
-                                        // TODO: PROTO RESOLVER - Maybe this should be moved.
-                                        if ((module == m_rootModule) && (m_candidatePermutations.size() > 0))
+                                        if (!itExporters.hasNext())
                                         {
-                                            currentConstraints.clear();
-                                            currentConstraints.putAll(currentConstraintsOrig);
-                                            candidateMap = m_candidatePermutations.remove(0);
-System.out.println("+++ TRYING ALTERNATIVE: " + candidateMap);
-                                            repeat = true;
+                                            // TODO: PROTO RESOLVER - Maybe this should be moved.
+                                            if ((module == m_rootModule) && (m_candidatePermutations.size() > 0))
+                                            {
+                                                currentConstraints.clear();
+                                                currentConstraints.putAll(currentConstraintsOrig);
+                                                candidateMap = m_candidatePermutations.remove(0);
+//System.out.println("+++ TRYING ALTERNATIVE: " + candidateMap);
+                                                repeat = true;
+                                            }
+                                            else
+                                            {
+                                                candidateMap.remove(imports.get(impIdx));
+                                                throw new ResolveException("Unresolved constraint "
+                                                    + imports.get(impIdx)
+                                                    + " in " + module);
+                                            }
                                         }
                                         else
                                         {
-                                            candidateMap.remove(imports.get(impIdx));
-                                            throw new ResolveException("Unresolved constraint "
-                                                + imports.get(impIdx)
-                                                + " in " + module);
+                                            repeat = true;
                                         }
+                                        break;
                                     }
-                                    else
-                                    {
-                                        repeat = true;
-                                    }
-                                    break;
                                 }
                             }
                         }
+                        while (repeat);
                     }
                     while (repeat);
 
@@ -244,7 +266,7 @@
                         it.hasNext(); )
                     {
                         Entry<String, Blame> entry = it.next();
-                        if ((ipGoal == null) || ipGoal.isSatistfiedBy(entry.getValue().m_exportedPackage))
+                        if ((ipGoal == null) || m_pkgCapSet.matches(entry.getValue().m_exportedPackage, ipGoal.getFilter()))
                         {
                             Blame blame = existingConstraints.get(entry.getKey());
                             if ((blame != null)
@@ -284,25 +306,12 @@
         }
     }
 
-    private List<Module> findExporters(ImportedPackage ip)
+    private Set<Capability> findExporters(ImportedPackage ip)
     {
-        List<Module> exporters = new ArrayList<Module>();
-        for (int modIdx = 0; modIdx < m_moduleList.size(); modIdx++)
-        {
-            List<ExportedPackage> exports = m_moduleList.get(modIdx).getExports();
-            for (int expIdx = 0; expIdx < exports.size(); expIdx++)
-            {
-                if (ip.isSatistfiedBy(exports.get(expIdx)))
-                {
-                    exporters.add(m_moduleList.get(modIdx));
-                    break;
-                }
-            }
-        }
-        return exporters;
+        return m_pkgCapSet.match(ip.getFilter());
     }
 
-    private static Map<String, Blame> calculatePackageConstraints(
+    private Map<String, Blame> calculatePackageConstraints(
         Module module, Module blameModule)
     {
         Map<String, Blame> pkgMap = new HashMap<String, Blame>();
@@ -313,18 +322,18 @@
             //       exports that the import will overlap the export.
             if (!hasOverlappingImport(module, exports.get(i)))
             {
-                pkgMap.put(exports.get(i).getName(), new Blame(module, exports.get(i), blameModule));
+                pkgMap.put(exports.get(i).getName(), new Blame(exports.get(i), blameModule));
             }
         }
         return pkgMap;
     }
 
-    private static boolean hasOverlappingImport(Module module, ExportedPackage ep)
+    private boolean hasOverlappingImport(Module module, ExportedPackage ep)
     {
         List<ImportedPackage> imports = module.getImports();
         for (int i = 0; i < imports.size(); i++)
         {
-            if (imports.get(i).getName().equals(ep.getName()))
+            if (m_pkgCapSet.matches(ep, imports.get(i).getFilter()))
             {
                 return true;
             }
@@ -333,7 +342,7 @@
     }
 
     private void checkConsistency(
-        Module targetModule, ImportedPackage ipGoal, Map<ImportedPackage, List<Module>> candidateMap,
+        Module targetModule, ImportedPackage ipGoal, Map<ImportedPackage, Set<Capability>> candidateMap,
         Map<String, Blame> existingConstraints, Map<String, Blame> currentConstraints)
         
         throws ResolverConflictException
@@ -347,7 +356,7 @@
         {
             Entry<String, Blame> entry = it.next();
             Blame current = entry.getValue();
-            if ((ipGoal == null) || ipGoal.isSatistfiedBy(current.m_exportedPackage))
+            if ((ipGoal == null) || m_pkgCapSet.matches(current.m_exportedPackage, ipGoal.getFilter()))
             {
                 verifyUses(targetModule, entry.getKey(),
                     candidateMap, existingConstraints, currentConstraints,
@@ -359,7 +368,7 @@
     }
 
     private void verifyUses(Module targetModule, String pkgName,
-        Map<ImportedPackage, List<Module>> candidateMap,
+        Map<ImportedPackage, Set<Capability>> candidateMap,
         Map<String, Blame> existingConstraints,
         Map<String, Blame> currentConstraints, Map<String, List<Module>> cycleMap)
     {
@@ -385,9 +394,11 @@
         }
 
         if ((existing != null)
-            && !existing.m_provider.equals(current.m_provider))
+            && !existing.m_exportedPackage.equals(current.m_exportedPackage))
         {
-            Map<ImportedPackage, List<Module>> candidateMapCopy = copyCandidateMap(candidateMap);
+//System.out.println("Conflict: " + existing.m_exportedPackage + " WITH " + current.m_exportedPackage);
+//System.out.println("Blame: " + existing.m_blameModules + " AND " + current.m_blameModules);
+            Map<ImportedPackage, Set<Capability>> candidateMapCopy = copyCandidateMap(candidateMap);
             boolean modified = false;
             boolean invalid = false;
             for (int blameIdx = 0; blameIdx < existing.m_blameModules.size(); blameIdx++)
@@ -404,13 +415,20 @@
                     // TODO: PROTO RESOLVER - This comment out part is too broad, since it removes
                     // the conflicting module from all candidate lists.
                     // Works for scenario 1, but fails for scenario 9.
-                    if (candidateMapCopy.get(blameImports.get(impIdx)).contains(existing.m_provider))
+//System.out.println("CHECKING " + candidateMapCopy.get(blameImports.get(impIdx)) + " FOR " + existing.m_exportedPackage);
+                    for (Iterator<Capability> cmIt =
+                            candidateMapCopy.get(blameImports.get(impIdx)).iterator();
+                        cmIt.hasNext(); )
                     {
-                        candidateMapCopy.get(blameImports.get(impIdx)).remove(existing.m_provider);
-                        modified = true;
-                        if (candidateMapCopy.get(blameImports.get(impIdx)).size() == 0)
+                        if (cmIt.next().getModule().equals(existing.m_exportedPackage.getModule()))
                         {
-                            invalid = true;
+                            cmIt.remove();
+                            modified = true;
+                            if (candidateMapCopy.get(blameImports.get(impIdx)).size() == 0)
+                            {
+                                invalid = true;
+                            }
+                            break;
                         }
                     }
                 }
@@ -448,15 +466,15 @@
         }
     }
 
-    private static Map<ImportedPackage, List<Module>> copyCandidateMap(
-        Map<ImportedPackage, List<Module>> candidateMap)
+    private static Map<ImportedPackage, Set<Capability>> copyCandidateMap(
+        Map<ImportedPackage, Set<Capability>> candidateMap)
     {
-        Map<ImportedPackage, List<Module>> copy = new HashMap<ImportedPackage, List<Module>>();
-        for (Iterator<Entry<ImportedPackage, List<Module>>> it = candidateMap.entrySet().iterator();
+        Map<ImportedPackage, Set<Capability>> copy = new HashMap<ImportedPackage, Set<Capability>>();
+        for (Iterator<Entry<ImportedPackage, Set<Capability>>> it = candidateMap.entrySet().iterator();
             it.hasNext(); )
         {
-            Entry<ImportedPackage, List<Module>> entry = it.next();
-            copy.put(entry.getKey(), new ArrayList(entry.getValue()));
+            Entry<ImportedPackage, Set<Capability>> entry = it.next();
+            copy.put(entry.getKey(), new HashSet(entry.getValue()));
         }
         return copy;
     }
@@ -500,7 +518,7 @@
     }
 
     private static Map<Module, List<Wire>> populateWireMap(
-        Module module, Map<ImportedPackage, List<Module>> candidateMap,
+        Module module, Map<ImportedPackage, Set<Capability>> candidateMap,
         Map<Module, List<Wire>> wireMap)
     {
         if (wireMap.get(module) == null)
@@ -511,47 +529,32 @@
             List<ImportedPackage> imports = module.getImports();
             for (int i = 0; i < imports.size(); i++)
             {
-                Module provider = candidateMap.get(imports.get(i)).get(0);
-                if (!provider.isResolved())
+                Capability cap = candidateMap.get(imports.get(i)).iterator().next();
+                if (!cap.getModule().isResolved())
                 {
-                    populateWireMap(candidateMap.get(imports.get(i)).get(0), candidateMap, wireMap);
+                    populateWireMap(cap.getModule(), candidateMap, wireMap);
                 }
 
                 // Ignore modules that import themselves.
-                if (!module.equals(provider))
+                if (!module.equals(cap.getModule()))
                 {
                     moduleWires.add(
                         new Wire(module,
                             imports.get(i),
-                            provider,
-                            getMatchingExport(provider, imports.get(i))));
+                            cap.getModule(),
+                            (ExportedPackage) cap));
                 }
             }
         }
         return wireMap;
     }
 
-    private static ExportedPackage getMatchingExport(Module exporter, ImportedPackage ip)
-    {
-        List<ExportedPackage> exports = exporter.getExports();
-        for (int i = 0; i < exports.size(); i++)
-        {
-            if (ip.isSatistfiedBy(exports.get(i)))
-            {
-                return exports.get(i);
-            }
-        }
-        return null;
-    }
-
     public static class Blame
     {
-        public final Module m_provider;
         public final ExportedPackage m_exportedPackage;
         public final List<Module> m_blameModules;
-        public Blame(Module provider, ExportedPackage exportedPackage, Module blameModule)
+        public Blame(ExportedPackage exportedPackage, Module blameModule)
         {
-            m_provider = provider;
             m_exportedPackage = exportedPackage;
             m_blameModules = new ArrayList<Module>();
             m_blameModules.add(blameModule);
@@ -559,7 +562,7 @@
 
         public String toString()
         {
-            return m_provider + " {Blamed on " + m_blameModules + "}";
+            return m_exportedPackage.getModule() + " {Blamed on " + m_blameModules + "}";
         }
     }
 



Mime
View raw message