Search in sources :

Example 41 with LockException

use of javax.jcr.lock.LockException in project jackrabbit by apache.

the class LockTest method testAddRemoveLockToken.

/**
     * Test lock token functionality
     */
public void testAddRemoveLockToken() throws Exception {
    // create new node
    Node n = testRootNode.addNode(nodeName1, testNodeType);
    ensureMixinType(n, mixLockable);
    testRootNode.getSession().save();
    // lock node and get lock token
    Lock lock = n.lock(false, false);
    String lockToken = lock.getLockToken();
    try {
        // assert: session must get a non-null lock token
        assertNotNull("session must get a non-null lock token", lockToken);
        // assert: session must hold lock token
        assertTrue("session must hold lock token", containsLockToken(superuser, lockToken));
        // remove lock token
        superuser.removeLockToken(lockToken);
        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));
        // assert: session must still hold lock token
        assertFalse("session must not hold lock token", containsLockToken(superuser, lockToken));
        // assert: session unable to modify node
        try {
            n.addNode(nodeName2, testNodeType);
            fail("session unable to modify node");
        } catch (LockException e) {
        // expected
        }
        // add lock token
        superuser.addLockToken(lockToken);
        // assert: session must get a non-null lock token
        assertNotNull("session must get a non-null lock token", lock.getLockToken());
        // assert: session must hold lock token
        assertTrue("session must hold lock token", containsLockToken(superuser, lock.getLockToken()));
        // assert: session able to modify node
        n.addNode(nodeName2, testNodeType);
    } finally {
        // make sure lock token is added even if test fail
        superuser.addLockToken(lockToken);
    }
}
Also used : LockException(javax.jcr.lock.LockException) Node(javax.jcr.Node) Lock(javax.jcr.lock.Lock)

Example 42 with LockException

use of javax.jcr.lock.LockException in project jackrabbit by apache.

the class LockManagerImpl method externalUnlock.

/**
     * {@inheritDoc}
     */
