Class SystemLevelLocker
- java.lang.Object
-
- org.terracotta.utilities.test.net.SystemLevelLocker
-
class SystemLevelLocker extends java.lang.ObjectSupplies a system-level locking mechanism supporting port management. An instance of this class may be used to coordinate port reservations among multiple JVM instances in a single system.
-
-
Field Summary
Fields Modifier and Type Field Description private java.nio.file.PathlockFilePathprivate static org.slf4j.LoggerLOGGERprivate java.nio.channels.FileChannelopenChannelprivate intoutstandingLocksprivate static java.nio.file.PathRELATIVE_LOCK_FILE_PATH
-
Constructor Summary
Constructors Constructor Description SystemLevelLocker()Creates a new instance ofSystemLevelLocker.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidcloseChannelIfUnused()ClosesopenChannelif there are no outstanding locks.(package private) booleanlock(PortManager.PortRef portRef)Obtains a system-level lock against the lock file for the identified port.private voidrelease(int port, java.nio.channels.FileLock lock)Release the system-level lock for the specifiedPortRefinstance.
-
-
-
Field Detail
-
LOGGER
private static final org.slf4j.Logger LOGGER
-
RELATIVE_LOCK_FILE_PATH
private static final java.nio.file.Path RELATIVE_LOCK_FILE_PATH
-
lockFilePath
private final java.nio.file.Path lockFilePath
-
openChannel
private java.nio.channels.FileChannel openChannel
-
outstandingLocks
private int outstandingLocks
-
-
Constructor Detail
-
SystemLevelLocker
SystemLevelLocker()
Creates a new instance ofSystemLevelLocker. This constructor creates or gains access to the common file used for port locking.The common locking file is not deleted when a
SystemLevelLockerinstance is discarded. The common locking file persists across uses and is re-used if available.- Throws:
java.lang.IllegalStateException- if the common locking file cannot be created or is otherwise not accessible
-
-
Method Detail
-
lock
boolean lock(PortManager.PortRef portRef)
Obtains a system-level lock against the lock file for the identified port.- Parameters:
portRef- thePortRefidentifying the port.- Returns:
trueif the system-level lock was obtained;falseotherwise- Throws:
java.lang.IllegalStateException- if the lock cannot be obtained because of anIOException
-
release
private void release(int port, java.nio.channels.FileLock lock)Release the system-level lock for the specifiedPortRefinstance.- Parameters:
port- thePortRefinstance to unlocklock- the existingFileLockinstance
-
closeChannelIfUnused
private void closeChannelIfUnused()
ClosesopenChannelif there are no outstanding locks.
-
-