Search in sources :

Example 11 with LockState

use of org.alfresco.repo.lock.mem.LockState in project alfresco-repository by Alfresco.

the class LockServiceImpl method unlock.

/**
 * @see org.alfresco.service.cmr.lock.LockService#unlock(NodeRef, boolean, boolean)
 */
@Override
@Extend(traitAPI = LockServiceTrait.class, extensionAPI = LockServiceExtension.class)
public void unlock(NodeRef nodeRef, boolean unlockChildren, boolean allowCheckedOut) throws UnableToReleaseLockException {
    invokeBeforeUnlock(nodeRef);
    // Unlock the parent
    nodeRef = tenantService.getName(nodeRef);
    LockState lockState = getLockState(nodeRef);
    if (lockState.isLockInfo()) {
        // MNT-231: forbidden to unlock a checked out node
        if (!allowCheckedOut && nodeService.hasAspect(nodeRef, ContentModel.ASPECT_CHECKED_OUT)) {
            throw new UnableToReleaseLockException(nodeRef, CAUSE.CHECKED_OUT);
        }
        // Remove the lock from persistent storage.
        Lifetime lifetime = lockState.getLifetime();
        if (lifetime == Lifetime.PERSISTENT) {
            addToIgnoreSet(nodeRef);
            behaviourFilter.disableBehaviour(nodeRef, ContentModel.ASPECT_VERSIONABLE);
            lockableAspectInterceptor.disableForThread();
            try {
                // Clear the lock
                if (nodeService.hasAspect(nodeRef, ContentModel.ASPECT_LOCKABLE)) {
                    nodeService.removeAspect(nodeRef, ContentModel.ASPECT_LOCKABLE);
                }
            } finally {
                behaviourFilter.enableBehaviour(nodeRef, ContentModel.ASPECT_VERSIONABLE);
                lockableAspectInterceptor.enableForThread();
                removeFromIgnoreSet(nodeRef);
            }
        } else if (lifetime == Lifetime.EPHEMERAL) {
            // Remove the ephemeral lock.
            lockStore.set(nodeRef, LockState.createUnlocked(nodeRef));
        } else {
            throw new IllegalStateException("Unhandled Lifetime value: " + lifetime);
        }
    }
    if (unlockChildren) {
        // Get the children and unlock them
        Collection<ChildAssociationRef> childAssocRefs = this.nodeService.getChildAssocs(nodeRef);
        for (ChildAssociationRef childAssocRef : childAssocRefs) {
            unlock(childAssocRef.getChildRef(), unlockChildren);
        }
    }
}
Also used : Lifetime(org.alfresco.repo.lock.mem.Lifetime) LockState(org.alfresco.repo.lock.mem.LockState) UnableToReleaseLockException(org.alfresco.service.cmr.lock.UnableToReleaseLockException) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) Extend(org.alfresco.traitextender.Extend)

Example 12 with LockState

use of org.alfresco.repo.lock.mem.LockState in project alfresco-repository by Alfresco.

the class LockServiceImpl method getLockState.

@Override
@Extend(traitAPI = LockServiceTrait.class, extensionAPI = LockServiceExtension.class)
public LockState getLockState(NodeRef nodeRef) {
    // Check in-memory for ephemeral locks first.
    nodeRef = tenantService.getName(nodeRef);
    LockState lockState = lockStore.get(nodeRef);
    if (lockState != null) {
        String lockOwner = lockState.getOwner();
        Date expiryDate = lockState.getExpires();
        LockStatus status = LockUtils.lockStatus(lockOwner, lockOwner, expiryDate);
        // in-memory ephemeral lock which is expired is irrelevant
        if (status.equals(LockStatus.LOCK_EXPIRED)) {
            lockState = null;
        }
    }
    // whether it represents a real lock
    if (lockState == null || !lockState.isLockInfo()) {
        // No in-memory state, so get from the DB.
        if (nodeService.exists(nodeRef) && nodeService.hasAspect(nodeRef, ContentModel.ASPECT_LOCKABLE)) {
            String lockOwner = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_LOCK_OWNER);
            Date expiryDate = (Date) nodeService.getProperty(nodeRef, ContentModel.PROP_EXPIRY_DATE);
            String lockTypeStr = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_LOCK_TYPE);
            LockType lockType = lockTypeStr != null ? LockType.valueOf(lockTypeStr) : null;
            String lifetimeStr = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_LOCK_LIFETIME);
            Lifetime lifetime = lifetimeStr != null ? Lifetime.valueOf(lifetimeStr) : null;
            String additionalInfo = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_LOCK_ADDITIONAL_INFO);
            // Mark lockstate as PERSISTENT as it was in the persistent storage!
            lockState = LockState.createLock(nodeRef, lockType, lockOwner, expiryDate, lifetime, additionalInfo);
        } else {
            // There is no lock information
            lockState = LockState.createUnlocked(nodeRef);
        }
    }
    // Never return a null LockState
    Assert.notNull(lockState);
    return lockState;
}
Also used : Lifetime(org.alfresco.repo.lock.mem.Lifetime) LockStatus(org.alfresco.service.cmr.lock.LockStatus) LockState(org.alfresco.repo.lock.mem.LockState) LockType(org.alfresco.service.cmr.lock.LockType) Date(java.util.Date) Extend(org.alfresco.traitextender.Extend)

