You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2019/06/07 16:18:24 UTC

[sling-org-apache-sling-feature] branch master updated: SLING-8478 : Adding method to match Requirements and Capabilities

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

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


The following commit(s) were added to refs/heads/master by this push:
     new a88b00c  SLING-8478 : Adding method to match Requirements and Capabilities
a88b00c is described below

commit a88b00c3f47cc3e80c19c772f297ccb567680fed
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Fri Jun 7 18:17:48 2019 +0200

    SLING-8478 : Adding method to match Requirements and Capabilities
---
 .../java/org/apache/sling/feature/Feature.java     | 32 ++++++++++--
 .../apache/sling/feature/MatchingRequirement.java  | 27 ++++++++++
 .../apache/sling/feature/builder/BuilderUtil.java  | 28 +++++-----
 .../apache/sling/feature/builder/MergeHandler.java | 18 +++----
 .../sling/feature/builder/FeatureBuilderTest.java  | 60 +++++++++++++++-------
 5 files changed, 119 insertions(+), 46 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/Feature.java b/src/main/java/org/apache/sling/feature/Feature.java
index 20371c6..dadeb17 100644
--- a/src/main/java/org/apache/sling/feature/Feature.java
+++ b/src/main/java/org/apache/sling/feature/Feature.java
@@ -20,11 +20,12 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 
 import org.apache.felix.utils.resource.CapabilityImpl;
 import org.apache.felix.utils.resource.RequirementImpl;
 import org.osgi.resource.Capability;
-import org.osgi.resource.Requirement;
+import org.osgi.resource.Resource;
 
 /**
  * A feature consists of
@@ -50,7 +51,7 @@ public class Feature implements Comparable<Feature> {
 
     private final Map<String,String> frameworkProperties = new HashMap<>();
 
-    private final List<Requirement> requirements = new ArrayList<>();
+    private final List<MatchingRequirement> requirements = new ArrayList<>();
 
     private final List<Capability> capabilities = new ArrayList<>();
 
@@ -154,7 +155,7 @@ public class Feature implements Comparable<Feature> {
      * The returned object is modifiable.
      * @return The list of requirements
      */
