From 412f2103ac32245c8c67c90d683c6859463dca33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz> Date: Tue, 19 Jun 2018 01:07:13 +0200 Subject: [PATCH] fix: lock workspace --- .../commands/FileLock.java | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/commands/FileLock.java b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/commands/FileLock.java index 7310922b..8cc79da1 100644 --- a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/commands/FileLock.java +++ b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/commands/FileLock.java @@ -17,11 +17,11 @@ public class FileLock implements Closeable { public static final Logger log = LoggerFactory.getLogger(cz.it4i.fiji.haas_spim_benchmark.commands.FileLock.class); private final Path localPath; - + private FileChannel fileChannel; private java.nio.channels.FileLock lock; - + public FileLock(Path lockPath) throws FileNotFoundException { this.localPath = lockPath; } @@ -31,11 +31,15 @@ public class FileLock implements Closeable { StandardOpenOption.CREATE); try { this.lock = fileChannel.tryLock(); + if (this.lock != null) { + return true; + } } catch (OverlappingFileLockException e) { - this.fileChannel.close(); - return false; + // IGNORE } - return true; + this.fileChannel.close(); + this.fileChannel = null; + return false; } @Override @@ -47,10 +51,12 @@ public class FileLock implements Closeable { log.error(e.getMessage(), e); } } - try { - fileChannel.close(); - } catch (IOException e) { - log.error(e.getMessage(), e); + if (fileChannel != null) { + try { + fileChannel.close(); + } catch (IOException e) { + log.error(e.getMessage(), e); + } } try { Files.delete(localPath); -- GitLab