jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1353536 - in /jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr: NodeImpl.java WorkspaceImpl.java lock/ lock/LockManagerImpl.java
Date Mon, 25 Jun 2012 13:43:58 GMT
Author: jukka
Date: Mon Jun 25 13:43:57 2012
New Revision: 1353536

URL: http://svn.apache.org/viewvc?rev=1353536&view=rev
Log:
OAK-150: Basic JCR LockManager support

Add basic LockManagerImpl and dummy locking support

Added:
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/lock/
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/lock/LockManagerImpl.java
Modified:
    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/WorkspaceImpl.java

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=1353536&r1=1353535&r2=1353536&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
Mon Jun 25 13:43:57 2012
@@ -56,9 +56,15 @@ import org.apache.jackrabbit.JcrConstant
 import org.apache.jackrabbit.commons.ItemNameMatcher;
 import org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter;
 import org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter;
+import org.apache.jackrabbit.oak.api.CommitFailedException;
+import org.apache.jackrabbit.oak.api.ContentSession;
 import org.apache.jackrabbit.oak.api.CoreValue;
+import org.apache.jackrabbit.oak.api.CoreValueFactory;
+import org.apache.jackrabbit.oak.api.Root;
+import org.apache.jackrabbit.oak.api.Tree;
 import org.apache.jackrabbit.oak.api.Tree.Status;
 import org.apache.jackrabbit.oak.commons.PathUtils;
+import org.apache.jackrabbit.oak.core.DefaultConflictHandler;
 import org.apache.jackrabbit.oak.jcr.value.ValueConverter;
 import org.apache.jackrabbit.oak.namepath.NameMapper;
 import org.apache.jackrabbit.oak.util.Function1;
@@ -68,6 +74,8 @@ import org.apache.jackrabbit.value.Value
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static javax.jcr.Property.JCR_LOCK_IS_DEEP;
+import static javax.jcr.Property.JCR_LOCK_OWNER;
 import static org.apache.jackrabbit.oak.util.Iterators.filter;
 
 /**
@@ -1021,51 +1029,99 @@ public class NodeImpl extends ItemImpl i
     }
 
     /**
-     * @see javax.jcr.Node#lock(boolean, boolean)
+     * Checks whether this node is locked by looking for the
+     * <code>jcr:lockOwner</code> property either on this node or
+     * on any ancestor that also has the <code>jcr:lockIsDeep</code>
+     * property set to <code>true</code>.
      */
     @Override
