use of javax.jcr.lock.Lock in project jackrabbit by apache.
the class DefaultItemCollection method lock.
/**
* Creates a lock on this resource by locking the underlying
* {@link javax.jcr.Node node}. Except for the {@link org.apache.jackrabbit.webdav.lock.LockInfo#isDeep()} }
* all information included in the <code>LockInfo</code> object is ignored.
* Lock timeout is defined by JCR implementation.
*
* @param reqLockInfo
* @return lock object representing the lock created on this resource.
* @throws org.apache.jackrabbit.webdav.DavException
* @see org.apache.jackrabbit.webdav.DavResource#lock(org.apache.jackrabbit.webdav.lock.LockInfo)
* @see Node#lock(boolean, boolean)
*/
@Override
public ActiveLock lock(LockInfo reqLockInfo) throws DavException {
if (!isLockable(reqLockInfo.getType(), reqLockInfo.getScope())) {
throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED);
}
if (Type.WRITE.equals(reqLockInfo.getType())) {
if (!exists()) {
log.warn("Cannot create a write lock for non-existing JCR node (" + getResourcePath() + ")");
throw new DavException(DavServletResponse.SC_NOT_FOUND);
}
try {
boolean sessionScoped = EXCLUSIVE_SESSION.equals(reqLockInfo.getScope());
long timeout = reqLockInfo.getTimeout();
if (timeout == LockInfo.INFINITE_TIMEOUT) {
timeout = Long.MAX_VALUE;
} else {
timeout = timeout / 1000;
}
javax.jcr.lock.LockManager lockMgr = getRepositorySession().getWorkspace().getLockManager();
Lock jcrLock = lockMgr.lock((item).getPath(), reqLockInfo.isDeep(), sessionScoped, timeout, reqLockInfo.getOwner());
ActiveLock lock = new JcrActiveLock(jcrLock);
// add reference to DAVSession for this lock
getSession().addReference(lock.getToken());
return lock;
} catch (RepositoryException e) {
// UnsupportedRepositoryOperationException should not occur...
throw new JcrDavException(e);
}
} else {
return super.lock(reqLockInfo);
}
}
use of javax.jcr.lock.Lock in project jackrabbit by apache.
the class AbstractLockTest method testGetLockOnNewChild.
public void testGetLockOnNewChild() throws RepositoryException {
Node newChild = lockedNode.addNode(nodeName3, testNodeType);
if (isDeep()) {
// get lock must succeed even if child is not lockable.
Lock lock = newChild.getLock();
assertNotNull(lock);
assertTrue("Lock.getNode() must return the lock holding node", lockedNode.isSame(lock.getNode()));
Lock lock2 = lockMgr.getLock(newChild.getPath());
assertNotNull(lock2);
assertTrue("Lock.getNode() must return the lock holding node", lockedNode.isSame(lock2.getNode()));
} else {
try {
newChild.getLock();
fail("Node.getLock() must throw if node is not locked.");
} catch (LockException e) {
// success
}
try {
lockMgr.getLock(newChild.getPath());
fail("LockManager.getLock(String) must throw if node is not locked.");
} catch (LockException e) {
// success
}
}
}
use of javax.jcr.lock.Lock in project jackrabbit by apache.
the class LockManagerTest method testNullOwnerHint.
public void testNullOwnerHint() throws RepositoryException, NotExecutableException {
assertLockable(testNode);
Lock l = lockMgr.lock(testPath, true, true, Long.MAX_VALUE, null);
assertNotNull(l.getLockOwner());
}
use of javax.jcr.lock.Lock in project jackrabbit by apache.
the class LockManagerTest method testLockTransfer2.
public void testLockTransfer2() throws Exception {
assertLockable(testNode);
boolean sessionScoped = false;
Lock l = lockMgr.lock(testPath, true, sessionScoped, Long.MAX_VALUE, null);
String ltoken = l.getLockToken();
Session other = getHelper().getReadWriteSession();
LockManager otherLockMgr = getLockManager(other);
try {
lockMgr.removeLockToken(ltoken);
otherLockMgr.addLockToken(ltoken);
lockMgr.addLockToken(ltoken);
if (!openScopedLockMultiple) {
fail("Adding token to another session must fail (see config property " + RepositoryStub.PROP_OPEN_SCOPED_LOCK_MULTIPLE + ".");
}
} catch (LockException e) {
if (openScopedLockMultiple) {
fail("Adding token to another session must not fail (see config property " + RepositoryStub.PROP_OPEN_SCOPED_LOCK_MULTIPLE + ".");
}
} finally {
otherLockMgr.removeLockToken(ltoken);
lockMgr.addLockToken(ltoken);
other.logout();
}
}
use of javax.jcr.lock.Lock in project jackrabbit by apache.
the class AbstractLockTest method testIsLockOwningSession.
/**
* Test {@link javax.jcr.lock.Lock#isLockOwningSession()}
*
* @throws RepositoryException If an exception occurs.
*/
public void testIsLockOwningSession() throws RepositoryException {
assertTrue("Session must be lock owner", lock.isLockOwningSession());
assertTrue("Session must be lock owner", lockedNode.getLock().isLockOwningSession());
assertTrue("Session must be lock owner", lockMgr.getLock(lockedNode.getPath()).isLockOwningSession());
Session otherSession = getHelper().getReadOnlySession();
try {
Lock lck = otherSession.getNode(lockedNode.getPath()).getLock();
assertFalse("Session must not be lock owner", lck.isLockOwningSession());
Lock lck2 = getLockManager(otherSession).getLock(lockedNode.getPath());
assertFalse("Session must not be lock owner", lck2.isLockOwningSession());
} finally {
otherSession.logout();
}
Session otherAdmin = getHelper().getSuperuserSession();
try {
Lock lck = otherAdmin.getNode(lockedNode.getPath()).getLock();
assertFalse("Other Session for the same userID must not be lock owner", lck.isLockOwningSession());
Lock lck2 = getLockManager(otherAdmin).getLock(lockedNode.getPath());
assertFalse("Other Session for the same userID must not be lock owner", lck2.isLockOwningSession());
} finally {
otherAdmin.logout();
}
}
Aggregations