package org.carlspring.strongbox.booters;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

/**
 * 
 * @author kalski
 *
 */
public class TempDirBooter
{
    private static final Logger logger = LoggerFactory.getLogger(TempDirBooter.class);
    
    @Inject
    private PropertiesBooter propertiesBooter;

    private Path lockFile;
    
    @PostConstruct
    public void initialize()
            throws IOException
    {
        lockFile = Paths.get(propertiesBooter.getVaultDirectory()).resolve("temp-dir-booter.lock");

        if (!lockExists())
        {
            createLockFile();
            createTempDir();
        }
    }
    
    public void createTempDir()
            throws IOException
    {
        String tempDirLocation = System.getProperty("java.io.tmpdir",
                                                    Paths.get(propertiesBooter.getVaultDirectory(), "tmp")
                                                         .toAbsolutePath()
                                                         .toString());
        Path tempDirPath = Paths.get(tempDirLocation).toAbsolutePath();
        if (!Files.exists(tempDirPath))
        {
            Files.createDirectories(tempDirPath);
        }

        logger.debug("Temporary directory: " + tempDirPath.toString() + ".");

        if (System.getProperty("java.io.tmpdir") == null)
        {
            System.setProperty("java.io.tmpdir", tempDirPath.toString());

            logger.debug("Set java.io.tmpdir to " + tempDirPath.toString() + ".");
        }
        else
        {
            logger.debug("The java.io.tmpdir is already set to " + System.getProperty("java.io.tmpdir") + ".");
        }
    }
    
    @SuppressFBWarnings(value = "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
    private void createLockFile()
            throws IOException
    {
        Files.createDirectories(lockFile.getParent());
        Files.createFile(lockFile);

        logger.debug(" -> Created lock file '" + lockFile.toAbsolutePath().toString() + "'...");
    }
    
    private boolean lockExists()
    {
        if (Files.exists(lockFile))
        {
            logger.debug(" -> Lock found: '" + propertiesBooter.getVaultDirectory() + "'!");

            return true;
        }
        else
        {
            logger.debug(" -> No lock found.");

            return false;
        }
    }
    
    @PreDestroy
    public void removeLock()
            throws IOException
    {
        Files.deleteIfExists(lockFile);

        logger.debug("Removed lock file '" + lockFile.toAbsolutePath().toString() + "'.");
    }

}

@@ -191,44 +191,8 @@
 ct;%0A
-import javax.annotation.PreDestroy;%0A
 impo
@@ -285,59 +285,79 @@
 ort 
-edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+com.hazelcast.core.HazelcastInstance;%0Aimport com.hazelcast.core.ILock;%0A
 %0A%0A/*
@@ -576,35 +576,66 @@
     
-private Path lockFile;%0A    
+@Inject%0A    private HazelcastInstance hazelcastInstance;%0A%0A
 %0A   
@@ -728,101 +728,69 @@
     
-lockFile = Paths.get(propertiesBooter.getVaultDirectory()).resolve(%22temp-dir-b
+ILock lock = hazelcastInstance.getLock(%22TempDirB
 ooter
-.l
+L
 ock%22);%0A
-%0A
     
@@ -801,19 +801,20 @@
 if (
-!
 lock
-Exists
+.tryLock
 ())%0A
@@ -839,26 +839,77 @@
     
-createLockFile();%0A
+logger.debug(%22 -%3E No lock found.%22);%0A%0A            try%0A            %7B%0A  
     
@@ -916,16 +916,18 @@
         
+  
 createTe
@@ -947,31 +947,295 @@
     
+    
 %7D%0A    
-%7D%0A    %0A    public
+        finally%0A            %7B%0A                lock.unlock();%0A%0A                logger.debug(%22Removed lock '%22 + lock.getName());%0A            %7D%0A        %7D%0A        else%0A        %7B%0A            logger.debug(%22 -%3E Lock found: '%22 + lock.getName() + %22'!%22);%0A        %7D%0A    %7D%0A    %0A    private
  voi
@@ -2265,910 +2265,6 @@
   %7D%0A
-    %0A    @SuppressFBWarnings(value = %22NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE%22)%0A    private void createLockFile()%0A            throws IOException%0A    %7B%0A        Files.createDirectories(lockFile.getParent());%0A        Files.createFile(lockFile);%0A%0A        logger.debug(%22 -%3E Created lock file '%22 + lockFile.toAbsolutePath().toString() + %22'...%22);%0A    %7D%0A    %0A    private boolean lockExists()%0A    %7B%0A        if (Files.exists(lockFile))%0A        %7B%0A            logger.debug(%22 -%3E Lock found: '%22 + propertiesBooter.getVaultDirectory() + %22'!%22);%0A%0A            return true;%0A        %7D%0A        else%0A        %7B%0A            logger.debug(%22 -%3E No lock found.%22);%0A%0A            return false;%0A        %7D%0A    %7D%0A    %0A    @PreDestroy%0A    public void removeLock()%0A            throws IOException%0A    %7B%0A        Files.deleteIfExists(lockFile);%0A%0A        logger.debug(%22Removed lock file '%22 + lockFile.toAbsolutePath().toString() + %22'.%22);%0A    %7D%0A%0A
 %7D%0A

add ILock locking instead of lock file