public static FileLock obtainLock(Path dirPath, String lockName) throws IOException {
        if (!Files.isDirectory(dirPath)) {
            Files.createDirectories(dirPath);
        }
        Path lockPath = dirPath.resolve(lockName);

        try {
            Files.createFile(lockPath);
        } catch (IOException ignore) {
            // we must create the file to have a truly canonical path.
            // if it's already created, we don't care. if it can't be created, it will fail below.
        }

        // fails if the lock file does not exist
        final Path realLockPath = lockPath.toRealPath();

        if (!LOCK_HELD.add(realLockPath.toString())) {
            throw new LockException("Lock held by this virtual machine on lock path: " + realLockPath);
        }
        FileChannel channel = null;
        FileLock lock;
        try {
            channel = FileChannel.open(realLockPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
            lock = channel.tryLock();
            if (lock == null) {
                throw new LockException("Lock held by another program on lock path: " + realLockPath);
            }
        } catch (IOException ex) {
            try {
                if (channel != null) {
                    channel.close();
                }
            } catch (Throwable t) {
                // suppress any channel close exceptions
            }

            boolean removed = LOCK_HELD.remove(realLockPath.toString());
            if (!removed) {
                throw new LockException("Lock path was cleared but never marked as held: " + realLockPath, ex);
            }

            throw ex;
        }

        LOCK_MAP.put(lock, realLockPath.toString());
        return lock;
    }
@Test
    public void ObtainLockOnOtherLocked() throws IOException {
        FileLock lock2 = FileLockFactory.obtainLock(lockDir, ".test2");
        assertThat(lock2.isValid(), is(equalTo(true)));
        assertThat(lock2.isShared(), is(equalTo(false)));
    }