Search in sources :

Example 6 with LockState

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

the class LockServiceImplTest method testEphemeralLock.

public void testEphemeralLock() {
    TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
    // Check that the node is not currently locked
    assertEquals(LockStatus.NO_LOCK, lockService.getLockStatus(noAspectNode));
    assertFalse(lockService.isLocked(noAspectNode));
    // Check that there really is no lockable aspect
    assertEquals(false, nodeService.hasAspect(noAspectNode, ContentModel.ASPECT_LOCKABLE));
    // Lock the node
    lockService.lock(noAspectNode, LockType.WRITE_LOCK, 86400, Lifetime.EPHEMERAL, "some extra data");
    // Check additionalInfo has been stored
    assertEquals("some extra data", lockService.getAdditionalInfo(noAspectNode));
    // Check that we can retrieve LockState
    LockState lockState = lockService.getLockState(noAspectNode);
    assertEquals(noAspectNode, lockState.getNodeRef());
    assertEquals(LockType.WRITE_LOCK, lockState.getLockType());
    assertEquals(GOOD_USER_NAME, lockState.getOwner());
    assertEquals(Lifetime.EPHEMERAL, lockState.getLifetime());
    assertNotNull(lockState.getExpires());
    assertEquals("some extra data", lockState.getAdditionalInfo());
    // The node should be locked
    assertEquals(LockStatus.LOCK_OWNER, lockService.getLockStatus(noAspectNode));
    assertTrue(lockService.isLocked(noAspectNode));
    // The node must still not have the lockable aspect applied
    assertEquals(false, nodeService.hasAspect(noAspectNode, ContentModel.ASPECT_LOCKABLE));
    // ...though the full node service should report that it is present
    NodeService fullNodeService = (NodeService) applicationContext.getBean("nodeService");
    assertEquals(true, fullNodeService.hasAspect(noAspectNode, ContentModel.ASPECT_LOCKABLE));
    TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
    assertEquals(LockStatus.LOCKED, lockService.getLockStatus(noAspectNode));
    assertTrue(lockService.isLocked(noAspectNode));
    // Test lock when already locked
    try {
        lockService.lock(noAspectNode, 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);
    assertEquals(LockStatus.LOCK_OWNER, lockService.getLockStatus(noAspectNode));
    assertTrue(lockService.isLocked(noAspectNode));
    // Test already locked by this user - relock
    try {
        lockService.lock(noAspectNode, LockType.WRITE_LOCK, 0, Lifetime.EPHEMERAL);
    } catch (Exception exception) {
        fail("No error should be thrown when a node is re-locked by the current lock owner.");
    }
    // The node should be locked
    assertEquals(LockStatus.LOCK_OWNER, lockService.getLockStatus(noAspectNode));
    assertTrue(lockService.isLocked(noAspectNode));
    // If we remove the lock info directly from the memory store then the node should no longer
    // be reported as locked (as it is an ephemeral lock)
    LockStore lockStore = (LockStore) applicationContext.getBean("lockStore");
    lockStore.clear();
    // The node must no longer be reported as locked
    assertEquals(LockStatus.NO_LOCK, lockService.getLockStatus(noAspectNode));
    assertFalse(lockService.isLocked(noAspectNode));
    // Lock again, ready to test unlocking an ephemeral lock.
    try {
        lockService.lock(noAspectNode, LockType.WRITE_LOCK, 0, Lifetime.EPHEMERAL);
    } catch (Exception exception) {
        fail("No error should be thrown when a node is re-locked by the current lock owner.");
    }
    assertEquals(LockStatus.LOCK_OWNER, lockService.getLockStatus(noAspectNode));
    assertTrue(lockService.isLocked(noAspectNode));
    lockService.unlock(noAspectNode);
    assertEquals(LockStatus.NO_LOCK, lockService.getLockStatus(noAspectNode));
    assertFalse(lockService.isLocked(noAspectNode));
}
Also used : NodeService(org.alfresco.service.cmr.repository.NodeService) LockState(org.alfresco.repo.lock.mem.LockState) UnableToAquireLockException(org.alfresco.service.cmr.lock.UnableToAquireLockException) LockStore(org.alfresco.repo.lock.mem.LockStore) 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)

Example 7 with LockState

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

the class LockServiceImplTest method checkLifetimeForExpiry.

