Search in sources :

Example 6 with LockException

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

the class SessionScopedLockTest method testImplicitUnlock2.

/**
     * Test locks are released when session logs out
     */
public void testImplicitUnlock2() throws RepositoryException, NotExecutableException {
    Session other = getHelper().getReadWriteSession();
    try {
        Node testNode = (Node) other.getItem(testRootNode.getPath());
        Node lockedNode = testNode.addNode(nodeName1, testNodeType);
        other.save();
        assertLockable(lockedNode);
        LockManager lMgr = getLockManager(other);
        Lock lock = lMgr.lock(lockedNode.getPath(), isDeep(), isSessionScoped(), getTimeoutHint(), getLockOwner());
        // access the locked noded added by another session
        testRootNode.refresh(false);
        Node n = (Node) superuser.getItem(lockedNode.getPath());
        // remove lock implicit by logout lock-holding session
        other.logout();
        // check if superuser session is properly informed about the unlock
        assertFalse(n.isLocked());
        assertFalse(n.holdsLock());
        try {
            n.getLock();
            fail("Upon logout of the session a session-scoped lock must be gone.");
        } catch (LockException e) {
        // ok
        }
    } finally {
        if (other.isLive()) {
            other.logout();
        }
    }
}
Also used : LockManager(javax.jcr.lock.LockManager) LockException(javax.jcr.lock.LockException) Node(javax.jcr.Node) Session(javax.jcr.Session) Lock(javax.jcr.lock.Lock)

Example 7 with LockException

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

the class LockManagerImpl method internalUnlock.

/**
     * Unlock a node (internal implementation)
     * @param node node to unlock
     * @throws LockException       if the node can not be unlocked
     * @throws RepositoryException if another error occurs
     */
boolean internalUnlock(NodeImpl node) throws LockException, RepositoryException {
    ClusterOperation operation = null;
    boolean successful = false;
    if (eventChannel != null) {
        operation = eventChannel.create(node.getNodeId());
    }
    acquire();
    try {
        SessionImpl session = (SessionImpl) node.getSession();
        // check whether node is locked by this session
        PathMap.Element<LockInfo> element = lockMap.map(getPath(session, node.getId()), true);
        if (element == null) {
            throw new LockException("Node not locked: " + node);
        }
        LockInfo info = element.get();
        if (info == null) {
            throw new LockException("Node not locked: " + node);
        }
        checkUnlock(info, session);
        getSessionLockManager(session).lockTokenRemoved(info.getLockToken());
        element.set(null);
        info.setLive(false);
        if (!info.isSessionScoped()) {
            save();
            successful = true;
        }
        return true;
    } finally {
        release();
        if (operation != null) {
            operation.ended(successful);
        }
    }
}
Also used : LockException(javax.jcr.lock.LockException) SessionImpl(org.apache.jackrabbit.core.SessionImpl) ClusterOperation(org.apache.jackrabbit.core.cluster.ClusterOperation) PathMap(org.apache.jackrabbit.spi.commons.name.PathMap)

Example 8 with LockException

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

the class LockManagerImpl method addLockToken.

/**
     * {@inheritDoc}
     */
