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

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


The following commit(s) were added to refs/heads/master by this push:
     new a5a22ea  SLING-9031 SLING-8548 helper method to check active run modes 
against spec
a5a22ea is described below

commit a5a22eadfbc8eb46add1f816e709224464ea9fb1
Author: Konrad Windszus <[email protected]>
AuthorDate: Tue Jun 30 13:32:48 2020 +0200

    SLING-9031 SLING-8548 helper method to check active run modes against spec
    
    The run mode spec may contain AND, OR and NOT
---
 pom.xml                                            |  5 ++-
 .../sling/settings/SlingSettingsService.java       | 23 +++++++++++
 .../settings/impl/SlingSettingsServiceImpl.java    | 45 ++++++++++++++++++++++
 .../org/apache/sling/settings/package-info.java    |  7 +---
 .../impl/SlingSettingsServiceImplTest.java         | 14 +++++++
 5 files changed, 86 insertions(+), 8 deletions(-)

diff --git a/pom.xml b/pom.xml
index 7df95a6..ccd5970 100644
--- a/pom.xml
+++ b/pom.xml
@@ -22,8 +22,8 @@
     <modelVersion>4.0.0</modelVersion>
     <parent>
         <groupId>org.apache.sling</groupId>
-        <artifactId>sling</artifactId>
-        <version>34</version>
+        <artifactId>sling-bundle-parent</artifactId>
+        <version>39</version>
         <relativePath />
     </parent>
 
@@ -45,6 +45,7 @@
     <properties>
         <site.jira.version.id>12315254</site.jira.version.id>
         <site.javadoc.exclude>**.impl.**</site.javadoc.exclude>
+        
<project.build.outputTimestamp>2019-10-02T08:04:00Z</project.build.outputTimestamp>
     </properties>
 
     <build>
