FELIX-4851 : [Core R6] Support FrameworkWiring.findProviders(Requirement)

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1640160 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/Felix.java b/framework/src/main/java/org/apache/felix/framework/Felix.java
index fa2c129..7d69e1e 100644
--- a/framework/src/main/java/org/apache/felix/framework/Felix.java
+++ b/framework/src/main/java/org/apache/felix/framework/Felix.java
@@ -99,6 +99,7 @@
 import org.osgi.framework.wiring.BundleWire;
 import org.osgi.framework.wiring.BundleWiring;
 import org.osgi.framework.wiring.FrameworkWiring;
+import org.osgi.resource.Requirement;
 import org.osgi.service.packageadmin.ExportedPackage;
 
 public class Felix extends BundleImpl implements Framework
@@ -5329,4 +5330,9 @@
             }
         }
     }
+
+    Collection<BundleCapability> findProviders(final Requirement requirement)
+    {
+        return m_resolver.findProvidersInternal(null, requirement, true, false);
+    }
 }
diff --git a/framework/src/main/java/org/apache/felix/framework/FrameworkWiringImpl.java b/framework/src/main/java/org/apache/felix/framework/FrameworkWiringImpl.java
index 5789947..0ab4c5e 100644
--- a/framework/src/main/java/org/apache/felix/framework/FrameworkWiringImpl.java
+++ b/framework/src/main/java/org/apache/felix/framework/FrameworkWiringImpl.java
@@ -191,10 +191,11 @@
         }
     }
 
-    /* (non-Javadoc)
+    /**
      * @see org.osgi.framework.wiring.FrameworkWiring#findProviders(org.osgi.resource.Requirement)
      */
-    public Collection<BundleCapability> findProviders(Requirement requirement) {
-        throw new UnsupportedOperationException(); // TODO
+    public Collection<BundleCapability> findProviders(final Requirement requirement)
+    {
+        return m_felix.findProviders(requirement);
     }
 }
\ No newline at end of file
diff --git a/framework/src/main/java/org/apache/felix/framework/ResolveContextImpl.java b/framework/src/main/java/org/apache/felix/framework/ResolveContextImpl.java
index 393dabf..270da4f 100644
--- a/framework/src/main/java/org/apache/felix/framework/ResolveContextImpl.java
+++ b/framework/src/main/java/org/apache/felix/framework/ResolveContextImpl.java
@@ -77,11 +77,13 @@
         return new ArrayList<BundleRevision>(m_ondemand);
     }
 
+    @Override
     public List<BundleCapability> findProviders(BundleRequirement br, boolean obeyMandatory)
     {
-        return m_state.findProvidersInternal(m_resolverHookrecord, br, obeyMandatory);
+        return m_state.findProvidersInternal(m_resolverHookrecord, br, obeyMandatory, true);
     }
 
+    @Override
     public int insertHostedCapability(List<BundleCapability> caps, HostedCapability hc)
     {
         int idx = Collections.binarySearch(caps, hc, new CandidateComparator());
@@ -93,11 +95,13 @@
         return idx;
     }
 
+    @Override
     public boolean isEffective(BundleRequirement br)
     {
         return m_state.isEffective(br);
     }
 
+    @Override
     public Map<BundleRevision, BundleWiring> getWirings()
     {
         return m_wirings;
diff --git a/framework/src/main/java/org/apache/felix/framework/StatefulResolver.java b/framework/src/main/java/org/apache/felix/framework/StatefulResolver.java
index 0270af8..ae3bcfc 100644
--- a/framework/src/main/java/org/apache/felix/framework/StatefulResolver.java
+++ b/framework/src/main/java/org/apache/felix/framework/StatefulResolver.java
@@ -59,6 +59,7 @@
 import org.osgi.framework.wiring.BundleRevision;
 import org.osgi.framework.wiring.BundleWire;
 import org.osgi.framework.wiring.BundleWiring;
+import org.osgi.resource.Requirement;
 
 class StatefulResolver
 {
@@ -175,11 +176,14 @@
     {
         ResolverHookRecord record = new ResolverHookRecord(
             Collections.<ServiceReference<ResolverHookFactory>, ResolverHook>emptyMap(), null);
-        return findProvidersInternal(record, req, obeyMandatory);
+        return findProvidersInternal(record, req, obeyMandatory, true);
     }
 
     synchronized List<BundleCapability> findProvidersInternal(
-        ResolverHookRecord record, BundleRequirement req, boolean obeyMandatory)
+        final ResolverHookRecord record,
+        final Requirement req,
+        final boolean obeyMandatory,
+        final boolean invokeHooksAndSecurity)
     {
         List<BundleCapability> result = new ArrayList<BundleCapability>();
 
@@ -213,7 +217,7 @@
             for (BundleCapability cap : matches)
             {
                 // Filter according to security.
-                if (filteredBySecurity(req, cap))
+                if (invokeHooksAndSecurity && filteredBySecurity((BundleRequirement)req, cap))
                 {
                     continue;
                 }
@@ -229,36 +233,39 @@
             }
         }
 
-        // If we have resolver hooks, then we may need to filter our results
-        // based on a whitelist and/or fine-grained candidate filtering.
-        if (!result.isEmpty() && !record.getResolverHookRefs().isEmpty())
+        if ( invokeHooksAndSecurity )
         {
-            // It we have a whitelist, then first filter out candidates
-            // from disallowed revisions.
-            if (record.getBundleRevisionWhitelist() != null)
+            // If we have resolver hooks, then we may need to filter our results
+            // based on a whitelist and/or fine-grained candidate filtering.
+            if (!result.isEmpty() && !record.getResolverHookRefs().isEmpty())
             {
-                for (Iterator<BundleCapability> it = result.iterator(); it.hasNext(); )
+                // It we have a whitelist, then first filter out candidates
+                // from disallowed revisions.
+                if (record.getBundleRevisionWhitelist() != null)
                 {
-                    if (!record.getBundleRevisionWhitelist().contains(it.next().getRevision()))
+                    for (Iterator<BundleCapability> it = result.iterator(); it.hasNext(); )
                     {
-                        it.remove();
+                        if (!record.getBundleRevisionWhitelist().contains(it.next().getRevision()))
+                        {
+                            it.remove();
+                        }
                     }
                 }
-            }
 
-            // Now give the hooks a chance to do fine-grained filtering.
-            ShrinkableCollection<BundleCapability> shrinkable =
-                new ShrinkableCollection<BundleCapability>(result);
-            for (ResolverHook hook : record.getResolverHooks())
-            {
-                try
+                // Now give the hooks a chance to do fine-grained filtering.
+                ShrinkableCollection<BundleCapability> shrinkable =
+                    new ShrinkableCollection<BundleCapability>(result);
+                for (ResolverHook hook : record.getResolverHooks())
                 {
-                    Felix.m_secureAction
-                        .invokeResolverHookMatches(hook, req, shrinkable);
-                }
-                catch (Throwable th)
-                {
-                    m_logger.log(Logger.LOG_WARNING, "Resolver hook exception.", th);
+                    try
+                    {
+                        Felix.m_secureAction
+                            .invokeResolverHookMatches(hook, (BundleRequirement)req, shrinkable);
+                    }
+                    catch (Throwable th)
+                    {
+                        m_logger.log(Logger.LOG_WARNING, "Resolver hook exception.", th);
+                    }
                 }
             }
         }