@Test public void releaseLocksWhenNoExistinLocks() throws AcmObjectLockException { Long objectId = 1L; String[] lockTypes = { "READ", "WRITE", "SHARED_WRITE", "DELETE" }; String objectType = "FILE"; String userId = "userId"; for (String lockType : lockTypes) { EasyMock.reset(objectLockDaoMock, applicationEventPublisherMock); expect(objectLockDaoMock.findLock(objectId, objectType)).andReturn(null).anyTimes(); replayAll(); fileObjectLockingProvider.releaseObjectLock(objectId, objectType, lockType, false, userId, null); verifyAll(); } }
@Override public synchronized void releaseObjectLock(Long objectId, String objectType, String lockType, boolean unlockChildObjects, String userId, Long lockId) throws AcmObjectLockException { log.trace("Releasing object lock[objectId={}, objectType={}, lockType={}] for user: [{}]", objectId, objectType, lockType, userId); FileLockType objectLockType = FileLockType.fromName(lockType); AcmObjectLock existingLock = objectLockService.findLock(objectId, objectType); if (existingLock == null) { return; } if (existingLock.getCreator().equals(userId)) { switch (objectLockType) { case READ: if (!existingLock.getLockType().equals(FileLockType.READ.name())) { return; } break; case WRITE: throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, false); break; case SHARED_WRITE: if (existingLock.getLockType().equals(FileLockType.SHARED_WRITE.name()) && !existingLock.getId().equals(lockId)) { throw new AcmObjectLockException(String.format( "[{}] not able to release object lock[objectId={}, objectType={}, lockType={}]. Reason: Object lockId mismatch!", userId, objectId, objectType, lockType, existingLock.getLockType())); } throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, false); break; case DELETE: throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, false); break; default: throw new AcmObjectLockException("Unimplemented handling of lock type: " + lockType); } } else { switch (objectLockType) { case READ: return; case WRITE: throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, true); break; case SHARED_WRITE: if (existingLock.getLockType().equals(FileLockType.SHARED_WRITE.name()) && !existingLock.getId().equals(lockId)) { throw new AcmObjectLockException(String.format( "[%s] not able to release object lock[objectId=%s, objectType=%s, lockType=%s]. Reason: Object lockId mismatch!", userId, objectId, objectType, lockType, existingLock.getLockType())); } if (existingLock.getLockType().equals(FileLockType.SHARED_WRITE.name())) { userId = existingLock.getCreator(); } throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, false); break; case DELETE: throw new AcmObjectLockException(String.format( "[{}] not able to release object lock[objectId={}, objectType={}, lockType={}]. Reason: Object already has a lock of type {} by user: [{}]", userId, objectId, objectType, lockType, existingLock.getLockType(), existingLock.getCreator())); default: throw new AcmObjectLockException("Unimplemented handling of lock type: " + lockType); } } objectLockService.removeLock(objectId, objectType, lockType, userId); }
FileLockingProvider implements ObjectLockingProvider { @Override public synchronized void releaseObjectLock(Long objectId, String objectType, String lockType, boolean unlockChildObjects, String userId, Long lockId) throws AcmObjectLockException { log.trace("Releasing object lock[objectId={}, objectType={}, lockType={}] for user: [{}]", objectId, objectType, lockType, userId); FileLockType objectLockType = FileLockType.fromName(lockType); AcmObjectLock existingLock = objectLockService.findLock(objectId, objectType); if (existingLock == null) { return; } if (existingLock.getCreator().equals(userId)) { switch (objectLockType) { case READ: if (!existingLock.getLockType().equals(FileLockType.READ.name())) { return; } break; case WRITE: throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, false); break; case SHARED_WRITE: if (existingLock.getLockType().equals(FileLockType.SHARED_WRITE.name()) && !existingLock.getId().equals(lockId)) { throw new AcmObjectLockException(String.format( "[{}] not able to release object lock[objectId={}, objectType={}, lockType={}]. Reason: Object lockId mismatch!", userId, objectId, objectType, lockType, existingLock.getLockType())); } throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, false); break; case DELETE: throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, false); break; default: throw new AcmObjectLockException("Unimplemented handling of lock type: " + lockType); } } else { switch (objectLockType) { case READ: return; case WRITE: throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, true); break; case SHARED_WRITE: if (existingLock.getLockType().equals(FileLockType.SHARED_WRITE.name()) && !existingLock.getId().equals(lockId)) { throw new AcmObjectLockException(String.format( "[%s] not able to release object lock[objectId=%s, objectType=%s, lockType=%s]. Reason: Object lockId mismatch!", userId, objectId, objectType, lockType, existingLock.getLockType())); } if (existingLock.getLockType().equals(FileLockType.SHARED_WRITE.name())) { userId = existingLock.getCreator(); } throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, false); break; case DELETE: throw new AcmObjectLockException(String.format( "[{}] not able to release object lock[objectId={}, objectType={}, lockType={}]. Reason: Object already has a lock of type {} by user: [{}]", userId, objectId, objectType, lockType, existingLock.getLockType(), existingLock.getCreator())); default: throw new AcmObjectLockException("Unimplemented handling of lock type: " + lockType); } } objectLockService.removeLock(objectId, objectType, lockType, userId); } }
FileLockingProvider implements ObjectLockingProvider { @Override public synchronized void releaseObjectLock(Long objectId, String objectType, String lockType, boolean unlockChildObjects, String userId, Long lockId) throws AcmObjectLockException { log.trace("Releasing object lock[objectId={}, objectType={}, lockType={}] for user: [{}]", objectId, objectType, lockType, userId); FileLockType objectLockType = FileLockType.fromName(lockType); AcmObjectLock existingLock = objectLockService.findLock(objectId, objectType); if (existingLock == null) { return; } if (existingLock.getCreator().equals(userId)) { switch (objectLockType) { case READ: if (!existingLock.getLockType().equals(FileLockType.READ.name())) { return; } break; case WRITE: throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, false); break; case SHARED_WRITE: if (existingLock.getLockType().equals(FileLockType.SHARED_WRITE.name()) && !existingLock.getId().equals(lockId)) { throw new AcmObjectLockException(String.format( "[{}] not able to release object lock[objectId={}, objectType={}, lockType={}]. Reason: Object lockId mismatch!", userId, objectId, objectType, lockType, existingLock.getLockType())); } throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, false); break; case DELETE: throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, false); break; default: throw new AcmObjectLockException("Unimplemented handling of lock type: " + lockType); } } else { switch (objectLockType) { case READ: return; case WRITE: throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, true); break; case SHARED_WRITE: if (existingLock.getLockType().equals(FileLockType.SHARED_WRITE.name()) && !existingLock.getId().equals(lockId)) { throw new AcmObjectLockException(String.format( "[%s] not able to release object lock[objectId=%s, objectType=%s, lockType=%s]. Reason: Object lockId mismatch!", userId, objectId, objectType, lockType, existingLock.getLockType())); } if (existingLock.getLockType().equals(FileLockType.SHARED_WRITE.name())) { userId = existingLock.getCreator(); } throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, false); break; case DELETE: throw new AcmObjectLockException(String.format( "[{}] not able to release object lock[objectId={}, objectType={}, lockType={}]. Reason: Object already has a lock of type {} by user: [{}]", userId, objectId, objectType, lockType, existingLock.getLockType(), existingLock.getCreator())); default: throw new AcmObjectLockException("Unimplemented handling of lock type: " + lockType); } } objectLockService.removeLock(objectId, objectType, lockType, userId); }  }
FileLockingProvider implements ObjectLockingProvider { @Override public synchronized void releaseObjectLock(Long objectId, String objectType, String lockType, boolean unlockChildObjects, String userId, Long lockId) throws AcmObjectLockException { log.trace("Releasing object lock[objectId={}, objectType={}, lockType={}] for user: [{}]", objectId, objectType, lockType, userId); FileLockType objectLockType = FileLockType.fromName(lockType); AcmObjectLock existingLock = objectLockService.findLock(objectId, objectType); if (existingLock == null) { return; } if (existingLock.getCreator().equals(userId)) { switch (objectLockType) { case READ: if (!existingLock.getLockType().equals(FileLockType.READ.name())) { return; } break; case WRITE: throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, false); break; case SHARED_WRITE: if (existingLock.getLockType().equals(FileLockType.SHARED_WRITE.name()) && !existingLock.getId().equals(lockId)) { throw new AcmObjectLockException(String.format( "[{}] not able to release object lock[objectId={}, objectType={}, lockType={}]. Reason: Object lockId mismatch!", userId, objectId, objectType, lockType, existingLock.getLockType())); } throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, false); break; case DELETE: throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, false); break; default: throw new AcmObjectLockException("Unimplemented handling of lock type: " + lockType); } } else { switch (objectLockType) { case READ: return; case WRITE: throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, true); break; case SHARED_WRITE: if (existingLock.getLockType().equals(FileLockType.SHARED_WRITE.name()) && !existingLock.getId().equals(lockId)) { throw new AcmObjectLockException(String.format( "[%s] not able to release object lock[objectId=%s, objectType=%s, lockType=%s]. Reason: Object lockId mismatch!", userId, objectId, objectType, lockType, existingLock.getLockType())); } if (existingLock.getLockType().equals(FileLockType.SHARED_WRITE.name())) { userId = existingLock.getCreator(); } throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, false); break; case DELETE: throw new AcmObjectLockException(String.format( "[{}] not able to release object lock[objectId={}, objectType={}, lockType={}]. Reason: Object already has a lock of type {} by user: [{}]", userId, objectId, objectType, lockType, existingLock.getLockType(), existingLock.getCreator())); default: throw new AcmObjectLockException("Unimplemented handling of lock type: " + lockType); } } objectLockService.removeLock(objectId, objectType, lockType, userId); }  @Override void checkIfObjectLockCanBeAcquired(Long objectId, String objectType, String lockType, boolean checkChildObjects, String userId); @Override synchronized AcmObjectLock acquireObjectLock(Long objectId, String objectType, String lockType, Long expiry,
            boolean lockChildObjects, String userId); @Override synchronized void releaseObjectLock(Long objectId, String objectType, String lockType, boolean unlockChildObjects, String userId,
            Long lockId); @Override Long getExpiryTimeInMilliseconds(); void setExpiryTimeInMilliseconds(Long expiryTimeInMilliseconds); AcmObjectLockService getObjectLockService(); void setObjectLockService(AcmObjectLockService objectLockService); }
