Interface UserResourceLockService<U extends User>

All Known Implementing Classes:
DefaultUserResourceLockService

public interface UserResourceLockService<U extends User>
Service used to lock, unlock, & validate locks on User objects. This is meant to be used for processes that should simultaneously modify a User.

Note that this is different from DefaultUserLockoutService. DefaultUserLockoutService is to facilitate the user account lockouts based on login attempts, while this service is to lock the user entity itself, preventing multiple processes from simultaneously modifying a User.

Author:
Sunny Yu
See Also:
  • Method Details

    • lockUser

      String lockUser(U user)
      Attempts to lock the User, if a lock is not already established.
      Parameters:
      user - The User that is being locked.
      Returns:
      The locked User.
      Throws:
      com.broadleafcommerce.common.messaging.exception.ResourceLockException - if the user is already locked.
    • lockUser

      String lockUser(U user, Duration lockTTL)
      Attempts to lock the User, if a lock is not already established.
      Parameters:
      user - The User that is being locked.
      lockTTL - How long the lock should be held.
      Returns:
      The locked User.
      Throws:
      com.broadleafcommerce.common.messaging.exception.ResourceLockException - if the user is already locked.
    • doWithLock

      default <V> V doWithLock(User user, Supplier<V> operation)
      A convenience method for lockUser(User) and unlockUser(String) to obtain a lock for a User, perform an operation, and then unlock the user.

      If the lock pre-existed before this action, then the lockToken is expected to be non-null. In that case, if the token correlates to an active lock, then the operation will be allowed to proceed. If the token does not match a known lock, then a ResourceLockException is thrown. If the token is known, but correlates to an expired or unlocked user lock, then we'll attempt to re-establish a lock for the operation.

      Type Parameters:
      V - type returned from operation
      Parameters:
      user - the User that is to be locked
      operation - the operation to perform within the lock
      Returns:
      the result of invoking operation
      Throws:
      com.broadleafcommerce.common.messaging.exception.ResourceLockException - if a lock cannot be obtained.
    • doWithLock

      <V> V doWithLock(User user, @Nullable String lockToken, Supplier<V> operation)
      A convenience method for lockUser(User) and unlockUser(String) to obtain a lock for a User, perform an operation, and then unlock the user.

      If the lock pre-existed before this action, then the lockToken is expected to be non-null. In that case, if the token correlates to an active lock, then the operation will be allowed to proceed. If the token does not match a known lock, then a ResourceLockException is thrown. If the token is known, but correlates to an expired or unlocked user lock, then we'll attempt to re-establish a lock for the operation.

      Type Parameters:
      V - type returned from operation
      Parameters:
      user - the User that is to be locked
      lockToken - Token granted to resource that owns the user lock.
      operation - the operation to perform within the lock
      Returns:
      the result of invoking operation
      Throws:
      com.broadleafcommerce.common.messaging.exception.ResourceLockException - if the token does not match a known lock.
    • doWithLock

      default <V> V doWithLock(User user, Function<String,V> function)
      A convenience method for lockUser(User) and unlockUser(String) to obtain a lock for a User, perform an operation, and then unlock the user.

      If the lock pre-existed before this action, then the lockToken is expected to be non-null. In that case, if the token correlates to an active lock, then the operation will be allowed to proceed. If the token does not match a known lock, then a ResourceLockException is thrown. If the token is known, but correlates to an expired or unlocked user lock, then we'll attempt to re-establish a lock for the operation.

      Type Parameters:
      V - type returned from operation
      Parameters:
      user - the User that is to be locked
      function - the function to perform within the lock
      Returns:
      the result of invoking operation
      Throws:
      com.broadleafcommerce.common.messaging.exception.ResourceLockException - if a lock cannot be obtained.
    • doWithLock

      default <V> V doWithLock(U user, Duration lockTTL, Function<String,V> function)
      A convenience method for lockUser(User) and unlockUser(String) to obtain a lock for a User, perform an operation, and then unlock the user.

      If the lock pre-existed before this action, then the lockToken is expected to be non-null. In that case, if the token correlates to an active lock, then the operation will be allowed to proceed. If the token does not match a known lock, then a ResourceLockException is thrown. If the token is known, but correlates to an expired or unlocked user lock, then we'll attempt to re-establish a lock for the operation.

      Type Parameters:
      V - type returned from operation
      Parameters:
      user - the User that is to be locked
      lockTTL - How long the lock should be held.
      function - the function to perform within the lock
      Returns:
      the result of invoking operation
      Throws:
      com.broadleafcommerce.common.messaging.exception.ResourceLockException - if a lock cannot be obtained.
    • doWithLock

      default <V> V doWithLock(User user, @Nullable String lockToken, Function<String,V> function)
      A convenience method for lockUser(User) and unlockUser(String) to obtain a lock for a User, perform an operation, and then unlock the user.

      If the lock pre-existed before this action, then the lockToken is expected to be non-null. In that case, if the token correlates to an active lock, then the operation will be allowed to proceed. If the token does not match a known lock, then a ResourceLockException is thrown. If the token is known, but correlates to an expired or unlocked user lock, then we'll attempt to re-establish a lock for the operation.

      Type Parameters:
      V - type returned from operation
      Parameters:
      user - the User that is to be locked
      lockToken - Token granted to resource that owns the user lock.
      function - the function to perform within the lock
      Returns:
      the result of invoking operation
      Throws:
      com.broadleafcommerce.common.messaging.exception.ResourceLockException - if the token does not match a known lock.
    • doWithLock

      <V> V doWithLock(User user, @Nullable Duration lockTTL, @Nullable String lockToken, Function<String,V> function)
      A convenience method for lockUser(User) and unlockUser(String) to obtain a lock for a User, perform an operation, and then unlock the user.

      If the lock pre-existed before this action, then the lockToken is expected to be non-null. In that case, if the token correlates to an active lock, then the operation will be allowed to proceed. If the token does not match a known lock, then a ResourceLockException is thrown. If the token is known, but correlates to an expired or unlocked user lock, then we'll attempt to re-establish a lock for the operation.

      Type Parameters:
      V - type returned from operation
      Parameters:
      user - the User that is to be locked
      lockTTL - How long the lock should be held.
      lockToken - Token granted to resource that owns the user lock.
      function - the function to perform within the lock
      Returns:
      the result of invoking operation
      Throws:
      com.broadleafcommerce.common.messaging.exception.ResourceLockException - if the token does not match a known lock.
    • unlockUser

      void unlockUser(@NonNull @NonNull String lockToken)
      Releases the lock against the user.
      Parameters:
      lockToken - Token granted to resource that owns the user lock.
      Throws:
      com.broadleafcommerce.common.messaging.exception.ResourceLockException - if the lock token does not match the User's lock.