Author: rickhall
Date: Fri Jan 8 17:34:06 2010
New Revision: 897271
URL: http://svn.apache.org/viewvc?rev=897271&view=rev
Log:
Factor out candidate comparision and modify to take into account resolved
status.
Added:
felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/CandidateComparator.java
Modified:
felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/ResolverStateImpl.java
felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/StatefulResolver.java
felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Wire.java
felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/proto3/Proto3Resolver.java
Added:
felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/CandidateComparator.java
URL:
http://svn.apache.org/viewvc/felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/CandidateComparator.java?rev=897271&view=auto
==============================================================================
---
felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/CandidateComparator.java
(added)
+++
felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/CandidateComparator.java
Fri Jan 8 17:34:06 2010
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.resolver;
+
+import java.util.Comparator;
+import org.apache.felix.resolver.cs.Capability;
+import org.apache.felix.resolver.manifestparser.Constants;
+
+public class CandidateComparator implements Comparator
+{
+ public int compare(Object arg1, Object arg2)
+ {
+ Capability cap1 = (Capability) arg1;
+ Capability cap2 = (Capability) arg2;
+
+ // First check resolved state, since resolved capabilities have
priority
+ // over unresolved ones.
+ int c = 0;
+ if (cap1.getModule().isResolved() && !cap2.getModule().isResolved())
+ {
+ c = 1;
+ }
+ if (!cap1.getModule().isResolved() && cap2.getModule().isResolved())
+ {
+ c = -1;
+ }
+
+ // Next compare version numbers.
+ if ((c == 0) &&
cap1.getNamespace().equals(Capability.MODULE_NAMESPACE))
+ {
+ 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);
+ }
+ }
+// TODO: PROTO3 RESOLVER - Need to change this to handle arbitrary capabilities
+// that may not have a natural ordering.
+ // Assume everything else is a package capability.
+ else if (c == 0)
+ {
+ 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);
+ }
+ }
+
+ // Finally, compare module identity.
+ if (c == 0)
+ {
+ c =
cap1.getModule().getName().compareTo(cap2.getModule().getName());
+ }
+
+ return c;
+ }
+}
\ No newline at end of file
Modified:
felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/ResolverStateImpl.java
URL:
http://svn.apache.org/viewvc/felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/ResolverStateImpl.java?rev=897271&r1=897270&r2=897271&view=diff
==============================================================================
---
felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/ResolverStateImpl.java
(original)
+++
felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/ResolverStateImpl.java
Fri Jan 8 17:34:06 2010
@@ -19,7 +19,6 @@
package org.apache.felix.resolver;
import java.util.ArrayList;
-import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
@@ -27,7 +26,6 @@
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.felix.FelixResolver;
import org.apache.felix.resolver.manifestparser.Constants;
public class ResolverStateImpl implements ResolverState
@@ -61,6 +59,11 @@
{
m_pkgCapSet.addCapability(caps.get(capIdx));
}
+ else
+ {
+ System.err.println(
+ "Ignoring unknown capability: " +
caps.get(capIdx).getNamespace());
+ }
}
}
}
@@ -77,7 +80,7 @@
private Set<Capability> getUnresolvedCandidates(Module module, Requirement
req)
{
- Set<Capability> result = createCandidateSet();
+ Set<Capability> result = new TreeSet(new CandidateComparator());
if (req.getNamespace().equals(Capability.MODULE_NAMESPACE))
{
@@ -96,75 +99,113 @@
throw new UnsupportedOperationException("Not supported yet.");
}
- public static Set<Capability> createCandidateSet()
+ private static Capability getSatisfyingCapability(Module module,
Requirement req)
{
- return new TreeSet(new Comparator() {
- public int compare(Object arg1, Object arg2)
+ List<Capability> caps = module.getCapabilities();
+ for (Capability cap : caps)
+ {
+ if (((RequirementImpl) req).isSatistfiedBy(cap))
{
- 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;
+ return cap;
+ }
+ }
+ return null;
+ }
+
+ public void moduleResolved(Module module)
+ {
+ if (module.isResolved())
+ {
+/*
+ // At this point, we need to remove all of the resolved module's
+ // capabilities from the "unresolved" package map and put them in
+ // in the "resolved" package map, with the exception of any
+ // package exports that are also imported. In that case we need
+ // to make sure that the import actually points to the resolved
+ // module and not another module. If it points to another module
+ // then the capability should be ignored, since the framework
+ // decided to honor the import and discard the export.
+ List<Capability> caps = module.getCapabilities();
+
+ // First remove all existing capabilities from the "unresolved"
map.
+ for (int capIdx = 0; (caps != null) && (capIdx < caps.length);
capIdx++)
+ {
+ if
(caps[capIdx].getNamespace().equals(ICapability.PACKAGE_NAMESPACE))
+ {
+ // Get package name.
+ String pkgName = (String)
+
caps[capIdx].getProperties().get(ICapability.PACKAGE_PROPERTY);
+ // Remove the module's capability for the package.
+ List capList = (List) m_unresolvedPkgIndex.get(pkgName);
+ capList.remove(caps[capIdx]);
}
-// TODO: PROTO3 RESOLVER - Need to change this to handle arbitrary capabilities
-// that may not have a natural ordering.
- // Assume everything else is a package capability.
- else
+ }
+
+ // Next create a copy of the module's capabilities so we can
+ // null out any capabilities that should be ignored.
+ ICapability[] capsCopy = (caps == null) ? null : new
ICapability[caps.length];
+ if (capsCopy != null)
+ {
+ System.arraycopy(caps, 0, capsCopy, 0, caps.length);
+ }
+ // Loop through the module's capabilities to determine which ones
+ // can be ignored by seeing which ones satifies the wire
requirements.
+// TODO: RB - Bug here because a requirement for a package need not overlap the
+// capability for that package and this assumes it does. This might
+// require us to introduce the notion of a substitutable capability.
+ IWire[] wires = module.getWires();
+ for (int capIdx = 0; (capsCopy != null) && (capIdx <
capsCopy.length); capIdx++)
+ {
+ // Loop through all wires to see if the current capability
+ // satisfies any of the wire requirements.
+ for (int wireIdx = 0; (wires != null) && (wireIdx <
wires.length); wireIdx++)
{
- int c = ((Comparable)
cap1.getAttribute(Capability.PACKAGE_ATTR).getValue())
-
.compareTo(cap2.getAttribute(Capability.PACKAGE_ATTR).getValue());
- if (c == 0)
+ // If one of the module's capabilities satifies the
requirement
+ // for an existing wire, this means the capability was
+ // substituted with another provider by the resolver and
+ // the module's capability was not used. Therefore, we
should
+ // null it here so it doesn't get added the list of
resolved
+ // capabilities for this module.
+ if
(wires[wireIdx].getRequirement().isSatisfied(capsCopy[capIdx]))
{
- 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());
- }
+ capsCopy[capIdx] = null;
+ break;
}
- return c;
}
}
- });
- }
- private static Capability getSatisfyingCapability(Module module,
Requirement req)
- {
- List<Capability> caps = module.getCapabilities();
- for (Capability cap : caps)
- {
- if (((RequirementImpl) req).isSatistfiedBy(cap))
+ // Now loop through all capabilities and add them to the "resolved"
+ // capability and package index maps, ignoring any that were
nulled out.
+ for (int capIdx = 0; (capsCopy != null) && (capIdx <
capsCopy.length); capIdx++)
{
- return cap;
+ if (capsCopy[capIdx] != null)
+ {
+ List resolvedCaps = (List) m_resolvedCapMap.get(module);
+ if (resolvedCaps == null)
+ {
+ m_resolvedCapMap.put(module, resolvedCaps = new
ArrayList());
+ }
+ if (!resolvedCaps.contains(capsCopy[capIdx]))
+ {
+ resolvedCaps.add(capsCopy[capIdx]);
+ }
+
+ // If the capability is a package, then add the exporter
module
+ // of the wire to the "resolved" package index and remove
it
+ // from the "unresolved" package index.
+ if
(capsCopy[capIdx].getNamespace().equals(ICapability.PACKAGE_NAMESPACE))
+ {
+ // Add to "resolved" package index.
+ indexPackageCapability(m_resolvedPkgIndex,
capsCopy[capIdx]);
+ }
+ }
}
+*/
}
- return null;
+
+//System.out.println("UNRESOLVED PACKAGES:");
+//dumpPackageIndex(m_unresolvedPkgIndex);
+//System.out.println("RESOLVED PACKAGES:");
+//dumpPackageIndex(m_resolvedPkgIndex);
}
}
\ No newline at end of file
Modified:
felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/StatefulResolver.java
URL:
http://svn.apache.org/viewvc/felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/StatefulResolver.java?rev=897271&r1=897270&r2=897271&view=diff
==============================================================================
---
felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/StatefulResolver.java
(original)
+++
felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/StatefulResolver.java
Fri Jan 8 17:34:06 2010
@@ -22,6 +22,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.logging.Logger;
import org.apache.felix.resolver.Resolver.ResolverState;
public class StatefulResolver
@@ -43,21 +44,33 @@
long endtime = System.currentTimeMillis();
System.out.println("Resolve time: " + (endtime - starttime));
System.out.println("Modules resolved: " + wireMap.size());
-// System.out.println("Wires:");
- for (Iterator<Entry<Module, List<Wire>>> it =
wireMap.entrySet().iterator();
- it.hasNext(); )
+
+ // Mark all modules as resolved.
+ markResolvedModules(wireMap);
+ }
+
+ private void markResolvedModules(Map<Module, List<Wire>> wireMap)
+ {
+ if (wireMap != null)
{
- Entry<Module, List<Wire>> entry = it.next();
- entry.getKey().resolve(entry.getValue());
-//System.out.println("SETTING WIRES FOR " + entry.getKey());
- if (entry.getValue().size() > 0)
+ // Iterate over the map to mark the modules as resolved and
+ // update our resolver data structures.
+ for (Entry<Module, List<Wire>> entry : wireMap.entrySet())
{
-// System.out.println(" " + entry.getKey());
- for (int i = 0; i < entry.getValue().size(); i++)
+ // Only add wires attribute if some exist; export
+ // only modules may not have wires.
+// TODO: RESOLVER - Seems stupid that we package these up as wires to tear
them apart.
+ List<Wire> wires = entry.getValue();
+ if (wires.size() > 0)
{
-// System.out.println(" " + entry.getValue().get(i));
- System.out.println(" " + entry.getKey() + " - " +
entry.getValue().get(i));
+ for (int wireIdx = 0; wireIdx < wires.size(); wireIdx++)
+ {
+ System.out.println("WIRE: " + wires.get(wireIdx));
+ }
+ entry.getKey().resolve(wires);
}
+
+// m_state.moduleResolved(module);
}
}
}
Modified:
felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Wire.java
URL:
http://svn.apache.org/viewvc/felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Wire.java?rev=897271&r1=897270&r2=897271&view=diff
==============================================================================
---
felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Wire.java
(original)
+++
felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Wire.java
Fri Jan 8 17:34:06 2010
@@ -58,6 +58,6 @@
public String toString()
{
- return "WIRE: " + m_req + " (" + m_importer + ") -> " + m_cap + " (" +
m_exporter + ")";
+ return m_req + " (" + m_importer + ") -> " + m_cap + " (" + m_exporter
+ ")";
}
}
\ No newline at end of file
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=897271&r1=897270&r2=897271&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
Fri Jan 8 17:34:06 2010
@@ -26,6 +26,8 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
+import java.util.TreeSet;
+import org.apache.felix.resolver.CandidateComparator;
import org.apache.felix.resolver.Module;
import org.apache.felix.resolver.ResolveException;
import org.apache.felix.resolver.Resolver;
@@ -717,7 +719,7 @@
new HashMap<Requirement, Set<Capability>>();
for (Entry<Requirement, Set<Capability>> entry :
candidateMap.entrySet())
{
- Set<Capability> candidates =
ResolverStateImpl.createCandidateSet();
+ Set<Capability> candidates = new TreeSet(new
CandidateComparator());
candidates.addAll(entry.getValue());
copy.put(entry.getKey(), candidates);
}