Class FencingTokenGenerator

java.lang.Object
com.loomcache.server.cp.FencingTokenGenerator

public final class FencingTokenGenerator extends Object
Generates monotonically increasing fencing tokens for lock acquisitions.

Purpose: Fencing tokens are used to prevent stale lock holders from accessing protected resources. Each lock acquisition receives a unique token that is globally ordered.

Thread Safety: This class is thread-safe. Multiple threads can safely generate tokens concurrently.

Since:
1.0
  • Constructor Details

    • FencingTokenGenerator

      public FencingTokenGenerator()
  • Method Details

    • generateToken

      public FencingToken generateToken(String lockName, String sessionId)
      Generates a new fencing token for the given lock and session.
      Parameters:
      lockName - the name of the lock (must not be null or empty)
      sessionId - the session ID (must not be null or empty)
      Returns:
      a new FencingToken with a monotonically increasing value
      Throws:
      NullPointerException - if lockName or sessionId is null
      IllegalArgumentException - if lockName or sessionId is empty
    • validateToken

      public boolean validateToken(FencingToken token, String expectedLock, String expectedSession)
      Validates a fencing token against the current state.

      A token is valid if:

      • It matches the expected lock name and session ID
      • Its value matches the latest token issued for that lock
      Parameters:
      token - the token to validate (must not be null)
      expectedLock - the expected lock name (must not be null)
      expectedSession - the expected session ID (must not be null)
      Returns:
      true if the token is valid for the given lock and session
      Throws:
      NullPointerException - if any parameter is null
    • isTokenStale

      public boolean isTokenStale(FencingToken token)
      Checks if a token is stale (older than the current global counter).
      Parameters:
      token - the token to check (must not be null)
      Returns:
      true if the token's value is less than the current global counter
      Throws:
      NullPointerException - if token is null
    • getCurrentTokenValue

      public long getCurrentTokenValue()
      Gets the current global token counter value.
      Returns:
      the current counter value
    • toString

      public String toString()
      Overrides:
      toString in class Object