jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1138898 - in /jackrabbit/sandbox/spi2microkernel/src: main/java/org/apache/jackrabbit/spi2microkernel/ main/java/org/apache/jackrabbit/spi2microkernel/util/ test/java/org/apache/jackrabbit/spi2microkernel/
Date Thu, 23 Jun 2011 14:29:55 GMT
Author: mduerig
Date: Thu Jun 23 14:29:54 2011
New Revision: 1138898

URL: http://svn.apache.org/viewvc?rev=1138898&view=rev
Log:
spi2microkernel prototype (WIP)
observation

Modified:
    jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/SubscriptionImpl.java
    jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Arrays.java
    jackrabbit/sandbox/spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/RepositoryTest.java

Modified: jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/SubscriptionImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/SubscriptionImpl.java?rev=1138898&r1=1138897&r2=1138898&view=diff
==============================================================================
--- jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/SubscriptionImpl.java
(original)
+++ jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/SubscriptionImpl.java
Thu Jun 23 14:29:54 2011
@@ -21,6 +21,7 @@ package org.apache.jackrabbit.spi2microk
 
 import org.apache.jackrabbit.mk.api.MicroKernel;
 import org.apache.jackrabbit.mk.json.JsopTokenizer;
+import org.apache.jackrabbit.mk.util.PathUtils;
 import org.apache.jackrabbit.spi.Event;
 import org.apache.jackrabbit.spi.EventBundle;
 import org.apache.jackrabbit.spi.EventFilter;
@@ -139,6 +140,12 @@ public class SubscriptionImpl implements
         }
     }
 
+    private static Path readPath(JsopTokenizer jsopTokenizer) {
+        String path = jsopTokenizer.readString();
+        path = path.substring(PathUtils.getNextSlash(path, 1)); // slash off workspace name
+        return Paths.stringToPath(path);
+    }
+
     private class EventSource {
         private final EventFilter[] eventFilters;
         private final List<String> commitLog;
@@ -231,7 +238,7 @@ public class SubscriptionImpl implements
                     case '-': removeNode(jsopTokenizer, eventTemplate, events); break;
                     case '^': setProperty(jsopTokenizer, eventTemplate, events); break;
                     case '>': moveNode(jsopTokenizer, eventTemplate, events); break;
-                    default: // todo log
+                    default: // skip 
                 }
             }
 
@@ -239,9 +246,8 @@ public class SubscriptionImpl implements
         }
 
         private void addItem(JsopTokenizer jsopTokenizer, EventTemplate eventTemplate, Collection<Event>
events) {
-            Path path = Paths.stringToPath(jsopTokenizer.readString());
+            Path path = readPath(jsopTokenizer);
             NodeId parentId = createNodeId(getParent(path));
-
             addItem(jsopTokenizer, eventTemplate, events, path, parentId);
         }
 
@@ -263,9 +269,13 @@ public class SubscriptionImpl implements
                 addInnerItems(jsopTokenizer, eventTemplate, events, nodeId);
             }
             else {
-                addItemEvent.setType(Event.PROPERTY_ADDED);
-                addItemEvent.setItemId(createPropertyId(parentId, path.getName()));
-                events.add(addItemEvent.buildEvent());
+                Name name = path.getName();
+                if (!name.getLocalName().startsWith(":")) {
+                    // skip internal property
+                    addItemEvent.setType(Event.PROPERTY_ADDED);
+                    addItemEvent.setItemId(createPropertyId(parentId, name));
+                    events.add(addItemEvent.buildEvent());
+                }
             }
         }
 
