Return-Path: Delivered-To: apmail-felix-commits-archive@www.apache.org Received: (qmail 82592 invoked from network); 7 Jan 2010 22:24:18 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 7 Jan 2010 22:24:18 -0000 Received: (qmail 61278 invoked by uid 500); 7 Jan 2010 22:24:18 -0000 Delivered-To: apmail-felix-commits-archive@felix.apache.org Received: (qmail 61210 invoked by uid 500); 7 Jan 2010 22:24:17 -0000 Mailing-List: contact commits-help@felix.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@felix.apache.org Delivered-To: mailing list commits@felix.apache.org Received: (qmail 61201 invoked by uid 99); 7 Jan 2010 22:24:17 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 07 Jan 2010 22:24:17 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 07 Jan 2010 22:24:15 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id B0B4C2388978; Thu, 7 Jan 2010 22:23:55 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r897034 - in /felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver: ./ cs/ felix/ manifestparser/ proto2/ proto3/ prototype/ Date: Thu, 07 Jan 2010 22:23:55 -0000 To: commits@felix.apache.org From: rickhall@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100107222355.B0B4C2388978@eris.apache.org> Author: rickhall Date: Thu Jan 7 22:23:53 2010 New Revision: 897034 URL: http://svn.apache.org/viewvc?rev=897034&view=rev Log: Factored out the resolver state. Added: felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/felix/FelixResolver.java - copied, changed from r896920, felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/felix/FelixResolverImpl.java Removed: felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/felix/FelixResolverImpl.java Modified: felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Main.java felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Main2.java felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Resolver.java felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/cs/CapabilitySet.java felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/manifestparser/Main.java felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/proto2/Proto2Resolver.java felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/proto3/Proto3Resolver.java felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/prototype/ProtoResolver.java Modified: felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Main.java URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Main.java?rev=897034&r1=897033&r2=897034&view=diff ============================================================================== --- felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Main.java (original) +++ felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Main.java Thu Jan 7 22:23:53 2010 @@ -25,6 +25,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import org.apache.felix.resolver.Resolver.ResolverState; import org.apache.felix.resolver.cs.Capability; import org.apache.felix.resolver.felix.FelixResolver; import org.apache.felix.resolver.proto3.Proto3Resolver; @@ -64,24 +65,26 @@ List moduleList = new ArrayList(); Module targetModule = setupScenario(moduleList, scenario); + ResolverState state = new ResolverStateImpl(moduleList); Resolver resolver = null; + if (legacy) { - resolver = new FelixResolver(moduleList); + resolver = new FelixResolver(); } else if (proto) { - resolver = new ProtoResolver(moduleList); + resolver = new ProtoResolver(); } else { - resolver = new Proto3Resolver(moduleList); + resolver = new Proto3Resolver(); } try { long starttime = System.currentTimeMillis(); - Map> wireMap = resolver.resolve(targetModule); + Map> wireMap = resolver.resolve(state, targetModule); long endtime = System.currentTimeMillis(); System.out.println("Resolve time: " + (endtime - starttime)); System.out.println("Wires:"); Modified: felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Main2.java URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Main2.java?rev=897034&r1=897033&r2=897034&view=diff ============================================================================== --- felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Main2.java (original) +++ felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Main2.java Thu Jan 7 22:23:53 2010 @@ -25,6 +25,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import org.apache.felix.resolver.Resolver.ResolverState; import org.apache.felix.resolver.cs.Capability; import org.apache.felix.resolver.felix.FelixResolver; import org.apache.felix.resolver.prototype.ProtoResolver; @@ -58,14 +59,15 @@ List moduleList = new ArrayList(); List targets = setupScenario(moduleList, scenario); - Resolver resolver = (legacy) ? new FelixResolver(moduleList) : new ProtoResolver(moduleList); + ResolverState state = new ResolverStateImpl(moduleList); + Resolver resolver = (legacy) ? new FelixResolver() : new ProtoResolver(); try { for (Module target: targets) { long starttime = System.currentTimeMillis(); - Map> wireMap = resolver.resolve(target); + Map> wireMap = resolver.resolve(state, target); long endtime = System.currentTimeMillis(); System.out.println("Resolve time: " + (endtime - starttime)); System.out.println("Wires:"); Modified: felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Resolver.java URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Resolver.java?rev=897034&r1=897033&r2=897034&view=diff ============================================================================== --- felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Resolver.java (original) +++ felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Resolver.java Thu Jan 7 22:23:53 2010 @@ -20,8 +20,17 @@ import java.util.List; import java.util.Map; +import java.util.Set; +import org.apache.felix.resolver.cs.Capability; +import org.apache.felix.resolver.cs.Requirement; public interface Resolver { - Map> resolve(Module module); + Map> resolve(ResolverState state, Module module); + + public static interface ResolverState + { + List getModules(); + Set getCandidates(Module module, Requirement req); + } } \ No newline at end of file Modified: felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/cs/CapabilitySet.java URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/cs/CapabilitySet.java?rev=897034&r1=897033&r2=897034&view=diff ============================================================================== --- felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/cs/CapabilitySet.java (original) +++ felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/cs/CapabilitySet.java Thu Jan 7 22:23:53 2010 @@ -191,7 +191,7 @@ private static final Class[] STRING_CLASS = new Class[] { String.class }; - private Object coerceType(Comparable lhs, String rhs) + private static Object coerceType(Comparable lhs, String rhs) { if (lhs instanceof String) { @@ -232,7 +232,7 @@ return rhsComparable; } - public boolean matches(Capability cap, SimpleFilter sf) + public static boolean matches(Capability cap, SimpleFilter sf) { boolean matched = true; Copied: felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/felix/FelixResolver.java (from r896920, 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/FelixResolver.java?p2=felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/felix/FelixResolver.java&p1=felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/felix/FelixResolverImpl.java&r1=896920&r2=897034&rev=897034&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/FelixResolver.java Thu Jan 7 22:23:53 2010 @@ -19,14 +19,18 @@ package org.apache.felix.resolver.felix; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; import org.apache.felix.resolver.Module; import org.apache.felix.resolver.ResolveException; +import org.apache.felix.resolver.Resolver; +import org.apache.felix.resolver.Resolver.ResolverState; import org.apache.felix.resolver.Wire; import org.apache.felix.resolver.cs.Capability; import org.apache.felix.resolver.cs.Requirement; @@ -34,13 +38,14 @@ import org.apache.felix.resolver.manifestparser.FelixRequirement; import org.apache.felix.resolver.manifestparser.R4Directive; -public class FelixResolverImpl +public class FelixResolver implements Resolver { // Reusable empty array. private static final Wire[] m_emptyWires = new Wire[0]; - public FelixResolverImpl() + public FelixResolver() { +System.out.println("+++ FELIX RESOLVER"); } // Returns a map of resolved bundles where the key is the module @@ -93,7 +98,16 @@ // to fire resolved events outside of the synchronized block. // The resolved module wire map maps a module to its array of // wires. - return populateWireMap(state, candidatesMap, rootModule, new HashMap()); + Map wireMap = populateWireMap(state, candidatesMap, rootModule, new HashMap()); + Map> newWireMap = new HashMap>(); + for (Iterator it = wireMap.entrySet().iterator(); it.hasNext(); ) + { + Map.Entry entry = (Map.Entry) it.next(); + Module m = (Module) entry.getKey(); + Wire[] wires = (Wire[]) entry.getValue(); + newWireMap.put(m, Arrays.asList(wires)); + } + return newWireMap; } private void populateCandidatesMap( @@ -122,8 +136,8 @@ // package maps. The "resolved" candidates have higher priority // than "unresolved" ones, so put the "resolved" candidates // at the front of the list of candidates. - List candidates = state.getResolvedCandidates(reqs.get(reqIdx)); - candidates.addAll(state.getUnresolvedCandidates(reqs.get(reqIdx))); + Set candidateSet = state.getCandidates(targetModule, reqs.get(reqIdx)); + List candidates = new ArrayList(candidateSet); // If we have candidates, then we need to recursively populate // the resolver map with each of them. @@ -1207,19 +1221,4 @@ return wireMap; } - - // - // Utility methods. - // - - // - // Inner classes. - // - - public static interface ResolverState - { - Module[] getModules(); - List getResolvedCandidates(Requirement req); - List getUnresolvedCandidates(Requirement req); - } } \ No newline at end of file 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=897034&r1=897033&r2=897034&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 Thu Jan 7 22:23:53 2010 @@ -34,6 +34,8 @@ import org.apache.felix.resolver.RequirementImpl; import org.apache.felix.resolver.Module; import org.apache.felix.resolver.Resolver; +import org.apache.felix.resolver.Resolver.ResolverState; +import org.apache.felix.resolver.ResolverStateImpl; import org.apache.felix.resolver.Wire; import org.apache.felix.resolver.cs.Capability; import org.apache.felix.resolver.cs.Requirement; @@ -335,29 +337,32 @@ System.out.println("Generated " + moduleList.size() + " modules."); + ResolverState state = new ResolverStateImpl(moduleList); Resolver resolver = null; + if (legacy) { - resolver = new FelixResolver(moduleList); + resolver = new FelixResolver(); } else if (proto) { - resolver = new ProtoResolver(moduleList); + resolver = new ProtoResolver(); } else { - resolver = new Proto3Resolver(moduleList); + resolver = new Proto3Resolver(); } try { - for (Module target = getTarget(moduleList); - target != null; - target = getTarget(moduleList)) +Module target = getTarget(moduleList); +// for (Module target = getTarget(moduleList); +// target != null; +// target = getTarget(moduleList)) { System.out.println("Target: " + target + " (" + target.getRequirements().size() + ")"); long starttime = System.currentTimeMillis(); - Map> wireMap = resolver.resolve(target); + Map> wireMap = resolver.resolve(state, target); long endtime = System.currentTimeMillis(); System.out.println("Resolve time: " + (endtime - starttime)); System.out.println("Modules resolved: " + wireMap.size()); @@ -374,7 +379,7 @@ for (int i = 0; i < entry.getValue().size(); i++) { // System.out.println(" " + entry.getValue().get(i)); -// System.out.println(" " + entry.getKey() + " - " + entry.getValue().get(i)); + System.out.println(" " + entry.getKey() + " - " + entry.getValue().get(i)); } } } @@ -703,7 +708,7 @@ private static RequirementImpl convertToRequirement(Module module, FelixRequirement req) { - RequirementImpl ip = new RequirementImpl(req.getNamespace()); + RequirementImpl ri = new RequirementImpl(req.getNamespace()); R4Directive[] dirs = req.getDirectives(); for (int dirIdx = 0; (dirs != null) && (dirIdx < dirs.length); dirIdx++) @@ -714,16 +719,16 @@ } if (dirs[dirIdx].getValue().equals(Constants.RESOLUTION_OPTIONAL)) { - ip.optional(); + ri.optional(); } } R4Attribute[] attrs = req.getAttributes(); for (int attrIdx = 0; (attrs != null) && (attrIdx < attrs.length); attrIdx++) { - ip.with(attrs[attrIdx].getName() + "=" + attrs[attrIdx].getValue()); + ri.with(attrs[attrIdx].getName() + "=" + attrs[attrIdx].getValue()); } - return ip; + return ri; } } \ No newline at end of file Modified: felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/proto2/Proto2Resolver.java URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/proto2/Proto2Resolver.java?rev=897034&r1=897033&r2=897034&view=diff ============================================================================== --- felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/proto2/Proto2Resolver.java (original) +++ felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/proto2/Proto2Resolver.java Thu Jan 7 22:23:53 2010 @@ -42,48 +42,18 @@ // 3. Uses constraints cannot conflict with other uses constraints, only with hard constraints. public class Proto2Resolver implements Resolver { - private final List m_modules; - private final CapabilitySet m_pkgCapSet; - private final CapabilitySet m_modCapSet; - private static final Map m_invokeCounts = new HashMap(); private static boolean m_isInvokeCount = false; - public Proto2Resolver(List modules) + public Proto2Resolver() { System.out.println("+++ PROTO2 RESOLVER"); - m_modules = modules; - - List indices = new ArrayList(); - indices.add(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE); - m_modCapSet = new CapabilitySet(indices); - - indices = new ArrayList(); - indices.add(Capability.PACKAGE_ATTR); - m_pkgCapSet = new CapabilitySet(indices); - - for (int modIdx = 0; modIdx < m_modules.size(); modIdx++) - { - List caps = m_modules.get(modIdx).getCapabilities(); - for (int capIdx = 0; capIdx < caps.size(); capIdx++) - { - if (caps.get(capIdx).getNamespace().equals(Capability.MODULE_NAMESPACE)) - { - m_modCapSet.addCapability(caps.get(capIdx)); - } - else if (caps.get(capIdx).getNamespace().equals(Capability.PACKAGE_NAMESPACE)) - { - m_pkgCapSet.addCapability(caps.get(capIdx)); - } - } - } - String v = System.getProperty("invoke.count"); m_isInvokeCount = (v == null) ? false : Boolean.valueOf(v); } - public Map> resolve(Module module) + public Map> resolve(ResolverState state, Module module) { if (m_isInvokeCount) { @@ -95,7 +65,7 @@ Map modulePkgMap; resolveRoot( - module, + state, module, new HashMap>(), modulePkgMap = new HashMap(), new HashMap()); @@ -140,6 +110,7 @@ } private void resolveRoot( + ResolverState state, Module module, Map> candidateMap, Map modulePkgMap, Map cycleMap) { @@ -161,7 +132,7 @@ System.out.println("+++ RESOLVING " + module); calculateExportedPackages(module, modulePkgMap); - populateCandidates(module, candidateMap); + populateCandidates(state, module, candidateMap); List reqs = module.getRequirements(); for (Requirement req : reqs) @@ -180,7 +151,8 @@ try { // Try to resolve the candidate. - resolveRoot(candCap.getModule(), candidateMap, modulePkgMap, cycleMap); + resolveRoot( + state, candCap.getModule(), candidateMap, modulePkgMap, cycleMap); // If we are here, the candidate resolved. Try to merge // the candidate's into the target module's packages. @@ -430,7 +402,8 @@ } } - private void populateCandidates(Module module, Map> candidateMap) + private void populateCandidates( + ResolverState state, Module module, Map> candidateMap) { if (m_isInvokeCount) { @@ -449,7 +422,7 @@ // those instead, otherwise find the matching providers. if (candidateMap.get(req) == null) { - Set candidates = findCandidates(req); + Set candidates = state.getCandidates(module, req); if ((candidates.size() == 0) && !req.isOptional()) { throw new RuntimeException("Unable to resolve " + module @@ -464,83 +437,6 @@ } } - private Set findCandidates(Requirement req) - { - if (m_isInvokeCount) - { - String methodName = new Exception().fillInStackTrace().getStackTrace()[0].getMethodName(); - Long count = m_invokeCounts.get(methodName); - count = (count == null) ? new Long(1) : new Long(count.longValue() + 1); - m_invokeCounts.put(methodName, count); - } - - Set result = new TreeSet(new Comparator() { - public int compare(Object arg1, Object arg2) - { - Capability cap1 = (Capability) arg1; - Capability cap2 = (Capability) arg2; - if (cap1.getNamespace().equals(Capability.MODULE_NAMESPACE)) - { - int c = ((Comparable) cap1.getAttribute(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE) - .getValue()).compareTo(cap2.getAttribute(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE) - .getValue()); - if (c == 0) - { - Version v1 = (cap1.getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE) == null) - ? Version.emptyVersion - : (Version) cap1.getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE).getValue(); - Version v2 = (cap1.getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE) == null) - ? Version.emptyVersion - : (Version) cap1.getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE).getValue(); - // Compare these in reverse order, since we want - // highest version to have priority. - c = v2.compareTo(v1); - if (c == 0) - { - c = cap1.getModule().getName().compareTo(cap2.getModule().getName()); - } - } - return c; - } -// TODO: PROTO2 RESOLVER - Need to change this to handle arbitrary capabilities -// that may not have a natural ordering. - // Assume everything else is a package capability. - else - { - int c = ((Comparable) cap1.getAttribute(Capability.PACKAGE_ATTR).getValue()) - .compareTo(cap2.getAttribute(Capability.PACKAGE_ATTR).getValue()); - if (c == 0) - { - Version v1 = (cap1.getAttribute(Capability.VERSION_ATTR) == null) - ? Version.emptyVersion - : (Version) cap1.getAttribute(Capability.VERSION_ATTR).getValue(); - Version v2 = (cap1.getAttribute(Capability.VERSION_ATTR) == null) - ? Version.emptyVersion - : (Version) cap1.getAttribute(Capability.VERSION_ATTR).getValue(); - // Compare these in reverse order, since we want - // highest version to have priority. - c = v2.compareTo(v1); - if (c == 0) - { - c = cap1.getModule().getName().compareTo(cap2.getModule().getName()); - } - } - return c; - } - } - }); - - if (req.getNamespace().equals(Capability.MODULE_NAMESPACE)) - { - result.addAll(m_modCapSet.match(req.getFilter())); - } - else if (req.getNamespace().equals(Capability.PACKAGE_NAMESPACE)) - { - result.addAll(m_pkgCapSet.match(req.getFilter())); - } - return result; - } - private void calculateExportedPackages( Module module, Map modulePkgMap) { @@ -589,7 +485,7 @@ for (int i = 0; i < reqs.size(); i++) { if (reqs.get(i).getNamespace().equals(Capability.PACKAGE_NAMESPACE) - && m_pkgCapSet.matches(cap, reqs.get(i).getFilter())) + && CapabilitySet.matches(cap, reqs.get(i).getFilter())) { return true; } Modified: felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/proto3/Proto3Resolver.java URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/proto3/Proto3Resolver.java?rev=897034&r1=897033&r2=897034&view=diff ============================================================================== --- felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/proto3/Proto3Resolver.java (original) +++ felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/proto3/Proto3Resolver.java Thu Jan 7 22:23:53 2010 @@ -19,7 +19,6 @@ package org.apache.felix.resolver.proto3; import java.util.ArrayList; -import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -27,59 +26,26 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import java.util.TreeSet; import org.apache.felix.resolver.Module; import org.apache.felix.resolver.ResolveException; import org.apache.felix.resolver.Resolver; -import org.apache.felix.resolver.Version; +import org.apache.felix.resolver.ResolverStateImpl; import org.apache.felix.resolver.Wire; import org.apache.felix.resolver.cs.Capability; import org.apache.felix.resolver.cs.CapabilitySet; import org.apache.felix.resolver.cs.Requirement; -import org.apache.felix.resolver.manifestparser.Constants; // 1. Treat hard pkg constraints separately from implied package constraints // 2. Map pkg constraints to a set of capabilities, not a single capability. // 3. Uses constraints cannot conflict with other uses constraints, only with hard constraints. public class Proto3Resolver implements Resolver { - private final List m_modules; - private final CapabilitySet m_pkgCapSet; - private final CapabilitySet m_modCapSet; - private static final Map m_invokeCounts = new HashMap(); - private static boolean m_isInvokeCount = false; - public Proto3Resolver(List modules) + public Proto3Resolver() { System.out.println("+++ PROTO3 RESOLVER"); - m_modules = modules; - - List indices = new ArrayList(); - indices.add(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE); - m_modCapSet = new CapabilitySet(indices); - - indices = new ArrayList(); - indices.add(Capability.PACKAGE_ATTR); - m_pkgCapSet = new CapabilitySet(indices); - - for (int modIdx = 0; modIdx < m_modules.size(); modIdx++) - { - List caps = m_modules.get(modIdx).getCapabilities(); - for (int capIdx = 0; capIdx < caps.size(); capIdx++) - { - if (caps.get(capIdx).getNamespace().equals(Capability.MODULE_NAMESPACE)) - { - m_modCapSet.addCapability(caps.get(capIdx)); - } - else if (caps.get(capIdx).getNamespace().equals(Capability.PACKAGE_NAMESPACE)) - { - m_pkgCapSet.addCapability(caps.get(capIdx)); - } - } - } - String v = System.getProperty("invoke.count"); m_isInvokeCount = (v == null) ? false : Boolean.valueOf(v); } @@ -87,7 +53,7 @@ private final List>> m_candidatePermutations = new ArrayList>>(); - public Map> resolve(Module module) + public Map> resolve(ResolverState state, Module module) { if (m_isInvokeCount) { @@ -104,7 +70,7 @@ Map> candidateMap = new HashMap>(); - populateCandidates(module, candidateMap, new HashSet()); + populateCandidates(state, module, candidateMap, new HashSet()); m_candidatePermutations.add(candidateMap); ResolveException rethrow = null; @@ -151,10 +117,11 @@ return wireMap; } - private void dumpCandidateMap(Map> candidateMap) + private static void dumpCandidateMap( + ResolverState state, Map> candidateMap) { System.out.println("=== CANDIDATE MAP ==="); - for (Module module : m_modules) + for (Module module : state.getModules()) { if (!module.isResolved()) { @@ -285,8 +252,9 @@ } } - private void populateCandidates( - Module module, Map> candidateMap, Set cycles) + private static void populateCandidates( + ResolverState state, Module module, + Map> candidateMap, Set cycles) { if (m_isInvokeCount) { @@ -307,7 +275,7 @@ List reqs = module.getRequirements(); for (Requirement req : reqs) { - Set candidates = findCandidates(req); + Set candidates = state.getCandidates(module, req); for (Iterator itCandCap = candidates.iterator(); itCandCap.hasNext(); ) { Capability candCap = itCandCap.next(); @@ -315,7 +283,7 @@ { try { - populateCandidates(candCap.getModule(), candidateMap, cycles); + populateCandidates(state, candCap.getModule(), candidateMap, cycles); } catch (ResolveException ex) { @@ -336,90 +304,7 @@ } } - private static Set createCandidateSet() - { - return new TreeSet(new Comparator() { - public int compare(Object arg1, Object arg2) - { - Capability cap1 = (Capability) arg1; - Capability cap2 = (Capability) arg2; - if (cap1.getNamespace().equals(Capability.MODULE_NAMESPACE)) - { - int c = ((Comparable) cap1.getAttribute(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE) - .getValue()).compareTo(cap2.getAttribute(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE) - .getValue()); - if (c == 0) - { - Version v1 = (cap1.getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE) == null) - ? Version.emptyVersion - : (Version) cap1.getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE).getValue(); - Version v2 = (cap1.getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE) == null) - ? Version.emptyVersion - : (Version) cap1.getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE).getValue(); - // Compare these in reverse order, since we want - // highest version to have priority. - c = v2.compareTo(v1); - if (c == 0) - { - c = cap1.getModule().getName().compareTo(cap2.getModule().getName()); - } - } - return c; - } -// TODO: PROTO2 RESOLVER - Need to change this to handle arbitrary capabilities -// that may not have a natural ordering. - // Assume everything else is a package capability. - else - { - int c = ((Comparable) cap1.getAttribute(Capability.PACKAGE_ATTR).getValue()) - .compareTo(cap2.getAttribute(Capability.PACKAGE_ATTR).getValue()); - if (c == 0) - { - Version v1 = (cap1.getAttribute(Capability.VERSION_ATTR) == null) - ? Version.emptyVersion - : (Version) cap1.getAttribute(Capability.VERSION_ATTR).getValue(); - Version v2 = (cap1.getAttribute(Capability.VERSION_ATTR) == null) - ? Version.emptyVersion - : (Version) cap1.getAttribute(Capability.VERSION_ATTR).getValue(); - // Compare these in reverse order, since we want - // highest version to have priority. - c = v2.compareTo(v1); - if (c == 0) - { - c = cap1.getModule().getName().compareTo(cap2.getModule().getName()); - } - } - return c; - } - } - }); - } - - private Set findCandidates(Requirement req) - { - if (m_isInvokeCount) - { - String methodName = new Exception().fillInStackTrace().getStackTrace()[0].getMethodName(); - Long count = m_invokeCounts.get(methodName); - count = (count == null) ? new Long(1) : new Long(count.longValue() + 1); - m_invokeCounts.put(methodName, count); - } - - Set result = createCandidateSet(); - - if (req.getNamespace().equals(Capability.MODULE_NAMESPACE)) - { - result.addAll(m_modCapSet.match(req.getFilter())); - } - else if (req.getNamespace().equals(Capability.PACKAGE_NAMESPACE)) - { - result.addAll(m_pkgCapSet.match(req.getFilter())); - } - - return result; - } - - private void calculateExportedPackages( + private static void calculateExportedPackages( Module module, List incomingReqs, Map modulePkgMap) { if (m_isInvokeCount) @@ -453,7 +338,7 @@ modulePkgMap.put(module, packages); } - private boolean hasOverlappingImport(Module module, Capability cap) + private static boolean hasOverlappingImport(Module module, Capability cap) { if (m_isInvokeCount) { @@ -467,7 +352,7 @@ for (int i = 0; i < reqs.size(); i++) { if (reqs.get(i).getNamespace().equals(Capability.PACKAGE_NAMESPACE) - && m_pkgCapSet.matches(cap, reqs.get(i).getFilter())) + && CapabilitySet.matches(cap, reqs.get(i).getFilter())) { return true; } @@ -832,7 +717,7 @@ new HashMap>(); for (Entry> entry : candidateMap.entrySet()) { - Set candidates = createCandidateSet(); + Set candidates = ResolverStateImpl.createCandidateSet(); candidates.addAll(entry.getValue()); copy.put(entry.getKey(), candidates); } @@ -934,7 +819,7 @@ return null; } - private class Packages + private static class Packages { public final Map m_exportedPkgs = new HashMap(); @@ -958,7 +843,7 @@ } } - private class Blame + private static class Blame { public final List m_reqs; public final Capability m_cap; 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=897034&r1=897033&r2=897034&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 Thu Jan 7 22:23:53 2010 @@ -36,9 +36,6 @@ public class ProtoResolver implements Resolver { - private final List m_moduleList; - private final CapabilitySet m_pkgCapSet; - private final CapabilitySet m_modCapSet; private final Map> m_moduleConstraintCache = new HashMap>(); private final Map> m_resolvingConstraintCache = @@ -52,64 +49,26 @@ private static boolean m_isInvokeCount = false; - public ProtoResolver(List moduleList) + public ProtoResolver() { System.out.println("+++ PROTO RESOLVER"); - m_moduleList = moduleList; - - List indices = new ArrayList(); - indices.add(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE); - m_modCapSet = new CapabilitySet(indices); - - indices = new ArrayList(); - indices.add(Capability.PACKAGE_ATTR); - m_pkgCapSet = new CapabilitySet(indices); - - for (int modIdx = 0; modIdx < m_moduleList.size(); modIdx++) - { - List caps = m_moduleList.get(modIdx).getCapabilities(); - for (int capIdx = 0; capIdx < caps.size(); capIdx++) - { - if (caps.get(capIdx).getNamespace().equals(Capability.MODULE_NAMESPACE)) - { - m_modCapSet.addCapability(caps.get(capIdx)); - } - else if (caps.get(capIdx).getNamespace().equals(Capability.PACKAGE_NAMESPACE)) - { - m_pkgCapSet.addCapability(caps.get(capIdx)); - } - } - } - String v = System.getProperty("invoke.count"); m_isInvokeCount = (v == null) ? false : Boolean.valueOf(v); } - public Module getModule(String name) - { - for (int modIdx = 0; modIdx < m_moduleList.size(); modIdx++) - { - if (m_moduleList.get(modIdx).getName().equals(name)) - { - return m_moduleList.get(modIdx); - } - } - return null; - } - private Module m_rootModule = null; private List>> m_candidatePermutations = new ArrayList>>(); - public Map> resolve(Module module) + public Map> resolve(ResolverState state, Module module) { m_invokeCounts.clear(); m_moduleConstraintCache.clear(); m_resolvingConstraintCache.clear(); m_resolvedConstraintCache.clear(); m_verifyCache.clear(); - Map> candidateMap = resolveRoot( - module); + Map> candidateMap = + resolveRoot(state, module); if (m_isInvokeCount) { @@ -118,7 +77,7 @@ return populateWireMap(module, candidateMap, new HashMap>()); } - private Map> resolveRoot(Module module) + private Map> resolveRoot(ResolverState state, Module module) { if (m_isInvokeCount) { @@ -146,7 +105,7 @@ List reqs = module.getRequirements(); for (Requirement req : reqs) { - Set candidates = findCandidates(req); + Set candidates = state.getCandidates(module, req); if ((candidates.size() == 0) && !req.isOptional()) { throw new RuntimeException("Unable to resolve " + module @@ -228,6 +187,7 @@ else { candidateConstraints = calculateCandidateConstraints( + state, cap.getModule(), cap, module, @@ -297,7 +257,7 @@ } private Map calculateCandidateConstraints( - Module module, Capability capGoal, Module blameModule, + ResolverState state, Module module, Capability capGoal, Module blameModule, Map> candidateMap, Map cycleMap) { @@ -349,7 +309,7 @@ // those instead, otherwise find the matching providers. if (candidateMap.get(req) == null) { - Set candidates = findCandidates(req); + Set candidates = state.getCandidates(module, req); if ((candidates.size() == 0) && !req.isOptional()) { throw new RuntimeException("Unable to resolve " + module @@ -411,6 +371,7 @@ else { candidateConstraints = calculateCandidateConstraints( + state, cap.getModule(), cap, module, @@ -509,83 +470,6 @@ return constraintsCopy; } - private Set findCandidates(Requirement req) - { - if (m_isInvokeCount) - { - String methodName = new Exception().fillInStackTrace().getStackTrace()[0].getMethodName(); - Long count = m_invokeCounts.get(methodName); - count = (count == null) ? new Long(1) : new Long(count.longValue() + 1); - m_invokeCounts.put(methodName, count); - } - - Set result = new TreeSet(new Comparator() { - public int compare(Object arg1, Object arg2) - { - Capability cap1 = (Capability) arg1; - Capability cap2 = (Capability) arg2; - if (cap1.getNamespace().equals(Capability.MODULE_NAMESPACE)) - { - int c = ((Comparable) cap1.getAttribute(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE) - .getValue()).compareTo(cap2.getAttribute(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE) - .getValue()); - if (c == 0) - { - Version v1 = (cap1.getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE) == null) - ? Version.emptyVersion - : (Version) cap1.getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE).getValue(); - Version v2 = (cap1.getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE) == null) - ? Version.emptyVersion - : (Version) cap1.getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE).getValue(); - // Compare these in reverse order, since we want - // highest version to have priority. - c = v2.compareTo(v1); - if (c == 0) - { - c = cap1.getModule().getName().compareTo(cap2.getModule().getName()); - } - } - return c; - } -// TODO: PROTO RESOLVER - Need to change this to handle arbitrary capabilities -// that may not have a natural ordering. - // Assume everything else is a package capability. - else - { - int c = ((Comparable) cap1.getAttribute(Capability.PACKAGE_ATTR).getValue()) - .compareTo(cap2.getAttribute(Capability.PACKAGE_ATTR).getValue()); - if (c == 0) - { - Version v1 = (cap1.getAttribute(Capability.VERSION_ATTR) == null) - ? Version.emptyVersion - : (Version) cap1.getAttribute(Capability.VERSION_ATTR).getValue(); - Version v2 = (cap1.getAttribute(Capability.VERSION_ATTR) == null) - ? Version.emptyVersion - : (Version) cap1.getAttribute(Capability.VERSION_ATTR).getValue(); - // Compare these in reverse order, since we want - // highest version to have priority. - c = v2.compareTo(v1); - if (c == 0) - { - c = cap1.getModule().getName().compareTo(cap2.getModule().getName()); - } - } - return c; - } - } - }); - - if (req.getNamespace().equals(Capability.MODULE_NAMESPACE)) - { - result.addAll(m_modCapSet.match(req.getFilter())); - } - else if (req.getNamespace().equals(Capability.PACKAGE_NAMESPACE)) - { - result.addAll(m_pkgCapSet.match(req.getFilter())); - } - return result; - } - private Map calculateConstraints( Module module, Module blameModule) { @@ -660,7 +544,7 @@ for (int i = 0; i < reqs.size(); i++) { if (reqs.get(i).getNamespace().equals(Capability.PACKAGE_NAMESPACE) - && m_pkgCapSet.matches(cap, reqs.get(i).getFilter())) + && CapabilitySet.matches(cap, reqs.get(i).getFilter())) { return true; }