public static boolean lock(File file) {
        synchronized(lockedFiles) {
            if (lockedFiles.containsKey(file))
                return false;   // already locked
        }
        FileLock lock = null;
        RandomAccessFile raf = null;
        try {
            // Note if the file does not already exist then an attempt will be
            // made to create it because of the use of "rw".
            raf = new RandomAccessFile(file, "rw");
            FileChannel channel = raf.getChannel();
            if (EXTERNAL_LOCK)
                lock = channel.lock();
        } catch (Exception e) {
            IOUtils.closeQuietly(raf, log);
            throw new FileLockException(e);
        }
        final boolean locked;
        synchronized(lockedFiles) {
            RandomAccessFile prev = lockedFiles.put(file, raf);
            if (prev == null) {
                locked = true;
            } else {
                // race condition: some other thread got locked it before this
                locked = false;
                lockedFiles.put(file, prev);    // put it back
            }
        }
        if (locked) {
            if (log.isDebugEnabled())
                log.debug("Locked file " + file + " with " + lock);
        } else {
            IOUtils.closeQuietly(raf, log);
        }
        return locked;
    }
Acquires an exclusive lock on the specified file, creating the file as
necessary. Caller of this method is responsible to call the
{@link #unlock(File)} method to prevent release leakage.

@return true if the locking is successful; false otherwise.

@throws FileLockException if we failed to lock the file
aws-java-sdk-s3/src/main/java/com/amazonaws/services/s3/internal/FileLocks.java