diff --git a/src/main/java/org/apache/sling/settings/SlingSettingsService.java 
b/src/main/java/org/apache/sling/settings/SlingSettingsService.java
index 7357954..e9462b1 100644
--- a/src/main/java/org/apache/sling/settings/SlingSettingsService.java
+++ b/src/main/java/org/apache/sling/settings/SlingSettingsService.java
@@ -88,6 +88,10 @@ public interface SlingSettingsService {
      */
     String RUN_MODE_INSTALL_OPTIONS = "sling.run.mode.install.options";
 
+    String RUN_MODE_SPEC_OR_SEPARATOR = ",";
+    String RUN_MODE_SPEC_AND_SEPARATOR = ".";
+    String RUN_MODE_SPEC_NOT_PREFIX = "-";
+
     /**
      * Utility method to generate an absolute path
      * within Sling Home.
@@ -125,6 +129,25 @@ public interface SlingSettingsService {
     Set<String> getRunModes();
 
     /**
+     * Checks if a given run mode spec is satisfied by the active run modes.
+     * A run mode spec consists out of run modes and operators (AND = ".", OR 
= "," and NOT = "-")
+     * and follows the following grammar in EBNF:
+     * <pre><code>
+     * run mode spec ::= conjunctions { "," conjunctions }
+     * conjunctions ::= conjunction { '.' conjunction }
+     * conjunction ::= notrunmode | runmode
+     * notrunmode ::= '-' runmode
+     * </code></pre>
+     * 
+     * The operator order is first "-" (not), second "." (AND), last "," (OR).
+     * @param spec the run mode spec string to check against
+     * @param activeRunModes the run modes against which to check
+     * @return the number of matching run modes or 0 if no match. If multiple 
disjunctions match the one with the highest number of matching run modes is 
returned.
+     * @since 1.4.0 (Sling Settings Bundle 1.3.11)
+     */
+    int getBestRunModeMatchCountFromSpec(String spec);
+
+    /**
      * Return the optional name of the instance.
      * @return The name of the instance or <code>null</code>.
      * @since 1.3
diff --git 
a/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java 
b/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java
index 28f2f78..16ae4b3 100644
--- a/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java
+++ b/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java
@@ -28,6 +28,7 @@ import java.io.Serializable;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Dictionary;
 import java.util.HashMap;
@@ -35,6 +36,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.regex.Pattern;
 
 import org.apache.sling.settings.SlingSettingsService;
 import org.osgi.framework.BundleContext;
@@ -285,6 +287,7 @@ public class SlingSettingsServiceImpl
     /**
      * @see 
org.apache.sling.settings.SlingSettingsService#getAbsolutePathWithinSlingHome(String)
      */
+    @Override
     public String getAbsolutePathWithinSlingHome(final String relativePath) {
         return new File(slingHome, relativePath).getAbsolutePath();
     }
@@ -292,6 +295,7 @@ public class SlingSettingsServiceImpl
     /**
      * @see org.apache.sling.settings.SlingSettingsService#getSlingId()
      */
+    @Override
     public String getSlingId() {
         return this.slingId;
     }
@@ -299,6 +303,7 @@ public class SlingSettingsServiceImpl
     /**
      * @see org.apache.sling.settings.SlingSettingsService#getSlingHome()
      */
+    @Override
     public URL getSlingHome() {
         return this.slingHomeUrl;
     }
@@ -306,6 +311,7 @@ public class SlingSettingsServiceImpl
     /**
      * @see org.apache.sling.settings.SlingSettingsService#getSlingHomePath()
      */
+    @Override
     public String getSlingHomePath() {
         return this.slingHome;
     }
@@ -313,13 +319,51 @@ public class SlingSettingsServiceImpl
     /**
      * @see org.apache.sling.settings.SlingSettingsService#getRunModes()
      */
+    @Override
     public Set<String> getRunModes() {
         return this.runModes;
     }
 
+    @Override
+    public int getBestRunModeMatchCountFromSpec(String spec) {
+        return getBestRunModeMatchCountFromSpec(spec, runModes);
+    }
+
+    static int getBestRunModeMatchCountFromSpec(String spec, 
Collection<String> activeRunModes) {
+        int numMatchingRunModes = 0;
+        // 1. support OR
+        for (String discjunctivePart : 
spec.split(Pattern.quote(RUN_MODE_SPEC_OR_SEPARATOR))) {
+            int newNumMatchingRunModes = 
getBestRunModeMatchCountFromConjunctions(discjunctivePart, activeRunModes);
+            if (newNumMatchingRunModes > numMatchingRunModes) {
+                numMatchingRunModes = newNumMatchingRunModes;
+            }
+        }
+        return numMatchingRunModes;
+    }
+
+    static int getBestRunModeMatchCountFromConjunctions(String conjunctions, 
Collection<String> activeRunModes) {
+        int numMatchingRunModes = 0;
+        // 2. support AND
+        for (String conjunctivePart : 
conjunctions.split(Pattern.quote(RUN_MODE_SPEC_AND_SEPARATOR))) {
+            // 3. support NOT operator
+            if (conjunctivePart.startsWith(RUN_MODE_SPEC_NOT_PREFIX)) {
+                if 
(activeRunModes.contains(conjunctivePart.substring(RUN_MODE_SPEC_NOT_PREFIX.length())))
 {
+                    return 0;
+                }
+            } else {
+                if (!activeRunModes.contains(conjunctivePart)) {
+                    return 0;
+                }
+            }
+            numMatchingRunModes++;
+        }
+        return numMatchingRunModes;
+    }
+
     /**
      * @see org.apache.sling.settings.SlingSettingsService#getSlingName()
      */
+    @Override
     public String getSlingName() {
         synchronized ( this.slingProps ) {
             String name = this.slingProps.get(SLING_NAME);
@@ -333,6 +377,7 @@ public class SlingSettingsServiceImpl
     /**
      * @see 
org.apache.sling.settings.SlingSettingsService#getSlingDescription()
      */
+    @Override
     public String getSlingDescription() {
         synchronized ( this.slingProps ) {
             String desc = this.slingProps.get(SLING_DESCRIPTION);
diff --git a/src/main/java/org/apache/sling/settings/package-info.java 
b/src/main/java/org/apache/sling/settings/package-info.java
index d569ad1..d30eb0d 100644
--- a/src/main/java/org/apache/sling/settings/package-info.java
+++ b/src/main/java/org/apache/sling/settings/package-info.java
@@ -16,12 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-
-/**
- *
- * @version 1.3.1
- */
[email protected]("1.3.1")
[email protected]("1.4.0")
 package org.apache.sling.settings;
 
 
diff --git 
a/src/test/java/org/apache/sling/settings/impl/SlingSettingsServiceImplTest.java
 
b/src/test/java/org/apache/sling/settings/impl/SlingSettingsServiceImplTest.java
index 6f8820a..4e7b8c0 100644
--- 
a/src/test/java/org/apache/sling/settings/impl/SlingSettingsServiceImplTest.java
+++ 
b/src/test/java/org/apache/sling/settings/impl/SlingSettingsServiceImplTest.java
@@ -28,11 +28,15 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.ObjectOutputStream;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 import org.apache.commons.lang.RandomStringUtils;
 import org.apache.sling.settings.SlingSettingsService;
 import org.junit.After;
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.osgi.framework.BundleContext;
@@ -106,6 +110,16 @@ public class SlingSettingsServiceImplTest {
         assertNotNull(slingId);
     }
 
+    @Test
+    public void testGetBestRunModeMatchCountFromSpec() {
+        Assert.assertEquals(0, 
SlingSettingsServiceImpl.getBestRunModeMatchCountFromSpec("test1.test2,-test3.test4",
 Collections.singleton("test5")));
+        Assert.assertEquals(0, 
SlingSettingsServiceImpl.getBestRunModeMatchCountFromSpec("test1.test2,-test3.test4",
 Stream.of("test1", "test3").collect(Collectors.toSet())));
+        Assert.assertEquals(0, 
SlingSettingsServiceImpl.getBestRunModeMatchCountFromSpec("test1.test2,-test3.test4",
 Stream.of("test2", "test3").collect(Collectors.toSet())));
+        Assert.assertEquals(2, 
SlingSettingsServiceImpl.getBestRunModeMatchCountFromSpec("test1.test2,-test3.test4",
 Stream.of("test1", "test2").collect(Collectors.toSet())));
+        Assert.assertEquals(2, 
SlingSettingsServiceImpl.getBestRunModeMatchCountFromSpec("test1.test2,-test3.test4",
 Stream.of("test2", "test4").collect(Collectors.toSet())));
+        Assert.assertEquals(3, 
SlingSettingsServiceImpl.getBestRunModeMatchCountFromSpec("test1.test2,-test3.test4,test5.test6.test7",
 Stream.of("test1", "test2", "test4", "test5", "test6", 
"test7").collect(Collectors.toSet())));
+    }
+
     private SlingSettingsService createSlingSettingsService(final File 
slingIdFile, final File optionsFile) throws IOException {
         BundleContext context = mock(BundleContext.class);
         when(context.getDataFile(SLING_ID_FILE_NAME)).thenReturn(slingIdFile);

Reply via email to