FileLockingProvider implements ObjectLockingProvider { @Override public synchronized void releaseObjectLock(Long objectId, String objectType, String lockType, boolean unlockChildObjects, String userId, Long lockId) throws AcmObjectLockException { log.trace("Releasing object lock[objectId={}, objectType={}, lockType={}] for user: [{}]", objectId, objectType, lockType, userId); FileLockType objectLockType = FileLockType.fromName(lockType); AcmObjectLock existingLock = objectLockService.findLock(objectId, objectType); if (existingLock == null) { return; } if (existingLock.getCreator().equals(userId)) { switch (objectLockType) { case READ: if (!existingLock.getLockType().equals(FileLockType.READ.name())) { return; } break; case WRITE: throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, false); break; case SHARED_WRITE: if (existingLock.getLockType().equals(FileLockType.SHARED_WRITE.name()) && !existingLock.getId().equals(lockId)) { throw new AcmObjectLockException(String.format( "[{}] not able to release object lock[objectId={}, objectType={}, lockType={}]. Reason: Object lockId mismatch!", userId, objectId, objectType, lockType, existingLock.getLockType())); } throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, false); break; case DELETE: throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, false); break; default: throw new AcmObjectLockException("Unimplemented handling of lock type: " + lockType); } } else { switch (objectLockType) { case READ: return; case WRITE: throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, true); break; case SHARED_WRITE: if (existingLock.getLockType().equals(FileLockType.SHARED_WRITE.name()) && !existingLock.getId().equals(lockId)) { throw new AcmObjectLockException(String.format( "[%s] not able to release object lock[objectId=%s, objectType=%s, lockType=%s]. Reason: Object lockId mismatch!", userId, objectId, objectType, lockType, existingLock.getLockType())); } if (existingLock.getLockType().equals(FileLockType.SHARED_WRITE.name())) { userId = existingLock.getCreator(); } throwErrorOnExistingLockExceptForReadLock(objectId, objectType, lockType, userId, existingLock, false); break; case DELETE: throw new AcmObjectLockException(String.format( "[{}] not able to release object lock[objectId={}, objectType={}, lockType={}]. Reason: Object already has a lock of type {} by user: [{}]", userId, objectId, objectType, lockType, existingLock.getLockType(), existingLock.getCreator())); default: throw new AcmObjectLockException("Unimplemented handling of lock type: " + lockType); } } objectLockService.removeLock(objectId, objectType, lockType, userId); }  @Override void checkIfObjectLockCanBeAcquired(Long objectId, String objectType, String lockType, boolean checkChildObjects, String userId); @Override synchronized AcmObjectLock acquireObjectLock(Long objectId, String objectType, String lockType, Long expiry,
            boolean lockChildObjects, String userId); @Override synchronized void releaseObjectLock(Long objectId, String objectType, String lockType, boolean unlockChildObjects, String userId,
            Long lockId); @Override Long getExpiryTimeInMilliseconds(); void setExpiryTimeInMilliseconds(Long expiryTimeInMilliseconds); AcmObjectLockService getObjectLockService(); void setObjectLockService(AcmObjectLockService objectLockService);  }