@@ -286,9 +296,9 @@ public class SubscriptionImpl implements
         }
 
         private void removeNode(JsopTokenizer jsopTokenizer, EventTemplate eventTemplate,
Collection<Event> events) {
-            Path path = Paths.stringToPath(jsopTokenizer.readString());
+            Path path = readPath(jsopTokenizer);
             NodeId parentId = createNodeId(getParent(path));
-            
+
             EventTemplate removeNodeEvent = eventTemplate.copy();
             removeNodeEvent.setType(Event.NODE_REMOVED);
             removeNodeEvent.setPath(path);
@@ -302,31 +312,34 @@ public class SubscriptionImpl implements
         }
 
         private void setProperty(JsopTokenizer jsopTokenizer, EventTemplate eventTemplate,
Collection<Event> events) {
-            Path path = Paths.stringToPath(jsopTokenizer.readString());
-            NodeId parentId = createNodeId(getParent(path));
-
-            EventTemplate setPropertyEvent = eventTemplate.copy();
-            setPropertyEvent.setPath(path);
-            setPropertyEvent.setParentId(parentId);
-            setPropertyEvent.setItemId(createNodeId(path));
-            // todo set primaryType, mixinTypes this needs context in the journal
-            // fixme it seems type based filtering does not respect the type hierarchy (see
JCR-2542)
+            Path path = readPath(jsopTokenizer);
+            Name name = path.getName();
+            if (!name.getLocalName().startsWith(":")) {
+                // skip internal property
+                NodeId parentId = createNodeId(getParent(path));
+
+                EventTemplate setPropertyEvent = eventTemplate.copy();
+                setPropertyEvent.setPath(path);
+                setPropertyEvent.setParentId(parentId);
+                setPropertyEvent.setItemId(createPropertyId(parentId, name));
+                // todo set primaryType, mixinTypes this needs context in the journal
+                // fixme it seems type based filtering does not respect the type hierarchy
(see JCR-2542)
+
+                jsopTokenizer.read(':');
+                if (jsopTokenizer.read() == JsopTokenizer.NULL) {
+                    setPropertyEvent.setType(Event.PROPERTY_REMOVED);
+                }
+                else {
+                    setPropertyEvent.setType(Event.PROPERTY_CHANGED);
+                }
 
-            jsopTokenizer.read(':');
-            if (jsopTokenizer.read() == JsopTokenizer.NULL) {
-                setPropertyEvent.setType(Event.PROPERTY_REMOVED);
+                events.add(setPropertyEvent.buildEvent());
+                // todo consider ways to generate events of (large) sub tree
             }
-            else {
-                setPropertyEvent.setType(Event.PROPERTY_CHANGED);
-            }
-
-            events.add(setPropertyEvent.buildEvent());
-            // todo consider ways to generate events of (large) sub tree
         }
 
         private void moveNode(JsopTokenizer jsopTokenizer, EventTemplate eventTemplate, Collection<Event>
events) {
             // todo implement moveNode
-            // Event#NODE_MOVED}
         }
 
         private String getString(JSONObject jsonObject, String key, boolean allowNull) {

Modified: jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Arrays.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Arrays.java?rev=1138898&r1=1138897&r2=1138898&view=diff
==============================================================================
--- jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Arrays.java
(original)
+++ jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Arrays.java
Thu Jun 23 14:29:54 2011
@@ -19,6 +19,9 @@
 
 package org.apache.jackrabbit.spi2microkernel.util;
 
+import java.util.HashSet;
+import java.util.Set;
+
 public final class Arrays {
     private Arrays() {}
 
@@ -30,5 +33,10 @@ public final class Arrays {
         }
         return false;
     }
-    
+
+    public static <T> Set<T> toSet(T... elements) {
+        Set<T> set = new HashSet<T>();
+        set.addAll(java.util.Arrays.asList(elements));
+        return set;
+    }
 }

Modified: jackrabbit/sandbox/spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/RepositoryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/RepositoryTest.java?rev=1138898&r1=1138897&r2=1138898&view=diff
==============================================================================
--- jackrabbit/sandbox/spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/RepositoryTest.java
(original)
+++ jackrabbit/sandbox/spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/RepositoryTest.java
Thu Jun 23 14:29:54 2011
@@ -22,6 +22,7 @@ import org.apache.jackrabbit.mk.MicroKer
 import org.apache.jackrabbit.mk.api.MicroKernel;
 import org.apache.jackrabbit.mk.json.JsonBuilder;
 import org.apache.jackrabbit.spi.commons.name.NameConstants;
+import org.apache.jackrabbit.spi2microkernel.util.Arrays;
 import org.apache.jackrabbit.spi2microkernel.util.Paths;
 import org.junit.After;
 import org.junit.Before;
@@ -51,10 +52,12 @@ import javax.jcr.observation.Observation
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.math.BigDecimal;
+import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Set;
 
 import static java.util.Arrays.asList;
@@ -906,23 +909,81 @@ public class RepositoryTest {
         }
     }
 
