You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by md...@apache.org on 2013/03/26 13:19:02 UTC

svn commit: r1461074 - in /jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr: ItemImpl.java SessionImpl.java

Author: mduerig
Date: Tue Mar 26 12:19:01 2013
New Revision: 1461074

URL: http://svn.apache.org/r1461074
Log:
OAK-247: SessionImpl should not inherit from AbstractSession
copy missing methods from base classes

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/SessionImpl.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=1461074&r1=1461073&r2=1461074&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 Tue Mar 26 12:19:01 2013
@@ -17,8 +17,10 @@
 package org.apache.jackrabbit.oak.jcr;
 
 import javax.annotation.Nonnull;
+import javax.jcr.AccessDeniedException;
 import javax.jcr.InvalidItemStateException;
 import javax.jcr.Item;
+import javax.jcr.ItemNotFoundException;
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
@@ -27,8 +29,9 @@ import javax.jcr.nodetype.ConstraintViol
 import javax.jcr.nodetype.ItemDefinition;
 import javax.jcr.nodetype.NodeTypeManager;
 
-import org.apache.jackrabbit.commons.AbstractItem;
+import org.apache.jackrabbit.oak.commons.PathUtils;
 import org.apache.jackrabbit.oak.jcr.delegate.ItemDelegate;
+import org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate;
 import org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate;
 import org.apache.jackrabbit.oak.jcr.delegate.SessionOperation;
 import org.apache.jackrabbit.oak.plugins.nodetype.DefinitionProvider;
@@ -38,7 +41,7 @@ import org.slf4j.LoggerFactory;
 /**
  * TODO document
  */
