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 2020/10/01 12:19:20 UTC

[sling-org-apache-sling-feature] branch master updated: SLING-9265 : Add support for optional bundles

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 4fee6a4  SLING-9265 : Add support for optional bundles
4fee6a4 is described below

commit 4fee6a49731b1b012ce3c80b127e346a077c4b52
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Thu Oct 1 14:18:54 2020 +0200

    SLING-9265 : Add support for optional bundles
---
 .../java/org/apache/sling/feature/Artifact.java    | 34 +++++++++++++-
 .../apache/sling/feature/ArtifactResolution.java   | 28 ++++++++++++
 .../org/apache/sling/feature/package-info.java     |  2 +-
 .../sling/feature/ArtifactResolutionTest.java      | 52 ++++++++++++++++++++++
 4 files changed, 113 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/Artifact.java b/src/main/java/org/apache/sling/feature/Artifact.java
index 90e416c..7486369 100644
--- a/src/main/java/org/apache/sling/feature/Artifact.java
+++ b/src/main/java/org/apache/sling/feature/Artifact.java
@@ -41,15 +41,18 @@ import javax.json.JsonValue.ValueType;
  * This class is not thread-safe.
  */
 public class Artifact implements Comparable<Artifact> {
+    
     /** Can be used in artifact metadata to specify an alias. Multiple aliases can be comma-separated. */
     public static final String KEY_ALIAS = "alias";
-
-
+    
     /** This key might be used by bundles to define the start order. */
     public static final String KEY_START_ORDER = "start-order";
 
     public static final String KEY_FEATURE_ORIGINS = "feature-origins";
 
+    /** This key might be used to set the resolution for an artifact. @since 1.6.0 */
+    public static final String KEY_RESOLUTION = "resolution";
+
     private static final String KEY_ID = "id";
 
     /** The artifact id. */
@@ -222,6 +225,33 @@ public class Artifact implements Comparable<Artifact> {
         }
     }
 
+    /**
+     * Get the artifact resolution
+     * @return The resolution
+     * @since 1.6.0
+     * @throws IllegalArgumentException  If the stored value is invalid
+     */
+    public ArtifactResolution getResolution() {
+        final String value = this.getMetadata().get(KEY_RESOLUTION);
+        if ( value == null ) {
+            return ArtifactResolution.MANDATORY;
+        }
+        return ArtifactResolution.valueOf(value.toUpperCase());
+    }
+
+    /**
+     * Set the artifact resolution
+     * @param value The resolution. If {@code null} the value will be removed.
+     * @since 1.6.0
+     */
+    public void setResolution(final ArtifactResolution value) {
+        if ( value == null ) {
+            this.getMetadata().remove(KEY_RESOLUTION);
+        } else {
+            this.getMetadata().put(KEY_RESOLUTION, value.name());
+        }
+    }
+
     @Override
     public int compareTo(final Artifact o) {
         return this.id.compareTo(o.id);
diff --git a/src/main/java/org/apache/sling/feature/ArtifactResolution.java b/src/main/java/org/apache/sling/feature/ArtifactResolution.java
new file mode 100644
index 0000000..326bc76
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/ArtifactResolution.java
@@ -0,0 +1,28 @@
+/*
+ * 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;
+
+/**
+ * Mandatory artifacts need to be resolved (default), optional might resolve
+ * @since 1.6.0
+ */
+public enum ArtifactResolution {
+    
+    MANDATORY,
+    OPTIONAL
+    
+}
diff --git a/src/main/java/org/apache/sling/feature/package-info.java b/src/main/java/org/apache/sling/feature/package-info.java
index 6ed34c0..9d1cc7f 100644
--- a/src/main/java/org/apache/sling/feature/package-info.java
+++ b/src/main/java/org/apache/sling/feature/package-info.java
@@ -17,7 +17,7 @@
  * under the License.
  */
 
-@org.osgi.annotation.versioning.Version("1.5.0")
+@org.osgi.annotation.versioning.Version("1.6.0")
 package org.apache.sling.feature;
 
 
diff --git a/src/test/java/org/apache/sling/feature/ArtifactResolutionTest.java b/src/test/java/org/apache/sling/feature/ArtifactResolutionTest.java
new file mode 100644
index 0000000..5fd51fb
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/ArtifactResolutionTest.java
@@ -0,0 +1,52 @@
+/*
+ * 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 org.junit.Test;
+
+public class ArtifactResolutionTest {
+
+    @Test public void testValidResolution() {
+        final Artifact a = new Artifact(ArtifactId.parse("g:a:1"));
+
+        assertNull(a.getMetadata().get(Artifact.KEY_RESOLUTION));
+        assertEquals(ArtifactResolution.MANDATORY, a.getResolution());
+        
+        a.setResolution(ArtifactResolution.MANDATORY);
+        assertEquals(ArtifactResolution.MANDATORY.name(), a.getMetadata().get(Artifact.KEY_RESOLUTION));
+        assertEquals(ArtifactResolution.MANDATORY, a.getResolution());
+
+        a.setResolution(ArtifactResolution.OPTIONAL);
+        assertEquals(ArtifactResolution.OPTIONAL.name(), a.getMetadata().get(Artifact.KEY_RESOLUTION));
+        assertEquals(ArtifactResolution.OPTIONAL, a.getResolution());
+
+        a.setResolution(null);
+        assertNull(a.getMetadata().get(Artifact.KEY_RESOLUTION));
+        assertEquals(ArtifactResolution.MANDATORY, a.getResolution());
+    }
+
+    @Test(expected = IllegalArgumentException.class) 
+    public void testInvalidResolution() {
+        final Artifact a = new Artifact(ArtifactId.parse("g:a:1"));
+        a.getMetadata().put(Artifact.KEY_RESOLUTION, "foo");
+
+        a.getResolution();
+    }
+}
\ No newline at end of file