Search in sources :

Example 6 with LockType

use of org.alfresco.service.cmr.lock.LockType in project alfresco-repository by Alfresco.

the class LockServiceImpl method checkForLock.

/**
 * {@inheritDoc}
 */
@Extend(traitAPI = LockServiceTrait.class, extensionAPI = LockServiceExtension.class)
public void checkForLock(NodeRef nodeRef) throws NodeLockedException {
    String userName = getUserName();
    nodeRef = tenantService.getName(nodeRef);
    // Ensure we have found a node reference
    if (nodeRef != null && userName != null) {
        String effectiveUserName = AuthenticationUtil.getRunAsUser();
        // Check to see if should just ignore this node - note: special MT System due to AuditableAspect
        if (!(ignore(nodeRef) || tenantService.getBaseNameUser(effectiveUserName).equals(AuthenticationUtil.getSystemUserName()))) {
            try {
                // Get the current lock status on the node ref
                LockStatus currentLockStatus = getLockStatus(nodeRef, userName);
                LockType lockType = getLockType(nodeRef);
                if (LockType.WRITE_LOCK.equals(lockType) == true && LockStatus.LOCKED.equals(currentLockStatus) == true) {
                    // Lock is of type Write Lock and the node is locked by another owner.
                    throw new NodeLockedException(nodeRef);
                } else if (LockType.READ_ONLY_LOCK.equals(lockType) == true && (LockStatus.LOCKED.equals(currentLockStatus) == true || LockStatus.LOCK_OWNER.equals(currentLockStatus) == true)) {
                    // modifications are prevented
                    throw new NodeLockedException(nodeRef);
                } else if (LockType.NODE_LOCK.equals(lockType) == true && (LockStatus.LOCKED.equals(currentLockStatus) == true || LockStatus.LOCK_OWNER.equals(currentLockStatus) == true)) {
                    // modifications are prevented
                    throw new NodeLockedException(nodeRef);
                }
            } catch (AspectMissingException exception) {
            // Ignore since this indicates that the node does not have the lock aspect applied
            }
        }
    }
}
Also used : LockStatus(org.alfresco.service.cmr.lock.LockStatus) NodeLockedException(org.alfresco.service.cmr.lock.NodeLockedException) LockType(org.alfresco.service.cmr.lock.LockType) AspectMissingException(org.alfresco.service.cmr.repository.AspectMissingException) Extend(org.alfresco.traitextender.Extend)

Example 7 with LockType

use of org.alfresco.service.cmr.lock.LockType 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 8 with LockType

use of org.alfresco.service.cmr.lock.LockType in project alfresco-repository by Alfresco.

the class LockServiceImplTest method testGetLockType.

/**
 * Test getLockType (and isLocked/isLockedReadOnly)
 */
public void testGetLockType() {
    TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
    // Get the lock type (should be null since the object is not locked)
    LockType lockType1 = this.lockService.getLockType(this.parentNode);
    assertNull(lockType1);
    // Lock the object for writing
    this.lockService.lock(this.parentNode, LockType.WRITE_LOCK);
    LockType lockType2 = this.lockService.getLockType(this.parentNode);
    assertNotNull(lockType2);
    assertEquals(LockType.WRITE_LOCK, lockType2);
    assertTrue(lockService.isLocked(parentNode));
    assertFalse(lockService.isLockedAndReadOnly(parentNode));
    // Unlock the node
    this.lockService.unlock(this.parentNode);
    LockType lockType3 = this.lockService.getLockType(this.parentNode);
    assertNull(lockType3);
    assertFalse(lockService.isLocked(parentNode));
    assertFalse(lockService.isLockedAndReadOnly(parentNode));
    // Lock the object for read only
    this.lockService.lock(this.parentNode, LockType.READ_ONLY_LOCK);
    LockType lockType4 = this.lockService.getLockType(this.parentNode);
    assertNotNull(lockType4);
    assertEquals(LockType.READ_ONLY_LOCK, lockType4);
    assertTrue(lockService.isLocked(parentNode));
    assertTrue(lockService.isLockedAndReadOnly(parentNode));
    // Lock the object for node lock
    this.lockService.lock(this.parentNode, LockType.NODE_LOCK);
    LockType lockType5 = this.lockService.getLockType(this.parentNode);
    assertNotNull(lockType5);
    assertEquals(LockType.NODE_LOCK, lockType5);
    assertTrue(lockService.isLocked(parentNode));
    assertTrue(lockService.isLockedAndReadOnly(parentNode));
    // Unlock the node
    this.lockService.unlock(this.parentNode);
    LockType lockType6 = this.lockService.getLockType(this.parentNode);
    assertNull(lockType6);
    assertFalse(lockService.isLocked(parentNode));
    assertFalse(lockService.isLockedAndReadOnly(parentNode));
    // Test with no apect node
    LockType lockType7 = this.lockService.getLockType(this.noAspectNode);
    assertTrue("lock type is not null", lockType7 == null);
}
Also used : LockType(org.alfresco.service.cmr.lock.LockType)

Aggregations

LockType (org.alfresco.service.cmr.lock.LockType)8 Serializable (java.io.Serializable)4 Date (java.util.Date)4 QName (org.alfresco.service.namespace.QName)4 Lifetime (org.alfresco.repo.lock.mem.Lifetime)3 LockStatus (org.alfresco.service.cmr.lock.LockStatus)3 NodeRef (org.alfresco.service.cmr.repository.NodeRef)3 NodeLockedException (org.alfresco.service.cmr.lock.NodeLockedException)2 AssociationRef (org.alfresco.service.cmr.repository.AssociationRef)2 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)2 Extend (org.alfresco.traitextender.Extend)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Pattern (java.util.regex.Pattern)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 AccessDeniedException (org.alfresco.jlan.server.filesys.AccessDeniedException)1 FileState (org.alfresco.jlan.server.filesys.cache.FileState)1