Class LockableFile


  • public class LockableFile
    extends java.lang.Object
    Process & thread lockable access to a file. Creates file if it does not already exist.
    • 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
    • Constructor Detail

      • LockableFile

        private LockableFile​(java.io.File file)
    • 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.IOException
        Lock 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.IOException
        Unlock 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()