You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by jo...@apache.org on 2021/10/29 17:01:15 UTC

[sling-org-apache-sling-i18n] 01/01: SLING-10884 add exclusion for the i18n ResourceBundleProvider

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

joerghoh pushed a commit to branch improvement/SLING-10884-excludes-for-ResourceBundleProvider
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-i18n.git

commit 553cb7bec17ca5b49e637580617fb666c16ca0b6
Author: Joerg Hoh <jh...@adobe.com>
AuthorDate: Fri Oct 29 18:59:32 2021 +0200

    SLING-10884 add exclusion for the i18n ResourceBundleProvider
---
 pom.xml                                            |  6 +++
 .../sling/i18n/impl/JcrResourceBundleProvider.java | 23 ++++++++++++
 .../ConcurrentJcrResourceBundleLoadingTest.java    |  5 +++
 .../i18n/impl/JcrResourceBundleProviderTest.java   | 43 ++++++++++++++++++++++
 4 files changed, 77 insertions(+)

diff --git a/pom.xml b/pom.xml
index 540bcad..7bc60b3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -316,5 +316,11 @@
             <version>${org.ops4j.pax.exam.version}</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+          <groupId>org.apache.sling</groupId>
+          <artifactId>org.apache.sling.testing.sling-mock.junit4</artifactId>
+          <version>3.0.0</version>
+          <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git a/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java b/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java
index 166160f..65632fd 100644
--- a/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java
+++ b/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java
@@ -111,6 +111,10 @@ public class JcrResourceBundleProvider implements ResourceBundleProvider, Resour
                 description = "In case of dictionary change events the cached "+
                         "resource bundle becomes invalid after the given delay (in ms). ")
         long invalidation_delay() default 5000;
+        
+        @AttributeDefinition(name="Excluded paths",
+                description="Do not check these paths for new ResourceBundles")
+        String[] excluded_paths() default {"/var/eventing"};
     }
 
     @Reference
@@ -167,6 +171,9 @@ public class JcrResourceBundleProvider implements ResourceBundleProvider, Resour
 
     private BundleTracker<Set<LocatorPaths>> locatorPathsTracker;
     private List<LocatorPaths> locatorPaths = new CopyOnWriteArrayList<>();
+    
+    // Ignore events in these paths
+    private String[] excludeddPaths;
 
     /**
      * Add a set of paths to the set that are inspected to
@@ -240,6 +247,10 @@ public class JcrResourceBundleProvider implements ResourceBundleProvider, Resour
         final ChangeStatus status = new ChangeStatus();
         try {
             for (final ResourceChange change : changes) {
+                
+                if (canIgnoreChange(change)) {
+                    continue;
+                }
                 this.onChange(status, change);
                 // if we need to reload all, we can skip all other events
                 if ( status.reloadAll ) {
@@ -261,6 +272,17 @@ public class JcrResourceBundleProvider implements ResourceBundleProvider, Resour
             }
         }
     }
+    
+    // skip if the change happens within a path configured in excludedPaths 
+    protected boolean canIgnoreChange(final ResourceChange change) {
+        for (String excludedPath: excludeddPaths) {
+            if (change.getPath().startsWith(excludedPath)) {
+                return true;
+            }
+        }
+        return false;
+    }
+    
 
     private void onChange(final ChangeStatus status, final ResourceChange change)
     throws LoginException {
@@ -433,6 +455,7 @@ public class JcrResourceBundleProvider implements ResourceBundleProvider, Resour
 
         this.bundleContext = context;
         this.invalidationDelay = config.invalidation_delay();
+        this.excludeddPaths = config.excluded_paths();
 
         locatorPathsTracker = new BundleTracker<>(this.bundleContext,
                 Bundle.ACTIVE, new LocatorPathsTracker(this));
diff --git a/src/test/java/org/apache/sling/i18n/impl/ConcurrentJcrResourceBundleLoadingTest.java b/src/test/java/org/apache/sling/i18n/impl/ConcurrentJcrResourceBundleLoadingTest.java
index 8c8acb4..d0d5249 100644
--- a/src/test/java/org/apache/sling/i18n/impl/ConcurrentJcrResourceBundleLoadingTest.java
+++ b/src/test/java/org/apache/sling/i18n/impl/ConcurrentJcrResourceBundleLoadingTest.java
@@ -104,6 +104,11 @@ public class ConcurrentJcrResourceBundleLoadingTest {
             public long invalidation_delay() {
                 return 5000;
             }
+            
+            @Override
+            public String[] excluded_paths() {
+                return new String[] {"/var/eventing"};
+            }
         });
         doReturn(null).when(provider, "createResourceResolver");
         doReturn(english).when(provider, "createResourceBundle", or(ArgumentMatchers.isNull(), any(ResourceResolver.class)), eq(null), eq(Locale.ENGLISH));
diff --git a/src/test/java/org/apache/sling/i18n/impl/JcrResourceBundleProviderTest.java b/src/test/java/org/apache/sling/i18n/impl/JcrResourceBundleProviderTest.java
index 877adb8..d9a5b61 100644
--- a/src/test/java/org/apache/sling/i18n/impl/JcrResourceBundleProviderTest.java
+++ b/src/test/java/org/apache/sling/i18n/impl/JcrResourceBundleProviderTest.java
@@ -18,13 +18,28 @@
  */
 package org.apache.sling.i18n.impl;
 
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.HashMap;
 import java.util.Locale;
