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<Module> moduleList = new ArrayList<Module>();
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<Module, List<Wire>> wireMap = resolver.resolve(targetModule);
+ Map<Module, List<Wire>> 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<Module> moduleList = new ArrayList<Module>();
List<Module> 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<Module, List<Wire>> wireMap = resolver.resolve(target);
+ Map<Module, List<Wire>> 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<Module, List<Wire>> resolve(Module module);
+ Map<Module, List<Wire>> resolve(ResolverState state, Module module);
+
+ public static interface ResolverState
+ {
+ List<Module> getModules();
+ Set<Capability> 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<Module, List<Wire>> newWireMap = new HashMap<Module, List<Wire>>();
+ 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<Capability> getResolvedCandidates(Requirement req);
- List<Capability> 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<Module, List<Wire>> wireMap = resolver.resolve(target);
+ Map<Module, List<Wire>> 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<Module> m_modules;
- private final CapabilitySet m_pkgCapSet;
- private final CapabilitySet m_modCapSet;
-
private static final Map<String, Long> m_invokeCounts = new
HashMap<String, Long>();
private static boolean m_isInvokeCount = false;
- public Proto2Resolver(List<Module> 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<Capability> 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<Module, List<Wire>> resolve(Module module)
+ public Map<Module, List<Wire>> resolve(ResolverState state, Module module)
{
if (m_isInvokeCount)
{
@@ -95,7 +65,7 @@
Map<Module, Packages> modulePkgMap;
resolveRoot(
- module,
+ state, module,
new HashMap<Requirement, Set<Capability>>(),
modulePkgMap = new HashMap<Module, Packages>(),
new HashMap<Module, Object>());
@@ -140,6 +110,7 @@
}
private void resolveRoot(
+ ResolverState state,
Module module, Map<Requirement, Set<Capability>> candidateMap,
Map<Module, Packages> modulePkgMap, Map<Module, Object> cycleMap)
{
@@ -161,7 +132,7 @@
System.out.println("+++ RESOLVING " + module);
calculateExportedPackages(module, modulePkgMap);
- populateCandidates(module, candidateMap);
+ populateCandidates(state, module, candidateMap);
List<Requirement> 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<Requirement,
Set<Capability>> candidateMap)
+ private void populateCandidates(
+ ResolverState state, Module module, Map<Requirement, Set<Capability>>
candidateMap)
{
if (m_isInvokeCount)
{
@@ -449,7 +422,7 @@
// those instead, otherwise find the matching providers.
if (candidateMap.get(req) == null)
{
- Set<Capability> candidates = findCandidates(req);
+ Set<Capability> candidates = state.getCandidates(module, req);
if ((candidates.size() == 0) && !req.isOptional())
{
throw new RuntimeException("Unable to resolve " + module
@@ -464,83 +437,6 @@
}
}
- private Set<Capability> 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<Capability> 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<Module, Packages> 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<Module> m_modules;
- private final CapabilitySet m_pkgCapSet;
- private final CapabilitySet m_modCapSet;
-
private static final Map<String, Long> m_invokeCounts = new
HashMap<String, Long>();
-
private static boolean m_isInvokeCount = false;
- public Proto3Resolver(List<Module> 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<Capability> 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<Map<Requirement, Set<Capability>>>
m_candidatePermutations =
new ArrayList<Map<Requirement, Set<Capability>>>();
- public Map<Module, List<Wire>> resolve(Module module)
+ public Map<Module, List<Wire>> resolve(ResolverState state, Module module)
{
if (m_isInvokeCount)
{
@@ -104,7 +70,7 @@
Map<Requirement, Set<Capability>> candidateMap =
new HashMap<Requirement, Set<Capability>>();
- populateCandidates(module, candidateMap, new HashSet<Module>());
+ populateCandidates(state, module, candidateMap, new
HashSet<Module>());
m_candidatePermutations.add(candidateMap);
ResolveException rethrow = null;
@@ -151,10 +117,11 @@
return wireMap;
}
- private void dumpCandidateMap(Map<Requirement, Set<Capability>>
candidateMap)
+ private static void dumpCandidateMap(
+ ResolverState state, Map<Requirement, Set<Capability>> 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<Requirement, Set<Capability>> candidateMap,
Set<Module> cycles)
+ private static void populateCandidates(
+ ResolverState state, Module module,
+ Map<Requirement, Set<Capability>> candidateMap, Set<Module> cycles)
{
if (m_isInvokeCount)
{
@@ -307,7 +275,7 @@
List<Requirement> reqs = module.getRequirements();
for (Requirement req : reqs)
{
- Set<Capability> candidates = findCandidates(req);
+ Set<Capability> candidates = state.getCandidates(module, req);
for (Iterator<Capability> 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<Capability> 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<Capability> 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<Capability> 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<Requirement> incomingReqs, Map<Module, Packages>
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<Requirement, Set<Capability>>();
for (Entry<Requirement, Set<Capability>> entry :
candidateMap.entrySet())
{
- Set<Capability> candidates = createCandidateSet();
+ Set<Capability> 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<String, Blame> m_exportedPkgs
= new HashMap<String, Blame>();
@@ -958,7 +843,7 @@
}
}
- private class Blame
+ private static class Blame
{
public final List<Requirement> 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<Module> m_moduleList;
- private final CapabilitySet m_pkgCapSet;
- private final CapabilitySet m_modCapSet;
private final Map<Module, Map<String, Blame>> m_moduleConstraintCache =
new HashMap<Module, Map<String, Blame>>();
private final Map<Module, Map<String, Blame>> m_resolvingConstraintCache =
@@ -52,64 +49,26 @@
private static boolean m_isInvokeCount = false;
- public ProtoResolver(List<Module> 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<Capability> 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<Map<Requirement, Set<Capability>>> m_candidatePermutations
= new ArrayList<Map<Requirement, Set<Capability>>>();
- public Map<Module, List<Wire>> resolve(Module module)
+ public Map<Module, List<Wire>> resolve(ResolverState state, Module module)
{
m_invokeCounts.clear();
m_moduleConstraintCache.clear();
m_resolvingConstraintCache.clear();
m_resolvedConstraintCache.clear();
m_verifyCache.clear();
- Map<Requirement, Set<Capability>> candidateMap = resolveRoot(
- module);
+ Map<Requirement, Set<Capability>> candidateMap =
+ resolveRoot(state, module);
if (m_isInvokeCount)
{
@@ -118,7 +77,7 @@
return populateWireMap(module, candidateMap, new HashMap<Module,
List<Wire>>());
}
- private Map<Requirement, Set<Capability>> resolveRoot(Module module)
+ private Map<Requirement, Set<Capability>> resolveRoot(ResolverState state,
Module module)
{
if (m_isInvokeCount)
{
@@ -146,7 +105,7 @@
List<Requirement> reqs = module.getRequirements();
for (Requirement req : reqs)
{
- Set<Capability> candidates = findCandidates(req);
+ Set<Capability> 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<String, Blame> calculateCandidateConstraints(
- Module module, Capability capGoal, Module blameModule,
+ ResolverState state, Module module, Capability capGoal, Module
blameModule,
Map<Requirement, Set<Capability>> candidateMap,
Map<Module, Boolean> cycleMap)
{
@@ -349,7 +309,7 @@
// those instead, otherwise find the matching providers.
if (candidateMap.get(req) == null)
{
- Set<Capability> candidates = findCandidates(req);
+ Set<Capability> 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<Capability> 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<Capability> 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<String, Blame> 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;
}