-    public List<Requirement> getRequirements() {
+    public List<MatchingRequirement> getRequirements() {
         return requirements;
     }
 
@@ -365,8 +366,9 @@ public class Feature implements Comparable<Feature> {
         result.getFrameworkProperties().putAll(this.getFrameworkProperties());
 
         // requirements
-        for(final Requirement r : this.getRequirements()) {
-            final Requirement c = new RequirementImpl(null, r.getNamespace(), r.getDirectives(), r.getAttributes());
+        for (final MatchingRequirement r : this.getRequirements()) {
+            final MatchingRequirement c = new MatchingRequirementImpl(null, r.getNamespace(), r.getDirectives(),
+                    r.getAttributes());
             result.getRequirements().add(c);
         }
 
@@ -438,4 +440,24 @@ public class Feature implements Comparable<Feature> {
                 + "]";
     }
 
+    private static class MatchingRequirementImpl extends RequirementImpl implements MatchingRequirement {
+
+        public MatchingRequirementImpl(Resource res, String ns, Map<String, String> dirs, Map<String, Object> attrs) {
+            super(res, ns, dirs, attrs);
+        }
+
+        @Override
+        public boolean equals(final Object o) {
+            if (this == o) {
+                return true;
+            }
+            if (o == null || !(o instanceof RequirementImpl)) {
+                return false;
+            }
+            final RequirementImpl that = (RequirementImpl) o;
+            return Objects.equals(resource, that.getResource()) && Objects.equals(namespace, that.getNamespace())
+                    && Objects.equals(attributes, that.getAttributes())
+                    && Objects.equals(directives, that.getDirectives());
+        }
+    }
 }
diff --git a/src/main/java/org/apache/sling/feature/MatchingRequirement.java b/src/main/java/org/apache/sling/feature/MatchingRequirement.java
new file mode 100644
index 0000000..60ecfc8
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/MatchingRequirement.java
@@ -0,0 +1,27 @@
+/*
+ * 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 org.osgi.annotation.versioning.ConsumerType;
+import org.osgi.resource.Capability;
+import org.osgi.resource.Requirement;
+
+@ConsumerType
+public interface MatchingRequirement extends Requirement {
+
+    boolean matches(Capability cap);
+}
diff --git a/src/main/java/org/apache/sling/feature/builder/BuilderUtil.java b/src/main/java/org/apache/sling/feature/builder/BuilderUtil.java
index d643dbe..173c87b 100644
--- a/src/main/java/org/apache/sling/feature/builder/BuilderUtil.java
+++ b/src/main/java/org/apache/sling/feature/builder/BuilderUtil.java
@@ -16,18 +16,6 @@
  */
 package org.apache.sling.feature.builder;
 
-import org.apache.sling.feature.Artifact;
-import org.apache.sling.feature.ArtifactId;
-import org.apache.sling.feature.Artifacts;
-import org.apache.sling.feature.Bundles;
-import org.apache.sling.feature.Configuration;
-import org.apache.sling.feature.Configurations;
-import org.apache.sling.feature.Extension;
-import org.apache.sling.feature.Feature;
-import org.osgi.framework.Version;
-import org.osgi.resource.Capability;
-import org.osgi.resource.Requirement;
-
 import java.io.StringReader;
 import java.io.StringWriter;
 import java.util.ArrayList;
@@ -52,6 +40,18 @@ import javax.json.JsonValue;
 import javax.json.JsonValue.ValueType;
 import javax.json.JsonWriter;
 
+import org.apache.sling.feature.Artifact;
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Artifacts;
+import org.apache.sling.feature.Bundles;
+import org.apache.sling.feature.Configuration;
+import org.apache.sling.feature.Configurations;
+import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.MatchingRequirement;
+import org.osgi.framework.Version;
+import org.osgi.resource.Capability;
+
 /**
  * Utility methods for the builders
  */
@@ -304,8 +304,8 @@ class BuilderUtil {
     }
 
     // requirements (add)
-    static void mergeRequirements(final List<Requirement> target, final List<Requirement> source) {
-        for(final Requirement req : source) {
+    static void mergeRequirements(final List<MatchingRequirement> target, final List<MatchingRequirement> source) {
+        for (final MatchingRequirement req : source) {
             if ( !target.contains(req) ) {
                 target.add(req);
             }
diff --git a/src/main/java/org/apache/sling/feature/builder/MergeHandler.java b/src/main/java/org/apache/sling/feature/builder/MergeHandler.java
index 9f20c6a..c62b426 100644
--- a/src/main/java/org/apache/sling/feature/builder/MergeHandler.java
+++ b/src/main/java/org/apache/sling/feature/builder/MergeHandler.java
@@ -35,16 +35,16 @@ public interface MergeHandler {
     /**
      * Merge the source extension into the target extension.
      *
-     * Only called if {@link #canMerge(Extension)} for the
-     * extension returned {@code true}.
-     * If the target does not yet contain this extension,
-     * then the targetEx argument is {@code null}. In that case
-     * the handler should the extension to the target.
+     * Only called if {@link #canMerge(Extension)} for the extension returned
+     * {@code true}. If the target does not yet contain this extension, then the
+     * targetEx argument is {@code null}. In that case the handler should add the
+     * extension to the target.
      *
-     * @param context Context for the handler
-     * @param target The target feature
-     * @param source The source feature
-     * @param targetEx The target extension or {@code null} if the extension does not exist in the target.
+     * @param context  Context for the handler
+     * @param target   The target feature
+     * @param source   The source feature
+     * @param targetEx The target extension or {@code null} if the extension does
+     *                 not exist in the target.
      * @param sourceEx The source extension
      * @throws IllegalStateException If the extensions can't be merged
      */
diff --git a/src/test/java/org/apache/sling/feature/builder/FeatureBuilderTest.java b/src/test/java/org/apache/sling/feature/builder/FeatureBuilderTest.java
index b46d7f3..f2a05fe 100644
--- a/src/test/java/org/apache/sling/feature/builder/FeatureBuilderTest.java
+++ b/src/test/java/org/apache/sling/feature/builder/FeatureBuilderTest.java
@@ -16,6 +16,23 @@
  */
 package org.apache.sling.feature.builder;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+
 import org.apache.felix.utils.resource.CapabilityImpl;
 import org.apache.felix.utils.resource.RequirementImpl;
 import org.apache.sling.feature.Artifact;
@@ -24,26 +41,12 @@ import org.apache.sling.feature.Configuration;
 import org.apache.sling.feature.Extension;
 import org.apache.sling.feature.ExtensionType;
 import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.MatchingRequirement;
 import org.apache.sling.feature.Prototype;
 import org.junit.Test;
 import org.osgi.resource.Capability;
 import org.osgi.resource.Requirement;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+import org.osgi.resource.Resource;
 
 public class FeatureBuilderTest {
 
@@ -223,7 +226,7 @@ public class FeatureBuilderTest {
     @Test public void testNoIncludesNoUpgrade() throws Exception {
         final Feature base = new Feature(ArtifactId.parse("org.apache.sling/test-feature/1.1"));
 
-        final Requirement r1 = new RequirementImpl(null, "osgi.contract",
+        final MatchingRequirement r1 = new MatchingRequirementImpl(null, "osgi.contract",
                 Collections.singletonMap("filter", "(&(osgi.contract=JavaServlet)(version=3.1))"), null);
         base.getRequirements().add(r1);
 
@@ -273,7 +276,7 @@ public class FeatureBuilderTest {
         final Prototype i1 = new Prototype(ArtifactId.parse("g/a/1"));
         base.setPrototype(i1);
 
-        final Requirement r1 = new RequirementImpl(null, "osgi.contract",
+        final MatchingRequirement r1 = new MatchingRequirementImpl(null, "osgi.contract",
                 Collections.singletonMap("filter", "(&(osgi.contract=JavaServlet)(version=3.1))"), null);
         base.getRequirements().add(r1);
 
@@ -857,4 +860,25 @@ public class FeatureBuilderTest {
             assertTrue(ise.getMessage().contains(" final "));
         }
     }
+
+    private static class MatchingRequirementImpl extends RequirementImpl implements MatchingRequirement {
+
+        public MatchingRequirementImpl(Resource res, String ns, Map<String, String> dirs, Map<String, Object> attrs) {
+            super(res, ns, dirs, attrs);
+        }
+
+        @Override
+        public boolean equals(final Object o) {
+            if (this == o) {
+                return true;
+            }
+            if (o == null || !(o instanceof RequirementImpl)) {
+                return false;
+            }
+            final RequirementImpl that = (RequirementImpl) o;
+            return Objects.equals(resource, that.getResource()) && Objects.equals(namespace, that.getNamespace())
+                    && Objects.equals(attributes, that.getAttributes())
+                    && Objects.equals(directives, that.getDirectives());
+        }
+    }
 }