private void checkLifetimeForExpiry(Lifetime expectedLifetime, int expirySecs, Lifetime requestedLifetime) {
    lockService.unlock(parentNode);
    assertNotEquals(LockStatus.LOCKED, lockService.getLockStatus(parentNode));
    lockService.lock(parentNode, LockType.WRITE_LOCK, expirySecs, requestedLifetime);
    LockState lock = lockService.getLockState(parentNode);
    assertEquals(expectedLifetime, lock.getLifetime());
    // Check that for any timeouts we test, a request for a persistent lock always yields a persistent lock.
    lockService.unlock(parentNode);
    assertNotEquals(LockStatus.LOCKED, lockService.getLockStatus(parentNode));
    lockService.lock(parentNode, LockType.WRITE_LOCK, expirySecs, Lifetime.PERSISTENT);
    lock = lockService.getLockState(parentNode);
    assertEquals(Lifetime.PERSISTENT, lock.getLifetime());
}
Also used : LockState(org.alfresco.repo.lock.mem.LockState)

Example 8 with LockState

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

the class LockServiceImplTest method testGetLockStatus.

/**
 * Test getLockStatus
 */
public void testGetLockStatus() {
    TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
    // Check an unlocked node
    LockStatus lockStatus1 = this.lockService.getLockStatus(this.parentNode);
    assertEquals(LockStatus.NO_LOCK, lockStatus1);
    this.lockService.lock(this.parentNode, LockType.WRITE_LOCK);
    TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
    // Check for locked status
    LockStatus lockStatus2 = this.lockService.getLockStatus(this.parentNode);
    assertEquals(LockStatus.LOCKED, lockStatus2);
    // Check lockstore is not used for persistent locks
    // Previously LockStore was doubling up as a cache - the change in requirements means a test
    // is necessary to ensure the work has been implemented correctly (despite being an odd test)
    LockStore lockStore = (LockStore) applicationContext.getBean("lockStore");
    lockStore.clear();
    LockState lockState = lockStore.get(parentNode);
    // Nothing stored against node ref
    assertNull(lockState);
    lockService.getLockStatus(parentNode);
    // In-memory store still empty - only used for ephemeral locks
    lockState = lockStore.get(parentNode);
    assertNull(lockState);
    TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
    // Check for lock owner status
    LockStatus lockStatus3 = this.lockService.getLockStatus(this.parentNode);
    assertEquals(LockStatus.LOCK_OWNER, lockStatus3);
    // Test with no apect node
    this.lockService.getLockStatus(this.noAspectNode);
    // Test method overload
    LockStatus lockStatus4 = this.lockService.getLockStatus(this.parentNode);
    assertEquals(LockStatus.LOCK_OWNER, lockStatus4);
}
Also used : LockStatus(org.alfresco.service.cmr.lock.LockStatus) LockStore(org.alfresco.repo.lock.mem.LockStore) LockState(org.alfresco.repo.lock.mem.LockState)

Example 9 with LockState

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

the class WebDAVLockServiceImplTest method mnt13183LockInfo.

@Test
public void mnt13183LockInfo() {
    // CIFS lock node to 1 hour
    lockService.lock(nodeRef1, LockType.WRITE_LOCK, 3600, Lifetime.EPHEMERAL, "lock_info_that_is_not_from_webdav");
    // WebDAV get lock info
    LockInfo lockInfoNodeRef1 = davLockService.getLockInfo(nodeRef1);
    assertNull("exclusiveLockToken is null", lockInfoNodeRef1.getExclusiveLockToken());
    String user = AuthenticationUtil.getFullyAuthenticatedUser();
    // WebDav lock, check marker
    davLockService.lock(nodeRef2, user, 3600);
    LockState lockState2 = lockService.getLockState(nodeRef2);
    assertNotNull("lockState is not null", lockState2);
    String additionalInfo2 = lockState2.getAdditionalInfo();
    assertNotNull("additionalInfo is not null", additionalInfo2);
    assertTrue("Check WEBDAV_LOCK marker", additionalInfo2.startsWith(LockInfoImpl.ADDINFO_WEBDAV_MARKER));
}
Also used : LockState(org.alfresco.repo.lock.mem.LockState) Test(org.junit.Test)

Example 10 with LockState

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

the class WebDAVLockServiceImpl method getLockInfo.

/**
 * Gets the lock status for the node reference relative to the current user.
 *
 * @see LockService#getLockStatus(org.alfresco.service.cmr.repository.NodeRef, String)
 *
 * @param nodeRef    the node reference
 * @return           the lock status
 */
@Override
public LockInfo getLockInfo(NodeRef nodeRef) {
    LockInfo lockInfo = null;
    LockState lockState = lockService.getLockState(nodeRef);
    if (lockState != null) {
        String additionalInfo = lockState.getAdditionalInfo();
        try {
            lockInfo = LockInfoImpl.fromJSON(additionalInfo);
        } catch (IllegalArgumentException e) {
            lockInfo = new LockInfoImpl();
        }
        lockInfo.setExpires(lockState.getExpires());
        lockInfo.setOwner(lockState.getOwner());
    }
    return lockInfo;
}
Also used : LockState(org.alfresco.repo.lock.mem.LockState)

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