public void addLockToken(SessionImpl session, String lt) throws LockException, RepositoryException {
    try {
        acquire();
        NodeId id = LockInfo.parseLockToken(lt);
        NodeImpl node = (NodeImpl) sysSession.getItemManager().getItem(id);
        Path path = node.getPrimaryPath();
        PathMap.Element<LockInfo> element = lockMap.map(path, true);
        if (element != null) {
            LockInfo info = element.get();
            if (info != null && !info.isLockHolder(session)) {
                if (info.getLockHolder() == null) {
                    info.setLockHolder(session);
                    if (info instanceof InternalLockInfo) {
                        session.addListener((InternalLockInfo) info);
                    }
                } else {
                    String msg = "Cannot add lock token: lock already held by other session.";
                    log.warn(msg);
                    info.throwLockException(msg, session);
                }
            }
        }
        // inform SessionLockManager
        getSessionLockManager(session).lockTokenAdded(lt);
    } catch (IllegalArgumentException e) {
        String msg = "Bad lock token: " + e.getMessage();
        log.warn(msg);
        throw new LockException(msg);
    } finally {
        release();
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) NodeImpl(org.apache.jackrabbit.core.NodeImpl) LockException(javax.jcr.lock.LockException) NodeId(org.apache.jackrabbit.core.id.NodeId) PathMap(org.apache.jackrabbit.spi.commons.name.PathMap)

Example 9 with LockException

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

the class NodeTest method testRemoveNodeParentLocked.

/**
     * Tests if <code>Node.remove()</code> throws a <code>LockException</code>
     * if the parent node of <code>Node</code> is locked.
     * <p>
     * The test creates a node <code>nodeName1</code> of type
     * <code>testNodeType</code> under <code>testRoot</code>, adds a child node
     * <code>nodeName2</code> and locks it with the superuser session. Then the
     * test tries to remove the <code>nodeName2</code>.
     */
public void testRemoveNodeParentLocked() throws LockException, NotExecutableException, RepositoryException {
    Session session = testRootNode.getSession();
    if (!isSupported(Repository.OPTION_LOCKING_SUPPORTED)) {
        throw new NotExecutableException("Locking is not supported.");
    }
    // create a node that is lockable
    Node node = testRootNode.addNode(nodeName1, testNodeType);
    // or try to make it lockable if it is not
    ensureMixinType(node, mixLockable);
    // create a child node
    Node subNode = node.addNode(nodeName2, testNodeType);
    testRootNode.getSession().save();
    // lock the node
    // remove first slash of path to get rel path to root
    String pathRelToRoot = node.getPath().substring(1);
    // access node through another session to lock it
    Session session2 = getHelper().getSuperuserSession();
    try {
        Node node2 = session2.getRootNode().getNode(pathRelToRoot);
        node2.lock(true, true);
        try {
            subNode.remove();
            session.save();
            fail("Removal of a Node must throw a LockException upon remove() " + "or upon save() if the parent of the node is locked");
        } catch (LockException e) {
        // success
        }
        // unlock to remove node at tearDown()
        node2.unlock();
    } finally {
        session2.logout();
    }
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) LockException(javax.jcr.lock.LockException) Node(javax.jcr.Node) Session(javax.jcr.Session)

Example 10 with LockException

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

the class NodeAddMixinTest method testLocked.

/**
     * Tests if <code>Node.addMixin(String mixinName)</code> throws a
     * <code>LockException</code> if <code>Node</code> is locked
     * <p>
     * The test creates a node <code>nodeName1</code> of type
     * <code>testNodeType</code> under <code>testRoot</code> and locks the node
     * with the superuser session. Then the test tries to add a mixin to
     * <code>nodeName1</code>  with the readWrite <code>Session</code>.
     */
public void testLocked() throws NotExecutableException, RepositoryException {
    Session session = testRootNode.getSession();
    if (!isSupported(Repository.OPTION_LOCKING_SUPPORTED)) {
        throw new NotExecutableException("Locking is not supported.");
    }
    // create a node that is lockable
    Node node = testRootNode.addNode(nodeName1, testNodeType);
    // or try to make it lockable if it is not
    ensureMixinType(node, mixLockable);
    testRootNode.getSession().save();
    String mixinName = NodeMixinUtil.getAddableMixinName(session, node);
    if (mixinName == null) {
        throw new NotExecutableException("No testable mixin node type found");
    }
    // remove first slash of path to get rel path to root
    String pathRelToRoot = node.getPath().substring(1);
    // access node through another session to lock it
    Session session2 = getHelper().getSuperuserSession();
    try {
        Node node2 = session2.getRootNode().getNode(pathRelToRoot);
        node2.lock(true, true);
        try {
            // implementation specific: either throw LockException upon
            // addMixin or upon save.
            node.addMixin(mixinName);
            node.save();
            fail("Node.addMixin(String mixinName) must throw a LockException " + "if the node is locked.");
        } catch (LockException e) {
        // success
        }
        // unlock to remove node at tearDown()
        node2.unlock();
    } finally {
        session2.logout();
    }
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) LockException(javax.jcr.lock.LockException) Node(javax.jcr.Node) Session(javax.jcr.Session)

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