Author: reschke
Date: Wed Jan 4 14:43:52 2012
New Revision: 1227171
URL: http://svn.apache.org/viewvc?rev=1227171&view=rev
Log:
JCR-2859: Make open scoped locks recoverable
Done by exposing the lock token to admins as well.
Modified:
jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockImpl.java
jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/XATest.java
jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/security/authorization/AbstractLockManagementTest.java
jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/lock/OpenScopedLockTest.java
Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockImpl.java?rev=1227171&r1=1227170&r2=1227171&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockImpl.java
Wed Jan 4 14:43:52 2012
@@ -22,6 +22,7 @@ import org.apache.jackrabbit.core.securi
import javax.jcr.Node;
import javax.jcr.RepositoryException;
+import javax.jcr.Session;
import javax.jcr.lock.LockException;
/**
@@ -78,7 +79,7 @@ class LockImpl implements javax.jcr.lock
* {@inheritDoc}
*/
public String getLockToken() {
- if (!info.isSessionScoped() && info.isLockHolder(node.getSession())) {
+ if (!info.isSessionScoped() && (info.isLockHolder(node.getSession()) || isAdminUser(node.getSession())))
{
return info.getLockToken();
} else {
return null;
@@ -151,4 +152,15 @@ class LockImpl implements javax.jcr.lock
return info.isLockHolder(node.getSession());
}
+ /**
+ * Check whether a session belongs to an administrative user.
+ */
+ private boolean isAdminUser(Session session) {
+ if (session instanceof SessionImpl) {
+ return ((SessionImpl) session).isAdmin();
+ } else {
+ // fallback. use hardcoded default admin ID
+ return "admin".equals(session.getUserID());
+ }
+ }
}
Modified: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/XATest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/XATest.java?rev=1227171&r1=1227170&r2=1227171&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/XATest.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/XATest.java
Wed Jan 4 14:43:52 2012
@@ -941,14 +941,21 @@ public class XATest extends AbstractJCRT
n.save();
superuser.removeLockToken(lockToken);
- assertNull("session must get a null lock token", lock.getLockToken());
+
+ String nlt = lock.getLockToken();
+ assertTrue("freshly obtained lock token must either be null or the same as the one
returned earlier",
+ nlt == null || nlt.equals(lockToken));
+
assertFalse("session must not hold lock token", containsLockToken(superuser, lockToken));
// commit
utx.commit();
+ nlt = lock.getLockToken();
+ assertTrue("freshly obtained lock token must either be null or the same as the one
returned earlier",
+ nlt == null || nlt.equals(lockToken));
+
assertFalse("session must not hold lock token", containsLockToken(superuser, lockToken));
- assertNull("session must get a null lock token", lock.getLockToken());
// start new Transaction and try to unlock
utx = new UserTransactionImpl(superuser);
@@ -1141,7 +1148,10 @@ public class XATest extends AbstractJCRT
assertTrue("session must hold lock token", containsLockToken(superuser, lockToken));
superuser.removeLockToken(lockToken);
- assertNull("session must get a null lock token", lock.getLockToken());
+
+ String nlt = lock.getLockToken();
+ assertTrue("freshly obtained lock token must either be null or the same as the one
returned earlier",
+ nlt == null || nlt.equals(lockToken));
// commit
utx.commit();
@@ -1149,7 +1159,9 @@ public class XATest extends AbstractJCRT
// refresh Lock Info
lock = n.getLock();
- assertNull("session must get a null lock token", lock.getLockToken());
+ nlt = lock.getLockToken();
+ assertTrue("freshly obtained lock token must either be null or the same as the one
returned earlier",
+ nlt == null || nlt.equals(lockToken));
Session other = getHelper().getSuperuserSession();
try {
@@ -1910,15 +1922,20 @@ public class XATest extends AbstractJCRT
assertTrue("session must hold lock token", containsLockToken(superuser, lockToken));
superuser.removeLockToken(lockToken);
- assertNull("session must get a null lock token", lock.getLockToken());
-
+
+ String nlt = lock.getLockToken();
+ assertTrue("freshly obtained lock token must either be null or the same as the one
returned earlier",
+ nlt == null || nlt.equals(lockToken));
+
// commit
utx.commit();
// refresh Lock Info
lock = n.getLock();
- assertNull("session must get a null lock token", lock.getLockToken());
+ nlt = lock.getLockToken();
+ assertTrue("freshly obtained lock token must either be null or the same as the one
returned earlier",
+ nlt == null || nlt.equals(lockToken));
Session other = getHelper().getSuperuserSession();
// start new Transaction and try to add lock token unlock the node and then remove
it
Modified: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/security/authorization/AbstractLockManagementTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/security/authorization/AbstractLockManagementTest.java?rev=1227171&r1=1227170&r2=1227171&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/security/authorization/AbstractLockManagementTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/security/authorization/AbstractLockManagementTest.java
Wed Jan 4 14:43:52 2012
@@ -22,7 +22,9 @@ import org.apache.jackrabbit.test.NotExe
import javax.jcr.AccessDeniedException;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
+import javax.jcr.Session;
import javax.jcr.lock.Lock;
+import javax.jcr.lock.LockManager;
/** <code>AbstractVersionAccessTest</code>... */
public abstract class AbstractLockManagementTest extends AbstractEvaluationTest {
@@ -129,4 +131,49 @@ public abstract class AbstractLockManage
boolean isLocked = n.isLocked();
assertFalse(isLocked);
}
+
+ public void testLockBreaking() throws RepositoryException, NotExecutableException {
+ String locktoken = null;
+ LockManager sulm = superuser.getWorkspace().getLockManager();
+ String lockedpath = null;
+
+ try {
+ Node trn = getTestNode();
+ modifyPrivileges(trn.getPath(), Privilege.JCR_READ, true);
+ modifyPrivileges(trn.getPath(), PrivilegeRegistry.REP_WRITE, true);
+ modifyPrivileges(trn.getPath(), Privilege.JCR_LOCK_MANAGEMENT, true);
+
+ Session lockingSession = trn.getSession();
+
+ assertFalse("super user and test user should have different user ids: " + lockingSession.getUserID()
+ " vs " + superuser.getUserID(),
+ lockingSession.getUserID().equals(superuser.getUserID()));
+
+ trn.addNode("locktest", "nt:unstructured");
+ trn.addMixin("mix:lockable");
+ lockingSession.save();
+
+ // let the "other" user lock the node
+ LockManager oulm = lockingSession.getWorkspace().getLockManager();
+ Lock l = oulm.lock(trn.getPath(), true, false, Long.MAX_VALUE, null);
+ lockedpath = trn.getPath();
+ locktoken = l.getLockToken();
+ lockingSession.logout();
+
+ // transfer the lock token to the super user and try the unlock
+
+ Node lockednode = superuser.getNode(lockedpath);
+ assertTrue(lockednode.isLocked());
+ Lock sl = sulm.getLock(lockedpath);
+ assertNotNull(sl.getLockToken());
+ sulm.addLockToken(sl.getLockToken());
+ sulm.unlock(lockedpath);
+ locktoken = null;
+ }
+ finally {
+ if (locktoken != null && lockedpath != null) {
+ sulm.addLockToken(locktoken);
+ sulm.unlock(lockedpath);
+ }
+ }
+ }
}
Modified: jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/lock/OpenScopedLockTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/lock/OpenScopedLockTest.java?rev=1227171&r1=1227170&r2=1227171&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/lock/OpenScopedLockTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/lock/OpenScopedLockTest.java
Wed Jan 4 14:43:52 2012
@@ -89,7 +89,10 @@ public class OpenScopedLockTest extends
String lockToken = lock.getLockToken();
try {
superuser.removeLockToken(lockToken);
- assertNull("After token transfer lock-token must not be visible", lock.getLockToken());
+
+ String nlt = lock.getLockToken();
+ assertTrue("freshly obtained lock token must either be null or the same as the
one returned earlier",
+ nlt == null || nlt.equals(lockToken));
} finally {
// move lock token back in order to have lock removed properly
superuser.addLockToken(lockToken);
|