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/21 15:15:58 UTC

svn commit: r901715 - in /felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework: FelixResolverState.java ModuleImpl.java resolver/ResolverImpl.java resolver/ResolverStateImpl.java resolver/WireModuleImpl.java

Author: rickhall
Date: Thu Jan 21 14:15:58 2010
New Revision: 901715

URL: http://svn.apache.org/viewvc?rev=901715&view=rev
Log:
More fixes.

Added:
    felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/resolver/WireModuleImpl.java
Modified:
    felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/FelixResolverState.java
    felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/ModuleImpl.java
    felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/resolver/ResolverImpl.java
    felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/resolver/ResolverStateImpl.java

Modified: felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/FelixResolverState.java
URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/FelixResolverState.java?rev=901715&r1=901714&r2=901715&view=diff
==============================================================================
--- felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/FelixResolverState.java (original)
+++ felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/FelixResolverState.java Thu Jan 21 14:15:58 2010
@@ -18,49 +18,54 @@
  */
 package org.apache.felix.framework;
 
-import java.security.ProtectionDomain;
 import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import org.apache.felix.framework.searchpolicy.ResolveException;
-import org.apache.felix.framework.searchpolicy.Resolver;
+import java.util.Set;
+import java.util.TreeSet;
+import org.apache.felix.framework.candidateset.Capability;
+import org.apache.felix.framework.candidateset.CapabilitySet;
+import org.apache.felix.framework.candidateset.Module;
+import org.apache.felix.framework.candidateset.Requirement;
+import org.apache.felix.framework.candidateset.Wire;
+import org.apache.felix.framework.resolver.CandidateComparator;
+import org.apache.felix.framework.resolver.ResolveException;
+import org.apache.felix.framework.resolver.Resolver;
 import org.apache.felix.framework.util.Util;
-import org.apache.felix.framework.util.manifestparser.R4Attribute;
-import org.apache.felix.framework.util.manifestparser.R4Directive;
-import org.apache.felix.framework.util.manifestparser.Requirement;
-import org.apache.felix.moduleloader.ICapability;
-import org.apache.felix.moduleloader.IModule;
-import org.apache.felix.moduleloader.IRequirement;
-import org.apache.felix.moduleloader.IWire;
 import org.osgi.framework.BundlePermission;
 import org.osgi.framework.Constants;
-import org.osgi.framework.PackagePermission;
 import org.osgi.framework.Version;
 
 public class FelixResolverState implements Resolver.ResolverState
 {
     private final Logger m_logger;
     // List of all modules.
-    private final List m_moduleList = new ArrayList();
+    private final List<Module> m_modules;
+    // Capability set for modules.
+    private final CapabilitySet m_modCapSet;
+    // Capability set for packages.
+    private final CapabilitySet m_pkgCapSet;
     // Map of fragment symbolic names to list of fragment modules sorted by version.
     private final Map m_fragmentMap = new HashMap();
-    // Maps a package name to a list of exporting capabilities.
-    private final Map m_unresolvedPkgIndex = new HashMap();
-    // Maps a package name to a list of exporting capabilities.
-    private final Map m_resolvedPkgIndex = new HashMap();
-    // Maps a module to a list of capabilities.
-    private final Map m_resolvedCapMap = new HashMap();
 
     public FelixResolverState(Logger logger)
     {
         m_logger = logger;
+        m_modules = new ArrayList<Module>();
+
+        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);
+
     }
 