-    @Nonnull
-    public Lock lock(boolean isDeep, boolean isSessionScoped) throws RepositoryException
{
-        return sessionDelegate.getLockManager().lock(getPath(), isDeep, isSessionScoped,
Long.MAX_VALUE, null);
+    public boolean isLocked() throws RepositoryException {
+        String lockOwner = sessionDelegate.getOakPathOrThrow(JCR_LOCK_OWNER);
+        String lockIsDeep = sessionDelegate.getOakPathOrThrow(JCR_LOCK_IS_DEEP);
+
+        if (dlg.getProperty(lockOwner) != null) {
+            return true;
+        }
+
+        NodeDelegate parent = dlg.getParent();
+        while (parent != null) {
+            if (parent.getProperty(lockOwner) != null) {
+                PropertyDelegate isDeep = parent.getProperty(lockIsDeep);
+                if (isDeep != null && !isDeep.isMultivalue()
+                        && isDeep.getValue().getBoolean()) {
+                    return true;
+                }
+            }
+            parent = dlg.getParent();
+        }
+
+        return false;
     }
 
     /**
-     * @see javax.jcr.Node#getLock()
+     * Checks whether this node holds a lock by looking for the
+     * <code>jcr:lockOwner</code> property.
      */
     @Override
-    @Nonnull
-    public Lock getLock() throws RepositoryException {
-        return sessionDelegate.getLockManager().getLock(getPath());
+    public boolean holdsLock() throws RepositoryException {
+        String lockOwner = sessionDelegate.getOakPathOrThrow(JCR_LOCK_OWNER);
+        return dlg.getProperty(lockOwner) != null;
     }
 
     /**
-     * @see javax.jcr.Node#unlock()
+     * @see javax.jcr.Node#getLock()
      */
     @Override
-    public void unlock() throws RepositoryException {
-        sessionDelegate.getLockManager().unlock(getPath());
+    @Nonnull
+    public Lock getLock() throws RepositoryException {
+        throw new UnsupportedRepositoryOperationException();
     }
 
     /**
-     * @see javax.jcr.Node#holdsLock()
+     * @see javax.jcr.Node#lock(boolean, boolean)
      */
     @Override
-    public boolean holdsLock() throws RepositoryException {
-        return sessionDelegate.getLockManager().holdsLock(getPath());
+    @Nonnull
+    public Lock lock(boolean isDeep, boolean isSessionScoped)
+            throws RepositoryException {
+        String userID = getSession().getUserID();
+
+        String lockOwner = sessionDelegate.getOakPathOrThrow(JCR_LOCK_OWNER);
+        String lockIsDeep = sessionDelegate.getOakPathOrThrow(JCR_LOCK_IS_DEEP);
+        try {
+            ContentSession session = sessionDelegate.getContentSession();
+            CoreValueFactory factory = session.getCoreValueFactory();
+            Root root = session.getCurrentRoot();
+            Tree tree = root.getTree(dlg.getPath());
+            tree.setProperty(lockOwner, factory.createValue(userID));
+            tree.setProperty(lockIsDeep, factory.createValue(isDeep));
+            root.commit(DefaultConflictHandler.OURS); // TODO: fail instead?
+        } catch (CommitFailedException e) {
+            throw new RepositoryException("Unable to lock " + this, e);
+        }
+
+        getSession().refresh(true);
+        return getLock();
     }
 
     /**
-     * @see javax.jcr.Node#isLocked() ()
+     * @see javax.jcr.Node#unlock()
      */
     @Override
-    public boolean isLocked() throws RepositoryException {
+    public void unlock() throws RepositoryException {
+        String lockOwner = sessionDelegate.getOakPathOrThrow(JCR_LOCK_OWNER);
+        String lockIsDeep = sessionDelegate.getOakPathOrThrow(JCR_LOCK_IS_DEEP);
         try {
-            return sessionDelegate.getLockManager().isLocked(getPath());
-        } catch (UnsupportedRepositoryOperationException ex) {
-            // when locking is not supported all nodes are considered not to be
-            // locked
-            return false;
+            Root root = sessionDelegate.getContentSession().getCurrentRoot();
+            Tree tree = root.getTree(dlg.getPath());
+            tree.removeProperty(lockOwner);
+            tree.removeProperty(lockIsDeep);
+            root.commit(DefaultConflictHandler.OURS);
+        } catch (CommitFailedException e) {
+            throw new RepositoryException("Unable to unlock " + this, e);
         }
+
+        getSession().refresh(true);
     }
 
     @Override

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/WorkspaceImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/WorkspaceImpl.java?rev=1353536&r1=1353535&r2=1353536&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/WorkspaceImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/WorkspaceImpl.java
Mon Jun 25 13:43:57 2012
@@ -33,6 +33,7 @@ import javax.jcr.version.VersionManager;
 import org.apache.jackrabbit.api.JackrabbitWorkspace;
 import org.apache.jackrabbit.api.security.authorization.PrivilegeManager;
 import org.apache.jackrabbit.oak.commons.PathUtils;
+import org.apache.jackrabbit.oak.jcr.lock.LockManagerImpl;
 import org.apache.jackrabbit.oak.jcr.nodetype.NodeTypeManagerImpl;
 import org.apache.jackrabbit.oak.jcr.query.QueryManagerImpl;
 import org.apache.jackrabbit.oak.jcr.security.privilege.PrivilegeManagerImpl;
@@ -56,12 +57,14 @@ public class WorkspaceImpl implements Ja
     private final NodeTypeManager nodeTypeManager;
     private final QueryManagerImpl queryManager;
 
+    private final LockManager lockManager;
+
     public WorkspaceImpl(SessionDelegate sessionDelegate)
             throws RepositoryException {
-
         this.sessionDelegate = sessionDelegate;
         this.nodeTypeManager = new NodeTypeManagerImpl(sessionDelegate.getValueFactory(),
sessionDelegate.getNamePathMapper());
         this.queryManager = new QueryManagerImpl(sessionDelegate);
+        this.lockManager = new LockManagerImpl(sessionDelegate.getSession());
     }
 
     //----------------------------------------------------------< Workspace >---
@@ -134,10 +137,8 @@ public class WorkspaceImpl implements Ja
     }
 
     @Override
-    public LockManager getLockManager() throws RepositoryException {
-        ensureIsAlive();
-
-        throw new UnsupportedRepositoryOperationException("TODO: Workspace.getLockManager");
+    public LockManager getLockManager() {
+        return lockManager;
     }
 
     @Override

Added: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/lock/LockManagerImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/lock/LockManagerImpl.java?rev=1353536&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/lock/LockManagerImpl.java
(added)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/lock/LockManagerImpl.java
Mon Jun 25 13:43:57 2012
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.jcr.lock;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.lock.Lock;
+import javax.jcr.lock.LockException;
+import javax.jcr.lock.LockManager;
+
+/**
+ * Simple lock manager implementation that just keeps track of a set of lock
+ * tokens and delegates all locking operations back to the {@link Session}
+ * and {@link Node} implementations.
+ */
+public class LockManagerImpl implements LockManager {
+
+    private final Session session;
+
+    private final Set<String> tokens = new HashSet<String>();
+
+    public LockManagerImpl(Session session) {
+        this.session = session;
+    }
+
+    @Override
+    public String[] getLockTokens() throws RepositoryException {
+        String[] array = tokens.toArray(new String[tokens.size()]);
+        Arrays.sort(array);
+        return array;
+    }
+
+    @Override
+    public void addLockToken(String lockToken) throws RepositoryException {
+        tokens.add(lockToken);
+    }
+
+    @Override
+    public void removeLockToken(String lockToken) throws RepositoryException {
+        if (!tokens.remove(lockToken)) {
+            throw new LockException(
+                    "Lock token " + lockToken + " is not held by this session");
+        }
+    }
+
+    @Override
+    public boolean isLocked(String absPath) throws RepositoryException {
+        return session.getNode(absPath).isLocked();
+    }
+
+    @Override
+    @SuppressWarnings("deprecation")
+    public boolean holdsLock(String absPath) throws RepositoryException {
+        return session.getNode(absPath).holdsLock();
+    }
+
+    @Override
+    @SuppressWarnings("deprecation")
+    public Lock getLock(String absPath) throws RepositoryException {
+        return session.getNode(absPath).getLock();
+    }
+
+    @Override
+    @SuppressWarnings("deprecation")
+    public Lock lock(
+            String absPath, boolean isDeep, boolean isSessionScoped,
+            long timeoutHint, String ownerInfo) throws RepositoryException {
+        return session.getNode(absPath).lock(isDeep, isSessionScoped);
+    }
+
+    @Override
+    @SuppressWarnings("deprecation")
+    public void unlock(String absPath) throws RepositoryException {
+        session.getNode(absPath).unlock();
+    }
+
+}
\ No newline at end of file



Mime
View raw message