jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1334454 - in /jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr: ItemDelegate.java ItemImpl.java NodeDelegate.java NodeImpl.java PropertyDelegate.java
Date Sat, 05 May 2012 17:12:59 GMT
Author: mduerig
Date: Sat May  5 17:12:58 2012
New Revision: 1334454

URL: http://svn.apache.org/viewvc?rev=1334454&view=rev
Log:
OAK-84: Delegates for Session, Node, Property and Item
- Make NodeDelegate more versatile
- Add javadoc to NodeDelegate

Modified:
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemDelegate.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeDelegate.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyDelegate.java

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemDelegate.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemDelegate.java?rev=1334454&r1=1334453&r2=1334454&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemDelegate.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemDelegate.java
Sat May  5 17:12:58 2012
@@ -22,4 +22,7 @@ public abstract class ItemDelegate {
     abstract String getName();
 
     abstract String getPath();
+
+    abstract boolean isStale();
+
 }

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemImpl.java?rev=1334454&r1=1334453&r2=1334454&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemImpl.java
Sat May  5 17:12:58 2012
@@ -135,8 +135,11 @@ abstract class ItemImpl extends Abstract
      * @throws RepositoryException if this item has been rendered invalid for some reason
      */
     void checkStatus() throws RepositoryException {
-        // check session status
+        if (dlg.isStale()) {
+            throw new InvalidItemStateException("stale");
+        }
 
+        // check session status
         if (!sessionDelegate.isAlive()) {
             throw new RepositoryException("This session has been closed.");
         }

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeDelegate.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeDelegate.java?rev=1334454&r1=1334453&r2=1334454&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeDelegate.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeDelegate.java
Sat May  5 17:12:58 2012
@@ -24,16 +24,18 @@ import org.apache.jackrabbit.oak.commons
 import org.apache.jackrabbit.oak.util.Function1;
 import org.apache.jackrabbit.oak.util.Iterators;
 
-import javax.jcr.InvalidItemStateException;
-import javax.jcr.ItemExistsException;
-import javax.jcr.ItemNotFoundException;
-import javax.jcr.PathNotFoundException;
-import javax.jcr.RepositoryException;
 import java.util.Iterator;
 import java.util.List;
 
+/**
+ * {@code NodeDelegate} serve as internal representations of {@code Node}s.
+ * The methods of this class do not throw checked exceptions. Instead clients
+ * are expected to inspect the return value and ensure that all preconditions
+ * hold before a method is invoked. Specifically the behaviour of all methods
+ * of this class but {@link #isStale()} is undefined if the instance is stale.
+ * An item is stale if the underlying items does not exist anymore.
+ */
 public class NodeDelegate extends ItemDelegate {
-
     private final SessionDelegate sessionDelegate;
     private Tree tree;
 
@@ -42,92 +44,173 @@ public class NodeDelegate extends ItemDe
         this.tree = tree;
     }
 
-    NodeDelegate addNode(String relPath) throws RepositoryException {
-        Tree parentState = getTree(PathUtils.getParentPath(relPath));
-        if (parentState == null) {
-            throw new PathNotFoundException(relPath);
-        }
-
-        String name = PathUtils.getName(relPath);
-        if (parentState.hasChild(name)) {
-            throw new ItemExistsException(relPath);
-        }
-
-        Tree added = parentState.addChild(name);
-        return new NodeDelegate(sessionDelegate, added);
-    }
-
-    Iterator<NodeDelegate> getChildren() {
-        return nodeDelegateIterator(getTree().getChildren().iterator());
-    }
-
-    long getChildrenCount() {
-        return getTree().getChildrenCount();
-    }
-
+    /**
+     * Get the name of this node
+     * @return oak name of the node
+     */
     @Override
     String getName() {
         return getTree().getName();
     }
 
-    Status getNodeStatus() throws InvalidItemStateException {
-        return check(getTree().getParent()).getChildStatus(getName());
+    /**
+     * Get the path of this node
+     * @return oak path of the node
+     */
+    @Override
+    String getPath() {
+        return '/' + getTree().getPath();
     }
 
-    NodeDelegate getNodeOrNull(String relOakPath) {
-        Tree tree = getTree(relOakPath);
-        return tree == null ? null : new NodeDelegate(sessionDelegate, tree);
+    /**
+     * Determine whether this node is stale
+     * @return  {@code true} iff stale
+     */
+    @Override
+    boolean isStale() {
+        return getTree() == null;
     }
 
-    NodeDelegate getParent() throws RepositoryException {
-        if (check(getTree()).getParent() == null) {
-            throw new ItemNotFoundException("Root has no parent");
+    /**
+     * Determine whether this is the root node
+     * @return  {@code true} iff this is the root node
+     */
+    boolean isRoot() {
+        return getParentTree() == null;
+    }
+
+    /**
+     * Get the status of this node
+     * @return  {@link Status} of this node
+     */
+    Status getStatus() {
+        Tree parent = getParentTree();
+        if (parent == null) {
+            return Status.EXISTING;  // FIXME: return correct status for root
+        }
+        else {
+            return parent.getChildStatus(getName());
         }
-
-        return new NodeDelegate(sessionDelegate, getTree().getParent());
     }
 
-    @Override
-    String getPath() {
-        return '/' + getTree().getPath();
+    /**
+     * Get the session which with this node is associated
+     * @return  {@link SessionDelegate} to which this node belongs
+     */
+    SessionDelegate getSessionDelegate() {
+        return sessionDelegate;
     }
 
-    Iterator<PropertyDelegate> getProperties() throws RepositoryException {
-        return propertyDelegateIterator(getTree().getProperties().iterator());
+    /**
+     * Get the parent of this node
+     * @return  parent of this node or {@code null} it this is the root
+     */
+    NodeDelegate getParent() {
+        Tree parent = getParentTree();
+        return parent == null ? null : new NodeDelegate(sessionDelegate, parent);
     }
 
+    /**
+     * Get the number of properties of this node
+     * @return  number of properties of this node
+     */
     long getPropertyCount() {
         return getTree().getPropertyCount();
     }
 
-    PropertyDelegate getPropertyOrNull(String relOakPath) {
-        Tree parent = getTree(PathUtils.getParentPath(relOakPath));
+    /**
+     * Get a property
+     * @param relPath  oak path
+     * @return  property at the path given by {@code relPath} or {@code null} if
+     * no such property exists
+     */
+    PropertyDelegate getProperty(String relPath) {
+        Tree parent = getTree(PathUtils.getParentPath(relPath));
         if (parent == null) {
             return null;
         }
 
-        String name = PathUtils.getName(relOakPath);
+        String name = PathUtils.getName(relPath);
         PropertyState propertyState = parent.getProperty(name);
-        return propertyState == null ? null : new PropertyDelegate(
-                sessionDelegate, parent, propertyState);
+        return propertyState == null
+            ? null
+            : new PropertyDelegate(sessionDelegate, parent, propertyState);
     }
 
-    SessionDelegate getSessionDelegate() {
-        return sessionDelegate;
+    /**
+     * Get the properties of this node
+     * @return  properties of this node
+     */
+    Iterator<PropertyDelegate> getProperties() {
+        return propertyDelegateIterator(getTree().getProperties().iterator());
     }
 
-    void remove() {
-        getTree().getParent().removeChild(getName());
+    /**
+     * Get the number of child nodes
+     * @return  number of child nodes of this node
+     */
+    long getChildCount() {
+        return getTree().getChildrenCount();
     }
 
-    PropertyDelegate setProperty(String oakName, CoreValue value) {
-        getTree().setProperty(oakName, value);
-        return getPropertyOrNull(oakName);
+    /**
+     * Get child node
+     * @param relPath  oak path
+     * @return  node at the path given by {@code relPath} or {@code null} if
+     * no such node exists
+     */
+    NodeDelegate getChild(String relPath) {
+        Tree tree = getTree(relPath);
+        return tree == null ? null : new NodeDelegate(sessionDelegate, tree);
+    }
+
+    /**
+     * Get child nodes
+     * @return  child nodes of this node
+     */
+    Iterator<NodeDelegate> getChildren() {
+        return nodeDelegateIterator(getTree().getChildren().iterator());
     }
 
-    PropertyDelegate setProperty(String oakName, List<CoreValue> value) {
-        getTree().setProperty(oakName, value);
-        return getPropertyOrNull(oakName);
+    /**
+     * Set a property
+     * @param name  oak name
+     * @param value
+     * @return  the set property
+     */
+    PropertyDelegate setProperty(String name, CoreValue value) {
+        getTree().setProperty(name, value);
+        return getProperty(name);
+    }
+
+    /**
+     * Set a multi valued property
+     * @param name  oak name
+     * @param value
+     * @return  the set property
+     */
+    PropertyDelegate setProperty(String name, List<CoreValue> value) {
+        getTree().setProperty(name, value);
+        return getProperty(name);
+    }
+
+    /**
+     * Add a child node
+     * @param name  oak name
+     * @return  the added node or {@code null} if such a node already exists
+     */
+    NodeDelegate addChild(String name) {
+        Tree tree = getTree();
+        return tree.hasChild(name)
+            ? null
+            : new NodeDelegate(sessionDelegate, tree.addChild(name));
+    }
+
+    /**
+     * Remove this node
+     */
+    void remove() {
+        getParentTree().removeChild(getName());
     }
 
     // -----------------------------------------------------------< private >---
@@ -143,13 +226,10 @@ public class NodeDelegate extends ItemDe
         return tree;
     }
 
-    private static Tree check(Tree t) throws InvalidItemStateException {
-        if (t == null) {
-            throw new InvalidItemStateException();
-        }
-        return t;
+    private Tree getParentTree() {
+        return getTree().getParent();
     }
-    
+
     private synchronized Tree getTree() {
         return tree = sessionDelegate.getTree(tree.getPath());
     }

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java?rev=1334454&r1=1334453&r2=1334454&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
Sat May  5 17:12:58 2012
@@ -21,6 +21,7 @@ import org.apache.jackrabbit.commons.ite
 import org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter;
 import org.apache.jackrabbit.oak.api.CoreValue;
 import org.apache.jackrabbit.oak.api.Tree.Status;
+import org.apache.jackrabbit.oak.commons.PathUtils;
 import org.apache.jackrabbit.oak.jcr.util.LogUtil;
 import org.apache.jackrabbit.oak.jcr.value.ValueConverter;
 import org.apache.jackrabbit.oak.util.Function1;
@@ -31,8 +32,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import javax.jcr.Binary;
-import javax.jcr.InvalidItemStateException;
 import javax.jcr.Item;
+import javax.jcr.ItemExistsException;
 import javax.jcr.ItemNotFoundException;
 import javax.jcr.ItemVisitor;
 import javax.jcr.Node;
@@ -96,7 +97,12 @@ public class NodeImpl extends ItemImpl i
      */
     @Override
     public Node getParent() throws RepositoryException {
-        return new NodeImpl(dlg.getParent());
+        checkStatus();
+        NodeDelegate parent = dlg.getParent();
+        if (parent == null) {
+            throw new ItemNotFoundException("Root has no parent");
+        }
+        return new NodeImpl(parent);
     }
 
     /**
@@ -104,11 +110,7 @@ public class NodeImpl extends ItemImpl i
      */
     @Override
     public boolean isNew() {
-        try {
-            return dlg.getNodeStatus() == Status.NEW;
-        } catch (InvalidItemStateException ex) {
-            return false;
-        }
+        return !dlg.isStale() && dlg.getStatus() == Status.NEW;
     }
 
     /**
@@ -116,11 +118,7 @@ public class NodeImpl extends ItemImpl i
      */
     @Override
     public boolean isModified() {
-        try {
-            return dlg.getNodeStatus() == Status.MODIFIED;
-        } catch (InvalidItemStateException ex) {
-            return false;
-        }
+        return !dlg.isStale() && dlg.getStatus() == Status.MODIFIED;
     }
 
     /**
@@ -128,6 +126,7 @@ public class NodeImpl extends ItemImpl i
      */
     @Override
     public void remove() throws RepositoryException {
+        checkStatus();
         dlg.remove();
     }
 
@@ -146,6 +145,7 @@ public class NodeImpl extends ItemImpl i
      */
     @Override
     public Node addNode(String relPath) throws RepositoryException {
+        checkStatus();
         return addNode(relPath, null);
     }
 
@@ -153,6 +153,17 @@ public class NodeImpl extends ItemImpl i
     public Node addNode(String relPath, String primaryNodeTypeName) throws RepositoryException
{
         checkStatus();
 
+        String oakPath = toOakPath(relPath);
+        String oakName = PathUtils.getName(oakPath);
+        NodeDelegate parent = dlg.getChild(PathUtils.getParentPath(oakPath));
+        if (parent == null) {
+            throw new PathNotFoundException(relPath);
+        }
+
+        if (parent.getChild(oakName) != null) {
+            throw new ItemExistsException(relPath);
+        }
+
         if (primaryNodeTypeName == null) {
             // TODO retrieve matching nt from effective definition based on name-matching.
             primaryNodeTypeName = NodeType.NT_UNSTRUCTURED;
@@ -166,7 +177,7 @@ public class NodeImpl extends ItemImpl i
         }
         // TODO: END
         
-        NodeDelegate added = dlg.addNode(toOakPath(relPath));
+        NodeDelegate added = parent.addChild(oakName);
         Node childNode = new NodeImpl(added);
         childNode.setPrimaryType(primaryNodeTypeName);
         return childNode;
@@ -442,6 +453,8 @@ public class NodeImpl extends ItemImpl i
 
     @Override
     public PropertyIterator getProperties(final String[] nameGlobs) throws RepositoryException
{
+        checkStatus();
+
         Iterator<PropertyDelegate> propertyNames = filter(dlg.getProperties(),
                 new Predicate<PropertyDelegate>() {
                     @Override
@@ -551,7 +564,7 @@ public class NodeImpl extends ItemImpl i
     public boolean hasNodes() throws RepositoryException {
         checkStatus();
 
-        return dlg.getChildrenCount() != 0;
+        return dlg.getChildCount() != 0;
     }
 
     @Override
@@ -950,14 +963,14 @@ public class NodeImpl extends ItemImpl i
     private NodeImpl getNodeOrNull(String relJcrPath)
             throws RepositoryException {
 
-        NodeDelegate nd = dlg.getNodeOrNull(toOakPath(relJcrPath));
+        NodeDelegate nd = dlg.getChild(toOakPath(relJcrPath));
         return nd == null ? null : new NodeImpl(nd);
     }
 
     private PropertyImpl getPropertyOrNull(String relJcrPath)
             throws RepositoryException {
 
-        PropertyDelegate pd = dlg.getPropertyOrNull(toOakPath(relJcrPath));
+        PropertyDelegate pd = dlg.getProperty(toOakPath(relJcrPath));
         return pd == null ? null : new PropertyImpl(pd);
     }
 

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyDelegate.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyDelegate.java?rev=1334454&r1=1334453&r2=1334454&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyDelegate.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyDelegate.java
Sat May  5 17:12:58 2012
@@ -160,6 +160,11 @@ public class PropertyDelegate extends It
         return parentPath.isEmpty() ? '/' + getName() : '/' + parentPath + '/' + getName();
     }
 
+    @Override
+    boolean isStale() {
+        return getParentTree() == null;
+    }
+
     SessionDelegate getSessionDelegate() {
         return sessionDelegate;
     }



Mime
View raw message