You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by da...@apache.org on 2018/04/27 09:51:40 UTC

[sling-org-apache-sling-feature] 20/22: Refactor to use Capabilities and Requirements from Felix utils project

This is an automated email from the ASF dual-hosted git repository.

davidb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature.git

commit f14073db5efd3e4f8a80e4c2dc0076af3fdc6718
Author: David Bosschaert <da...@gmail.com>
AuthorDate: Sun Apr 22 21:19:43 2018 +0100

    Refactor to use Capabilities and Requirements from Felix utils project
---
 pom.xml                                            |   9 +-
 .../feature/AbstractCapabilityRequirement.java     | 114 ---------------------
 .../java/org/apache/sling/feature/Feature.java     |   6 +-
 .../org/apache/sling/feature/OSGiCapability.java   |  59 -----------
 .../org/apache/sling/feature/OSGiRequirement.java  |  59 -----------
 .../sling/feature/CapabilityRequirementTest.java   |  68 ------------
 6 files changed, 12 insertions(+), 303 deletions(-)

diff --git a/pom.xml b/pom.xml
index 46f2116..35078c5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -77,11 +77,18 @@
             <version>1.0-alpha-1</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.utils</artifactId>
+            <version>1.11.0-SNAPSHOT</version>
+            <scope>provided</scope>
+        </dependency>
 
-      <!-- Testing -->
+        <!-- Testing -->
         <dependency>
         	    <groupId>junit</groupId>
         	    <artifactId>junit</artifactId>
+            <scope>test</scope>
         </dependency>
 
         <dependency>
diff --git a/src/main/java/org/apache/sling/feature/AbstractCapabilityRequirement.java b/src/main/java/org/apache/sling/feature/AbstractCapabilityRequirement.java
deleted file mode 100644
index 8a4982f..0000000
--- a/src/main/java/org/apache/sling/feature/AbstractCapabilityRequirement.java
+++ /dev/null
@@ -1,114 +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.sling.feature;
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.osgi.resource.Resource;
-
-abstract class AbstractCapabilityRequirement {
-
-    /** The namespace. Required. */
-    private final String namespace;
-
-    /** Optional resource. */
-    private final Resource resource;
-
-    /** Optional attributes. Never null. */
-    private final Map<String, Object> attributes;
-
-    /** Optional attributes. Never null. */
-    private final Map<String, String> directives;
-
-    AbstractCapabilityRequirement(final Resource res, final String ns, final Map<String, Object> attrs, final Map<String, String> dirs) {
-        if ( ns == null ) {
-            throw new IllegalArgumentException("Namespace must not be null.");
-        }
-        resource = res;
-        namespace = ns;
-        attributes = attrs == null ? Collections.emptyMap() : Collections.unmodifiableMap(new HashMap<>(attrs));
-        directives = dirs == null ? Collections.emptyMap() : Collections.unmodifiableMap(new HashMap<>(dirs));
-    }
-
-    /**
-     * Return the namespace.
-     * @return The namespace. This is never @{code null}.
-     */
-    public String getNamespace() {
-        return namespace;
-    }
-
-    /**
-     * Return the attributes.
-     * @return The attributes, might be empty.
-     */
-    public Map<String, Object> getAttributes() {
-        return attributes;
-    }
-
-    /**
-     * Return the directives.
-     * @return The directives, might be empty.
-     */
-    public Map<String, String> getDirectives() {
-        return directives;
-    }
-
-    /**
-     * Return the resource.
-     * @return The resource or @{code null}.
-     */
-    public Resource getResource() {
-        return resource;
-    }
-
-    @Override
-    public int hashCode() {
-        final int prime = 31;
-        int result = 1;
-        result = prime * result + attributes.hashCode();
-        result = prime * result + directives.hashCode();
-        result = prime * result + namespace.hashCode();
-        return result;
-    }
-
-    @Override
-    public boolean equals(Object obj) {
-        if (this == obj)
-            return true;
-        if (obj == null)
-            return false;
-        if (getClass() != obj.getClass())
-            return false;
-        AbstractCapabilityRequirement other = (AbstractCapabilityRequirement) obj;
-        if (!namespace.equals(other.namespace))
-            return false;
-        if (!attributes.equals(other.attributes))
-            return false;
-        if (!directives.equals(other.directives))
-            return false;
-        return true;
-    }
-
-    @Override
-    public String toString() {
-        return getClass().getSimpleName() + " [resource=" + resource + ", namespace=" + namespace + ", attributes=" + attributes
-                + ", directives=" + directives + "]";
-    }
-}
diff --git a/src/main/java/org/apache/sling/feature/Feature.java b/src/main/java/org/apache/sling/feature/Feature.java
index b1ad406..98f1e0b 100644
--- a/src/main/java/org/apache/sling/feature/Feature.java
+++ b/src/main/java/org/apache/sling/feature/Feature.java
@@ -16,6 +16,8 @@
  */
 package org.apache.sling.feature;
 
