You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ri...@apache.org on 2010/01/08 17:04:14 UTC

svn commit: r897241 - in /felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver: Resolver.java ResolverStateImpl.java proto3/Proto3Resolver.java

Author: rickhall
Date: Fri Jan  8 16:04:11 2010
New Revision: 897241

URL: http://svn.apache.org/viewvc?rev=897241&view=rev
Log:
Add resolver state implementation.

Added:
    felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/ResolverStateImpl.java
Modified:
    felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Resolver.java
    felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/proto3/Proto3Resolver.java

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=897241&r1=897240&r2=897241&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 Fri Jan  8 16:04:11 2010
@@ -30,7 +30,6 @@
 
     public static interface ResolverState
     {
-        List<Module> getModules();
         Set<Capability> getCandidates(Module module, Requirement req);
     }
 }
\ No newline at end of file

Added: 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=897241&view=auto
==============================================================================
--- felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/ResolverStateImpl.java (added)
+++ felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/ResolverStateImpl.java Fri Jan  8 16:04:11 2010
@@ -0,0 +1,170 @@
+/*
+ *  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.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+import org.apache.felix.resolver.Resolver.ResolverState;
+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
+{
+    private final List<Module> m_modules;
+    private final CapabilitySet m_pkgCapSet;
+    private final CapabilitySet m_modCapSet;
+
+    public ResolverStateImpl(List<Module> modules)
+    {
+        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));
+                }
+            }
+        }
+    }
+
+    public List<Module> getModules()
+    {
+        return m_modules;
+    }
+
+    public Set<Capability> getCandidates(Module module, Requirement req)
+    {
+        return getUnresolvedCandidates(module, req);
+    }
+
+    private Set<Capability> getUnresolvedCandidates(Module module, Requirement req)
+    {
+        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 Set<Capability> getResolvedCandidates(Module module, Requirement req)
+    {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+    public 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: 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
+                {
+                    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 static Capability getSatisfyingCapability(Module module, Requirement req)
+    {
+        List<Capability> caps = module.getCapabilities();
+        for (Capability cap : caps)
+        {
+            if (((RequirementImpl) req).isSatistfiedBy(cap))
+            {
+                return cap;
+            }
+        }
+        return null;
+    }
+}
\ 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=897241&r1=897240&r2=897241&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 16:04:11 2010
@@ -121,7 +121,7 @@
         ResolverState state, Map<Requirement, Set<Capability>> candidateMap)
     {
         System.out.println("=== CANDIDATE MAP ===");
-        for (Module module : state.getModules())
+        for (Module module : ((ResolverStateImpl) state).getModules())
         {
             if (!module.isResolved())
             {