Author: mduerig
Date: Wed Mar 12 21:46:09 2014
New Revision: 1576938

URL: http://svn.apache.org/r1576938
Log:
OAK-1514: Support for Sling JcrResourceListener
Report a selected set of extra properties

Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/NodeObserver.java
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/NodeObserverTest.java

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/NodeObserver.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/NodeObserver.java?rev=1576938&r1=1576937&r2=1576938&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/NodeObserver.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/NodeObserver.java
 Wed Mar 12 21:46:09 2014
@@ -19,13 +19,18 @@
 
 package org.apache.jackrabbit.oak.plugins.observation;
 
+import static java.util.Collections.addAll;
+
+import java.util.Map;
 import java.util.Set;
 
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
+import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.api.Type;
 import org.apache.jackrabbit.oak.commons.PathUtils;
 import org.apache.jackrabbit.oak.core.ImmutableRoot;
 import org.apache.jackrabbit.oak.namepath.GlobalNameMapper;
@@ -42,15 +47,18 @@ import org.apache.jackrabbit.oak.spi.sta
  */
 public abstract class NodeObserver implements Observer {
     private final String path;
+    private final Set<String> propertyNames = Sets.newHashSet();
 
     private NodeState previousRoot;
 
     /**
      * Create a new instance for observing the given path.
      * @param path
+     * @param propertyNames  names of properties to report even without a 
change
      */
-    protected NodeObserver(String path) {
+    protected NodeObserver(String path, String... propertyNames) {
         this.path = path;
+        addAll(this.propertyNames, propertyNames);
     }
 
     /**
@@ -59,6 +67,7 @@ public abstract class NodeObserver imple
      * @param added      Names of the added properties.
      * @param deleted    Names of the deleted properties.
      * @param changed    Names of the changed properties.
+     * @param properties Properties as specified in the constructor
      * @param commitInfo commit info associated with this change.
      */
     protected abstract void added(
@@ -66,6 +75,7 @@ public abstract class NodeObserver imple
             @Nonnull Set<String> added,
             @Nonnull Set<String> deleted,
             @Nonnull Set<String> changed,
+            @Nonnull Map<String, String> properties,
             @Nonnull CommitInfo commitInfo);
 
     /**
@@ -74,6 +84,7 @@ public abstract class NodeObserver imple
      * @param added      Names of the added properties.
      * @param deleted    Names of the deleted properties.
      * @param changed    Names of the changed properties.
+     * @param properties Properties as specified in the constructor
      * @param commitInfo commit info associated with this change.
      */
     protected abstract void deleted(
@@ -81,6 +92,7 @@ public abstract class NodeObserver imple
             @Nonnull Set<String> added,
             @Nonnull Set<String> deleted,
             @Nonnull Set<String> changed,
+            @Nonnull Map<String, String> properties,
             @Nonnull CommitInfo commitInfo);
 
     /**
@@ -89,6 +101,7 @@ public abstract class NodeObserver imple
      * @param added      Names of the added properties.
      * @param deleted    Names of the deleted properties.
      * @param changed    Names of the changed properties.
+     * @param properties Properties as specified in the constructor
      * @param commitInfo commit info associated with this change.
      */
     protected abstract void changed(
@@ -96,6 +109,7 @@ public abstract class NodeObserver imple
             @Nonnull Set<String> added,
             @Nonnull Set<String> deleted,
             @Nonnull Set<String> changed,
+            @Nonnull Map<String, String> properties,
             @Nonnull CommitInfo commitInfo);
 
     @Override
@@ -153,19 +167,33 @@ public abstract class NodeObserver imple
         public void leave(NodeState before, NodeState after) {
             switch (eventType) {
                 case ADDED:
-                    added(namePathMapper.getJcrPath(path), added, deleted, 
changed, commitInfo);
+                    added(namePathMapper.getJcrPath(path), added, deleted, 
changed,
+                            collectProperties(after), commitInfo);
                     break;
                 case DELETED:
-                    deleted(namePathMapper.getJcrPath(path), added, deleted, 
changed, commitInfo);
+                    deleted(namePathMapper.getJcrPath(path), added, deleted, 
changed,
+                            collectProperties(before), commitInfo);
                     break;
                 case CHANGED:
                     if (!added.isEmpty() || ! deleted.isEmpty() || 
!changed.isEmpty()) {
-                        changed(namePathMapper.getJcrPath(path), added, 
deleted, changed, commitInfo);
+                        changed(namePathMapper.getJcrPath(path), added, 
deleted, changed,
+                                collectProperties(after), commitInfo);
                     }
                     break;
             }
         }
 
+        private Map<String, String> collectProperties(NodeState node) {
+            Map<String, String> properties = Maps.newHashMap();
+            for (String name : propertyNames) {
+                PropertyState p = node.getProperty(name);
+                if (p != null && !p.isArray()) {
+                    properties.put(name, p.getValue(Type.STRING));
+                }
+            }
+            return properties;
+        }
+
         @Override
         public EventHandler getChildHandler(String name, NodeState before, 
NodeState after) {
             if (!before.exists()) {

Modified: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/NodeObserverTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/NodeObserverTest.java?rev=1576938&r1=1576937&r2=1576938&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/NodeObserverTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/NodeObserverTest.java
 Wed Mar 12 21:46:09 2014
@@ -19,6 +19,8 @@
 
 package org.apache.jackrabbit.oak.plugins.observation;
 
+import static com.google.common.collect.Maps.newHashMap;
+import static com.google.common.collect.Sets.newHashSet;
 import static 
org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState.EMPTY_NODE;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
@@ -30,8 +32,6 @@ import javax.annotation.Nonnull;
 
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
 import org.apache.jackrabbit.oak.spi.commit.CommitInfo;
 import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
@@ -44,6 +44,7 @@ public class NodeObserverTest {
     {
         NodeBuilder builder = EMPTY_NODE.builder();
         builder.setChildNode("m").setChildNode("n").setProperty("p", 1);
+        builder.getChildNode("m").getChildNode("n").setProperty("extra", 42);
         
builder.getChildNode("m").getChildNode("n").setChildNode("o").setProperty("q", 
2);
         builder.setChildNode("a").setChildNode("b").setProperty("p", 1);
         before = builder.getNodeState();
@@ -53,7 +54,7 @@ public class NodeObserverTest {
 
     @Before
     public void setup() {
-        nodeObserver = new TestNodeObserver("/m/n");
+        nodeObserver = new TestNodeObserver("/m/n", "extra");
         nodeObserver.contentChanged(before, CommitInfo.EMPTY);
     }
 
@@ -66,6 +67,7 @@ public class NodeObserverTest {
         assertEquals(ImmutableMap.of("/m/n/new", ImmutableSet.of("p")), 
nodeObserver.added);
         assertTrue(nodeObserver.deleted.isEmpty());
         assertTrue(nodeObserver.changed.isEmpty());
+        assertTrue(nodeObserver.properties.isEmpty());
     }
 
     @Test
@@ -77,6 +79,7 @@ public class NodeObserverTest {
         assertTrue(nodeObserver.added.isEmpty());
         assertEquals(ImmutableMap.of("/m/n/o", ImmutableSet.of("q")), 
nodeObserver.deleted);
         assertTrue(nodeObserver.changed.isEmpty());
+        assertTrue(nodeObserver.properties.isEmpty());
     }
 
     @Test
@@ -88,6 +91,7 @@ public class NodeObserverTest {
         assertTrue(nodeObserver.added.isEmpty());
         assertTrue(nodeObserver.deleted.isEmpty());
         assertEquals(ImmutableMap.of("/m/n", ImmutableSet.of("p")), 
nodeObserver.changed);
+        assertEquals(ImmutableMap.of("/m/n", ImmutableMap.of("extra", "42")), 
nodeObserver.properties);
     }
 
     @Test
@@ -99,6 +103,7 @@ public class NodeObserverTest {
         assertTrue(nodeObserver.added.isEmpty());
         assertTrue(nodeObserver.deleted.isEmpty());
         assertTrue(nodeObserver.changed.isEmpty());
+        assertTrue(nodeObserver.properties.isEmpty());
     }
 
     @Test
@@ -110,6 +115,7 @@ public class NodeObserverTest {
         assertTrue(nodeObserver.added.isEmpty());
         assertTrue(nodeObserver.deleted.isEmpty());
         assertTrue(nodeObserver.changed.isEmpty());
+        assertTrue(nodeObserver.properties.isEmpty());
     }
 
     @Test
@@ -121,17 +127,19 @@ public class NodeObserverTest {
         assertTrue(nodeObserver.added.isEmpty());
         assertTrue(nodeObserver.deleted.isEmpty());
         assertTrue(nodeObserver.changed.isEmpty());
+        assertTrue(nodeObserver.properties.isEmpty());
     }
 
     //------------------------------------------------------------< 
TestNodeObserver >---
 
     private static class TestNodeObserver extends NodeObserver {
-        private final Map<String, Set<String>> added = Maps.newHashMap();
-        private final Map<String, Set<String>> deleted = Maps.newHashMap();
-        private final Map<String, Set<String>> changed = Maps.newHashMap();
+        private final Map<String, Set<String>> added = newHashMap();
+        private final Map<String, Set<String>> deleted = newHashMap();
+        private final Map<String, Set<String>> changed = newHashMap();
+        private final Map<String, Map<String, String>> properties = 
newHashMap();
 
-        protected TestNodeObserver(String path) {
-            super(path);
+        protected TestNodeObserver(String path, String... propertyNames) {
+            super(path, propertyNames);
         }
 
         @Override
@@ -140,8 +148,12 @@ public class NodeObserverTest {
                 @Nonnull Set<String> added,
                 @Nonnull Set<String> deleted,
                 @Nonnull Set<String> changed,
+                @Nonnull Map<String, String> properties,
                 @Nonnull CommitInfo commitInfo) {
-            update(this.added, path, added);
+            this.added.put(path, newHashSet(added));
+            if (!properties.isEmpty()) {
+                this.properties.put(path, newHashMap(properties));
+            }
         }
 
         @Override
@@ -150,8 +162,12 @@ public class NodeObserverTest {
                 @Nonnull Set<String> added,
                 @Nonnull Set<String> deleted,
                 @Nonnull Set<String> changed,
+                @Nonnull Map<String, String> properties,
                 @Nonnull CommitInfo commitInfo) {
-            update(this.deleted, path, deleted);
+            this.deleted.put(path, newHashSet(deleted));
+            if (!properties.isEmpty()) {
+                this.properties.put(path, newHashMap(properties));
+            }
         }
 
         @Override
@@ -160,17 +176,13 @@ public class NodeObserverTest {
                 @Nonnull Set<String> added,
                 @Nonnull Set<String> deleted,
                 @Nonnull Set<String> changed,
+                @Nonnull Map<String, String> properties,
                 @Nonnull CommitInfo commitInfo) {
-            update(this.changed, path, changed);
-        }
-
-        private static void update(Map<String, Set<String>> map, String key, 
Set<String> value) {
-            Set<String> current = map.get(key);
-            if (current == null) {
-                current = Sets.newHashSet();
+            this.changed.put(path, newHashSet(changed));
+            if (!properties.isEmpty()) {
+                this.properties.put(path, newHashMap(properties));
             }
-            current.addAll(value);
-            map.put(key, current);
         }
+
     }
 }


Reply via email to