Package io.ocfl.core

Class FileLocker

java.lang.Object
io.ocfl.core.FileLocker

public class FileLocker extends Object
Provides locks for logical paths, so that an object may be safely modified by multiple threads.
  • Constructor Details

    • FileLocker

      public FileLocker(Duration timeoutDuration)
      Parameters:
      timeoutDuration - the max amount of time to wait for a file lock
  • Method Details

    • lock

      public ReentrantLock lock(String logicalPath)
      Returns a lock on the specified logical path or throws a LockException if a lock was unable to be acquired. This lock MUST be released in a finally block.
      Parameters:
      logicalPath - the path to lock
      Returns:
      the lock
      Throws:
      LockException - when unable to acquire a lock
    • withLock

      public void withLock(String logicalPath, Runnable runnable)
      Executes the runnable after acquire a lock on the specified logical path. If the lock cannot be acquired, a LockException is thrown.
      Parameters:
      logicalPath - the path to lock
      Throws:
      LockException - when unable to acquire a lock
    • withLock

      public <T> T withLock(String logicalPath, UncheckedCallable<T> callable)
      Executes the callable after acquire a lock on the specified logical path. If the lock cannot be acquired, a LockException is thrown.
      Parameters:
      logicalPath - the path to lock
      Returns:
      the output of the callable
      Throws:
      LockException - when unable to acquire a lock