Class DefaultPaymentLockService<P extends Payment>

java.lang.Object
com.broadleafcommerce.paymenttransaction.service.DefaultPaymentLockService<P>
All Implemented Interfaces:
PaymentLockService<P>

public class DefaultPaymentLockService<P extends Payment> extends Object implements PaymentLockService<P>
  • Field Details

  • Constructor Details

    • DefaultPaymentLockService

      public DefaultPaymentLockService(PaymentRepository<com.broadleafcommerce.data.tracking.core.Trackable> repository, com.broadleafcommerce.data.tracking.core.service.CrudEntityHelper helper, PaymentVersionValidationService paymentVersionValidationService, com.broadleafcommerce.common.messaging.repository.ResourceLockRepository<?,?> resourceLockRepository, PaymentTransactionServiceProperties properties)
  • Method Details

    • lockPayment

      public String lockPayment(@NonNull P payment, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Description copied from interface: PaymentLockService
      Attempts to lock the Payment, if a lock is not already established.
      Specified by:
      lockPayment in interface PaymentLockService<P extends Payment>
      Parameters:
      payment - The Payment that is being locked.
      contextInfo - Context information related to multitenancy.
      Returns:
      The locked Payment.
    • lockPayment

      public String lockPayment(@NonNull P payment, @NonNull @NonNull Duration lockTTL, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Description copied from interface: PaymentLockService
      Attempts to lock the Payment, if a lock is not already established.
      Specified by:
      lockPayment in interface PaymentLockService<P extends Payment>
      Parameters:
      payment - The Payment that is being locked.
      lockTTL - How long the lock should be held.
      contextInfo - Context information related to multitenancy.
      Returns:
      The locked Payment.
    • lockPayments

      public Map<String,String> lockPayments(@NonNull @NonNull List<P> payments, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Description copied from interface: PaymentLockService
      Attempts to lock the Payments, if a lock is not already established. If a lock attempt fails for any of the payments, then none of them will be locked.

      Note that each lock's time-to-live will be multiplied by the total number of payments to lock

      Specified by:
      lockPayments in interface PaymentLockService<P extends Payment>
      Parameters:
      payments - The Payments that are being locked.
      contextInfo - Context information related to multitenancy.
      Returns:
      A map of payment id to lock token.
    • doWithLock

      public <V> V doWithLock(@NonNull @NonNull Payment payment, @Nullable String lockToken, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo, @NonNull @NonNull Supplier<V> operation)
      Description copied from interface: PaymentLockService
      A convenience method for PaymentLockService.lockPayment(Payment, ContextInfo) and PaymentLockService.unlockPayment(String) to obtain a lock for a Payment, perform an operation, and then unlock the payment. 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 payment lock, then we'll attempt to re-establish a lock for the operation.
      Specified by:
      doWithLock in interface PaymentLockService<P extends Payment>
      Type Parameters:
      V - type returned from operation
      Parameters:
      payment - the Payment that is to be locked
      lockToken - Token granted to resource that owns the payment lock.
      contextInfo - Context information related to multitenancy.
      operation - the operation to perform within the lock
      Returns:
      the result of invoking operation
    • doWithLock

      public <V> V doWithLock(@NonNull @NonNull Payment payment, @Nullable Duration lockTTL, @Nullable String lockToken, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo, @NonNull @NonNull Function<String,V> function)
      Description copied from interface: PaymentLockService
      A convenience method for PaymentLockService.lockPayment(Payment, ContextInfo) and PaymentLockService.unlockPayment(String) to obtain a lock for a Payment, perform an operation, and then unlock the payment. 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 payment lock, then we'll attempt to re-establish a lock for the operation.
      Specified by:
      doWithLock in interface PaymentLockService<P extends Payment>
      Type Parameters:
      V - type returned from operation
      Parameters:
      payment - the Payment that is to be locked
      lockTTL - How long the lock should be held.
      lockToken - Token granted to resource that owns the payment lock.
      contextInfo - Context information related to multitenancy.
      function - the function to perform within the lock
      Returns:
      the result of invoking operation
    • unlockPayment

      public void unlockPayment(@NonNull @NonNull String lockToken)
      Description copied from interface: PaymentLockService
      Releases the lock against the payment.
      Specified by:
      unlockPayment in interface PaymentLockService<P extends Payment>
      Parameters:
      lockToken - Token granted to resource that owns the payment lock.
    • unlockPayments

      public void unlockPayments(@NonNull @NonNull Collection<String> lockTokens)
      Description copied from interface: PaymentLockService
      Releases the lock against the list of payments.
      Specified by:
      unlockPayments in interface PaymentLockService<P extends Payment>
      Parameters:
      lockTokens - Tokens granted to resource that owns the payment locks.
    • _doWithTemporaryLock

      protected <V> V _doWithTemporaryLock(@NonNull @NonNull Payment payment, @NonNull @NonNull String lockToken, @NonNull @NonNull Supplier<V> operation)
      A convenience method for lockPayment(Payment, ContextInfo) and unlockPayment(String) to obtain a lock for a Payment, perform an operation, and then unlock the payment. 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 payment lock, then we'll attempt to re-establish a lock for the operation.
      Type Parameters:
      V - type returned from operation
      Parameters:
      payment - the Payment that is to be locked
      lockToken - Token granted to resource that owns the payment 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 Payment payment, @NonNull @NonNull Supplier<V> operation)
      A convenience method for lockPayment(Payment, ContextInfo) and unlockPayment(String) to obtain a lock for a Payment, perform an operation, and then unlock the payment.
      Type Parameters:
      V - type returned from operation
      Parameters:
      payment - the Payment 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 Payment payment, @NonNull @NonNull String lockToken, @NonNull @NonNull Function<String,V> function)
      A convenience method for lockPayment(Payment, ContextInfo) and unlockPayment(String) to obtain a lock for a Payment, perform an operation, and then unlock the payment. 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 payment lock, then we'll attempt to re-establish a lock for the operation.
      Type Parameters:
      V - type returned from operation
      Parameters:
      payment - the Payment that is to be locked
      lockToken - Token granted to resource that owns the payment 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 Payment payment, @Nullable Duration lockTTL, @NonNull @NonNull String lockToken, @NonNull @NonNull Function<String,V> function)
      A convenience method for lockPayment(Payment, ContextInfo) and unlockPayment(String) to obtain a lock for a Payment, perform an operation, and then unlock the payment. 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 payment lock, then we'll attempt to re-establish a lock for the operation.
      Type Parameters:
      V - type returned from operation
      Parameters:
      payment - the Payment that is to be locked
      lockTTL - How long the lock should be held.
      lockToken - Token granted to resource that owns the payment 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 Payment payment, @NonNull @NonNull Function<String,V> function)
      A convenience method for lockPayment(Payment, ContextInfo) and unlockPayment(String) to obtain a lock for a Payment, perform an operation, and then unlock the payment.
      Type Parameters:
      V - type returned from operation
      Parameters:
      payment - the Payment 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 Payment payment, @Nullable Duration lockTTL, @NonNull @NonNull Function<String,V> function)
      A convenience method for lockPayment(Payment, ContextInfo) and unlockPayment(String) to obtain a lock for a Payment, perform an operation, and then unlock the payment.
      Type Parameters:
      V - type returned from operation
      Parameters:
      payment - the Payment 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.
    • validatePaymentVersion

      protected void validatePaymentVersion(@NonNull P payment, @Nullable Integer requestVersion)
      Validate that the provided Payment is not out-of-date.
      Parameters:
      payment - the up-to-date Payment
      requestVersion - the version that was provided as part of the request
      Throws:
      StalePaymentVersionException - if the requestVersion is out of date.
    • readByContextId

      protected P readByContextId(@NonNull @NonNull String id, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
    • getRepository

      protected PaymentRepository<com.broadleafcommerce.data.tracking.core.Trackable> getRepository()
    • getHelper

      protected com.broadleafcommerce.data.tracking.core.service.CrudEntityHelper getHelper()
    • getPaymentVersionValidationService

      protected PaymentVersionValidationService getPaymentVersionValidationService()
    • getResourceLockRepository

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

      protected PaymentTransactionServiceProperties getProperties()