Example 13 with LockState

use of org.alfresco.repo.lock.mem.LockState in project alfresco-repository by Alfresco.

the class LockServiceImplTest method testPersistentLockMayStoreAdditionalInfo.

public void testPersistentLockMayStoreAdditionalInfo() {
    lockService.lock(noAspectNode, LockType.NODE_LOCK, 0, Lifetime.PERSISTENT, "additional info");
    LockState lockState = lockService.getLockState(noAspectNode);
    assertEquals("additional info", lockState.getAdditionalInfo());
}
Also used : LockState(org.alfresco.repo.lock.mem.LockState)

Example 14 with LockState

use of org.alfresco.repo.lock.mem.LockState in project alfresco-repository by Alfresco.

the class LockServiceImplTest method testLock.

/**
 * Test lock
 */
public void testLock() {
    TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
    // Check that the node is not currently locked
    assertEquals(LockStatus.NO_LOCK, this.lockService.getLockStatus(this.parentNode));
    assertFalse(lockService.isLocked(parentNode));
    // Test valid lock
    this.lockService.lock(this.parentNode, LockType.WRITE_LOCK);
    assertEquals(LockStatus.LOCK_OWNER, this.lockService.getLockStatus(this.parentNode));
    assertTrue(lockService.isLocked(parentNode));
    // Check that we can retrieve LockState
    LockState lockState = lockService.getLockState(parentNode);
    assertEquals(parentNode, lockState.getNodeRef());
    assertEquals(LockType.WRITE_LOCK, lockState.getLockType());
    assertEquals(GOOD_USER_NAME, lockState.getOwner());
    assertEquals(Lifetime.PERSISTENT, lockState.getLifetime());
    assertEquals(null, lockState.getExpires());
    assertEquals(null, lockState.getAdditionalInfo());
    // Check the correct properties have been set
    Map<QName, Serializable> props = nodeService.getProperties(parentNode);
    assertEquals(GOOD_USER_NAME, props.get(ContentModel.PROP_LOCK_OWNER));
    assertEquals(LockType.WRITE_LOCK.toString(), props.get(ContentModel.PROP_LOCK_TYPE));
    assertEquals(Lifetime.PERSISTENT.toString(), props.get(ContentModel.PROP_LOCK_LIFETIME));
    assertEquals(null, props.get(ContentModel.PROP_EXPIRY_DATE));
    TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
    assertEquals(LockStatus.LOCKED, this.lockService.getLockStatus(this.parentNode));
    assertTrue(lockService.isLocked(parentNode));
    // Test lock when already locked
    try {
        this.lockService.lock(this.parentNode, LockType.WRITE_LOCK);
        fail("The user should not be able to lock the node since it is already locked by another user.");
    } catch (UnableToAquireLockException exception) {
        System.out.println(exception.getMessage());
    }
    TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
    // Test already locked by this user
    try {
        this.lockService.lock(this.parentNode, LockType.WRITE_LOCK);
    } catch (Exception exception) {
        fail("No error should be thrown when a node is re-locked by the current lock owner.");
    }
    // Test with no apect node
    this.lockService.lock(this.noAspectNode, LockType.WRITE_LOCK);
    assertTrue(lockService.isLocked(noAspectNode));
}
Also used : Serializable(java.io.Serializable) QName(org.alfresco.service.namespace.QName) LockState(org.alfresco.repo.lock.mem.LockState) UnableToAquireLockException(org.alfresco.service.cmr.lock.UnableToAquireLockException) NodeLockedException(org.alfresco.service.cmr.lock.NodeLockedException) UnableToReleaseLockException(org.alfresco.service.cmr.lock.UnableToReleaseLockException) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) NotSupportedException(javax.transaction.NotSupportedException) UnableToAquireLockException(org.alfresco.service.cmr.lock.UnableToAquireLockException) SystemException(javax.transaction.SystemException)

Aggregations

LockState (org.alfresco.repo.lock.mem.LockState)14 LockStatus (org.alfresco.service.cmr.lock.LockStatus)5 Extend (org.alfresco.traitextender.Extend)5 UnableToAquireLockException (org.alfresco.service.cmr.lock.UnableToAquireLockException)4 UnableToReleaseLockException (org.alfresco.service.cmr.lock.UnableToReleaseLockException)4 Date (java.util.Date)3 NotSupportedException (javax.transaction.NotSupportedException)3 SystemException (javax.transaction.SystemException)3 AccessDeniedException (org.alfresco.repo.security.permissions.AccessDeniedException)3 NodeLockedException (org.alfresco.service.cmr.lock.NodeLockedException)3 Lifetime (org.alfresco.repo.lock.mem.Lifetime)2 LockStore (org.alfresco.repo.lock.mem.LockStore)2 Serializable (java.io.Serializable)1 LockType (org.alfresco.service.cmr.lock.LockType)1 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)1 NodeService (org.alfresco.service.cmr.repository.NodeService)1 QName (org.alfresco.service.namespace.QName)1 Pair (org.alfresco.util.Pair)1 Test (org.junit.Test)1