-    public synchronized void addModule(IModule module)
+    public synchronized void addModule(Module module)
     {
         if (Util.isFragment(module))
         {
@@ -77,7 +82,7 @@
 //dumpPackageIndex(m_resolvedPkgIndex);
     }
 
-    public synchronized void removeModule(IModule module)
+    public synchronized void removeModule(Module module)
     {
         if (Util.isFragment(module))
         {
@@ -89,10 +94,10 @@
         }
     }
 
-    private void addFragment(IModule fragment)
+    private void addFragment(Module fragment)
     {
 // TODO: FRAGMENT - This should check to make sure that the host allows fragments.
-        IModule bestFragment = indexFragment(m_fragmentMap, fragment);
+        Module bestFragment = indexFragment(m_fragmentMap, fragment);
 
         // If the newly added fragment is the highest version for
         // its given symbolic name, then try to merge it to any
@@ -108,20 +113,20 @@
             List matchingHosts = getMatchingHosts(fragment);
             for (int hostIdx = 0; hostIdx < matchingHosts.size(); hostIdx++)
             {
-                IModule host = ((ICapability) matchingHosts.get(hostIdx)).getModule();
+                Module host = ((Capability) matchingHosts.get(hostIdx)).getModule();
 
                 // Get the fragments currently attached to the host so we
                 // can remove the older version of the current fragment, if any.
-                IModule[] fragments = ((ModuleImpl) host).getFragments();
-                List fragmentList = new ArrayList();
+                List<Module> fragments = ((ModuleImpl) host).getFragments();
+                List<Module> fragmentList = new ArrayList();
                 for (int fragIdx = 0;
-                    (fragments != null) && (fragIdx < fragments.length);
+                    (fragments != null) && (fragIdx < fragments.size());
                     fragIdx++)
                 {
-                    if (!fragments[fragIdx].getSymbolicName().equals(
+                    if (!fragments.get(fragIdx).getSymbolicName().equals(
                         bestFragment.getSymbolicName()))
                     {
-                        fragmentList.add(fragments[fragIdx]);
+                        fragmentList.add(fragments.get(fragIdx));
                     }
                 }
 
@@ -131,7 +136,7 @@
                     (index < 0) && (listIdx < fragmentList.size());
                     listIdx++)
                 {
-                    IModule f = (IModule) fragmentList.get(listIdx);
+                    Module f = fragmentList.get(listIdx);
                     if (bestFragment.getBundle().getBundleId()
                         < f.getBundle().getBundleId())
                     {
@@ -142,20 +147,16 @@
                     (index < 0) ? fragmentList.size() : index, bestFragment);
 
                 // Remove host's existing exported packages from index.
-                ICapability[] caps = host.getCapabilities();
-                for (int i = 0; (caps != null) && (i < caps.length); i++)
+                List<Capability> caps = host.getCapabilities();
+                for (int i = 0; (caps != null) && (i < caps.size()); i++)
                 {
-                    if (caps[i].getNamespace().equals(ICapability.PACKAGE_NAMESPACE))
+                    if (caps.get(i).getNamespace().equals(Capability.MODULE_NAMESPACE))
                     {
-                        // Get package name.
-                        String pkgName = (String)
-                            caps[i].getProperties().get(ICapability.PACKAGE_PROPERTY);
-                        // Remove from "unresolved" package map.
-                        List capList = (List) m_unresolvedPkgIndex.get(pkgName);
-                        if (capList != null)
-                        {
-                            capList.remove(caps[i]);
-                        }
+                        m_modCapSet.removeCapability(caps.get(i));
+                    }
+                    else if (caps.get(i).getNamespace().equals(Capability.PACKAGE_NAMESPACE))
+                    {
+                        m_pkgCapSet.removeCapability(caps.get(i));
                     }
                 }
 
@@ -163,9 +164,7 @@
                 checkForConflicts(host, fragmentList);
 
                 // Attach the fragments to the host.
-                fragments = (fragmentList.size() == 0)
-                    ? null
-                    : (IModule[]) fragmentList.toArray(new IModule[fragmentList.size()]);
+                fragments = (fragmentList.size() == 0) ? null : fragmentList;
                 try
                 {
                     ((ModuleImpl) host).attachFragments(fragments);
@@ -186,18 +185,22 @@
 
                 // Reindex the host's exported packages.
                 caps = host.getCapabilities();
-                for (int i = 0; (caps != null) && (i < caps.length); i++)
+                for (int i = 0; (caps != null) && (i < caps.size()); i++)
                 {
-                    if (caps[i].getNamespace().equals(ICapability.PACKAGE_NAMESPACE))
+                    if (caps.get(i).getNamespace().equals(Capability.MODULE_NAMESPACE))
+                    {
+                        m_modCapSet.addCapability(caps.get(i));
+                    }
+                    else if (caps.get(i).getNamespace().equals(Capability.PACKAGE_NAMESPACE))
                     {
-                        indexPackageCapability(m_unresolvedPkgIndex, caps[i]);
+                        m_pkgCapSet.addCapability(caps.get(i));
                     }
                 }
             }
         }
     }
 
-    private void removeFragment(IModule fragment)
+    private void removeFragment(Module fragment)
     {
         // Get fragment list, which may be null for system bundle fragments.
         List fragList = (List) m_fragmentMap.get(fragment.getSymbolicName());
@@ -217,71 +220,63 @@
             List matchingHosts = getMatchingHosts(fragment);
             for (int hostIdx = 0; hostIdx < matchingHosts.size(); hostIdx++)
             {
-                IModule host = ((ICapability) matchingHosts.get(hostIdx)).getModule();
+                Module host = ((Capability) matchingHosts.get(hostIdx)).getModule();
 
                 // Check to see if the removed fragment was actually merged with
                 // the host, since it might not be if it wasn't the highest version.
                 // If it was, recalculate the fragments for the host.
-                IModule[] fragments = ((ModuleImpl) host).getFragments();
-                for (int fragIdx = 0;
-                    (fragments != null) && (fragIdx < fragments.length);
-                    fragIdx++)
+                List<Module> fragments = ((ModuleImpl) host).getFragments();
+                if (fragments.contains(fragment))
                 {
-                    if (!fragments[fragIdx].equals(fragment))
-                    {
-                        List fragmentList = getMatchingFragments(host);
+                    List fragmentList = getMatchingFragments(host);
 
-                        // Remove host's existing exported packages from index.
-                        ICapability[] caps = host.getCapabilities();
-                        for (int i = 0; (caps != null) && (i < caps.length); i++)
+                    // Remove host's existing exported packages from index.
+                    List<Capability> caps = host.getCapabilities();
+                    for (int i = 0; (caps != null) && (i < caps.size()); i++)
+                    {
+                        if (caps.get(i).getNamespace().equals(Capability.MODULE_NAMESPACE))
                         {
-                            if (caps[i].getNamespace().equals(ICapability.PACKAGE_NAMESPACE))
-                            {
-                                // Get package name.
-                                String pkgName = (String)
-                                    caps[i].getProperties().get(ICapability.PACKAGE_PROPERTY);
-                                // Remove from "unresolved" package map.
-                                List capList = (List) m_unresolvedPkgIndex.get(pkgName);
-                                if (capList != null)
-                                {
-                                    capList.remove(caps[i]);
-                                }
-                            }
+                            m_modCapSet.removeCapability(caps.get(i));
                         }
+                        else if (caps.get(i).getNamespace().equals(Capability.PACKAGE_NAMESPACE))
+                        {
+                            m_pkgCapSet.removeCapability(caps.get(i));
+                        }
+                    }
 
-                        // Check if fragment conflicts with existing metadata.
-                        checkForConflicts(host, fragmentList);
+                    // Check if fragment conflicts with existing metadata.
+                    checkForConflicts(host, fragmentList);
 
-                        // Attach the fragments to the host.
-                        fragments = (fragmentList.size() == 0)
-                            ? null
-                            : (IModule[]) fragmentList.toArray(new IModule[fragmentList.size()]);
+                    // Attach the fragments to the host.
+                    try
+                    {
+                        ((ModuleImpl) host).attachFragments(fragmentList);
+                    }
+                    catch (Exception ex)
+                    {
+                        // Try to clean up by removing all fragments.
                         try
                         {
-                            ((ModuleImpl) host).attachFragments(fragments);
+                            ((ModuleImpl) host).attachFragments(null);
                         }
-                        catch (Exception ex)
+                        catch (Exception ex2)
                         {
-                            // Try to clean up by removing all fragments.
-                            try
-                            {
-                                ((ModuleImpl) host).attachFragments(null);
-                            }
-                            catch (Exception ex2)
-                            {
-                            }
-                            m_logger.log(Logger.LOG_ERROR,
-                                "Serious error attaching fragments.", ex);
                         }
+                        m_logger.log(Logger.LOG_ERROR,
+                            "Serious error attaching fragments.", ex);
+                    }
 
-                        // Reindex the host's exported packages.
-                        caps = host.getCapabilities();
-                        for (int i = 0; (caps != null) && (i < caps.length); i++)
+                    // Reindex the host's exported packages.
+                    caps = host.getCapabilities();
+                    for (int i = 0; (caps != null) && (i < caps.size()); i++)
+                    {
+                        if (caps.get(i).getNamespace().equals(Capability.MODULE_NAMESPACE))
                         {
-                            if (caps[i].getNamespace().equals(ICapability.PACKAGE_NAMESPACE))
-                            {
-                                indexPackageCapability(m_unresolvedPkgIndex, caps[i]);
-                            }
+                            m_modCapSet.addCapability(caps.get(i));
+                        }
+                        else if (caps.get(i).getNamespace().equals(Capability.PACKAGE_NAMESPACE))
+                        {
+                            m_pkgCapSet.addCapability(caps.get(i));
                         }
                     }
                 }
@@ -289,10 +284,10 @@
         }
     }
 
-    private List getMatchingHosts(IModule fragment)
+    private List getMatchingHosts(Module fragment)
     {
         // Find the fragment's host requirement.
-        IRequirement hostReq = getFragmentHostRequirement(fragment);
+        Requirement hostReq = getFragmentHostRequirement(fragment);
 
         // Create a list of all matching hosts for this fragment.
         List matchingHosts = new ArrayList();
@@ -305,9 +300,9 @@
                 return matchingHosts;
             }
         }
-        for (int hostIdx = 0; (hostReq != null) && (hostIdx < m_moduleList.size()); hostIdx++)
+        for (int hostIdx = 0; (hostReq != null) && (hostIdx < m_modules.size()); hostIdx++)
         {
-            IModule host = (IModule) m_moduleList.get(hostIdx);
+            Module host = m_modules.get(hostIdx);
             // Only look at unresolved hosts, since we don't support
             // dynamic attachment of fragments.
             if (host.isResolved()
@@ -318,7 +313,7 @@
             }
 
             // Find the host capability for the current host.
-            ICapability hostCap = Util.getSatisfyingCapability(host, hostReq);
+            Capability hostCap = Util.getSatisfyingCapability(host, hostReq);
 
             // If there is no host capability in the current module,
             // then just ignore it.
@@ -342,13 +337,14 @@
         return matchingHosts;
     }
 
-    private void checkForConflicts(IModule host, List fragmentList)
+    private void checkForConflicts(Module host, List fragmentList)
     {
         if ((fragmentList == null) || (fragmentList.size() == 0))
         {
             return;
         }
-
+// TODO: FELIX3 - Fix fragment conflict detection.
+/*
         // Verify the fragments do not have conflicting imports.
         // For now, just check for duplicate imports, but in the
         // future we might want to make this more fine grained.
@@ -356,10 +352,10 @@
         final int MODULE_IDX = 0, REQ_IDX = 1;
         Map ipMerged = new HashMap();
         Map rbMerged = new HashMap();
-        IRequirement[] reqs = host.getRequirements();
-        for (int reqIdx = 0; (reqs != null) && (reqIdx < reqs.length); reqIdx++)
+        List<Requirement> reqs = host.getRequirements();
+        for (int reqIdx = 0; (reqs != null) && (reqIdx < reqs.size()); reqIdx++)
         {
-            if (reqs[reqIdx].getNamespace().equals(ICapability.PACKAGE_NAMESPACE))
+            if (reqs.get(reqIdx).getNamespace().equals(Capability.PACKAGE_NAMESPACE))
             {
                 ipMerged.put(
                     ((Requirement) reqs[reqIdx]).getTargetName(),
@@ -435,8 +431,9 @@
                 rbMerged.put(entry.getKey(), entry.getValue());
             }
         }
+*/
     }
-
+/*
     private boolean isRequirementConflicting(
         Requirement existing, Requirement additional)
     {
@@ -536,29 +533,27 @@
         // They do no conflict.
         return false;
     }
-
-    private void addHost(IModule host)
+*/
+    private void addHost(Module host)
     {
         // When a module is added, we first need to pre-merge any potential fragments
         // into the host and then second create an aggregated list of unresolved
         // capabilities to simplify later processing when resolving bundles.
-        m_moduleList.add(host);
+        m_modules.add(host);
 
         //
         // First, merge applicable fragments.
         //
 
-        List fragmentList = getMatchingFragments(host);
+        List<Module> fragments = getMatchingFragments(host);
 
         // Attach any fragments we found for this host.
-        if (fragmentList.size() > 0)
+        if (fragments.size() > 0)
         {
             // Check if fragment conflicts with existing metadata.
-            checkForConflicts(host, fragmentList);
+            checkForConflicts(host, fragments);
 
             // Attach the fragments to the host.
-            IModule[] fragments =
-                (IModule[]) fragmentList.toArray(new IModule[fragmentList.size()]);
             try
             {
                 ((ModuleImpl) host).attachFragments(fragments);
@@ -582,53 +577,43 @@
         // Second, index module's capabilities.
         //
 
-        ICapability[] caps = host.getCapabilities();
+        List<Capability> caps = host.getCapabilities();
 
         // Add exports to unresolved package map.
-        for (int i = 0; (caps != null) && (i < caps.length); i++)
+        for (int i = 0; (caps != null) && (i < caps.size()); i++)
         {
-            if (caps[i].getNamespace().equals(ICapability.PACKAGE_NAMESPACE))
+            if (caps.get(i).getNamespace().equals(Capability.MODULE_NAMESPACE))
+            {
+                m_modCapSet.addCapability(caps.get(i));
+            }
+            else if (caps.get(i).getNamespace().equals(Capability.PACKAGE_NAMESPACE))
             {
-                indexPackageCapability(m_unresolvedPkgIndex, caps[i]);
+                m_pkgCapSet.addCapability(caps.get(i));
             }
         }
     }
 
-    private void removeHost(IModule host)
+    private void removeHost(Module host)
     {
         // We need remove the host's exports from the "resolved" and
         // "unresolved" package maps, remove its dependencies on fragments
         // and exporters, and remove it from the module list.
-        m_moduleList.remove(host);
+        m_modules.remove(host);
 
         // Remove exports from package maps.
-        ICapability[] caps = host.getCapabilities();
-        for (int i = 0; (caps != null) && (i < caps.length); i++)
+        List<Capability> caps = host.getCapabilities();
+        for (int i = 0; (caps != null) && (i < caps.size()); i++)
         {
-            if (caps[i].getNamespace().equals(ICapability.PACKAGE_NAMESPACE))
+            if (caps.get(i).getNamespace().equals(Capability.MODULE_NAMESPACE))
             {
-                // Get package name.
-                String pkgName = (String)
-                    caps[i].getProperties().get(ICapability.PACKAGE_PROPERTY);
-                // Remove from "unresolved" package map.
-                List capList = (List) m_unresolvedPkgIndex.get(pkgName);
-                if (capList != null)
-                {
-                    capList.remove(caps[i]);
-                }
-
-                // Remove from "resolved" package map.
-                capList = (List) m_resolvedPkgIndex.get(pkgName);
-                if (capList != null)
-                {
-                    capList.remove(caps[i]);
-                }
+                m_modCapSet.removeCapability(caps.get(i));
+            }
+            else if (caps.get(i).getNamespace().equals(Capability.PACKAGE_NAMESPACE))
+            {
+                m_pkgCapSet.removeCapability(caps.get(i));
             }
         }
 
-        // Remove the module from the "resolved" map.
-        m_resolvedCapMap.remove(host);
-
         // Set fragments to null, which will remove the module from all
         // of its dependent fragment modules.
         try
@@ -644,11 +629,11 @@
         ((ModuleImpl) host).setWires(null);
     }
 
-    private List getMatchingFragments(IModule host)
+    private List getMatchingFragments(Module host)
     {
         // Find the host capability for the current host.
-        ICapability[] caps = Util.getCapabilityByNamespace(host, ICapability.HOST_NAMESPACE);
-        ICapability hostCap = (caps.length == 0) ? null : caps[0];
+        List<Capability> caps = Util.getCapabilityByNamespace(host, Capability.HOST_NAMESPACE);
+        Capability hostCap = (caps.size() == 0) ? null : caps.get(0);
 
         // If we have a host capability, then loop through all fragments trying to
         // find ones that match.
@@ -665,10 +650,10 @@
         {
             Map.Entry entry = (Map.Entry) it.next();
             List fragments = (List) entry.getValue();
-            IModule fragment = null;
+            Module fragment = null;
             for (int i = 0; (fragment == null) && (i < fragments.size()); i++)
             {
-                IModule f = (IModule) fragments.get(i);
+                Module f = (Module) fragments.get(i);
                 if (!((BundleImpl) f.getBundle()).isStale()
                     && !((BundleImpl) f.getBundle()).isRemovalPending())
                 {
@@ -688,11 +673,11 @@
                     continue;
                 }
             }
-            IRequirement hostReq = getFragmentHostRequirement(fragment);
+            Requirement hostReq = getFragmentHostRequirement(fragment);
 
             // If we have a host requirement, then loop through each host and
             // see if it matches the host requirement.
-            if ((hostReq != null) && hostReq.isSatisfied(hostCap))
+            if ((hostReq != null) && CapabilitySet.matches(hostCap, hostReq.getFilter()))
             {
                 // Now add the new fragment in bundle ID order.
                 int index = -1;
@@ -700,7 +685,7 @@
                     (index < 0) && (listIdx < fragmentList.size());
                     listIdx++)
                 {
-                    IModule existing = (IModule) fragmentList.get(listIdx);
+                    Module existing = (Module) fragmentList.get(listIdx);
                     if (fragment.getBundle().getBundleId()
                         < existing.getBundle().getBundleId())
                     {
@@ -715,16 +700,16 @@
         return fragmentList;
     }
 
-    public synchronized IModule findHost(IModule rootModule) throws ResolveException
+    public synchronized Module findHost(Module rootModule) throws ResolveException
     {
-        IModule newRootModule = rootModule;
+        Module newRootModule = rootModule;
         if (Util.isFragment(rootModule))
         {
             List matchingHosts = getMatchingHosts(rootModule);
-            IModule currentBestHost = null;
+            Module currentBestHost = null;
             for (int hostIdx = 0; hostIdx < matchingHosts.size(); hostIdx++)
             {
-                IModule host = ((ICapability) matchingHosts.get(hostIdx)).getModule();
+                Module host = ((Capability) matchingHosts.get(hostIdx)).getModule();
                 if (currentBestHost == null)
                 {
                     currentBestHost = host;
@@ -746,16 +731,16 @@
         return newRootModule;
     }
 
-    private IRequirement getFragmentHostRequirement(IModule fragment)
+    private Requirement getFragmentHostRequirement(Module fragment)
     {
         // Find the fragment's host requirement.
-        IRequirement[] reqs = fragment.getRequirements();
-        IRequirement hostReq = null;
-        for (int reqIdx = 0; (hostReq == null) && (reqIdx < reqs.length); reqIdx++)
+        List<Requirement> reqs = fragment.getRequirements();
+        Requirement hostReq = null;
+        for (int reqIdx = 0; (hostReq == null) && (reqIdx < reqs.size()); reqIdx++)
         {
-            if (reqs[reqIdx].getNamespace().equals(ICapability.HOST_NAMESPACE))
+            if (reqs.get(reqIdx).getNamespace().equals(Capability.HOST_NAMESPACE))
             {
-                hostReq = reqs[reqIdx];
+                hostReq = reqs.get(reqIdx);
             }
         }
         return hostReq;
@@ -767,60 +752,30 @@
      * to capture additional capabilities.
      * @param module The module being refresh, which should always be the system bundle.
     **/
-    synchronized void refreshSystemBundleModule(IModule module)
+    synchronized void refreshSystemBundleModule(Module module)
     {
         // The system bundle module should always be resolved, so we only need
         // to update the resolved capability map.
-        ICapability[] caps = module.getCapabilities();
-        for (int i = 0; (caps != null) && (i < caps.length); i++)
+        List<Capability> caps = module.getCapabilities();
+        for (int i = 0; (caps != null) && (i < caps.size()); i++)
         {
-            List resolvedCaps = (List) m_resolvedCapMap.get(module);
-            if (resolvedCaps == null)
+            if (caps.get(i).getNamespace().equals(Capability.MODULE_NAMESPACE))
             {
-                m_resolvedCapMap.put(module, resolvedCaps = new ArrayList());
+                m_modCapSet.addCapability(caps.get(i));
             }
-            if (!resolvedCaps.contains(caps[i]))
+            else if (caps.get(i).getNamespace().equals(Capability.PACKAGE_NAMESPACE))
             {
-                resolvedCaps.add(caps[i]);
-            }
-
-            // 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 (caps[i].getNamespace().equals(ICapability.PACKAGE_NAMESPACE))
-            {
-                // Add to "resolved" package index.
-                indexPackageCapability(m_resolvedPkgIndex, caps[i]);
-            }
-        }
-    }
-
-    private void dumpPackageIndex(Map pkgIndex)
-    {
-        for (Iterator i = pkgIndex.entrySet().iterator(); i.hasNext(); )
-        {
-            Map.Entry entry = (Map.Entry) i.next();
-            List capList = (List) entry.getValue();
-            if (capList.size() > 0)
-            {
-                if (!((capList.size() == 1) && ((ICapability) capList.get(0)).getModule().getId().equals("0")))
-                {
-                    System.out.println("  " + entry.getKey());
-                    for (int j = 0; j < capList.size(); j++)
-                    {
-                        System.out.println("    " + ((ICapability) capList.get(j)).getModule());
-                    }
-                }
+                m_pkgCapSet.addCapability(caps.get(i));
             }
         }
     }
 
-    public synchronized IModule[] getModules()
+    /*public */synchronized List<Module> getModules()
     {
-        return (IModule[]) m_moduleList.toArray(new IModule[m_moduleList.size()]);
+        return m_modules;
     }
 
-    public synchronized void moduleResolved(IModule module)
+    public synchronized void moduleResolved(Module module)
     {
         if (module.isResolved())
         {
@@ -832,40 +787,22 @@
             // 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.
-            ICapability[] caps = module.getCapabilities();
+            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]);
-                }
-            }
-
-            // Next create a copy of the module's capabilities so we can
+            // 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);
-            }
+            List<Capability> capsCopy = (caps == null) ? null : new ArrayList(caps);
             // 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++)
+            List<Wire> wires = module.getWires();
+            for (int capIdx = 0; (capsCopy != null) && (capIdx < caps.size()); 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++)
+                for (int wireIdx = 0; (wires != null) && (wireIdx < wires.size()); wireIdx++)
                 {
                     // If one of the module's capabilities satifies the requirement
                     // for an existing wire, this means the capability was
@@ -873,9 +810,10 @@
                     // 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]))
+                    if (CapabilitySet.matches(
+                        caps.get(capIdx), wires.get(wireIdx).getRequirement().getFilter()))
                     {
-                        capsCopy[capIdx] = null;
+                        capsCopy.remove(caps.get(capIdx));
                         break;
                     }
                 }
@@ -883,276 +821,44 @@
 
             // 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++)
+// TODO: FELIX3 - This is actually reversed, we need to remove exports that were imported.
+/*
+            for (int capIdx = 0; (capsCopy != null) && (capIdx < capsCopy.size()); capIdx++)
             {
-                if (capsCopy[capIdx] != null)
+                if (capsCopy.get(capIdx).getNamespace().equals(Capability.MODULE_NAMESPACE))
                 {
-                    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]);
-                    }
+                    m_modCapSet.addCapability(capsCopy.get(capIdx));
                 }
-            }
-        }
-
-//System.out.println("UNRESOLVED PACKAGES:");
-//dumpPackageIndex(m_unresolvedPkgIndex);
-//System.out.println("RESOLVED PACKAGES:");
-//dumpPackageIndex(m_resolvedPkgIndex);
-    }
-
-    public synchronized List getResolvedCandidates(IRequirement req, IModule reqModule)
-    {
-        // Synchronized on the module manager to make sure that no
-        // modules are added, removed, or resolved.
-        List candidates = new ArrayList();
-        if (req.getNamespace().equals(ICapability.PACKAGE_NAMESPACE)
-            && (((Requirement) req).getTargetName() != null))
-        {
-            String pkgName = ((Requirement) req).getTargetName();
-            List capList = (List) m_resolvedPkgIndex.get(pkgName);
-
-            for (int capIdx = 0; (capList != null) && (capIdx < capList.size()); capIdx++)
-            {
-                ICapability cap = (ICapability) capList.get(capIdx);
-                if (req.isSatisfied(cap))
-                {
-                    if (System.getSecurityManager() != null)
-                    {
-                        if (reqModule != ((ICapability) capList.get(capIdx)).getModule())
-                        {
-                            if ((!((BundleProtectionDomain)((ICapability) 
-                                capList.get(capIdx)).getModule().getSecurityContext()).impliesDirect(
-                                new PackagePermission(((Requirement) req).getTargetName(), PackagePermission.EXPORTONLY))) ||
-                                !((reqModule == null) ||
-                                ((BundleProtectionDomain) reqModule.getSecurityContext()).impliesDirect(
-                                new PackagePermission(((Requirement) req).getTargetName(), ((ICapability) 
-                                capList.get(capIdx)).getModule().getBundle(),PackagePermission.IMPORT))
-                                ))
-                            {
-                                continue;
-                            }
-                        }
-                    }
-                    candidates.add(cap);
-                }
-            }
-        }
-        else
-        {
-            Iterator i = m_resolvedCapMap.entrySet().iterator();
-            while (i.hasNext())
-            {
-                Map.Entry entry = (Map.Entry) i.next();
-                IModule module = (IModule) entry.getKey();
-                List caps = (List) entry.getValue();
-                for (int capIdx = 0; (caps != null) && (capIdx < caps.size()); capIdx++)
+                else if (capsCopy.get(capIdx).getNamespace().equals(Capability.PACKAGE_NAMESPACE))
                 {
-                    ICapability cap = (ICapability) caps.get(capIdx);
-                    if (req.isSatisfied(cap))
-                    {
-                        if (System.getSecurityManager() != null)
-                        {
-                            if (req.getNamespace().equals(ICapability.PACKAGE_NAMESPACE) && (
-                                !((BundleProtectionDomain) cap.getModule().getSecurityContext()).impliesDirect(
-                                new PackagePermission((String) cap.getProperties().get(ICapability.PACKAGE_PROPERTY), PackagePermission.EXPORTONLY)) ||
-                                !((reqModule == null) ||
-                                ((BundleProtectionDomain) reqModule.getSecurityContext()).impliesDirect(
-                                new PackagePermission((String) cap.getProperties().get(ICapability.PACKAGE_PROPERTY), cap.getModule().getBundle(),PackagePermission.IMPORT))
-                                )))
-                            {
-                                if (reqModule != cap.getModule())
-                                {
-                                    continue;
-                                }
-                            }
-                            if (req.getNamespace().equals(ICapability.MODULE_NAMESPACE) && (
-                                !((BundleProtectionDomain) cap.getModule().getSecurityContext()).impliesDirect(
-                                new BundlePermission(cap.getModule().getSymbolicName(), BundlePermission.PROVIDE)) ||
-                                !((reqModule == null) ||
-                                ((BundleProtectionDomain) reqModule.getSecurityContext()).impliesDirect(
-                                new BundlePermission(reqModule.getSymbolicName(), BundlePermission.REQUIRE))
-                                )))
-                            {
-                                continue;
-                            }
-                        }
-                        candidates.add(cap);
-                    }
+                    m_pkgCapSet.addCapability(capsCopy.get(capIdx));
                 }
             }
+*/
         }
-        Collections.sort(candidates);
-        return candidates;
     }
 
-    public synchronized List getUnresolvedCandidates(IRequirement req, IModule reqModule)
+    public Set<Capability> getCandidates(Module module, Requirement req)
     {
-        // Get all matching unresolved capabilities.
-        List candidates = new ArrayList();
-        if (req.getNamespace().equals(ICapability.PACKAGE_NAMESPACE) &&
-            (((Requirement) req).getTargetName() != null))
-        {
-            List capList = (List) m_unresolvedPkgIndex.get(((Requirement) req).getTargetName());
-            for (int capIdx = 0; (capList != null) && (capIdx < capList.size()); capIdx++)
-            {
-                // If compatible and it is not currently resolved, then add
-                // the unresolved candidate to the list.
-                if (req.isSatisfied((ICapability) capList.get(capIdx)))
-                {
-                    if (System.getSecurityManager() != null)
-                    {
-                        if (reqModule != ((ICapability) capList.get(capIdx)).getModule())
-                        {
-                            if (!((BundleProtectionDomain)((ICapability) 
-                                capList.get(capIdx)).getModule().getSecurityContext()).impliesDirect(
-                                new PackagePermission(((Requirement) req).getTargetName(), PackagePermission.EXPORTONLY)) ||
-                                !((reqModule == null) ||
-                                ((BundleProtectionDomain) reqModule.getSecurityContext()).impliesDirect(
-                                new PackagePermission(((Requirement) req).getTargetName(), ((ICapability) 
-                                capList.get(capIdx)).getModule().getBundle(),PackagePermission.IMPORT))
-                                ))
-                            {
-                                continue;
-                            }
-                        }
-                    }
-                    candidates.add(capList.get(capIdx));
-                }
-            }
+        Set<Capability> result = new TreeSet(new CandidateComparator());
+
+        if (req.getNamespace().equals(Capability.MODULE_NAMESPACE))
+        {
+            result.addAll(m_modCapSet.match(req.getFilter()));
         }
-        else
+        else if (req.getNamespace().equals(Capability.PACKAGE_NAMESPACE))
         {
-            IModule[] modules = getModules();
-            for (int modIdx = 0; (modules != null) && (modIdx < modules.length); modIdx++)
-            {
-                // Get the module's export package for the target package.
-                ICapability cap = Util.getSatisfyingCapability(modules[modIdx], req);
-                // If compatible and it is not currently resolved, then add
-                // the unresolved candidate to the list.
-                if ((cap != null) && !modules[modIdx].isResolved())
-                {
-                    if (System.getSecurityManager() != null)
-                    {
-                        if (req.getNamespace().equals(ICapability.PACKAGE_NAMESPACE) && (
-                            !((BundleProtectionDomain) cap.getModule().getSecurityContext()).impliesDirect(
-                            new PackagePermission((String) cap.getProperties().get(ICapability.PACKAGE_PROPERTY), PackagePermission.EXPORTONLY)) ||
-                            !((reqModule == null) ||
-                            ((BundleProtectionDomain) reqModule.getSecurityContext()).impliesDirect(
-                            new PackagePermission((String) cap.getProperties().get(ICapability.PACKAGE_PROPERTY), cap.getModule().getBundle(),PackagePermission.IMPORT))
-                            )))
-                        {
-                            if (reqModule != cap.getModule())
-                            {
-                                continue;
-                            }
-                        }
-                        if (req.getNamespace().equals(ICapability.MODULE_NAMESPACE) && (
-                                !((BundleProtectionDomain) cap.getModule().getSecurityContext()).impliesDirect(
-                                new BundlePermission(cap.getModule().getSymbolicName(), BundlePermission.PROVIDE)) ||
-                                !((reqModule == null) ||
-                                ((BundleProtectionDomain) reqModule.getSecurityContext()).impliesDirect(
-                                new BundlePermission(reqModule.getSymbolicName(), BundlePermission.REQUIRE))
-                                )))
-                            {
-                                continue;
-                            }
-                    }
-                    candidates.add(cap);
-                }
-            }
+            result.addAll(m_pkgCapSet.match(req.getFilter()));
         }
 
-        // Create list of compatible providers.
-        Collections.sort(candidates);
-        return candidates;
+        return result;
     }
 
     //
     // Utility methods.
     //
 
-    private void indexPackageCapability(Map map, ICapability capability)
-    {
-        if (capability.getNamespace().equals(ICapability.PACKAGE_NAMESPACE))
-        {
-            String pkgName = (String)
-                capability.getProperties().get(ICapability.PACKAGE_PROPERTY);
-            List capList = (List) map.get(pkgName);
-
-            // We want to add the capability into the list of exporters
-            // in sorted order (descending version and ascending bundle
-            // identifier). Insert using a simple binary search algorithm.
-            if (capList == null)
-            {
-                capList = new ArrayList();
-                capList.add(capability);
-            }
-            else
-            {
-                Version version = (Version)
-                    capability.getProperties().get(ICapability.VERSION_PROPERTY);
-                Version middleVersion = null;
-                int top = 0, bottom = capList.size() - 1, middle = 0;
-                while (top <= bottom)
-                {
-                    middle = (bottom - top) / 2 + top;
-                    middleVersion = (Version)
-                        ((ICapability) capList.get(middle))
-                            .getProperties().get(ICapability.VERSION_PROPERTY);
-                    // Sort in reverse version order.
-                    int cmp = middleVersion.compareTo(version);
-                    if (cmp < 0)
-                    {
-                        bottom = middle - 1;
-                    }
-                    else if (cmp == 0)
-                    {
-                        // Sort further by ascending bundle ID.
-                        long middleId = ((ICapability) capList.get(middle))
-                            .getModule().getBundle().getBundleId();
-                        long exportId = capability.getModule().getBundle().getBundleId();
-                        if (middleId < exportId)
-                        {
-                            top = middle + 1;
-                        }
-                        else
-                        {
-                            bottom = middle - 1;
-                        }
-                    }
-                    else
-                    {
-                        top = middle + 1;
-                    }
-                }
-
-                // Ignore duplicates.
-                if ((top >= capList.size()) || (capList.get(top) != capability))
-                {
-                    capList.add(top, capability);
-                }
-            }
-
-            map.put(pkgName, capList);
-        }
-    }
-
-    private IModule indexFragment(Map map, IModule module)
+    private Module indexFragment(Map map, Module module)
     {
         List modules = (List) map.get(module.getSymbolicName());
 
@@ -1173,7 +879,7 @@
             while (top <= bottom)
             {
                 middle = (bottom - top) / 2 + top;
-                middleVersion = ((IModule) modules.get(middle)).getVersion();
+                middleVersion = ((Module) modules.get(middle)).getVersion();
                 // Sort in reverse version order.
                 int cmp = middleVersion.compareTo(version);
                 if (cmp < 0)
@@ -1183,7 +889,7 @@
                 else if (cmp == 0)
                 {
                     // Sort further by ascending bundle ID.
-                    long middleId = ((IModule) modules.get(middle)).getBundle().getBundleId();
+                    long middleId = ((Module) modules.get(middle)).getBundle().getBundleId();
                     long exportId = module.getBundle().getBundleId();
                     if (middleId < exportId)
                     {
@@ -1209,6 +915,6 @@
 
         map.put(module.getSymbolicName(), modules);
 
-        return (IModule) modules.get(0);
+        return (Module) modules.get(0);
     }
 }
\ No newline at end of file

Modified: felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/ModuleImpl.java
URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/ModuleImpl.java?rev=901715&r1=901714&r2=901715&view=diff
==============================================================================
--- felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/ModuleImpl.java (original)
+++ felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/ModuleImpl.java Thu Jan 21 14:15:58 2010
@@ -50,6 +50,7 @@
 import org.apache.felix.framework.candidateset.Wire;
 import org.apache.felix.framework.resolver.ResolveException;
 import org.apache.felix.framework.resolver.WireImpl;
+import org.apache.felix.framework.resolver.WireModuleImpl;
 import org.apache.felix.framework.util.CompoundEnumeration;
 import org.apache.felix.framework.util.FelixConstants;
 import org.apache.felix.framework.util.SecureAction;
@@ -63,7 +64,6 @@
 import org.osgi.framework.BundleException;
 import org.osgi.framework.BundleReference;
 import org.osgi.framework.Constants;
-import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.Version;
 
 public class ModuleImpl implements Module

Modified: felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/resolver/ResolverImpl.java
URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/resolver/ResolverImpl.java?rev=901715&r1=901714&r2=901715&view=diff
==============================================================================
--- felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/resolver/ResolverImpl.java (original)
+++ felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/resolver/ResolverImpl.java Thu Jan 21 14:15:58 2010
@@ -41,6 +41,9 @@
     private static final Map<String, Long> m_invokeCounts = new HashMap<String, Long>();
     private static boolean m_isInvokeCount = false;
 
+    // Reusable empty array.
+    private static final List<Wire> m_emptyWires = new ArrayList<Wire>(0);
+
     public ResolverImpl()
     {
 System.out.println("+++ PROTO3 RESOLVER");
@@ -814,8 +817,10 @@
 
         if (!module.isResolved() && (wireMap.get(module) == null))
         {
+            wireMap.put(module, m_emptyWires);
+
+            List<Wire> packageWires = new ArrayList<Wire>();
             List<Wire> moduleWires = new ArrayList<Wire>();
-            wireMap.put(module, moduleWires);
 
             Packages pkgs = modulePkgMap.get(module);
             for (Entry<String, Blame> entry : pkgs.m_importedPkgs.entrySet())
@@ -828,7 +833,7 @@
                 // Ignore modules that import themselves.
                 if (!module.equals(entry.getValue().m_cap.getModule()))
                 {
-                    moduleWires.add(
+                    packageWires.add(
                         new WireImpl(module,
                             entry.getValue().m_reqs.get(entry.getValue().m_reqs.size() - 1),
                             entry.getValue().m_cap.getModule(),
@@ -871,15 +876,21 @@
                         if (!module.equals(rbEntry.getValue().getModule()))
                         {
                             moduleWires.add(
-                                new WireImpl(module,
+                                new WireModuleImpl(module,
                                     rbEntry.getKey(),
                                     rbEntry.getValue().getModule(),
-                                    rbEntry.getValue()));
+                                    rbEntry.getValue(),
+                                    pkgs.getRequiredPackages(rbEntry.getValue())));
                         }
                     }
                 }
             }
+
+            // Combine wires with module wires last.
+            packageWires.addAll(moduleWires);
+            wireMap.put(module, packageWires);
         }
+
         return wireMap;
     }
 
@@ -917,6 +928,24 @@
             m_requiredPkgs.putAll(packages.m_requiredPkgs);
             m_usedPkgs.putAll(packages.m_usedPkgs);
         }
+
+        public List<String> getRequiredPackages(Capability source)
+        {
+            List<String> pkgs = new ArrayList();
+            for (Entry<String, List<Blame>> entry : m_requiredPkgs.entrySet())
+            {
+                for (Blame blame : entry.getValue())
+                {
+                    if (blame.m_cap.equals(source))
+                    {
+                        pkgs.add((String)
+                            blame.m_cap.getAttribute(Capability.PACKAGE_ATTR).getValue());
+                        break;
+                    }
+                }
+            }
+            return pkgs;
+        }
     }
 
     private static class Blame

Modified: felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/resolver/ResolverStateImpl.java
URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/resolver/ResolverStateImpl.java?rev=901715&r1=901714&r2=901715&view=diff
==============================================================================
--- felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/resolver/ResolverStateImpl.java (original)
+++ felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/resolver/ResolverStateImpl.java Thu Jan 21 14:15:58 2010
@@ -76,11 +76,6 @@
 
     public Set<Capability> getCandidates(Module module, Requirement req)
     {
-        return getUnresolvedCandidates(module, req);
-    }
-
-    private Set<Capability> getUnresolvedCandidates(Module module, Requirement req)
-    {
         Set<Capability> result = new TreeSet(new CandidateComparator());
 
         if (req.getNamespace().equals(Capability.MODULE_NAMESPACE))
@@ -94,9 +89,4 @@
 
         return result;
     }
-
-    private Set<Capability> getResolvedCandidates(Module module, Requirement req)
-    {
-        throw new UnsupportedOperationException("Not supported yet.");
-    }
 }
\ No newline at end of file

Added: felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/resolver/WireModuleImpl.java
URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/resolver/WireModuleImpl.java?rev=901715&view=auto
==============================================================================
--- felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/resolver/WireModuleImpl.java (added)
+++ felix/sandbox/rickhall/framework-proto/src/main/java/org/apache/felix/framework/resolver/WireModuleImpl.java Thu Jan 21 14:15:58 2010
@@ -0,0 +1,168 @@
+/*
+ * 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.framework.resolver;
+
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Map;
+import org.apache.felix.framework.candidateset.Capability;
+import org.apache.felix.framework.candidateset.Module;
+import org.apache.felix.framework.candidateset.Requirement;
+import org.apache.felix.framework.candidateset.Wire;
+import org.apache.felix.framework.util.Util;
+import org.apache.felix.moduleloader.ResourceNotFoundException;
+
+public class WireModuleImpl implements Wire
+{
+    private final Module m_importer;
+    private final Requirement m_requirement;
+    private final Module m_exporter;
+    private final Capability m_capability;
+    private final List<String> m_packages;
+
+    public WireModuleImpl(Module importer, Requirement requirement,
+        Module exporter, Capability capability, List<String> packages)
+    {
+        m_importer = importer;
+        m_requirement = requirement;
+        m_exporter = exporter;
+        m_capability = capability;
+        m_packages = packages;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.felix.framework.searchpolicy.IWire#getImporter()
+     */
+    public Module getImporter()
+    {
+        return m_importer;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.felix.framework.searchpolicy.IWire#getRequirement()
+     */
+    public Requirement getRequirement()
+    {
+        return m_requirement;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.felix.framework.searchpolicy.IWire#getExporter()
+     */
+    public Module getExporter()
+    {
+        return m_exporter;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.felix.framework.searchpolicy.IWire#getCapability()
+     */
+    public Capability getCapability()
+    {
+        return m_capability;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.felix.framework.searchpolicy.IWire#hasPackage(java.lang.String)
+     */
+    public boolean hasPackage(String pkgName)
+    {
+        return m_packages.contains(pkgName);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.felix.framework.searchpolicy.IWire#getClass(java.lang.String)
+     */
+    public Class getClass(String name) throws ClassNotFoundException
+    {
+        // Get the package of the target class.
+        String pkgName = Util.getClassPackage(name);
+        if (m_packages.contains(pkgName))
+        {
+            try
+            {
+                Class clazz = m_exporter.getClassByDelegation(name);
+                if (clazz != null)
+                {
+                    return clazz;
+                }
+            }
+            catch (ClassNotFoundException ex)
+            {
+                // Do not throw the exception here, since we want
+                // to continue search other package sources and
+                // ultimately the module's own content.
+            }
+        }
+
+        return null;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.felix.framework.searchpolicy.IWire#getResource(java.lang.String)
+     */
+    public URL getResource(String name) throws ResourceNotFoundException
+    {
+        // Get the package of the target class.
+        String pkgName = Util.getResourcePackage(name);
+        if (m_packages.contains(pkgName))
+        {
+            URL url = m_exporter.getResourceByDelegation(name);
+            if (url != null)
+            {
+                return url;
+            }
+
+            // Don't throw ResourceNotFoundException because module
+            // dependencies support split packages.
+        }
+
+        return null;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.felix.framework.searchpolicy.IWire#getResources(java.lang.String)
+     */
+    public Enumeration getResources(String name) throws ResourceNotFoundException
+    {
+        // Get the package of the target class.
+        String pkgName = Util.getResourcePackage(name);
+
+        // See if we have a resolved package for the resource's package.
+        if (m_packages.contains(pkgName))
+        {
+            Enumeration urls = m_exporter.getResourcesByDelegation(name);
+            if (urls != null)
+            {
+                return urls;
+            }
+
+            // Don't throw ResourceNotFoundException because module
+            // dependencies support split packages.
+        }
+
+        return null;
+    }
+
+    public String toString()
+    {
+        return m_importer + " -> " + m_capability + " -> " + m_exporter;
+    }
+}
\ No newline at end of file