FELIX-2113: leverage FELIX-2110 to implement obr:resolve and obr:find commands

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@912553 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/shell/obr/src/main/java/org/apache/felix/karaf/shell/obr/FindCommand.java b/karaf/shell/obr/src/main/java/org/apache/felix/karaf/shell/obr/FindCommand.java
index 51368e2..3de0a6e 100644
--- a/karaf/shell/obr/src/main/java/org/apache/felix/karaf/shell/obr/FindCommand.java
+++ b/karaf/shell/obr/src/main/java/org/apache/felix/karaf/shell/obr/FindCommand.java
@@ -21,9 +21,6 @@
 
 import org.apache.felix.gogo.commands.Argument;
 import org.apache.felix.gogo.commands.Command;
-import org.apache.felix.karaf.shell.obr.util.RequirementImpl;
-import org.osgi.framework.Filter;
-import org.osgi.framework.FrameworkUtil;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.Version;
 import org.osgi.service.obr.Capability;
@@ -41,7 +38,7 @@
     protected void doExecute(RepositoryAdmin admin) throws Exception {
         List<Resource> matching = new ArrayList<Resource>();
         Repository[] repos = admin.listRepositories();
-        Requirement req = parseRequirement(requirement);
+        Requirement req = parseRequirement(admin, requirement);
         for (int repoIdx = 0; (repos != null) && (repoIdx < repos.length); repoIdx++) {
             Resource[] resources = repos[repoIdx].getResources();
             for (int resIdx = 0; (resources != null) && (resIdx < resources.length); resIdx++) {
@@ -65,7 +62,7 @@
         }
     }
 
-    private Requirement parseRequirement(String req) throws InvalidSyntaxException {
+    private Requirement parseRequirement(RepositoryAdmin admin, String req) throws InvalidSyntaxException {
         int p = req.indexOf(':');
         String name;
         String filter;
@@ -83,8 +80,7 @@
         if (!filter.startsWith("(")) {
             filter = "(" + filter + ")";
         }
-        Filter flt = FrameworkUtil.createFilter(filter);
-        return new RequirementImpl(name, flt);
+        return admin.requirement(name, filter);
     }
 
 }
\ No newline at end of file
diff --git a/karaf/shell/obr/src/main/java/org/apache/felix/karaf/shell/obr/ResolveCommand.java b/karaf/shell/obr/src/main/java/org/apache/felix/karaf/shell/obr/ResolveCommand.java
index a64a6f7..fe5929d 100644
--- a/karaf/shell/obr/src/main/java/org/apache/felix/karaf/shell/obr/ResolveCommand.java
+++ b/karaf/shell/obr/src/main/java/org/apache/felix/karaf/shell/obr/ResolveCommand.java
@@ -21,10 +21,6 @@
 import org.apache.felix.gogo.commands.Argument;
 import org.apache.felix.gogo.commands.Command;
 import org.apache.felix.gogo.commands.Option;
-import org.apache.felix.karaf.shell.obr.util.RequirementImpl;
-import org.apache.felix.karaf.shell.obr.util.ResourceImpl;
-import org.osgi.framework.Filter;
-import org.osgi.framework.FrameworkUtil;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.service.obr.RepositoryAdmin;
 import org.osgi.service.obr.Requirement;
@@ -41,9 +37,10 @@
     List<String> requirements;
 
     protected void doExecute(RepositoryAdmin admin) throws Exception {
-        Resource resource = new ResourceImpl(null, null, null, null, null, null, getRequirements(), null, null, null);
         Resolver resolver = admin.resolver();
-        resolver.add(resource);
+        for (Requirement requirement : getRequirements(admin)) {
+            resolver.add(requirement);
+        }
         if (resolver.resolve()) {
             Resource[] resources;
             resources = resolver.getRequiredResources();
@@ -95,15 +92,15 @@
         }
     }
 
-    private Requirement[] getRequirements() throws InvalidSyntaxException {
+    private Requirement[] getRequirements(RepositoryAdmin admin) throws InvalidSyntaxException {
         Requirement[] reqs = new Requirement[requirements.size()];
         for (int i = 0; i < reqs.length; i++) {
-            reqs[i] = parseRequirement(requirements.get(i));
+            reqs[i] = parseRequirement(admin, requirements.get(i));
         }
         return reqs;
     }
 
-    private Requirement parseRequirement(String req) throws InvalidSyntaxException {
+    private Requirement parseRequirement(RepositoryAdmin admin, String req) throws InvalidSyntaxException {
         int p = req.indexOf(':');
         String name;
         String filter;
@@ -121,8 +118,7 @@
         if (!filter.startsWith("(")) {
             filter = "(" + filter + ")";
         }
-        Filter flt = FrameworkUtil.createFilter(filter);
-        return new RequirementImpl(name, flt);
+        return admin.requirement(name, filter);
     }
 
 }
\ No newline at end of file
diff --git a/karaf/shell/obr/src/main/java/org/apache/felix/karaf/shell/obr/util/IteratorToEnumeration.java b/karaf/shell/obr/src/main/java/org/apache/felix/karaf/shell/obr/util/IteratorToEnumeration.java
deleted file mode 100644
index fc29ca9..0000000
--- a/karaf/shell/obr/src/main/java/org/apache/felix/karaf/shell/obr/util/IteratorToEnumeration.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * 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.karaf.shell.obr.util;
-
-import java.util.Enumeration;
-import java.util.Iterator;
-
-public class IteratorToEnumeration implements Enumeration
-{
-    private Iterator m_iter = null;
-
-    public IteratorToEnumeration(Iterator iter)
-    {
-        m_iter = iter;
-    }
-
-    public boolean hasMoreElements()
-    {
-        if (m_iter == null)
-            return false;
-        return m_iter.hasNext();
-    }
-
-    public Object nextElement()
-    {
-        if (m_iter == null)
-            return null;
-        return m_iter.next();
-    }
-}
diff --git a/karaf/shell/obr/src/main/java/org/apache/felix/karaf/shell/obr/util/MapToDictionary.java b/karaf/shell/obr/src/main/java/org/apache/felix/karaf/shell/obr/util/MapToDictionary.java
deleted file mode 100644
index 15283fa..0000000
--- a/karaf/shell/obr/src/main/java/org/apache/felix/karaf/shell/obr/util/MapToDictionary.java
+++ /dev/null
@@ -1,98 +0,0 @@
-/*
- * 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.karaf.shell.obr.util;
-
-import java.util.Dictionary;
-import java.util.Enumeration;
-import java.util.Map;
-
-/**
- * This is a simple class that implements a <tt>Dictionary</tt>
- * from a <tt>Map</tt>. The resulting dictionary is immutatable.
-**/
-public class MapToDictionary extends Dictionary
-{
-    /**
-     * Map source.
-    **/
-    private Map m_map = null;
-
-    public MapToDictionary(Map map)
-    {
-        m_map = map;
-    }
-
-    public void setSourceMap(Map map)
-    {
-        m_map = map;
-    }
-
-    public Enumeration elements()
-    {
-        if (m_map == null)
-        {
-            return null;
-        }
-        return new IteratorToEnumeration(m_map.values().iterator());
-    }
-
-    public Object get(Object key)
-    {
-        if (m_map == null)
-        {
-            return null;
-        }
-        return m_map.get(key);
-    }
-
-    public boolean isEmpty()
-    {
-        if (m_map == null)
-        {
-            return true;
-        }
-        return m_map.isEmpty();
-    }
-
-    public Enumeration keys()
-    {
-        if (m_map == null)
-        {
-            return null;
-        }
-        return new IteratorToEnumeration(m_map.keySet().iterator());
-    }
-
-    public Object put(Object key, Object value)
-    {
-        throw new UnsupportedOperationException();
-    }
-
-    public Object remove(Object key)
-    {
-        throw new UnsupportedOperationException();
-    }
-
-    public int size()
-    {
-        if (m_map == null)
-        {
-            return 0;
-        }
-        return m_map.size();
-    }
-}
\ No newline at end of file
diff --git a/karaf/shell/obr/src/main/java/org/apache/felix/karaf/shell/obr/util/RequirementImpl.java b/karaf/shell/obr/src/main/java/org/apache/felix/karaf/shell/obr/util/RequirementImpl.java
deleted file mode 100644
index 7ccf6e8..0000000
--- a/karaf/shell/obr/src/main/java/org/apache/felix/karaf/shell/obr/util/RequirementImpl.java
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * 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.karaf.shell.obr.util;
-
-import org.osgi.framework.Filter;
-import org.osgi.service.obr.Capability;
-import org.osgi.service.obr.Requirement;
-
-/**
- * @version $Rev$ $Date$
- */
-public class RequirementImpl implements Requirement {
-    private final String name;
-    private final Filter filter;
-    private final boolean multiple;
-    private final boolean optional;
-    private final boolean extend;
-    private final String comment;
-
-    public RequirementImpl(String name, Filter filter) {
-        this(name, filter, false, false, false, null);
-    }
-
-    public RequirementImpl(String name, Filter filter, boolean multiple, boolean optional, boolean extend, String comment) {
-        this.name = name;
-        this.filter = filter;
-        this.multiple = multiple;
-        this.optional = optional;
-        this.extend = extend;
-        this.comment = comment;
-    }
-
-    public String getName() {
-        return name;
-    }
-
-    public String getFilter() {
-        return filter.toString();
-    }
-
-    public boolean isMultiple() {
-        return multiple;
-    }
-
-    public boolean isOptional() {
-        return optional;
-    }
-
-    public boolean isExtend() {
-        return extend;
-    }
-
-    public String getComment() {
-        return comment;
-    }
-
-    public boolean isSatisfied(Capability capability) {
-        return filter.match(new MapToDictionary(capability.getProperties()));
-    }
-}
diff --git a/karaf/shell/obr/src/main/java/org/apache/felix/karaf/shell/obr/util/ResourceImpl.java b/karaf/shell/obr/src/main/java/org/apache/felix/karaf/shell/obr/util/ResourceImpl.java
deleted file mode 100644
index cdb7f45..0000000
--- a/karaf/shell/obr/src/main/java/org/apache/felix/karaf/shell/obr/util/ResourceImpl.java
+++ /dev/null
@@ -1,111 +0,0 @@
-/*
- * 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.karaf.shell.obr.util;
-
-import org.osgi.framework.Version;
-import org.osgi.service.obr.Capability;
-import org.osgi.service.obr.Repository;
-import org.osgi.service.obr.Requirement;
-import org.osgi.service.obr.Resource;
-
-import java.net.URL;
-import java.util.Map;
-
-/**
- * @version $Rev$ $Date$
- */
-public class ResourceImpl implements Resource
-{
-
-  private final Map properties;
-  private final String symbolicName;
-  private final String presentationName;
-  private final Version version;
-  private final String id;
-  private final URL url;
-  private final Requirement[] requirements;
-  private final Capability[] capabilities;
-  private final String[] categories;
-  private final Repository repository;
-
-
-  public ResourceImpl(Map properties, String symbolicName, String presentationName, Version version, String id, URL url, Requirement[] requirements, Capability[] capabilities, String[] categories, Repository repository)
-  {
-    this.properties = properties;
-    this.symbolicName = symbolicName;
-    this.presentationName = presentationName;
-    this.version = version;
-    this.id = id;
-    this.url = url;
-    this.requirements = requirements;
-    this.capabilities = capabilities;
-    this.categories = categories;
-    this.repository = repository;
-  }
-
-  public Map getProperties()
-  {
-    return properties;
-  }
-
-  public String getSymbolicName()
-  {
-    return symbolicName;
-  }
-
-  public String getPresentationName()
-  {
-    return presentationName;
-  }
-
-  public Version getVersion()
-  {
-    return version;
-  }
-
-  public String getId()
-  {
-    return id;
-  }
-
-  public URL getURL()
-  {
-    return url;
-  }
-
-  public Requirement[] getRequirements()
-  {
-    return requirements;
-  }
-
-  public Capability[] getCapabilities()
-  {
-    return capabilities;
-  }
-
-  public String[] getCategories()
-  {
-    return categories;
-  }
-
-  public Repository getRepository()
-  {
-    return repository;
-  }
-}