-abstract class ItemImpl<T extends ItemDelegate> extends AbstractItem {
+abstract class ItemImpl<T extends ItemDelegate> implements Item {
 
     protected final SessionContext sessionContext;
     protected final T dlg;
@@ -103,6 +106,63 @@ abstract class ItemImpl<T extends ItemDe
         return sessionContext.getSession();
     }
 
+    @Override
+    public Item getAncestor(final int depth) throws RepositoryException {
+        return perform(new SessionOperation<Item>() {
+            @Override
+            protected void checkPreconditions() throws RepositoryException {
+                checkStatus();
+            }
+
+            @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 new NodeImpl<NodeDelegate>(nd, sessionContext);
+                }
+
+                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 + ')');
+                    }
+                }
+                slash = PathUtils.getNextSlash(path, slash + 1);
+                if (slash == -1) {
+                    return ItemImpl.this;
+                }
+
+                NodeDelegate nd = sessionDelegate.getNode(path.substring(0, slash));
+                if (nd == null) {
+                    throw new AccessDeniedException(this + ": Ancestor access denied (" + depth + ')');
+                }
+                return new NodeImpl<NodeDelegate>(nd, sessionContext);
+            }
+        });
+    }
+
+    @Override
+    public int getDepth() throws RepositoryException {
+        return perform(new SessionOperation<Integer>() {
+            @Override
+            protected void checkPreconditions() throws RepositoryException {
+                checkStatus();
+            }
+
+            @Override
+            public Integer perform() throws RepositoryException {
+                return PathUtils.getDepth(dlg.getPath());
+            }
+        });
+    }
+
     /**
      * @see Item#isSame(javax.jcr.Item)
      */

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java?rev=1461074&r1=1461073&r2=1461074&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java (original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java Tue Mar 26 12:19:01 2013
@@ -16,6 +16,9 @@
  */
 package org.apache.jackrabbit.oak.jcr;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.util.Arrays;
 import java.util.HashSet;
 import java.util.Locale;
@@ -25,6 +28,7 @@ import java.util.Set;
 import javax.annotation.Nonnull;
 import javax.jcr.AccessDeniedException;
 import javax.jcr.Credentials;
+import javax.jcr.InvalidSerializedDataException;
 import javax.jcr.Item;
 import javax.jcr.ItemNotFoundException;
 import javax.jcr.NamespaceException;
@@ -45,7 +49,11 @@ import javax.jcr.security.AccessControlM
 import org.apache.jackrabbit.api.JackrabbitSession;
 import org.apache.jackrabbit.api.security.principal.PrincipalManager;
 import org.apache.jackrabbit.api.security.user.UserManager;
-import org.apache.jackrabbit.commons.AbstractSession;
+import org.apache.jackrabbit.commons.xml.DocumentViewExporter;
+import org.apache.jackrabbit.commons.xml.Exporter;
+import org.apache.jackrabbit.commons.xml.ParsingContentHandler;
+import org.apache.jackrabbit.commons.xml.SystemViewExporter;
+import org.apache.jackrabbit.commons.xml.ToXmlContentHandler;
 import org.apache.jackrabbit.oak.api.TreeLocation;
 import org.apache.jackrabbit.oak.commons.PathUtils;
 import org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate;
@@ -61,11 +69,12 @@ import org.apache.jackrabbit.util.XMLCha
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
 
 /**
  * TODO document
  */
-public class SessionImpl extends AbstractSession implements JackrabbitSession {
+public class SessionImpl implements JackrabbitSession {
 
     /**
      * logger instance
@@ -315,6 +324,11 @@ public class SessionImpl extends Abstrac
     }
 
     @Override
+    public void removeItem(String absPath) throws RepositoryException {
+        getItem(absPath).remove();
+    }
+
+    @Override
     public void save() throws RepositoryException {
         ensureIsAlive();
         dlg.save();
@@ -356,6 +370,95 @@ public class SessionImpl extends Abstrac
         return new ImportHandler(getNode(parentAbsPath), sessionContext, uuidBehavior);
     }
 
+    @Override
+    public void importXML(String parentAbsPath, InputStream in, int uuidBehavior)
+            throws IOException, RepositoryException {
+        try {
+            ContentHandler handler = getImportContentHandler(parentAbsPath, uuidBehavior);
+            new ParsingContentHandler(handler).parse(in);
+        } catch (SAXException e) {
+            Throwable exception = e.getException();
+            if (exception instanceof RepositoryException) {
+                throw (RepositoryException) exception;
+            } else if (exception instanceof IOException) {
+                throw (IOException) exception;
+            } else {
+                throw new InvalidSerializedDataException("XML parse error", e);
+            }
+        } finally {
+            // JCR-2903
+            if (in != null) {
+                try { in.close(); } catch (IOException ignore) {}
+            }
+        }
+    }
+
+    /**
+     * Exports content at the given path using the given exporter.
+     *
+     * @param path of the node to be exported
+     * @param exporter document or system view exporter
+     * @throws SAXException if the SAX event handler failed
+     * @throws RepositoryException if another error occurs
+     */
+    private synchronized void export(String path, Exporter exporter)
+            throws SAXException, RepositoryException {
+        Item item = getItem(path);
+        if (item.isNode()) {
+            exporter.export((Node) item);
+        } else {
+            throw new PathNotFoundException("XML export is not defined for properties: " + path);
+        }
+    }
+
+    @Override
+    public void exportSystemView(String absPath, ContentHandler contentHandler, boolean skipBinary, boolean noRecurse)
+            throws SAXException, RepositoryException {
+        export(absPath, new SystemViewExporter(this, contentHandler, !noRecurse, !skipBinary));
+    }
+
+    @Override
+    public void exportSystemView(String absPath, OutputStream out, boolean skipBinary, boolean noRecurse)
+            throws IOException, RepositoryException {
+        try {
+            ContentHandler handler = new ToXmlContentHandler(out);
+            exportSystemView(absPath, handler, skipBinary, noRecurse);
+        } catch (SAXException e) {
+            Exception exception = e.getException();
+            if (exception instanceof RepositoryException) {
+                throw (RepositoryException) exception;
+            } else if (exception instanceof IOException) {
+                throw (IOException) exception;
+            } else {
+                throw new RepositoryException("Error serializing system view XML", e);
+            }
+        }
+    }
+
+    @Override
+    public void exportDocumentView(String absPath, ContentHandler contentHandler, boolean skipBinary,
+            boolean noRecurse) throws SAXException, RepositoryException {
+        export(absPath, new DocumentViewExporter(this, contentHandler, !noRecurse, !skipBinary));
+    }
+
+    @Override
+    public void exportDocumentView(String absPath, OutputStream out, boolean skipBinary, boolean noRecurse)
+            throws IOException, RepositoryException {
+        try {
+            ContentHandler handler = new ToXmlContentHandler(out);
+            exportDocumentView(absPath, handler, skipBinary, noRecurse);
+        } catch (SAXException e) {
+            Exception exception = e.getException();
+            if (exception instanceof RepositoryException) {
+                throw (RepositoryException) exception;
+            } else if (exception instanceof IOException) {
+                throw (IOException) exception;
+            } else {
+                throw new RepositoryException("Error serializing document view XML", e);
+            }
+        }
+    }
+
     @Nonnull
     private LockManager getLockManager() {
         return sessionContext.getLockManager();