FELIX-2109: OBR should support matching resources for a given requirement

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@912356 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/bundlerepository/pom.xml b/bundlerepository/pom.xml
index 5d62239..227715c 100644
--- a/bundlerepository/pom.xml
+++ b/bundlerepository/pom.xml
@@ -33,7 +33,7 @@
     <dependency>
       <groupId>${pom.groupId}</groupId>
       <artifactId>org.osgi.service.obr</artifactId>
-      <version>1.0.2</version>
+      <version>1.1.0-SNAPSHOT</version>
     </dependency>
     <dependency>
       <groupId>${pom.groupId}</groupId>
diff --git a/bundlerepository/src/main/java/org/apache/felix/bundlerepository/RepositoryAdminImpl.java b/bundlerepository/src/main/java/org/apache/felix/bundlerepository/RepositoryAdminImpl.java
index 3ee0d5f..15ef8a4 100644
--- a/bundlerepository/src/main/java/org/apache/felix/bundlerepository/RepositoryAdminImpl.java
+++ b/bundlerepository/src/main/java/org/apache/felix/bundlerepository/RepositoryAdminImpl.java
@@ -31,6 +31,7 @@
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Filter;
 import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.service.obr.Capability;
 import org.osgi.service.obr.Repository;
 import org.osgi.service.obr.RepositoryAdmin;
 import org.osgi.service.obr.Requirement;
@@ -164,6 +165,56 @@
         return resources;
     }
 
+    public synchronized Resource[] discoverResources(Requirement[] requirements)
+    {
+        if (!m_initialized)
+        {
+            initialize();
+        }
+
+        Resource[] resources = null;
+        MapToDictionary dict = new MapToDictionary(null);
+        Repository[] repos = listRepositories();
+        List matchList = new ArrayList();
+        for (int repoIdx = 0; (repos != null) && (repoIdx < repos.length); repoIdx++)
+        {
+            resources = repos[repoIdx].getResources();
+            for (int resIdx = 0; (resources != null) && (resIdx < resources.length); resIdx++)
+            {
+                dict.setSourceMap(resources[resIdx].getProperties());
+                boolean match = true;
+                for (int reqIdx = 0; (requirements != null) && (reqIdx < requirements.length); reqIdx++)
+                {
+                    boolean reqMatch = false;
+                    Capability[] caps = resources[resIdx].getCapabilities();
+                    for (int capIdx = 0; (caps != null) && (capIdx < caps.length); capIdx++)
+                    {
+                        if (caps[capIdx].getName().equals(requirements[reqIdx].getName())
+                                && requirements[reqIdx].isSatisfied(caps[capIdx]))
+                        {
+                            reqMatch = true;
+                            break;
+                        }
+                    }
+                    match &= reqMatch;
+                    if (!match)
+                    {
+                        break;
+                    }
+                }
+                if (match)
+                {
+                    matchList.add(resources[resIdx]);
+                }
+            }
+        }
+
+        // Convert matching resources to an array an sort them by name.
+        resources = (Resource[]) matchList.toArray(new Resource[matchList.size()]);
+        Arrays.sort(resources, m_nameComparator);
+        return resources;
+    }
+
     public Requirement requirement(String name, String filter) {
         RequirementImpl req = new RequirementImpl();
         req.setName(name);
diff --git a/bundlerepository/src/test/java/org/apache/felix/bundlerepository/RepositoryImplTest.java b/bundlerepository/src/test/java/org/apache/felix/bundlerepository/RepositoryImplTest.java
index 0413378..afc4c62 100644
--- a/bundlerepository/src/test/java/org/apache/felix/bundlerepository/RepositoryImplTest.java
+++ b/bundlerepository/src/test/java/org/apache/felix/bundlerepository/RepositoryImplTest.java
@@ -42,7 +42,7 @@
         assertEquals("referred.xml", refs[0].getUrl());
 
         // expect two resources
-        Resource[] res = repoAdmin.discoverResources(null);
+        Resource[] res = repoAdmin.discoverResources((String) null);
         assertNotNull("Expect Resource", res);
         assertEquals("Expect two resources", 2, res.length);
 
@@ -71,7 +71,7 @@
         assertEquals("referred.xml", refs[0].getUrl());
 
         // expect one resource (referral is not followed
-        Resource[] res = repoAdmin.discoverResources(null);
+        Resource[] res = repoAdmin.discoverResources((String) null);
         assertNotNull("Expect Resource", res);
         assertEquals("Expect one resource", 1, res.length);
 
diff --git a/bundlerepository/src/test/java/org/apache/felix/bundlerepository/ResolverImplTest.java b/bundlerepository/src/test/java/org/apache/felix/bundlerepository/ResolverImplTest.java
index 65bcca1..7decd10 100644
--- a/bundlerepository/src/test/java/org/apache/felix/bundlerepository/ResolverImplTest.java
+++ b/bundlerepository/src/test/java/org/apache/felix/bundlerepository/ResolverImplTest.java
@@ -23,6 +23,7 @@
 import junit.framework.TestCase;
 
 import org.osgi.service.obr.Repository;
+import org.osgi.service.obr.Requirement;
 import org.osgi.service.obr.Resolver;
 import org.osgi.service.obr.Resource;
 
@@ -53,7 +54,17 @@
 
     }
 
-    public void testResolveReq() throws Exception 
+    public void testMatchingReq() throws Exception
+    {
+        RepositoryAdminImpl repoAdmin = createRepositoryAdmin();
+        repoAdmin.addRepository(getClass().getResource("/repo_for_resolvertest.xml"));
+
+        Resource[] res = repoAdmin.discoverResources(new Requirement[] { repoAdmin.requirement("package", "(package=org.apache.felix.test.osgi)") });
+        assertNotNull(res);
+        assertEquals(1, res.length);
+    }
+
+    public void testResolveReq() throws Exception
     {
         RepositoryAdminImpl repoAdmin = createRepositoryAdmin();
         repoAdmin.addRepository(getClass().getResource("/repo_for_resolvertest.xml"));
diff --git a/org.osgi.service.obr/src/main/java/org/osgi/service/obr/RepositoryAdmin.java b/org.osgi.service.obr/src/main/java/org/osgi/service/obr/RepositoryAdmin.java
index 29fad00..dc822d0 100644
--- a/org.osgi.service.obr/src/main/java/org/osgi/service/obr/RepositoryAdmin.java
+++ b/org.osgi.service.obr/src/main/java/org/osgi/service/obr/RepositoryAdmin.java
@@ -76,6 +76,14 @@
     Resource[] discoverResources(String filterExpr);
 
     /**
+     * Discover any resources that match the given requirements.
+     *
+     * @param requirements
+     * @return List of resources matching the filter
+     */
+    Resource[] discoverResources(Requirement[] requirements);
+
+    /**
      * Create a resolver.
      * 
      * @return