jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r1436385 - in /jackrabbit/oak/trunk: oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/ChangeFilter.java oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/ObservationManagerImpl.java oak-jcr/pom.xml
Date Mon, 21 Jan 2013 14:48:27 GMT
Author: mreutegg
Date: Mon Jan 21 14:48:26 2013
New Revision: 1436385

URL: http://svn.apache.org/viewvc?rev=1436385&view=rev
Log:
OAK-144 Implement observation
- implement support for node type filtering

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/ChangeFilter.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/ObservationManagerImpl.java
    jackrabbit/oak/trunk/oak-jcr/pom.xml

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/ChangeFilter.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/ChangeFilter.java?rev=1436385&r1=1436384&r2=1436385&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/ChangeFilter.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/ChangeFilter.java
Mon Jan 21 14:48:26 2013
@@ -16,35 +16,70 @@
  */
 package org.apache.jackrabbit.oak.plugins.observation;
 
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+import javax.jcr.RepositoryException;
+import javax.jcr.nodetype.NoSuchNodeTypeException;
+
+import org.apache.jackrabbit.oak.api.Tree;
 import org.apache.jackrabbit.oak.commons.PathUtils;
+import org.apache.jackrabbit.oak.core.ReadOnlyTree;
+import org.apache.jackrabbit.oak.namepath.NamePathMapper;
+import org.apache.jackrabbit.oak.plugins.nodetype.ReadOnlyNodeTypeManager;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * TODO document
  */
 class ChangeFilter {
+
+    private static final Logger log = LoggerFactory.getLogger(ChangeFilter.class);
+
+    private final ReadOnlyNodeTypeManager ntMgr;
+    private final NamePathMapper namePathMapper;
     private final int eventTypes;
     private final String path;
     private final boolean deep;
     private final String[] uuid;          // TODO implement filtering by uuid
-    private final String[] nodeTypeName;  // TODO implement filtering by nodeTypeName
+    private final String[] nodeTypeOakName;
     private final boolean noLocal;        // TODO implement filtering by noLocal
 
-    public ChangeFilter(int eventTypes, String path, boolean deep, String[] uuid, String[]
nodeTypeName,
-            boolean noLocal) {
+    public ChangeFilter(ReadOnlyNodeTypeManager ntMgr,
+                        NamePathMapper namePathMapper, int eventTypes,
+                        String path, boolean deep, String[] uuid,
+                        String[] nodeTypeName, boolean noLocal)
+            throws NoSuchNodeTypeException, RepositoryException {
+        this.ntMgr = ntMgr;
+        this.namePathMapper = namePathMapper;
         this.eventTypes = eventTypes;
         this.path = path;
         this.deep = deep;
         this.uuid = uuid;
-        this.nodeTypeName = nodeTypeName;
+        this.nodeTypeOakName = validateNodeTypeNames(nodeTypeName);
         this.noLocal = noLocal;
     }
 
-    public boolean include(int eventType) {
+    public boolean include(int eventType, String path, NodeState associatedParentNode) {
+        return include(eventType)
+                && include(path)
+                && includeByType(new ReadOnlyTree(associatedParentNode));
+    }
+
+    public boolean includeChildren(String path) {
+        return PathUtils.isAncestor(path, this.path) ||
+                path.equals(this.path) ||
+                deep && PathUtils.isAncestor(this.path, path);
+    }
+
+    //-----------------------------< internal >---------------------------------
+
+    private boolean include(int eventType) {
         return (this.eventTypes & eventType) != 0;
     }
 
-    public boolean include(String path) {
+    private boolean include(String path) {
         boolean equalPaths = this.path.equals(path);
         if (!deep && !equalPaths) {
             return false;
@@ -55,13 +90,54 @@ class ChangeFilter {
         return true;
     }
 
-    public boolean include(int eventType, String path, NodeState associatedParentNode) {
-        return include(eventType) && include(path);
+    /**
+     * Checks whether to include an event based on the type of the associated
+     * parent node and the node type filter.
+     *
+     * @param associatedParentNode the associated parent node of the event.
+     * @return whether to include the event based on the type of the associated
+     *         parent node.
+     */
+    private boolean includeByType(Tree associatedParentNode) {
+        if (nodeTypeOakName == null) {
+            return true;
+        }
+        try {
+            for (String oakName : nodeTypeOakName) {
+                if (ntMgr.isNodeType(associatedParentNode, oakName)) {
+                    return true;
+                }
+            }
+        } catch (RepositoryException e) {
+            // shouldn't happen, because node type was validated in constructor
+            // FIXME: rather throw?
+            log.warn("Unable to check node type of associated parent node", e);
+        }
+        // filter has node types set but none matched
+        return false;
     }
 
-    public boolean includeChildren(String path) {
-        return PathUtils.isAncestor(path, this.path) ||
-                path.equals(this.path) ||
-                deep && PathUtils.isAncestor(this.path, path);
+    /**
+     * Validates the given node type names.
+     *
+     * @param nodeTypeNames the node type names.
+     * @return the node type names as oak names.
+     * @throws NoSuchNodeTypeException if one of the node type names refers to
+     *                                 an non-existing node type.
+     * @throws RepositoryException     if an error occurs while reading from the
+     *                                 node type manager.
+     */
+    @CheckForNull
+    private String[] validateNodeTypeNames(@Nullable String[] nodeTypeNames)
+            throws NoSuchNodeTypeException, RepositoryException {
+        if (nodeTypeNames == null) {
+            return null;
+        }
+        String[] oakNames = new String[nodeTypeNames.length];
+        for (int i = 0; i < nodeTypeNames.length; i++) {
+            ntMgr.getNodeType(nodeTypeNames[i]);
+            oakNames[i] = namePathMapper.getOakName(nodeTypeNames[i]);
+        }
+        return oakNames;
     }
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/ObservationManagerImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/ObservationManagerImpl.java?rev=1436385&r1=1436384&r2=1436385&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/ObservationManagerImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/ObservationManagerImpl.java
Mon Jan 21 14:48:26 2013
@@ -21,6 +21,7 @@ import java.util.Map;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.atomic.AtomicBoolean;
 
+import javax.annotation.Nonnull;
 import javax.jcr.RepositoryException;
 import javax.jcr.UnsupportedRepositoryOperationException;
 import javax.jcr.observation.EventJournal;
@@ -31,8 +32,11 @@ import javax.jcr.observation.Observation
 import com.google.common.base.Preconditions;
 import org.apache.jackrabbit.commons.iterator.EventListenerIteratorAdapter;
 import org.apache.jackrabbit.oak.api.Root;
+import org.apache.jackrabbit.oak.api.Tree;
 import org.apache.jackrabbit.oak.core.RootImpl;
 import org.apache.jackrabbit.oak.namepath.NamePathMapper;
+import org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeConstants;
+import org.apache.jackrabbit.oak.plugins.nodetype.ReadOnlyNodeTypeManager;
 import org.apache.jackrabbit.oak.spi.observation.ChangeExtractor;
 
 /**
@@ -44,12 +48,14 @@ public class ObservationManagerImpl impl
     private final ScheduledExecutorService executor;
     private final Map<EventListener, ChangeProcessor> processors = new HashMap<EventListener,
ChangeProcessor>();
     private final AtomicBoolean hasEvents = new AtomicBoolean(false);
+    private final ReadOnlyNodeTypeManager ntMgr;
 
     public ObservationManagerImpl(Root root, NamePathMapper namePathMapper, ScheduledExecutorService
executor) {
         Preconditions.checkArgument(root instanceof RootImpl, "root must be of actual type
RootImpl");
         this.root = ((RootImpl) root);
         this.namePathMapper = namePathMapper;
         this.executor = executor;
+        this.ntMgr = new NTMgr();
     }
 
     public synchronized void dispose() {
@@ -71,7 +77,8 @@ public class ObservationManagerImpl impl
     @Override
     public synchronized void addEventListener(EventListener listener, int eventTypes, String
absPath,
             boolean isDeep, String[] uuid, String[] nodeTypeName, boolean noLocal) throws
RepositoryException {
-        ChangeFilter filter = new ChangeFilter(eventTypes, absPath, isDeep, uuid, nodeTypeName,
noLocal);
+        ChangeFilter filter = new ChangeFilter(ntMgr, namePathMapper, eventTypes,
+                absPath, isDeep, uuid, nodeTypeName, noLocal);
         ChangeProcessor processor = processors.get(listener);
         if (processor == null) {
             processor = new ChangeProcessor(this, listener, filter);
@@ -125,4 +132,18 @@ public class ObservationManagerImpl impl
     void setHasEvents() {
         hasEvents.set(true);
     }
+
+    private final class NTMgr extends ReadOnlyNodeTypeManager {
+
+        @Override
+        protected Tree getTypes() {
+            return root.getTree(NodeTypeConstants.NODE_TYPES_PATH);
+        }
+
+        @Nonnull
+        @Override
+        protected NamePathMapper getNamePathMapper() {
+            return namePathMapper;
+        }
+    }
 }

Modified: jackrabbit/oak/trunk/oak-jcr/pom.xml
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/pom.xml?rev=1436385&r1=1436384&r2=1436385&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/pom.xml (original)
+++ jackrabbit/oak/trunk/oak-jcr/pom.xml Mon Jan 21 14:48:26 2013
@@ -211,7 +211,6 @@
       org.apache.jackrabbit.test.api.observation.NodeReorderTest#testNodeReorderMove    
           <!-- reorder not supported -->
       org.apache.jackrabbit.test.api.observation.NodeReorderTest#testNodeReorderSameName
           <!-- reorder not supported -->
       org.apache.jackrabbit.test.api.observation.NodeReorderTest#testNodeReorderSameNameWithRemove
 <!-- reorder not supported -->
-      org.apache.jackrabbit.test.api.observation.AddEventListenerTest#testNodeType
       org.apache.jackrabbit.test.api.observation.AddEventListenerTest#testNoLocalTrue
       org.apache.jackrabbit.test.api.observation.GetIdentifierTest#testNodeAdded
       org.apache.jackrabbit.test.api.observation.GetIdentifierTest#testNodeMoved



Mime
View raw message