Class LockableFile
- java.lang.Object
-
- net.adoptopenjdk.icedteaweb.lockingfile.LockableFile
-
public class LockableFile extends java.lang.ObjectProcess & thread lockable access to a file. Creates file if it does not already exist.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static interfaceLockableFile.InterProcessLockInterface for abstracting away the details of how a file is locked system wide.private classLockableFile.LockFileThis is an alternative approach to locking a file system-wide.private classLockableFile.NioFileLockThis is the recommended way to lock a file system wide (across multiple processes).
-
Field Summary
Fields Modifier and Type Field Description private java.io.Filefileprivate static java.util.Map<java.io.File,LockableFile>instanceCacheprivate static Loggerloggerprivate LockableFile.InterProcessLockprocessLockprivate booleanreadOnlyprivate java.util.concurrent.locks.ReentrantLockthreadLock
-
Constructor Summary
Constructors Modifier Constructor Description privateLockableFile(java.io.File file)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private booleancanWriteFile(java.io.File file)private booleancanWriteParent(java.io.File file)private voidcreateFileIfMissing()private voidcreateLockableFile()private voidcreateParentDirIfMissing(java.io.File f)java.io.FilegetFile()Get the file being locked.static LockableFilegetInstance(java.io.File file)Get a LockableFile for a given File.booleanisHeldByCurrentThread()booleanisReadOnly()private booleanisReadOnly(java.io.File file)voidlock()Lock access to the file.Lock is reentrant.booleantryLock()voidunlock()Unlock access to the file.Lock is reentrant.
-
-
-
Field Detail
-
logger
private static final Logger logger
-
instanceCache
private static final java.util.Map<java.io.File,LockableFile> instanceCache
-
file
private final java.io.File file
-
readOnly
private final boolean readOnly
-
threadLock
private final java.util.concurrent.locks.ReentrantLock threadLock
-
processLock
private final LockableFile.InterProcessLock processLock
-
-
Method Detail
-
getInstance
public static LockableFile getInstance(java.io.File file)
Get a LockableFile for a given File. Ensures that we share the same instance for all threads.- Parameters:
file- the file to lock- Returns:
- a LockableFile instance
-
isReadOnly
private boolean isReadOnly(java.io.File file)
-
canWriteFile
private boolean canWriteFile(java.io.File file)
-
canWriteParent
private boolean canWriteParent(java.io.File file)
-
createParentDirIfMissing
private void createParentDirIfMissing(java.io.File f)
-
createLockableFile
private void createLockableFile()
-
getFile
public java.io.File getFile()
Get the file being locked.- Returns:
- the file
-
isReadOnly
public boolean isReadOnly()
- Returns:
- if the file is read only.
-
lock
public void lock() throws java.io.IOExceptionLock access to the file.Lock is reentrant.- Throws:
java.io.IOException- if an I/O error occurs.
-
tryLock
public boolean tryLock()
-
createFileIfMissing
private void createFileIfMissing() throws java.io.IOException- Throws:
java.io.IOException
-
unlock
public void unlock() throws java.io.IOExceptionUnlock access to the file.Lock is reentrant. Does not do anything if not holding the lock.- Throws:
java.io.IOException- if an I/O error occurs.
-
isHeldByCurrentThread
public boolean isHeldByCurrentThread()
-
-