Class DefaultUserResourceLockService<U extends User>

java.lang.Object
com.broadleafcommerce.auth.user.service.DefaultUserResourceLockService<U>
All Implemented Interfaces:
UserResourceLockService<U>

public class DefaultUserResourceLockService<U extends User> extends Object implements UserResourceLockService<U>
  • Field Details

  • Constructor Details

    • DefaultUserResourceLockService

      public DefaultUserResourceLockService(com.broadleafcommerce.common.messaging.repository.ResourceLockRepository<?,?> resourceLockRepository, AuthenticationResourceLockProperties properties)
  • Method Details

    • lockUser

      public String lockUser(@NonNull U user)
      Description copied from interface: UserResourceLockService
      Attempts to lock the User, if a lock is not already established.
      Specified by:
      lockUser in interface UserResourceLockService<U extends User>
      Parameters:
      user - The User that is being locked.
      Returns:
      The locked User.
    • lockUser

      public String lockUser(@NonNull U user, @NonNull @NonNull Duration lockTTL)
      Description copied from interface: UserResourceLockService
      Attempts to lock the User, if a lock is not already established.
      Specified by:
      lockUser in interface UserResourceLockService<U extends User>
      Parameters:
      user - The User that is being locked.
      lockTTL - How long the lock should be held.
      Returns:
      The locked User.
    • doWithLock

      public <V> V doWithLock(@NonNull @NonNull User user, @Nullable String lockToken, @NonNull @NonNull Supplier<V> operation)
      Description copied from interface: UserResourceLockService
      A convenience method for UserResourceLockService.lockUser(User) and UserResourceLockService.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.

      Specified by:
      doWithLock in interface UserResourceLockService<U extends User>
      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
    • doWithLock

      public <V> V doWithLock(@NonNull @NonNull User user, @Nullable Duration lockTTL, @Nullable String lockToken, @NonNull @NonNull Function<String,V> function)
      Description copied from interface: UserResourceLockService
      A convenience method for UserResourceLockService.lockUser(User) and UserResourceLockService.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.

      Specified by:
      doWithLock in interface UserResourceLockService<U extends User>
      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
    • unlockUser

      public void unlockUser(@NonNull @NonNull String lockToken)
      Description copied from interface: UserResourceLockService
      Releases the lock against the user.
      Specified by:
      unlockUser in interface UserResourceLockService<U extends User>
      Parameters:
      lockToken - Token granted to resource that owns the user lock.
    • _doWithTemporaryLock

      protected <V> V _doWithTemporaryLock(@NonNull @NonNull User user, @NonNull @NonNull String lockToken, @NonNull @NonNull Supplier<V> operation)
      A convenience method for perform an operation with an existing lock token. If the given lock token is LockStatus.ACTIVE, then the operation is performed, otherwise a new lock is obtained via _doWithTemporaryLock(User, Supplier).
      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.
    • _doWithTemporaryLock

      protected <V> V _doWithTemporaryLock(@NonNull @NonNull User user, @NonNull @NonNull Supplier<V> operation)
      A convenience method for locking the user with the configured time-to-live from AuthenticationResourceLockProperties.getUserLockTtl(), perform an operation, and then unlock the user.
      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.
    • _doWithTemporaryLock

      protected <V> V _doWithTemporaryLock(@NonNull @NonNull User user, @NonNull @NonNull String lockToken, @NonNull @NonNull Function<String,V> function)
      A convenience method for locking the user with the configured time-to-live from AuthenticationResourceLockProperties.getUserLockTtl(), perform an operation, and then unlock the user.
      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.
    • _doWithTemporaryLock

      protected <V> V _doWithTemporaryLock(@NonNull @NonNull User user, @Nullable Duration lockTTL, @NonNull @NonNull String lockToken, @NonNull @NonNull Function<String,V> function)
      A convenience method for perform an operation with an existing lock token. If the given lock token is LockStatus.ACTIVE, then the operation is performed, otherwise a new lock is obtained via _doWithTemporaryLock(User, Function).
      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.
    • _doWithTemporaryLock

      protected <V> V _doWithTemporaryLock(@NonNull @NonNull User user, @NonNull @NonNull Function<String,V> function)
      A convenience method for locking the User, perform an operation, and then unlock the user.
      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.
    • _doWithTemporaryLock

      protected <V> V _doWithTemporaryLock(@NonNull @NonNull User user, @Nullable Duration lockTTL, @NonNull @NonNull Function<String,V> function)
      A convenience method for locking the User, perform an operation, and then unlock the user.
      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.
    • getResourceLockRepository

      protected com.broadleafcommerce.common.messaging.repository.ResourceLockRepository<?,?> getResourceLockRepository()
    • getProperties

      protected AuthenticationResourceLockProperties getProperties()