You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DirectoryLock.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * SonarQube Runner - API
  3. * Copyright (C) 2011 SonarSource
  4. * sonarqube@googlegroups.com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.runner.cache;
  21. import java.io.IOException;
  22. import java.io.PrintWriter;
  23. import java.io.RandomAccessFile;
  24. import java.io.StringWriter;
  25. import java.nio.channels.FileChannel;
  26. import java.nio.channels.FileLock;
  27. import java.nio.file.Files;
  28. import java.nio.file.Path;
  29. public class DirectoryLock {
  30. static final String LOCK_FILE_NAME = ".sonar_lock";
  31. private final Path lockFilePath;
  32. private final Logger logger;
  33. private RandomAccessFile lockRandomAccessFile;
  34. private FileChannel lockChannel;
  35. private FileLock lockFile;
  36. public DirectoryLock(Path directory, Logger logger) {
  37. this.logger = logger;
  38. this.lockFilePath = directory.resolve(LOCK_FILE_NAME).toAbsolutePath();
  39. }
  40. public String getFileLockName() {
  41. return LOCK_FILE_NAME;
  42. }
  43. public void lock() {
  44. try {
  45. lockRandomAccessFile = new RandomAccessFile(lockFilePath.toFile(), "rw");
  46. lockChannel = lockRandomAccessFile.getChannel();
  47. lockFile = lockChannel.lock(0, 1024, false);
  48. } catch (IOException e) {
  49. throw new IllegalStateException("Failed to create lock in " + lockFilePath.toString(), e);
  50. }
  51. }
  52. public boolean tryLock() {
  53. try {
  54. lockRandomAccessFile = new RandomAccessFile(lockFilePath.toFile(), "rw");
  55. lockChannel = lockRandomAccessFile.getChannel();
  56. lockFile = lockChannel.tryLock(0, 1024, false);
  57. return lockFile != null;
  58. } catch (IOException e) {
  59. throw new IllegalStateException("Failed to create lock in " + lockFilePath.toString(), e);
  60. }
  61. }
  62. public void unlock() {
  63. if (lockFile != null) {
  64. try {
  65. lockFile.release();
  66. lockFile = null;
  67. } catch (IOException e) {
  68. logger.error("Error releasing lock", e);
  69. }
  70. }
  71. if (lockChannel != null) {
  72. try {
  73. lockChannel.close();
  74. lockChannel = null;
  75. } catch (IOException e) {
  76. logger.error("Error closing file channel", e);
  77. }
  78. }
  79. if (lockRandomAccessFile != null) {
  80. try {
  81. lockRandomAccessFile.close();
  82. lockRandomAccessFile = null;
  83. } catch (IOException e) {
  84. logger.error("Error closing file", e);
  85. }
  86. }
  87. try {
  88. Files.delete(lockFilePath);
  89. } catch (IOException e) {
  90. // ignore, as an error happens if another process just started to acquire the same lock
  91. StringWriter errors = new StringWriter();
  92. e.printStackTrace(new PrintWriter(errors));
  93. logger.debug("Couldn't delete lock file: " + lockFilePath.toString() + " " + errors.toString());
  94. }
  95. }
  96. }