-    @Ignore  // todo make this into real test
     @Test
     public void observation() throws RepositoryException, InterruptedException {
+        final Set<String> addNodes = Arrays.toSet(
+                testPath + "/1",
+                testPath + "/2",
+                testPath + "/3");
+
+        final Set<String> removeNodes = Arrays.toSet(
+                testPath + "/2");
+
+        final Set<String> addProperties = Arrays.toSet(
+                testPath + "/property",
+                testPath + "/prop0",
+                testPath + "/1/prop1",
+                testPath + "/1/prop2",
+                testPath + "/1/jcr:primaryType",
+                testPath + "/2/jcr:primaryType",
+                testPath + "/3/jcr:primaryType");
+
+        final Set<String> setProperties = Arrays.toSet(
+                testPath + "/1/prop1");
+
+        final Set<String> removeProperties = Arrays.toSet(
+                testPath + "/1/prop2");
+
+        final List<Event> failedEvents = new ArrayList<Event>();
+
         ObservationManager obsMgr = getSession().getWorkspace().getObservationManager();
         obsMgr.setUserData("my user data");
-        obsMgr.addEventListener(new EventListener(){
+        obsMgr.addEventListener(new EventListener() {
                 public void onEvent(EventIterator events) {
                     while (events.hasNext()) {
-                        System.out.println(events.next());
+                        Event event = events.nextEvent();
+                        try {
+                            switch (event.getType()) {
+                                case Event.NODE_ADDED:
+                                    if (!addNodes.remove(event.getPath())) {
+                                        failedEvents.add(event);
+                                    }
+                                    break;
+                                case Event.NODE_REMOVED:
+                                    if (!removeNodes.remove(event.getPath())) {
+                                        failedEvents.add(event);
+                                    }
+                                    break;
+                                case Event.PROPERTY_ADDED:
+                                    if (!addProperties.remove(event.getPath())) {
+                                        failedEvents.add(event);
+                                    }
+                                    break;
+                                case Event.PROPERTY_CHANGED:
+                                    if (!setProperties.remove(event.getPath())) {
+                                        failedEvents.add(event);
+                                    }
+                                    break;
+                                case Event.PROPERTY_REMOVED:
+                                    if (!removeProperties.remove(event.getPath())) {
+                                        failedEvents.add(event);
+                                    }
+                                    break;
+                                default: 
+                                    failedEvents.add(event);
+                            }
+                        }                        
+                        catch (RepositoryException e) {
+                            failedEvents.add(event);
+                        }
                     }
                 }
             },
             Event.NODE_ADDED | Event.NODE_REMOVED | Event.NODE_MOVED | Event.PROPERTY_ADDED
|
             Event.PROPERTY_REMOVED | Event.PROPERTY_CHANGED | Event.PERSIST, "/", true, null,
null, false);
-
+        
         Node n = getNode(testPath);
-        n.setProperty("prop", "val");
+        n.setProperty("prop0", "val0");
         Node n1 = n.addNode("1");
         n1.setProperty("prop1", "val1");
         n1.setProperty("prop2", "val2");
@@ -935,35 +996,14 @@ public class RepositoryTest {
         n1.getProperty("prop2").remove();
         n.getNode("2").remove();  
         getSession().save();
-        Thread.sleep(20000);
-    }
-
-    @Ignore  // todo make this into real test
-    @Test 
-    public void observation2() throws RepositoryException, InterruptedException {
-        Node n = getNode(testPath);
-        Node n1 = n.addNode("1");
-        n1.setProperty("prop1", "val1");
-        n1.setProperty("prop2", "val2");
-        Node n2 = n1.addNode("2");
-        n2.addNode("3");
-        getSession().save();
-
-        ObservationManager obsMgr = getSession().getWorkspace().getObservationManager();
-        obsMgr.addEventListener(new EventListener(){
-                public void onEvent(EventIterator events) {
-                    while (events.hasNext()) {
-                        System.out.println(events.next());
-                    }
-                }
-            },
-            Event.NODE_ADDED | Event.NODE_REMOVED | Event.NODE_MOVED | Event.PROPERTY_ADDED
|
-            Event.PROPERTY_REMOVED | Event.PROPERTY_CHANGED | Event.PERSIST, "/", true, null,
null, false);
-
 
-        n1.remove();
-        getSession().save();
-        Thread.sleep(10000);
+        Thread.sleep(5000);
+        assertTrue(addNodes.isEmpty());
+        assertTrue(removeNodes.isEmpty());
+        assertTrue(addProperties.isEmpty());
+        assertTrue(removeProperties.isEmpty());
+        assertTrue(setProperties.isEmpty());
+        assertTrue(failedEvents.isEmpty());
     }
 
     //------------------------------------------< private >---



Mime
View raw message