+import java.util.Map;
 
+import org.apache.sling.api.resource.observation.ResourceChange;
+import org.apache.sling.commons.scheduler.ScheduleOptions;
+import org.apache.sling.commons.scheduler.Scheduler;
+import org.apache.sling.serviceusermapping.ServiceUserMapped;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
 import org.junit.Assert;
+import org.junit.Rule;
 import org.junit.Test;
+import org.mockito.Mockito;
 
 
 public class JcrResourceBundleProviderTest {
+    
+    @Rule
+    public final SlingContext context = new SlingContext();
 
     @Test
     public void testToLocale() {
@@ -94,5 +109,33 @@ public class JcrResourceBundleProviderTest {
         // Lowercase Private use Country 'xa'
         Assert.assertEquals(new Locale(Locale.GERMAN.getLanguage(), "XA"), JcrResourceBundleProvider.toLocale("de_xa"));
     }
+    
+    @Test
+    public void testPathExclusions() {
+        JcrResourceBundleProvider sut = new JcrResourceBundleProvider();
+        Map<String,Object> props = new HashMap<>();
+        props.put("excluded.paths", new String[] {"/excluded/path"});
+        
+        Scheduler scheduler = Mockito.mock(Scheduler.class);
+        Mockito.when(scheduler.schedule(Mockito.any(), Mockito.any())).thenReturn(false); // is ignored
+        Mockito.when(scheduler.AT(Mockito.any())).thenReturn(Mockito.mock(ScheduleOptions.class));
+        Mockito.when(scheduler.NOW()).thenReturn(Mockito.mock(ScheduleOptions.class));
+        context.registerService(Scheduler.class,scheduler);
+        
+        ServiceUserMapped serviceUserMapped = Mockito.mock(ServiceUserMapped.class);
+        context.registerService(ServiceUserMapped.class,serviceUserMapped);
+        
+        context.registerInjectActivateService(sut, props);
+        ResourceChange c1 = Mockito.mock(ResourceChange.class);
+        Mockito.when(c1.getPath()).thenReturn("/excluded/path");
+        ResourceChange c2 = Mockito.mock(ResourceChange.class);
+        Mockito.when(c2.getPath()).thenReturn("/another/path");
+        ResourceChange c3 = Mockito.mock(ResourceChange.class);
+        Mockito.when(c3.getPath()).thenReturn("/excluded/path/node");
+        
+        assertTrue(sut.canIgnoreChange(c1));
+        assertFalse(sut.canIgnoreChange(c2));
+        assertTrue(sut.canIgnoreChange(c3));
+    }
 
 }