You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:38:43 UTC

[sling-org-apache-sling-fsresource] 10/29: SLING-6440 simplify FileMonitor

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

rombert pushed a commit to annotated tag org.apache.sling.fsresource-2.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-fsresource.git

commit 299edbd8103826d59a3ea32c2ce2e5ee45bea01d
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Tue Feb 28 16:21:54 2017 +0000

    SLING-6440 simplify FileMonitor
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/fsresource@1784773 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/fsprovider/internal/FileMonitor.java     | 32 +++++++++-------------
 .../sling/fsprovider/internal/FileMonitorTest.java | 12 ++------
 2 files changed, 16 insertions(+), 28 deletions(-)

diff --git a/src/main/java/org/apache/sling/fsprovider/internal/FileMonitor.java b/src/main/java/org/apache/sling/fsprovider/internal/FileMonitor.java
index 05d01b7..7d77482 100644
--- a/src/main/java/org/apache/sling/fsprovider/internal/FileMonitor.java
+++ b/src/main/java/org/apache/sling/fsprovider/internal/FileMonitor.java
@@ -20,10 +20,8 @@ package org.apache.sling.fsprovider.internal;
 
 import java.io.File;
 import java.util.ArrayList;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 import java.util.Timer;
 import java.util.TimerTask;
 
@@ -208,17 +206,22 @@ public final class FileMonitor extends TimerTask {
             log.debug("Detected change for resource {} : {}", monitorable.path, changeType);
         }
 
+        List<ResourceChange> changes = null;
         for (final ObserverConfiguration config : reporter.getObserverConfigurations()) {
-            if ( config.matches(monitorable.path) ) {
-                List<ResourceChange> changes = collectResourceChanges(monitorable, changeType);
+            if (config.matches(monitorable.path)) {
+                if (changes == null) {
+                    changes = collectResourceChanges(monitorable, changeType);
+                }
                 if (log.isTraceEnabled()) {
                     for (ResourceChange change : changes) {
                         log.debug("Send change for resource {}: {} to {}", change.getPath(), change.getType(), config);
                     }
                 }
-                reporter.reportChanges(changes, false);
             }
         }
+        if (changes != null) {
+            reporter.reportChanges(changes, false);
+        }
     }
     
     @SuppressWarnings("unchecked")
@@ -230,7 +233,7 @@ public final class FileMonitor extends TimerTask {
                 Map<String,Object> content = (Map<String,Object>)contentFile.getContent();
                 // we cannot easily report the diff of resource changes between two content files
                 // so we simulate a removal of the toplevel node and then add all nodes contained in the current content file again.
-                changes.add(buildContentResourceChange(ChangeType.REMOVED, content, monitorable.path));
+                changes.add(buildContentResourceChange(ChangeType.REMOVED,  monitorable.path));
                 addContentResourceChanges(changes, ChangeType.ADDED, content, monitorable.path);
             }
             else {
@@ -238,14 +241,14 @@ public final class FileMonitor extends TimerTask {
             }
         }
         else {
-            changes.add(new ResourceChange(changeType, monitorable.path, false, null, null, null));
+            changes.add(buildContentResourceChange(changeType, monitorable.path));
         }
         return changes;
     }
     @SuppressWarnings("unchecked")
     private void addContentResourceChanges(final List<ResourceChange> changes, final ChangeType changeType,
             final Map<String,Object> content, final String path) {
-        changes.add(buildContentResourceChange(changeType, content, path));
+        changes.add(buildContentResourceChange(changeType,  path));
         if (content != null) {
             for (Map.Entry<String,Object> entry : content.entrySet()) {
                 if (entry.getValue() instanceof Map) {
@@ -255,17 +258,8 @@ public final class FileMonitor extends TimerTask {
             }
         }
     }
-    private ResourceChange buildContentResourceChange(final ChangeType changeType, final Map<String,Object> content, final String path) {
-        Set<String> addedPropertyNames = null;
-        if (content != null && changeType == ChangeType.ADDED) {
-            addedPropertyNames = new HashSet<>();
-            for (Map.Entry<String,Object> entry : content.entrySet()) {
-                if (!(entry.getValue() instanceof Map)) {
-                    addedPropertyNames.add(entry.getKey());
-                }
-            }
-        }
-        return new ResourceChange(changeType, path, false, addedPropertyNames, null, null);
+    private ResourceChange buildContentResourceChange(final ChangeType changeType, final String path) {
+        return new ResourceChange(changeType, path, false, null, null, null);
     }
 
     /**
diff --git a/src/test/java/org/apache/sling/fsprovider/internal/FileMonitorTest.java b/src/test/java/org/apache/sling/fsprovider/internal/FileMonitorTest.java
index f9cc25c..020405d 100644
--- a/src/test/java/org/apache/sling/fsprovider/internal/FileMonitorTest.java
+++ b/src/test/java/org/apache/sling/fsprovider/internal/FileMonitorTest.java
@@ -38,8 +38,6 @@ import org.apache.sling.testing.mock.sling.junit.SlingContextCallback;
 import org.junit.Rule;
 import org.junit.Test;
 
-import com.google.common.collect.ImmutableSet;
-
 /**
  * Test events when changing filesystem content.
  */
@@ -185,8 +183,8 @@ public class FileMonitorTest {
 
         assertEquals(3, changes.size());
         assertChange(changes, "/fs-test/folder1", ChangeType.CHANGED);
-        assertChange(changes, "/fs-test/folder1/file1c", ChangeType.ADDED, "prop1");
-        assertChange(changes, "/fs-test/folder1/file1c/child1", ChangeType.ADDED, "prop2");
+        assertChange(changes, "/fs-test/folder1/file1c", ChangeType.ADDED);
+        assertChange(changes, "/fs-test/folder1/file1c/child1", ChangeType.ADDED);
     }
     
     @Test
@@ -205,19 +203,15 @@ public class FileMonitorTest {
     }
     
     
-    private void assertChange(List<ResourceChange> changes, String path, ChangeType changeType, String... addedPropertyNames) {
+    private void assertChange(List<ResourceChange> changes, String path, ChangeType changeType) {
         boolean found = false;
         for (ResourceChange change : changes) {
             if (StringUtils.equals(change.getPath(), path) && change.getType() == changeType) {
                 found = true;
-                if (addedPropertyNames.length > 0) {
-                    assertEquals(ImmutableSet.copyOf(addedPropertyNames), change.getAddedPropertyNames());
-                }
                 break;
             }
         }
         assertTrue("Change with path=" + path + ", changeType=" + changeType, found);
-
     }
     
     static class ResourceListener implements ResourceChangeListener {

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.