jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1506543 - in /jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr: ItemImpl.java NodeImpl.java PropertyImpl.java
Date Wed, 24 Jul 2013 13:16:30 GMT
Author: jukka
Date: Wed Jul 24 13:16:30 2013
New Revision: 1506543

URL: http://svn.apache.org/r1506543
Log:
OAK-672: Avoid JCR APIs calling other JCR APIs

Make ItemReadOperation static and rename to ItemOperation.
Adjust many ItemImpl methods to better match the impl/delegate contract boundary.

Modified:
    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/NodeImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyImpl.java

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=1506543&r1=1506542&r2=1506543&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
Wed Jul 24 13:16:30 2013
@@ -73,11 +73,19 @@ abstract class ItemImpl<T extends ItemDe
         this.sessionDelegate = sessionContext.getSessionDelegate();
     }
 
-    protected abstract class ItemReadOperation<U> extends SessionOperation<U>
{
+    protected static abstract class ItemOperation<U> extends SessionOperation<U>
{
+
+        protected final ItemDelegate item;
+
+        protected ItemOperation(ItemDelegate item) {
+            this.item = item;
+        }
+
         @Override
         protected void checkPreconditions() throws RepositoryException {
-            dlg.checkAlive();
+            item.checkAlive();
         }
+
     }
 
     protected abstract class ItemWriteOperation<U> extends SessionOperation<U>
{
@@ -94,7 +102,7 @@ abstract class ItemImpl<T extends ItemDe
     }
 
     /**
-     * Perform the passed {@link org.apache.jackrabbit.oak.jcr.ItemImpl.ItemReadOperation}.
+     * Perform the passed {@link SessionOperation}.
      * @param op  operation to perform
      * @param <U>  return type of the operation
      * @return  the result of {@code op.perform()}
@@ -106,9 +114,9 @@ abstract class ItemImpl<T extends ItemDe
     }
 
     /**
-     * Perform the passed {@link org.apache.jackrabbit.oak.jcr.ItemImpl.ItemReadOperation}
assuming it does not throw an
-     * {@code RepositoryException}. If it does, wrap it into and throw it as an
-     * {@code IllegalArgumentException}.
+     * Perform the passed {@link SessionOperation} assuming it does not throw an
+     * {@code RepositoryException}. If it does, wrap it into and throw it as a
+     * {@code RuntimeException}.
      * @param op  operation to perform
      * @param <U>  return type of the operation
      * @return  the result of {@code op.perform()}
@@ -117,11 +125,9 @@ abstract class ItemImpl<T extends ItemDe
     protected final <U> U safePerform(@Nonnull SessionOperation<U> op) {
         try {
             return sessionDelegate.perform(op);
-        }
-        catch (RepositoryException e) {
-            String msg = "Unexpected exception thrown by operation " + op;
-            log.error(msg, e);
-            throw new IllegalArgumentException(msg, e);
+        } catch (RepositoryException e) {
+            throw new RuntimeException(
+                    "Unexpected exception thrown by operation " + op, e);
         }
     }
 
@@ -133,14 +139,14 @@ abstract class ItemImpl<T extends ItemDe
     @Override
     @Nonnull
     public String getName() throws RepositoryException {
-        return perform(new ItemReadOperation<String>() {
+        String oakName = perform(new ItemOperation<String>(dlg) {
             @Override
             public String perform() throws RepositoryException {
-                String oakName = dlg.getName();
-                // special case name of root node
-                return oakName.isEmpty() ? "" : toJcrPath(dlg.getName());
+                return item.getName();
             }
         });
+        // special case name of root node
+        return oakName.isEmpty() ? "" : toJcrPath(dlg.getName());
     }
 
     /**
@@ -149,12 +155,12 @@ abstract class ItemImpl<T extends ItemDe
     @Override
     @Nonnull
     public String getPath() throws RepositoryException {
-        return perform(new ItemReadOperation<String>() {
+        return toJcrPath(perform(new ItemOperation<String>(dlg) {
             @Override
             public String perform() throws RepositoryException {
-                return toJcrPath(dlg.getPath());
+                return item.getPath();
             }
-        });
+        }));
     }
 
     @Override
@@ -165,44 +171,48 @@ abstract class ItemImpl<T extends ItemDe
 
     @Override
     public Item getAncestor(final int depth) throws RepositoryException {
-        return perform(new ItemReadOperation<Item>() {
+        if (depth < 0) {
+            throw new ItemNotFoundException(
+                    getPath() + "Invalid ancestor depth " + depth);
+        } else if (depth == 0) {
+            return sessionContext.getSession().getRootNode();
+        }
+
+        ItemDelegate ancestor = perform(new ItemOperation<ItemDelegate>(dlg) {
             @Override
-            protected Item perform() throws RepositoryException {
-                if (depth < 0) {
-                    throw new ItemNotFoundException(this + ": Invalid ancestor depth (" +
depth + ')');
-                } else if (depth == 0) {
-                    NodeDelegate nd = sessionDelegate.getRootNode();
-                    if (nd == null) {
-                        throw new AccessDeniedException("Root node is not accessible.");
-                    }
-                    return sessionContext.createNodeOrNull(nd);
-                }
+            protected ItemDelegate perform() throws RepositoryException {
+                String path = item.getPath();
 
-                String path = dlg.getPath();
                 int slash = 0;
                 for (int i = 0; i < depth - 1; i++) {
                     slash = PathUtils.getNextSlash(path, slash + 1);
                     if (slash == -1) {
-                        throw new ItemNotFoundException(this + ": Invalid ancestor depth
(" + depth + ')');
+                        throw new ItemNotFoundException(
+                                path + ": Invalid ancestor depth " + depth);
                     }
                 }
                 slash = PathUtils.getNextSlash(path, slash + 1);
                 if (slash == -1) {
-                    return ItemImpl.this;
+                    return item;
                 }
 
-                NodeDelegate nd = sessionDelegate.getNode(path.substring(0, slash));
-                if (nd == null) {
-                    throw new AccessDeniedException(this + ": Ancestor access denied (" +
depth + ')');
-                }
-                return sessionContext.createNodeOrNull(nd);
+                return sessionDelegate.getNode(path.substring(0, slash));
             }
         });
+
+        if (ancestor == dlg) {
+            return this;
+        } else if (ancestor instanceof NodeDelegate) {
+            return sessionContext.createNodeOrNull((NodeDelegate) ancestor);
+        } else {
+            throw new AccessDeniedException(
+                    getPath() + ": Access denied to ancestor at depth " + depth);
+        }
     }
 
     @Override
     public int getDepth() throws RepositoryException {
-        return perform(new ItemReadOperation<Integer>() {
+        return perform(new ItemOperation<Integer>(dlg) {
             @Override
             public Integer perform() throws RepositoryException {
                 return PathUtils.getDepth(dlg.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=1506543&r1=1506542&r2=1506543&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
Wed Jul 24 13:16:30 2013
@@ -121,7 +121,7 @@ public class NodeImpl<T extends NodeDele
     @Override
     @Nonnull
     public Node getParent() throws RepositoryException {
-        return perform(new ItemReadOperation<Node>() {
+        return perform(new ItemOperation<Node>(dlg) {
             @Override
             public Node perform() throws RepositoryException {
                 if (dlg.isRoot()) {
@@ -142,7 +142,7 @@ public class NodeImpl<T extends NodeDele
      */
     @Override
     public boolean isNew() {
-        return safePerform(new ItemReadOperation<Boolean>() {
+        return safePerform(new ItemOperation<Boolean>(dlg) {
             @Override
             public Boolean perform() {
                 return dlg.exists() && dlg.getStatus() == Status.NEW;
@@ -155,7 +155,7 @@ public class NodeImpl<T extends NodeDele
      */
     @Override
     public boolean isModified() {
-        return safePerform(new ItemReadOperation<Boolean>() {
+        return safePerform(new ItemOperation<Boolean>(dlg) {
             @Override
             public Boolean perform() {
                 return dlg.exists() && dlg.getStatus() == Status.MODIFIED;
@@ -480,7 +480,7 @@ public class NodeImpl<T extends NodeDele
     @Override
     @Nonnull
     public Node getNode(final String relPath) throws RepositoryException {
-        return perform(new ItemReadOperation<Node>() {
+        return perform(new ItemOperation<Node>(dlg) {
             @Override
             public Node perform() throws RepositoryException {
                 String oakPath = getOakPathOrThrowNotFound(relPath);
@@ -498,7 +498,7 @@ public class NodeImpl<T extends NodeDele
     @Override
     @Nonnull
     public NodeIterator getNodes() throws RepositoryException {
-        return perform(new ItemReadOperation<NodeIterator>() {
+        return perform(new ItemOperation<NodeIterator>(dlg) {
             @Override
             public NodeIterator perform() throws RepositoryException {
                 Iterator<NodeDelegate> children = dlg.getChildren();
@@ -526,7 +526,7 @@ public class NodeImpl<T extends NodeDele
     public NodeIterator getNodes(final String namePattern)
             throws RepositoryException {
 
-        return perform(new ItemReadOperation<NodeIterator>() {
+        return perform(new ItemOperation<NodeIterator>(dlg) {
             @Override
             public NodeIterator perform() throws RepositoryException {
                 Iterator<NodeDelegate> children = Iterators.filter(dlg.getChildren(),
@@ -545,7 +545,7 @@ public class NodeImpl<T extends NodeDele
     @Override
     @Nonnull
     public NodeIterator getNodes(final String[] nameGlobs) throws RepositoryException {
-        return perform(new ItemReadOperation<NodeIterator>() {
+        return perform(new ItemOperation<NodeIterator>(dlg) {
             @Override
             public NodeIterator perform() throws RepositoryException {
                 Iterator<NodeDelegate> children = Iterators.filter(dlg.getChildren(),
@@ -564,7 +564,7 @@ public class NodeImpl<T extends NodeDele
     @Override
     @Nonnull
     public Property getProperty(final String relPath) throws RepositoryException {
-        return perform(new ItemReadOperation<PropertyImpl>() {
+        return perform(new ItemOperation<PropertyImpl>(dlg) {
             @Override
             public PropertyImpl perform() throws RepositoryException {
                 String oakPath = getOakPathOrThrowNotFound(relPath);
@@ -581,7 +581,7 @@ public class NodeImpl<T extends NodeDele
     @Override
     @Nonnull
     public PropertyIterator getProperties() throws RepositoryException {
-        return perform(new ItemReadOperation<PropertyIterator>() {
+        return perform(new ItemOperation<PropertyIterator>(dlg) {
             @Override
             public PropertyIterator perform() throws RepositoryException {
                 Iterator<PropertyDelegate> properties = dlg.getProperties();
@@ -594,7 +594,7 @@ public class NodeImpl<T extends NodeDele
     @Override
     @Nonnull
     public PropertyIterator getProperties(final String namePattern) throws RepositoryException
{
-        return perform(new ItemReadOperation<PropertyIterator>() {
+        return perform(new ItemOperation<PropertyIterator>(dlg) {
             @Override
             public PropertyIterator perform() throws RepositoryException {
                 Iterator<PropertyDelegate> properties = Iterators.filter(dlg.getProperties(),
@@ -613,7 +613,7 @@ public class NodeImpl<T extends NodeDele
     @Override
     @Nonnull
     public PropertyIterator getProperties(final String[] nameGlobs) throws RepositoryException
{
-        return perform(new ItemReadOperation<PropertyIterator>() {
+        return perform(new ItemOperation<PropertyIterator>(dlg) {
             @Override
             public PropertyIterator perform() throws RepositoryException {
                 Iterator<PropertyDelegate> propertyNames = Iterators.filter(dlg.getProperties(),
@@ -635,7 +635,7 @@ public class NodeImpl<T extends NodeDele
     @Override
     @Nonnull
     public Item getPrimaryItem() throws RepositoryException {
-        return perform(new ItemReadOperation<Item>() {
+        return perform(new ItemOperation<Item>(dlg) {
             @Override
             public Item perform() throws RepositoryException {
                 String name = getPrimaryNodeType().getPrimaryItemName();
@@ -659,7 +659,7 @@ public class NodeImpl<T extends NodeDele
     @Override
     @Nonnull
     public String getUUID() throws RepositoryException {
-        return perform(new ItemReadOperation<String>() {
+        return perform(new ItemOperation<String>(dlg) {
             @Override
             public String perform() throws RepositoryException {
                 if (isNodeType(NodeType.MIX_REFERENCEABLE)) {
@@ -674,7 +674,7 @@ public class NodeImpl<T extends NodeDele
     @Override
     @Nonnull
     public String getIdentifier() throws RepositoryException {
-        return perform(new ItemReadOperation<String>() {
+        return perform(new ItemOperation<String>(dlg) {
             @Override
             public String perform() throws RepositoryException {
                 return dlg.getIdentifier();
@@ -689,7 +689,7 @@ public class NodeImpl<T extends NodeDele
     }
 
     private PropertyIterator internalGetReferences(final String name, final boolean weak)
throws RepositoryException {
-        return perform(new ItemReadOperation<PropertyIterator>() {
+        return perform(new ItemOperation<PropertyIterator>(dlg) {
             @Override
             public PropertyIterator perform() throws InvalidItemStateException {
                 IdentifierManager idManager = sessionDelegate.getIdManager();
@@ -743,7 +743,7 @@ public class NodeImpl<T extends NodeDele
 
     @Override
     public boolean hasNode(final String relPath) throws RepositoryException {
-        return perform(new ItemReadOperation<Boolean>() {
+        return perform(new ItemOperation<Boolean>(dlg) {
             @Override
             public Boolean perform() throws RepositoryException {
                 String oakPath = getOakPathOrThrow(relPath);
@@ -754,7 +754,7 @@ public class NodeImpl<T extends NodeDele
 
     @Override
     public boolean hasProperty(final String relPath) throws RepositoryException {
-        return perform(new ItemReadOperation<Boolean>() {
+        return perform(new ItemOperation<Boolean>(dlg) {
             @Override
             public Boolean perform() throws RepositoryException {
                 String oakPath = getOakPathOrThrow(relPath);
@@ -770,7 +770,7 @@ public class NodeImpl<T extends NodeDele
 
     @Override
     public boolean hasProperties() throws RepositoryException {
-        return perform(new ItemReadOperation<Boolean>() {
+        return perform(new ItemOperation<Boolean>(dlg) {
             @Override
             public Boolean perform() throws RepositoryException {
                 return dlg.getPropertyCount() != 0;
@@ -784,7 +784,7 @@ public class NodeImpl<T extends NodeDele
     @Override
     @Nonnull
     public NodeType getPrimaryNodeType() throws RepositoryException {
-        return perform(new ItemReadOperation<NodeType>() {
+        return perform(new ItemOperation<NodeType>(dlg) {
             @Override
             public NodeType perform() throws RepositoryException {
                 String primaryNtName;
@@ -804,7 +804,7 @@ public class NodeImpl<T extends NodeDele
     @Override
     @Nonnull
     public NodeType[] getMixinNodeTypes() throws RepositoryException {
-        return perform(new ItemReadOperation<NodeType[]>() {
+        return perform(new ItemOperation<NodeType[]>(dlg) {
             @Override
             public NodeType[] perform() throws RepositoryException {
                 // TODO: check if transient changes to mixin-types are reflected here
@@ -825,7 +825,7 @@ public class NodeImpl<T extends NodeDele
 
     @Override
     public boolean isNodeType(final String nodeTypeName) throws RepositoryException {
-        return perform(new ItemReadOperation<Boolean>() {
+        return perform(new ItemOperation<Boolean>(dlg) {
             @Override
             protected Boolean perform() throws RepositoryException {
                 String oakName = getOakName(nodeTypeName);
@@ -896,7 +896,7 @@ public class NodeImpl<T extends NodeDele
     @Override
     public boolean canAddMixin(String mixinName) throws RepositoryException {
         final String oakTypeName = getOakName(mixinName);
-        return perform(new ItemReadOperation<Boolean>() {
+        return perform(new ItemOperation<Boolean>(dlg) {
             @Override
             public Boolean perform() throws RepositoryException {
                 PropertyState prop = PropertyStates.createProperty(JCR_MIXINTYPES, singleton(oakTypeName),
NAMES);
@@ -912,7 +912,7 @@ public class NodeImpl<T extends NodeDele
     @Override
     @Nonnull
     public NodeDefinition getDefinition() throws RepositoryException {
-        return perform(new ItemReadOperation<NodeDefinition>() {
+        return perform(new ItemOperation<NodeDefinition>(dlg) {
             @Override
             protected NodeDefinition perform() throws RepositoryException {
                 NodeDelegate parent = dlg.getParent();
@@ -1083,7 +1083,7 @@ public class NodeImpl<T extends NodeDele
      */
     @Override
     public boolean isLocked() throws RepositoryException {
-        return perform(new ItemReadOperation<Boolean>() {
+        return perform(new ItemOperation<Boolean>(dlg) {
             @Override
             protected Boolean perform() throws RepositoryException {
                 String lockOwner = getOakPathOrThrow(JCR_LOCK_OWNER);
@@ -1119,7 +1119,7 @@ public class NodeImpl<T extends NodeDele
      */
     @Override
     public boolean holdsLock() throws RepositoryException {
-        return perform(new ItemReadOperation<Boolean>() {
+        return perform(new ItemOperation<Boolean>(dlg) {
             @Override
             protected Boolean perform() throws RepositoryException {
                 String lockOwner = getOakPathOrThrow(JCR_LOCK_OWNER);
@@ -1241,7 +1241,7 @@ public class NodeImpl<T extends NodeDele
     @Override
     @Nonnull
     public NodeIterator getSharedSet() throws RepositoryException {
-        return perform(new ItemReadOperation<NodeIterator>() {
+        return perform(new ItemOperation<NodeIterator>(dlg) {
             @Override
             protected NodeIterator perform() {
                 return new NodeIteratorAdapter(ImmutableSet.of(NodeImpl.this));

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyImpl.java?rev=1506543&r1=1506542&r2=1506543&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyImpl.java
Wed Jul 24 13:16:30 2013
@@ -66,7 +66,7 @@ public class PropertyImpl extends ItemIm
     @Override
     @Nonnull
     public Node getParent() throws RepositoryException {
-        return perform(new ItemReadOperation<Node>() {
+        return perform(new ItemOperation<Node>(dlg) {
             @Override
             public Node perform() throws RepositoryException {
                 NodeDelegate parent = dlg.getParent();
@@ -81,7 +81,7 @@ public class PropertyImpl extends ItemIm
 
     @Override
     public boolean isNew() {
-        return safePerform(new ItemReadOperation<Boolean>() {
+        return safePerform(new ItemOperation<Boolean>(dlg) {
             @Override
             public Boolean perform() {
                 return dlg.getStatus() == Status.NEW;
@@ -91,7 +91,7 @@ public class PropertyImpl extends ItemIm
 
     @Override
     public boolean isModified() {
-        return safePerform(new ItemReadOperation<Boolean>() {
+        return safePerform(new ItemOperation<Boolean>(dlg) {
             @Override
             public Boolean perform() {
                 return dlg.getStatus() == Status.MODIFIED;
@@ -223,7 +223,7 @@ public class PropertyImpl extends ItemIm
     @Override
     @Nonnull
     public Value getValue() throws RepositoryException {
-        return perform(new ItemReadOperation<Value>() {
+        return perform(new ItemOperation<Value>(dlg) {
             @Override
             public Value perform() throws RepositoryException {
                 return ValueFactoryImpl.createValue(dlg.getSingleState(), sessionContext);
@@ -234,7 +234,7 @@ public class PropertyImpl extends ItemIm
     @Override
     @Nonnull
     public Value[] getValues() throws RepositoryException {
-        return perform(new ItemReadOperation<List<Value>>() {
+        return perform(new ItemOperation<List<Value>>(dlg) {
             @Override
             public List<Value> perform() throws RepositoryException {
                 return ValueFactoryImpl.createValues(dlg.getMultiState(), sessionContext);
@@ -291,7 +291,7 @@ public class PropertyImpl extends ItemIm
     @Override
     @Nonnull
     public Node getNode() throws RepositoryException {
-        return perform(new ItemReadOperation<Node>() {
+        return perform(new ItemOperation<Node>(dlg) {
             @Override
             public Node perform() throws RepositoryException {
                 Value value = getValue();
@@ -343,7 +343,7 @@ public class PropertyImpl extends ItemIm
     @Override
     @Nonnull
     public Property getProperty() throws RepositoryException {
-        return perform(new ItemReadOperation<Property>() {
+        return perform(new ItemOperation<Property>(dlg) {
             @Override
             public Property perform() throws RepositoryException {
                 Value value = getValue();
@@ -378,7 +378,7 @@ public class PropertyImpl extends ItemIm
     @Override
     @Nonnull
     public PropertyDefinition getDefinition() throws RepositoryException {
-        return perform(new ItemReadOperation<PropertyDefinition>() {
+        return perform(new ItemOperation<PropertyDefinition>(dlg) {
             @Override
             protected PropertyDefinition perform() throws RepositoryException {
                 return getDefinitionProvider().getDefinition(
@@ -389,7 +389,7 @@ public class PropertyImpl extends ItemIm
 
     @Override
     public int getType() throws RepositoryException {
-        return perform(new ItemReadOperation<Integer>() {
+        return perform(new ItemOperation<Integer>(dlg) {
             @Override
             public Integer perform() throws RepositoryException {
                 return dlg.getPropertyState().getType().tag();
@@ -399,7 +399,7 @@ public class PropertyImpl extends ItemIm
 
     @Override
     public boolean isMultiple() throws RepositoryException {
-        return perform(new ItemReadOperation<Boolean>() {
+        return perform(new ItemOperation<Boolean>(dlg) {
             @Override
             public Boolean perform() throws RepositoryException {
                 return dlg.getPropertyState().isArray();



Mime
View raw message