001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info AT sonarsource DOT com
005 *
006 * This program is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.home.cache;
021
022import java.io.IOException;
023import java.io.RandomAccessFile;
024import java.nio.channels.FileChannel;
025import java.nio.channels.FileLock;
026import java.nio.file.Path;
027
028public class DirectoryLock {
029  public static final String LOCK_FILE_NAME = ".sonar_lock";
030  private final Path lockFilePath;
031  private final Logger logger;
032
033  private RandomAccessFile lockRandomAccessFile;
034  private FileChannel lockChannel;
035  private FileLock lockFile;
036
037  public DirectoryLock(Path directory, Logger logger) {
038    this.logger = logger;
039    this.lockFilePath = directory.resolve(LOCK_FILE_NAME).toAbsolutePath();
040  }
041
042  public String getFileLockName() {
043    return LOCK_FILE_NAME;
044  }
045  
046  public void lock() {
047    try {
048      lockRandomAccessFile = new RandomAccessFile(lockFilePath.toFile(), "rw");
049      lockChannel = lockRandomAccessFile.getChannel();
050      lockFile = lockChannel.lock(0, 1024, false);
051    } catch (IOException e) {
052      throw new IllegalStateException("Failed to create lock in " + lockFilePath.toString(), e);
053    }
054  }
055  
056  public boolean tryLock() {
057    try {
058      lockRandomAccessFile = new RandomAccessFile(lockFilePath.toFile(), "rw");
059      lockChannel = lockRandomAccessFile.getChannel();
060      lockFile = lockChannel.tryLock(0, 1024, false);
061
062      return lockFile != null;
063    } catch (IOException e) {
064      throw new IllegalStateException("Failed to create lock in " + lockFilePath.toString(), e);
065    }
066  }
067
068  public void unlock() {
069    if (lockFile != null) {
070      try {
071        lockFile.release();
072        lockFile = null;
073      } catch (IOException e) {
074        logger.error("Error releasing lock", e);
075      }
076    }
077    if (lockChannel != null) {
078      try {
079        lockChannel.close();
080        lockChannel = null;
081      } catch (IOException e) {
082        logger.error("Error closing file channel", e);
083      }
084    }
085    if (lockRandomAccessFile != null) {
086      try {
087        lockRandomAccessFile.close();
088        lockRandomAccessFile = null;
089      } catch (IOException e) {
090        logger.error("Error closing file", e);
091      }
092    }
093  }
094}