+import org.apache.felix.utils.resource.CapabilityImpl;
+import org.apache.felix.utils.resource.RequirementImpl;
 import org.osgi.resource.Capability;
 import org.osgi.resource.Requirement;
 
@@ -312,13 +314,13 @@ public class Feature implements Comparable<Feature> {
 
         // requirements
         for(final Requirement r : this.getRequirements()) {
-            final Requirement c = new OSGiRequirement(r.getNamespace(), r.getAttributes(), r.getDirectives());
+            final Requirement c = new RequirementImpl(null, r.getNamespace(), r.getDirectives(), r.getAttributes());
             result.getRequirements().add(c);
         }
 
         // capabilities
         for(final Capability r : this.getCapabilities()) {
-            final Capability c = new OSGiCapability(r.getNamespace(), r.getAttributes(), r.getDirectives());
+            final Capability c = new CapabilityImpl(null, r.getNamespace(), r.getDirectives(), r.getAttributes());
             result.getCapabilities().add(c);
         }
 
diff --git a/src/main/java/org/apache/sling/feature/OSGiCapability.java b/src/main/java/org/apache/sling/feature/OSGiCapability.java
deleted file mode 100644
index 5c2000d..0000000
--- a/src/main/java/org/apache/sling/feature/OSGiCapability.java
+++ /dev/null
@@ -1,59 +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.sling.feature;
-
-import java.util.Map;
-
-import org.osgi.resource.Capability;
-import org.osgi.resource.Resource;
-
-/**
- * Implementation of the OSGi Capability interface.
- */
-public class OSGiCapability extends AbstractCapabilityRequirement implements Capability {
-    /**
-     * Create a capability that is not associated with a resource.
-     * @param res The resource associated with the capability. May be null.
-     * @param ns The namespace of the capability.
-     * @param attrs The attributes of the capability.
-     * @param dirs The directives of the capability.
-     */
-    public OSGiCapability(String ns, Map<String, Object> attrs, Map<String, String> dirs) {
-        this(null, ns, attrs, dirs);
-    }
-
-    /**
-     * Create a capability.
-     * @param res The resource associated with the capability. May be null.
-     * @param ns The namespace of the capability.
-     * @param attrs The attributes of the capability.
-     * @param dirs The directives of the capability.
-     */
-    public OSGiCapability(Resource res, String ns, Map<String, Object> attrs, Map<String, String> dirs) {
-        super(res, ns, attrs, dirs);
-    }
-
-    /**
-     * Create a capability based on an existing capability, providing the resource.
-     * The namespace, attributes and directives are copied from the provided capability.
-     * @param resource The resource to be associated with the capability
-     * @param capability The capability to base the new requirement on.
-     */
-    public OSGiCapability(Resource resource, Capability capability) {
-        this(resource, capability.getNamespace(), capability.getAttributes(), capability.getDirectives());
-    }
-}
diff --git a/src/main/java/org/apache/sling/feature/OSGiRequirement.java b/src/main/java/org/apache/sling/feature/OSGiRequirement.java
deleted file mode 100644
index 2727379..0000000
--- a/src/main/java/org/apache/sling/feature/OSGiRequirement.java
+++ /dev/null
@@ -1,59 +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.sling.feature;
-
-import java.util.Map;
-
-import org.osgi.resource.Requirement;
-import org.osgi.resource.Resource;
-
-/**
- * Implementation of the OSGi Requirement interface.
- */
-public class OSGiRequirement extends AbstractCapabilityRequirement implements Requirement {
-    /**
-     * Create a requirement that is not associated with a resource.
-     * @param res The resource associated with the requirement.
-     * @param ns The namespace of the requirement.
-     * @param attrs The attributes of the requirement.
-     * @param dirs The directives of the requirement.
-     */
-    public OSGiRequirement(String ns, Map<String, Object> attrs, Map<String, String> dirs) {
-        this(null, ns, attrs, dirs);
-    }
-
-    /**
-     * Create a requirement.
-     * @param res The resource associated with the requirement.
-     * @param ns The namespace of the requirement.
-     * @param attrs The attributes of the requirement.
-     * @param dirs The directives of the requirement.
-     */
-    public OSGiRequirement(Resource res, String ns, Map<String, Object> attrs, Map<String, String> dirs) {
-        super(res, ns, attrs, dirs);
-    }
-
-    /**
-     * Create a requirement based on an existing requirement, providing the resource.
-     * The namespace, attributes and directives are copied from the provided requirement.
-     * @param resource The resource to be associated with the requirement
-     * @param requirement The requirement to base the new requirement on.
-     */
-    public OSGiRequirement(Resource resource, Requirement requirement) {
-        this(resource, requirement.getNamespace(), requirement.getAttributes(), requirement.getDirectives());
-    }
-}
diff --git a/src/test/java/org/apache/sling/feature/CapabilityRequirementTest.java b/src/test/java/org/apache/sling/feature/CapabilityRequirementTest.java
deleted file mode 100644
index e834bad..0000000
--- a/src/test/java/org/apache/sling/feature/CapabilityRequirementTest.java
+++ /dev/null
@@ -1,68 +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.sling.feature;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.junit.Test;
-import org.osgi.resource.Capability;
-import org.osgi.resource.Requirement;
-import org.osgi.resource.Resource;
-
-public class CapabilityRequirementTest {
-    @Test
-    public void testCapability() {
-        Map<String, Object> attrs = new HashMap<>();
-        attrs.put("org.foo", "1234");
-        attrs.put("bar", 456);
-        Map<String, String> dirs = new HashMap<>();
-        dirs.put("my_dir", "my_value");
-        Capability c = new OSGiCapability("org.foo", attrs, dirs);
-        assertEquals("org.foo", c.getNamespace());
-        assertEquals(attrs, c.getAttributes());
-        assertEquals(dirs, c.getDirectives());
-        assertNull(c.getResource());
-    }
-
-    @Test
-    public void testRequirement() {
-        Resource tr = new TestResource();
-        Requirement r = new OSGiRequirement(tr, "testing",
-                Collections.emptyMap(), Collections.emptyMap());
-        assertEquals(tr, r.getResource());
-        assertEquals(0, r.getAttributes().size());
-        assertEquals(0, r.getDirectives().size());
-    }
-
-    private static class TestResource implements Resource {
-        @Override
-        public List<Capability> getCapabilities(String namespace) {
-            return Collections.emptyList();
-        }
-
-        @Override
-        public List<Requirement> getRequirements(String namespace) {
-            return Collections.emptyList();
-        }
-    }
-}

-- 
To stop receiving notification emails like this one, please contact
davidb@apache.org.