public void externalUnlock(NodeId nodeId) throws RepositoryException {
    acquire();
    try {
        Path path = getPath(sysSession, nodeId);
        PathMap.Element<LockInfo> element = lockMap.map(path, true);
        if (element == null) {
            throw new LockException("Node not locked: " + path.toString());
        }
        LockInfo info = element.get();
        if (info == null) {
            throw new LockException("Node not locked: " + path.toString());
        }
        element.set(null);
        info.setLive(false);
        save();
    } finally {
        release();
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) LockException(javax.jcr.lock.LockException) PathMap(org.apache.jackrabbit.spi.commons.name.PathMap)

Example 43 with LockException

use of javax.jcr.lock.LockException in project jackrabbit by apache.

the class XAEnvironment method removeLockToken.

/**
     * Remove lock token from this environment.
     * @param session
     * @param lt lock token
     * @throws RepositoryException
     */
public void removeLockToken(SessionImpl session, String lt) throws RepositoryException {
    try {
        NodeId id = LockInfo.parseLockToken(lt);
        NodeImpl node = (NodeImpl) session.getItemManager().getItem(id);
        LockInfo info = getLockInfo(node);
        if (info != null) {
            if (info.isLockHolder(session)) {
                info.setLockHolder(null);
            } else if (info.getLockHolder() != null) {
                String msg = "Cannot remove lock token: lock held by other session.";
                log.warn(msg);
                throw new LockException(msg);
            }
        }
        // inform SessionLockManager
        getSessionLockManager(session).lockTokenRemoved(lt);
    } catch (IllegalArgumentException e) {
        String msg = "Bad lock token: " + e.getMessage();
        log.warn(msg);
        throw new LockException(msg);
    }
}
Also used : NodeImpl(org.apache.jackrabbit.core.NodeImpl) LockException(javax.jcr.lock.LockException) NodeId(org.apache.jackrabbit.core.id.NodeId)

Example 44 with LockException

use of javax.jcr.lock.LockException in project jackrabbit by apache.

the class XAEnvironment method addLockToken.

/**
     * Add lock token to this environment.
     * @param session
     * @param lt lock token
     * @throws RepositoryException
     */
public void addLockToken(SessionImpl session, String lt) throws RepositoryException {
    try {
        NodeId id = LockInfo.parseLockToken(lt);
        NodeImpl node = (NodeImpl) session.getItemManager().getItem(id);
        LockInfo info = getLockInfo(node);
        if (info != null && !info.isLockHolder(session)) {
            if (info.getLockHolder() == null) {
                info.setLockHolder(session);
            } else {
                String msg = "Cannot add lock token: lock already held by other session.";
                log.warn(msg);
                throw new LockException(msg);
            }
        }
        // inform SessionLockManager
        getSessionLockManager(session).lockTokenAdded(lt);
    } catch (IllegalArgumentException e) {
        String msg = "Bad lock token: " + e.getMessage();
        log.warn(msg);
        throw new LockException(msg);
    }
}
Also used : NodeImpl(org.apache.jackrabbit.core.NodeImpl) LockException(javax.jcr.lock.LockException) NodeId(org.apache.jackrabbit.core.id.NodeId)

Example 45 with LockException

use of javax.jcr.lock.LockException in project jackrabbit by apache.

the class XAEnvironment method lock.

/**
     * Lock some node.
     * @param node node to lock
     * @param isDeep <code>true</code> to deep lock this node;
     *               <code>false</code> otherwise
     * @param isSessionScoped <code>true</code> if lock should be session scoped;
     *                        <code>false</code> otherwise
     * @param timeoutHint
     * @param ownerInfo
     * @throws LockException if node is already locked
     * @throws RepositoryException if an error occurs
     */
public LockInfo lock(NodeImpl node, boolean isDeep, boolean isSessionScoped, long timeoutHint, String ownerInfo) throws LockException, RepositoryException {
    NodeId id = node.getNodeId();
    // check negative set first
    XALockInfo info = unlockedNodesMap.get(id);
    if (info != null) {
        // if settings are compatible, this is effectively a no-op
        if (info.isDeep() == isDeep && info.isSessionScoped() == isSessionScoped) {
            unlockedNodesMap.remove(id);
            operations.remove(info);
            return lockMgr.getLockInfo(id);
        }
    }
    // verify node is not already locked.
    if (isLocked(node)) {
        throw new LockException("Node locked.");
    }
    // create a new lock info for this node
    String lockOwner = (ownerInfo != null) ? ownerInfo : node.getSession().getUserID();
    info = new XALockInfo(node, isSessionScoped, isDeep, timeoutHint, lockOwner);
    SessionImpl session = (SessionImpl) node.getSession();
    info.setLockHolder(session);
    info.setLive(true);
    LockManagerImpl.getSessionLockManager(session).lockTokenAdded(info.getLockToken());
    lockedNodesMap.put(id, info);
    operations.add(info);
    return info;
}
Also used : LockException(javax.jcr.lock.LockException) NodeId(org.apache.jackrabbit.core.id.NodeId) SessionImpl(org.apache.jackrabbit.core.SessionImpl)

Aggregations

LockException (javax.jcr.lock.LockException)61 Node (javax.jcr.Node)33 Session (javax.jcr.Session)24 Lock (javax.jcr.lock.Lock)13 RepositoryException (javax.jcr.RepositoryException)11 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)11 NodeImpl (org.apache.jackrabbit.core.NodeImpl)7 LockManager (javax.jcr.lock.LockManager)6 NodeId (org.apache.jackrabbit.core.id.NodeId)6 ItemNotFoundException (javax.jcr.ItemNotFoundException)5 PathMap (org.apache.jackrabbit.spi.commons.name.PathMap)5 RetentionManager (javax.jcr.retention.RetentionManager)4 SessionImpl (org.apache.jackrabbit.core.SessionImpl)4 Value (javax.jcr.Value)3 Path (org.apache.jackrabbit.spi.Path)3 IOException (java.io.IOException)2 InvalidItemStateException (javax.jcr.InvalidItemStateException)2 PathNotFoundException (javax.jcr.PathNotFoundException)2 Property (javax.jcr.Property)2 UnsupportedRepositoryOperationException (javax.jcr.UnsupportedRepositoryOperationException)2