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);
}
}
}
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;
}
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());
}
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));
}
Aggregations