Search in sources :

Example 6 with Lock

use of javax.jcr.lock.Lock 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 Lock

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

the class LockManagerImpl method lock.

/**
     * @see LockStateManager#lock(NodeState,boolean,boolean,long,String)
     */
public Lock lock(NodeState nodeState, boolean isDeep, boolean isSessionScoped, long timeoutHint, String ownerHint) throws RepositoryException {
    // retrieve node first
    Node lhNode;
    Item item = itemManager.getItem(nodeState.getHierarchyEntry());
    if (item.isNode()) {
        lhNode = (Node) item;
    } else {
        throw new RepositoryException("Internal error: ItemManager returned Property from NodeState");
    }
    // execute the operation
    LockOperation op = LockOperation.create(nodeState, isDeep, isSessionScoped, timeoutHint, ownerHint);
    wspManager.execute(op);
    Lock lock = new LockImpl(new LockState(nodeState, op.getLockInfo()), lhNode);
    return lock;
}
Also used : Item(javax.jcr.Item) LockOperation(org.apache.jackrabbit.jcr2spi.operation.LockOperation) Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) Lock(javax.jcr.lock.Lock)

Example 8 with Lock

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

the class LockManagerImpl method getLocks.

/**
     * {@inheritDoc}
     */
public Lock[] getLocks(SessionImpl session) throws RepositoryException {
    acquire();
    LockInfo[] infos = getLockInfos(session);
    try {
        Lock[] locks = new Lock[infos.length];
        for (int i = 0; i < infos.length; i++) {
            NodeImpl holder = (NodeImpl) session.getItemManager().getItem(infos[i].getId());
            locks[i] = new LockImpl(infos[i], holder);
        }
        return locks;
    } finally {
        release();
    }
}
Also used : NodeImpl(org.apache.jackrabbit.core.NodeImpl) Lock(javax.jcr.lock.Lock) XAReentrantLock(org.apache.jackrabbit.core.util.XAReentrantLock)

Example 9 with Lock

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

the class Locked method with.

/**
     * Executes the method {@link #run} within the scope of a lock held on
     * <code>lockable</code>.
     *
     * @param lockable the node where the lock is obtained from.
     * @param isDeep   <code>true</code> if <code>lockable</code> will be locked
     *                 deep.
     * @param timeout  time in milliseconds to wait at most to acquire the lock.
     * @param isSessionScoped <code>true</code> if the lock is session scoped.
     * @return the object returned by {@link #run} or {@link #TIMED_OUT} if the
     *         lock on <code>lockable</code> could not be acquired within the
     *         specified timeout.
     * @throws IllegalArgumentException if <code>timeout</code> is negative or
     *                                  <code>lockable</code> is not
     *                                  <i>mix:lockable</i>.
     * @throws RepositoryException      if {@link #run} throws an exception.
     * @throws UnsupportedRepositoryOperationException
     *                                  if this repository does not support
     *                                  locking.
     * @throws InterruptedException     if this thread is interrupted while
     *                                  waiting for the lock on node
     *                                  <code>lockable</code>.
     */
public Object with(Node lockable, boolean isDeep, long timeout, boolean isSessionScoped) throws UnsupportedRepositoryOperationException, RepositoryException, InterruptedException {
    if (timeout < 0) {
        throw new IllegalArgumentException("timeout must be >= 0");
    }
    Session session = lockable.getSession();
    EventListener listener = null;
    try {
        // check whether the lockable can be locked at all
        String mix = session.getNamespacePrefix(MIX);
        if (!lockable.isNodeType(mix + ":lockable")) {
            throw new IllegalArgumentException("Node is not lockable");
        }
        Lock lock = tryLock(lockable, isDeep, timeout, isSessionScoped);
        if (lock != null) {
            return runAndUnlock(lock);
        }
        if (timeout == 0) {
            return TIMED_OUT;
        }
        long timelimit;
        if (timeout == Long.MAX_VALUE) {
            timelimit = Long.MAX_VALUE;
        } else {
            timelimit = System.currentTimeMillis() + timeout;
        }
        // node is locked by other session -> register event listener if possible
        if (isObservationSupported(session)) {
            ObservationManager om = session.getWorkspace().getObservationManager();
            listener = new EventListener() {

                public void onEvent(EventIterator events) {
                    synchronized (Locked.this) {
                        Locked.this.notify();
                    }
                }
            };
            om.addEventListener(listener, Event.PROPERTY_REMOVED, lockable.getPath(), false, null, null, true);
        }
        // the current thread when the lockable node is possibly unlocked
        for (; ; ) {
            synchronized (this) {
                lock = tryLock(lockable, isDeep, timeout, isSessionScoped);
                if (lock != null) {
                    return runAndUnlock(lock);
                } else {
                    // check timeout
                    if (System.currentTimeMillis() > timelimit) {
                        return TIMED_OUT;
                    }
                    if (listener != null) {
                        // event listener *should* wake us up, however
                        // there is a chance that removal of the lockOwner
                        // property is notified before the node is acutally
                        // unlocked. therefore we use a safety net to wait
                        // at most 1000 millis.
                        this.wait(Math.min(1000, timeout));
                    } else {
                        // repository does not support observation
                        // wait at most 50 millis then retry
                        this.wait(Math.min(50, timeout));
                    }
                }
            }
        }
    } finally {
        if (listener != null) {
            session.getWorkspace().getObservationManager().removeEventListener(listener);
        }
    }
}
Also used : ObservationManager(javax.jcr.observation.ObservationManager) EventListener(javax.jcr.observation.EventListener) EventIterator(javax.jcr.observation.EventIterator) Session(javax.jcr.Session) Lock(javax.jcr.lock.Lock)

Example 10 with Lock

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

the class DavResourceImpl method getLock.

/**
     * @see DavResource#getLock(Type, Scope)
     */
public ActiveLock getLock(Type type, Scope scope) {
    ActiveLock lock = null;
    if (exists() && Type.WRITE.equals(type) && Scope.EXCLUSIVE.equals(scope)) {
        // try to retrieve the repository lock information first
        try {
            if (node.isLocked()) {
                Lock jcrLock = node.getLock();
                if (jcrLock != null && jcrLock.isLive()) {
                    lock = new JcrActiveLock(jcrLock);
                    String lockroot = locator.getFactory().createResourceLocator(locator.getPrefix(), locator.getWorkspacePath(), jcrLock.getNode().getPath(), false).getHref(false);
                    lock.setLockroot(lockroot);
                }
            }
        } catch (RepositoryException e) {
        // LockException (no lock applies) >> should never occur
        // RepositoryException, AccessDeniedException or another error >> ignore
        }
        // could not retrieve a jcr-lock. test if a simple webdav lock is present.
        if (lock == null) {
            lock = lockManager.getLock(type, scope, this);
        }
    }
    return lock;
}
Also used : ActiveLock(org.apache.jackrabbit.webdav.lock.ActiveLock) JcrActiveLock(org.apache.jackrabbit.webdav.jcr.lock.JcrActiveLock) 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) SupportedLock(org.apache.jackrabbit.webdav.lock.SupportedLock)

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