From 8755485b48882b9a9caa9c67549b79fc60d0633f Mon Sep 17 00:00:00 2001
From: pkupczyk <pkupczyk>
Date: Mon, 8 Dec 2014 10:12:36 +0000
Subject: [PATCH] SSDM-1290 - CKAN - publication webapp bugfixing - added
 caching of mesh terms in an http session - make a local directory check
 thread safe

SVN: 32989
---
 .../utils/DssPropertyParametersUtil.java      | 22 ++++++++++++++-----
 .../utils/DssPropertyParametersUtilTest.java  | 18 +++++++--------
 2 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/DssPropertyParametersUtil.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/DssPropertyParametersUtil.java
index c01e4cb97df..fbb040268ab 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/DssPropertyParametersUtil.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/DssPropertyParametersUtil.java
@@ -231,19 +231,31 @@ public class DssPropertyParametersUtil
             String dirDescription, String pathKey)
     {
         assertDirExists(fileOperations, dir, dirDescription, pathKey);
-        File emptyTestFileInDir = new File(dir, EMPTY_TEST_FILE.getName());
+
+        File emptyTestFile = null;
+        File emptyTestFileInDir = null;
+
         try
         {
-            fileOperations.createNewFile(EMPTY_TEST_FILE);
-            if (fileOperations.rename(EMPTY_TEST_FILE, emptyTestFileInDir) == false)
+            // make the file name unique so that multiple threads can safely perform this check at the same time
+            emptyTestFile = fileOperations.createTempFile(EMPTY_TEST_FILE.getName(), "");
+            emptyTestFileInDir = new File(dir, emptyTestFile.getName());
+
+            if (fileOperations.rename(emptyTestFile, emptyTestFileInDir) == false)
             {
                 throw createException(NON_LOCAL_DIR_TEMPLATE.createFreshCopy(), dir,
                         dirDescription, pathKey);
             }
         } finally
         {
-            fileOperations.delete(EMPTY_TEST_FILE);
-            fileOperations.delete(emptyTestFileInDir);
+            if (emptyTestFile != null)
+            {
+                fileOperations.delete(emptyTestFile);
+            }
+            if (emptyTestFileInDir != null)
+            {
+                fileOperations.delete(emptyTestFileInDir);
+            }
         }
     }
 
diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/DssPropertyParametersUtilTest.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/DssPropertyParametersUtilTest.java
index f861d825a8c..19bcad7215b 100644
--- a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/DssPropertyParametersUtilTest.java
+++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/DssPropertyParametersUtilTest.java
@@ -64,7 +64,7 @@ public class DssPropertyParametersUtilTest extends AssertJUnit
         File expectedDir = new File(System.getProperty("user.dir"), "dss-tmp");
         prepareForMkdirs(expectedDir);
         prepareForExists(expectedDir);
-        prepareForCreateNewFile(EMPTY_TEST_FILE);
+        prepareForCreateTempFile(EMPTY_TEST_FILE);
         File movedEmptyTestFile = new File(expectedDir, EMPTY_TEST_FILE.getName());
         prepareForRenameFile(EMPTY_TEST_FILE, movedEmptyTestFile, true);
         prepareForDeleteFile(EMPTY_TEST_FILE);
@@ -85,7 +85,7 @@ public class DssPropertyParametersUtilTest extends AssertJUnit
         properties.setProperty(DssPropertyParametersUtil.DSS_TEMP_DIR_PATH, expectedDir.getPath());
         prepareForMkdirs(expectedDir);
         prepareForExists(expectedDir);
-        prepareForCreateNewFile(EMPTY_TEST_FILE);
+        prepareForCreateTempFile(EMPTY_TEST_FILE);
         File movedEmptyTestFile = new File(expectedDir, EMPTY_TEST_FILE.getName());
         prepareForRenameFile(EMPTY_TEST_FILE, movedEmptyTestFile, true);
         prepareForDeleteFile(EMPTY_TEST_FILE);
@@ -130,7 +130,7 @@ public class DssPropertyParametersUtilTest extends AssertJUnit
         properties.setProperty(DssPropertyParametersUtil.DSS_TEMP_DIR_PATH, expectedDir.getPath());
         prepareForMkdirs(expectedDir);
         prepareForExists(expectedDir);
-        prepareForCreateNewFile(EMPTY_TEST_FILE);
+        prepareForCreateTempFile(EMPTY_TEST_FILE);
         File movedEmptyTestFile = new File(expectedDir, EMPTY_TEST_FILE.getName());
         prepareForRenameFile(EMPTY_TEST_FILE, movedEmptyTestFile, false);
         prepareForDeleteFile(EMPTY_TEST_FILE);
@@ -157,7 +157,7 @@ public class DssPropertyParametersUtilTest extends AssertJUnit
         File expectedDir = new File(System.getProperty("user.dir"), "log-registrations");
         prepareForMkdirs(expectedDir);
         prepareForExists(expectedDir);
-        prepareForCreateNewFile(EMPTY_TEST_FILE);
+        prepareForCreateTempFile(EMPTY_TEST_FILE);
         File movedEmptyTestFile = new File(expectedDir, EMPTY_TEST_FILE.getName());
         prepareForRenameFile(EMPTY_TEST_FILE, movedEmptyTestFile, true);
         prepareForDeleteFile(EMPTY_TEST_FILE);
@@ -180,7 +180,7 @@ public class DssPropertyParametersUtilTest extends AssertJUnit
                 expectedDir.getPath());
         prepareForMkdirs(expectedDir);
         prepareForExists(expectedDir);
-        prepareForCreateNewFile(EMPTY_TEST_FILE);
+        prepareForCreateTempFile(EMPTY_TEST_FILE);
         File movedEmptyTestFile = new File(expectedDir, EMPTY_TEST_FILE.getName());
         prepareForRenameFile(EMPTY_TEST_FILE, movedEmptyTestFile, true);
         prepareForDeleteFile(EMPTY_TEST_FILE);
@@ -228,7 +228,7 @@ public class DssPropertyParametersUtilTest extends AssertJUnit
                 expectedDir.getPath());
         prepareForMkdirs(expectedDir);
         prepareForExists(expectedDir);
-        prepareForCreateNewFile(EMPTY_TEST_FILE);
+        prepareForCreateTempFile(EMPTY_TEST_FILE);
         File movedEmptyTestFile = new File(expectedDir, EMPTY_TEST_FILE.getName());
         prepareForRenameFile(EMPTY_TEST_FILE, movedEmptyTestFile, false);
         prepareForDeleteFile(EMPTY_TEST_FILE);
@@ -283,13 +283,13 @@ public class DssPropertyParametersUtilTest extends AssertJUnit
             });
     }
 
-    private void prepareForCreateNewFile(final File file)
+    private void prepareForCreateTempFile(final File file)
     {
         context.checking(new Expectations()
             {
                 {
-                    one(fileOperations).createNewFile(file);
-                    will(returnValue(true));
+                    one(fileOperations).createTempFile(file.getName(), "");
+                    will(returnValue(file));
                 }
             });
     }
-- 
GitLab