Search in sources :

Example 21 with Lock

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);
    }
}
Also used : ActiveLock(org.apache.jackrabbit.webdav.lock.ActiveLock) JcrActiveLock(org.apache.jackrabbit.webdav.jcr.lock.JcrActiveLock) DavException(org.apache.jackrabbit.webdav.DavException) JcrActiveLock(org.apache.jackrabbit.webdav.jcr.lock.JcrActiveLock) RepositoryException(javax.jcr.RepositoryException) ActiveLock(org.apache.jackrabbit.webdav.lock.ActiveLock) Lock(javax.jcr.lock.Lock) JcrActiveLock(org.apache.jackrabbit.webdav.jcr.lock.JcrActiveLock)

Example 22 with Lock

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
        }
    }
}
Also used : LockException(javax.jcr.lock.LockException) Node(javax.jcr.Node) Lock(javax.jcr.lock.Lock)

Example 23 with Lock

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());
}
Also used : Lock(javax.jcr.lock.Lock)

Example 24 with Lock

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();
    }
}
Also used : LockManager(javax.jcr.lock.LockManager) LockException(javax.jcr.lock.LockException) Lock(javax.jcr.lock.Lock) Session(javax.jcr.Session)

Example 25 with Lock

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();
    }
}
Also used : Session(javax.jcr.Session) Lock(javax.jcr.lock.Lock)

Aggregations

Lock (javax.jcr.lock.Lock)67 Node (javax.jcr.Node)41 Session (javax.jcr.Session)20 LockException (javax.jcr.lock.LockException)13 UserTransaction (javax.transaction.UserTransaction)12 RepositoryException (javax.jcr.RepositoryException)8 LockManager (javax.jcr.lock.LockManager)7 JcrActiveLock (org.apache.jackrabbit.webdav.jcr.lock.JcrActiveLock)5 ActiveLock (org.apache.jackrabbit.webdav.lock.ActiveLock)4 UserTransactionImpl (org.apache.jackrabbit.core.UserTransactionImpl)3 DavException (org.apache.jackrabbit.webdav.DavException)3 EventIterator (javax.jcr.observation.EventIterator)2 EventListener (javax.jcr.observation.EventListener)2 ObservationManager (javax.jcr.observation.ObservationManager)2 StaleItemStateException (org.apache.jackrabbit.core.state.StaleItemStateException)2 LockInfo (org.apache.jackrabbit.spi.LockInfo)2 SupportedLock (org.apache.jackrabbit.webdav.lock.SupportedLock)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 URI (java.net.URI)1