Author: jukka
Date: Wed Aug 28 01:32:41 2013
New Revision: 1518047
URL: http://svn.apache.org/r1518047
Log:
OAK-150: Basic JCR LockManager support
Use the new lock operations also in NodeImpl
Added:
jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/lock/LockOperation.java
(with props)
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/delegate/NodeDelegate.java
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
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java?rev=1518047&r1=1518046&r2=1518047&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 Aug 28 01:32:41 2013
@@ -61,11 +61,7 @@ import org.apache.jackrabbit.api.Jackrab
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.PropertyState;
-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.api.Type;
import org.apache.jackrabbit.oak.commons.PathUtils;
@@ -74,8 +70,8 @@ import org.apache.jackrabbit.oak.jcr.del
import org.apache.jackrabbit.oak.jcr.delegate.PropertyDelegate;
import org.apache.jackrabbit.oak.jcr.delegate.VersionManagerDelegate;
import org.apache.jackrabbit.oak.jcr.lock.LockImpl;
+import org.apache.jackrabbit.oak.jcr.lock.LockOperation;
import org.apache.jackrabbit.oak.jcr.operation.NodeOperation;
-import org.apache.jackrabbit.oak.jcr.operation.SessionOperation;
import org.apache.jackrabbit.oak.jcr.version.VersionHistoryImpl;
import org.apache.jackrabbit.oak.jcr.version.VersionImpl;
import org.apache.jackrabbit.oak.plugins.memory.PropertyStates;
@@ -88,13 +84,9 @@ import org.slf4j.LoggerFactory;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Arrays.asList;
import static java.util.Collections.singleton;
-import static javax.jcr.Property.JCR_LOCK_IS_DEEP;
-import static javax.jcr.Property.JCR_LOCK_OWNER;
import static org.apache.jackrabbit.JcrConstants.JCR_LOCKISDEEP;
-import static org.apache.jackrabbit.JcrConstants.JCR_LOCKOWNER;
import static org.apache.jackrabbit.JcrConstants.JCR_MIXINTYPES;
import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
-import static org.apache.jackrabbit.JcrConstants.MIX_LOCKABLE;
import static org.apache.jackrabbit.oak.api.Type.NAME;
import static org.apache.jackrabbit.oak.api.Type.NAMES;
@@ -1169,30 +1161,14 @@ public class NodeImpl<T extends NodeDele
@Override @Nonnull
public Lock lock(final boolean isDeep, boolean isSessionScoped)
throws RepositoryException {
- checkLockable(); // TODO: use perform()
- perform(new SessionOperation<Void>(true) {
+ perform(new LockOperation<Void>(sessionDelegate, dlg) {
@Override
- public Void perform() throws RepositoryException {
- ContentSession session = sessionDelegate.getContentSession();
- String path = dlg.getPath();
- String userID = session.getAuthInfo().getUserID();
-
- try {
- Root root = session.getLatestRoot();
- Tree tree = root.getTree(path);
- if (!tree.exists()) {
- throw new ItemNotFoundException();
- }
- tree.setProperty(JCR_LOCKOWNER, userID);
- tree.setProperty(JCR_LOCKISDEEP, isDeep);
- root.commit(); // TODO: fail instead?
- } catch (CommitFailedException e) {
- throw new RepositoryException("Unable to lock " + path, e);
- }
+ public Void perform(NodeDelegate node) throws RepositoryException {
+ node.lock(isDeep);
return null;
}
});
- getSession().refresh(true);
+ getSession().refresh(true); // TODO: better refresh
return new LockImpl(sessionContext, dlg);
}
@@ -1201,24 +1177,14 @@ public class NodeImpl<T extends NodeDele
*/
@Override
public void unlock() throws RepositoryException {
- checkLockable();
- // TODO: use perform
- String lockOwner = getOakPathOrThrow(JCR_LOCK_OWNER);
- String lockIsDeep = getOakPathOrThrow(JCR_LOCK_IS_DEEP);
- try {
- Root root = sessionDelegate.getContentSession().getLatestRoot();
- Tree tree = root.getTree(dlg.getPath());
- if (!tree.exists()) {
- throw new ItemNotFoundException();
- }
- tree.removeProperty(lockOwner);
- tree.removeProperty(lockIsDeep);
- root.commit();
- } catch (CommitFailedException e) {
- throw new RepositoryException("Unable to unlock " + this, e);
- }
-
- getSession().refresh(true);
+ perform(new LockOperation<Void>(sessionDelegate, dlg) {
+ @Override
+ public Void perform(NodeDelegate node) throws RepositoryException {
+ node.unlock();
+ return null;
+ }
+ });
+ getSession().refresh(true); // TODO: better refresh
}
@Override @Nonnull
@@ -1266,24 +1232,6 @@ public class NodeImpl<T extends NodeDele
//------------------------------------------------------------< internal
>---
- /**
- * Checks if this node is lockable; otherwise throws a LockException.
- * @throws LockException if the node is not lockable.
- */
- private void checkLockable() throws RepositoryException {
- perform(new NodeOperation<Void>(dlg) {
- @Override
- public Void perform() throws RepositoryException {
- if (!getNodeTypeManager().isNodeType(node.getTree(),
MIX_LOCKABLE)) {
- String msg = "Unable to perform a locking operation on a
non-lockable node: " + getPath();
- log.debug(msg);
- throw new LockException(msg);
- }
- return null;
- }
- });
- }
-
private EffectiveNodeType getEffectiveNodeType() throws
RepositoryException {
return getNodeTypeManager().getEffectiveNodeType(dlg.getTree());
}
Modified:
jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/delegate/NodeDelegate.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/delegate/NodeDelegate.java?rev=1518047&r1=1518046&r2=1518047&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/delegate/NodeDelegate.java
(original)
+++
jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/delegate/NodeDelegate.java
Wed Aug 28 01:32:41 2013
@@ -525,12 +525,12 @@ public class NodeDelegate extends ItemDe
}
private boolean isNodeType(String typeName) {
- return isNodeType(
- tree, typeName,
- sessionDelegate.getRoot().getTree(NODE_TYPES_PATH));
+ return isNodeType(tree, typeName, sessionDelegate.getRoot());
}
- private boolean isNodeType(Tree tree, String typeName, Tree typeRoot) {
+ private boolean isNodeType(Tree tree, String typeName, Root root) {
+ Tree typeRoot = root.getTree(NODE_TYPES_PATH);
+
String primaryName = TreeUtil.getName(tree, JCR_PRIMARYTYPE);
if (typeName.equals(primaryName)) {
return true;
@@ -754,9 +754,10 @@ public class NodeDelegate extends ItemDe
String path = getPath();
Root root = sessionDelegate.getContentSession().getLatestRoot();
- Tree typeRoot = root.getTree(NODE_TYPES_PATH);
Tree tree = root.getTree(path);
- if (!isNodeType(tree, MIX_LOCKABLE, typeRoot)) {
+ if (!tree.exists()) {
+ throw new ItemNotFoundException("Node " + path + " does not
exist");
+ } else if (!isNodeType(tree, MIX_LOCKABLE, root)) {
throw new LockException("Node " + path + " is not lockable");
} else if (tree.hasProperty(JCR_LOCKISDEEP)) {
throw new LockException("Node " + path + " is already locked");
@@ -785,9 +786,10 @@ public class NodeDelegate extends ItemDe
String path = getPath();
Root root = sessionDelegate.getContentSession().getLatestRoot();
- Tree typeRoot = root.getTree(NODE_TYPES_PATH);
Tree tree = root.getTree(path);
- if (!isNodeType(tree, MIX_LOCKABLE, typeRoot)) {
+ if (!tree.exists()) {
+ throw new ItemNotFoundException("Node " + path + " does not
exist");
+ } else if (!isNodeType(tree, MIX_LOCKABLE, root)) {
throw new LockException("Node " + path + " is not lockable");
} else if (!tree.hasProperty(JCR_LOCKISDEEP)) {
throw new LockException("Node " + path + " is not locked");
Modified:
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=1518047&r1=1518046&r2=1518047&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/lock/LockManagerImpl.java
(original)
+++
jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/lock/LockManagerImpl.java
Wed Aug 28 01:32:41 2013
@@ -22,7 +22,6 @@ import java.util.Set;
import javax.annotation.Nonnull;
import javax.jcr.Node;
-import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.lock.Lock;
@@ -73,7 +72,7 @@ public class LockManagerImpl implements
@Override
public boolean isLocked(String absPath) throws RepositoryException {
- return perform(new LockOperation<Boolean>(absPath) {
+ return perform(new LockOperation<Boolean>(sessionContext, absPath) {
@Override
protected Boolean perform(NodeDelegate node) {
return node.isLocked();
@@ -83,7 +82,7 @@ public class LockManagerImpl implements
@Override
public boolean holdsLock(String absPath) throws RepositoryException {
- return perform(new LockOperation<Boolean>(absPath) {
+ return perform(new LockOperation<Boolean>(sessionContext, absPath) {
@Override
protected Boolean perform(NodeDelegate node) {
return node.holdsLock(false);
@@ -93,12 +92,13 @@ public class LockManagerImpl implements
@Override @Nonnull
public Lock getLock(String absPath) throws RepositoryException {
- NodeDelegate lock = perform(new LockOperation<NodeDelegate>(absPath) {
- @Override
- protected NodeDelegate perform(NodeDelegate node) {
- return node.getLock();
- }
- });
+ NodeDelegate lock = perform(
+ new LockOperation<NodeDelegate>(sessionContext, absPath) {
+ @Override
+ protected NodeDelegate perform(NodeDelegate node) {
+ return node.getLock();
+ }
+ });
if (lock != null) {
return new LockImpl(sessionContext, lock);
} else {
@@ -110,20 +110,22 @@ public class LockManagerImpl implements
public Lock lock(
String absPath, final boolean isDeep, boolean isSessionScoped,
long timeoutHint, String ownerInfo) throws RepositoryException {
- NodeDelegate lock = perform(new LockOperation<NodeDelegate>(absPath) {
- @Override
- protected NodeDelegate perform(NodeDelegate node)
- throws RepositoryException {
- node.lock(isDeep);
- return node;
- }
- });
+ NodeDelegate lock = perform(
+ new LockOperation<NodeDelegate>(sessionContext, absPath) {
+ @Override
+ protected NodeDelegate perform(NodeDelegate node)
+ throws RepositoryException {
+ node.lock(isDeep);
+ return node;
+ }
+ });
+ sessionContext.getSession().refresh(true); // TODO: better refresh
return new LockImpl(sessionContext, lock);
}
@Override
public void unlock(String absPath) throws RepositoryException {
- perform(new LockOperation<Void>(absPath) {
+ perform(new LockOperation<Void>(sessionContext, absPath) {
@Override
protected Void perform(NodeDelegate node)
throws RepositoryException {
@@ -131,6 +133,7 @@ public class LockManagerImpl implements
return null;
}
});
+ sessionContext.getSession().refresh(true); // TODO: better refresh
}
private <T> T perform(SessionOperation<T> operation)
@@ -138,28 +141,4 @@ public class LockManagerImpl implements
return delegate.perform(operation);
}
- private abstract class LockOperation<T> extends SessionOperation<T> {
-
- private final String path;
-
- public LockOperation(String absPath) throws PathNotFoundException {
- this.path = sessionContext.getOakPathOrThrowNotFound(absPath);
- }
-
- @Override
- public T perform() throws RepositoryException {
- NodeDelegate node = delegate.getNode(path);
- if (node != null) {
- return perform(node);
- } else {
- throw new PathNotFoundException(
- "Node " + path + " not found");
- }
- }
-
- protected abstract T perform(NodeDelegate node)
- throws RepositoryException;
-
- }
-
}
\ No newline at end of file
Added:
jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/lock/LockOperation.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/lock/LockOperation.java?rev=1518047&view=auto
==============================================================================
---
jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/lock/LockOperation.java
(added)
+++
jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/lock/LockOperation.java
Wed Aug 28 01:32:41 2013
@@ -0,0 +1,72 @@
+/*
+ * 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 javax.jcr.PathNotFoundException;
+import javax.jcr.RepositoryException;
+
+import org.apache.jackrabbit.oak.jcr.SessionContext;
+import org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate;
+import org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate;
+import org.apache.jackrabbit.oak.jcr.operation.SessionOperation;
+
+/**
+ * Abstract base class for locking operations.
+ *
+ * @param <T> return type of the {@link #perform()} method
+ */
+public abstract class LockOperation<T> extends SessionOperation<T> {
+
+ private final SessionDelegate session;
+
+ private final NodeDelegate node;
+
+ private final String path;
+
+ public LockOperation(SessionDelegate session, NodeDelegate node)
+ throws PathNotFoundException {
+ this.session = session;
+ this.path = null;
+ this.node = node;
+ }
+
+ public LockOperation(SessionContext context, String absPath)
+ throws PathNotFoundException {
+ this.session = context.getSessionDelegate();
+ this.path = context.getOakPathOrThrowNotFound(absPath);
+ this.node = null;
+ }
+
+ @Override
+ public T perform() throws RepositoryException {
+ if (node != null) {
+ return perform(node);
+ } else {
+ NodeDelegate node = session.getNode(path);
+ if (node != null) {
+ return perform(node);
+ } else {
+ throw new PathNotFoundException(
+ "Node " + path + " not found");
+ }
+ }
+ }
+
+ protected abstract T perform(NodeDelegate node)
+ throws RepositoryException;
+
+}
\ No newline at end of file
Propchange:
jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/lock/LockOperation.java
------------------------------------------------------------------------------
svn:eol-style = native