From 7e21c4081806f9bd91c00b7e3b4857041e1c53af Mon Sep 17 00:00:00 2001
From: cramakri <cramakri>
Date: Thu, 19 Jan 2012 14:35:43 +0000
Subject: [PATCH] MINOR: Made pre-storage filename generation more robust

SVN: 24237
---
 .../DssRegistrationLogDirectoryHelper.java    | 25 +-----
 .../etlserver/DssUniqueFilenameGenerator.java | 79 +++++++++++++++++++
 ...tOmniscientTopLevelDataSetRegistrator.java |  8 +-
 .../systemtests/DssComponentTest.java         |  4 +-
 4 files changed, 89 insertions(+), 27 deletions(-)
 create mode 100644 datastore_server/source/java/ch/systemsx/cisd/etlserver/DssUniqueFilenameGenerator.java

diff --git a/datastore_server/source/java/ch/systemsx/cisd/etlserver/DssRegistrationLogDirectoryHelper.java b/datastore_server/source/java/ch/systemsx/cisd/etlserver/DssRegistrationLogDirectoryHelper.java
index d0afffead17..783ef45bb32 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/etlserver/DssRegistrationLogDirectoryHelper.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/etlserver/DssRegistrationLogDirectoryHelper.java
@@ -18,9 +18,6 @@ package ch.systemsx.cisd.etlserver;
 
 import java.io.File;
 import java.io.IOException;
-import java.util.GregorianCalendar;
-
-import org.apache.commons.lang.time.DateFormatUtils;
 
 import ch.systemsx.cisd.base.exceptions.IOExceptionUnchecked;
 import ch.systemsx.cisd.common.filesystem.IFileOperations;
@@ -102,27 +99,7 @@ public class DssRegistrationLogDirectoryHelper
      */
     String generateLogFileName(String name, String threadName)
     {
-        String sectionSeparator = "_";
-
-        // The log file name is YYYY-MM-DD_HH-mm-ss-SSS_threadName_name.log
-        StringBuilder logFilename = new StringBuilder();
-        GregorianCalendar calendar = new GregorianCalendar();
-
-        String dateSection = DateFormatUtils.ISO_DATE_FORMAT.format(calendar);
-        logFilename.append(dateSection);
-        logFilename.append(sectionSeparator);
-
-        String timeSection = DateFormatUtils.format(calendar, "HH-mm-ss-SSS");
-        logFilename.append(timeSection);
-        logFilename.append(sectionSeparator);
-
-        logFilename.append(threadName);
-        logFilename.append(sectionSeparator);
-
-        logFilename.append(name);
-        logFilename.append(".log");
-
-        return logFilename.toString();
+        return new DssUniqueFilenameGenerator(threadName, name, ".log").generateFilename();
     }
 
     private void createDirectoryIfNecessary(File dir)
diff --git a/datastore_server/source/java/ch/systemsx/cisd/etlserver/DssUniqueFilenameGenerator.java b/datastore_server/source/java/ch/systemsx/cisd/etlserver/DssUniqueFilenameGenerator.java
new file mode 100644
index 00000000000..d8a982d56d8
--- /dev/null
+++ b/datastore_server/source/java/ch/systemsx/cisd/etlserver/DssUniqueFilenameGenerator.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2012 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.etlserver;
+
+import java.util.GregorianCalendar;
+
+import org.apache.commons.lang.time.DateFormatUtils;
+
+/**
+ * Generate a filename comprised of a timestamp and the information specified in the constructor.
+ * 
+ * @author Chandrasekhar Ramakrishnan
+ */
+public class DssUniqueFilenameGenerator
+{
+    private final String sectionSeparator = "_";
+
+    private final String threadName;
+
+    private final String name;
+
+    private final String extensionOrNull;
+
+    /**
+     * Specify the information used to generate the filename
+     * 
+     * @param threadName
+     * @param name
+     * @param extensionOrNull
+     */
+    public DssUniqueFilenameGenerator(String threadName, String name, String extensionOrNull)
+    {
+        super();
+        this.threadName = threadName;
+        this.name = name;
+        this.extensionOrNull = extensionOrNull;
+    }
+
+    public String generateFilename()
+    {
+
+        // The log file name is YYYY-MM-DD_HH-mm-ss-SSS_threadName_name.log
+        StringBuilder filename = new StringBuilder();
+        GregorianCalendar calendar = new GregorianCalendar();
+
+        String dateSection = DateFormatUtils.ISO_DATE_FORMAT.format(calendar);
+        filename.append(dateSection);
+        filename.append(sectionSeparator);
+
+        String timeSection = DateFormatUtils.format(calendar, "HH-mm-ss-SSS");
+        filename.append(timeSection);
+        filename.append(sectionSeparator);
+
+        filename.append(threadName);
+        filename.append(sectionSeparator);
+
+        filename.append(name);
+        if (null != extensionOrNull)
+        {
+            filename.append(extensionOrNull);
+        }
+        return filename.toString();
+    }
+
+}
diff --git a/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/AbstractOmniscientTopLevelDataSetRegistrator.java b/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/AbstractOmniscientTopLevelDataSetRegistrator.java
index 765be5f9dc6..71970e67d59 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/AbstractOmniscientTopLevelDataSetRegistrator.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/AbstractOmniscientTopLevelDataSetRegistrator.java
@@ -45,6 +45,7 @@ import ch.systemsx.cisd.common.utilities.ExtendedProperties;
 import ch.systemsx.cisd.common.utilities.IDelegatedActionWithResult;
 import ch.systemsx.cisd.etlserver.AbstractTopLevelDataSetRegistrator;
 import ch.systemsx.cisd.etlserver.DataStrategyStore;
+import ch.systemsx.cisd.etlserver.DssUniqueFilenameGenerator;
 import ch.systemsx.cisd.etlserver.IDataStrategyStore;
 import ch.systemsx.cisd.etlserver.IPostRegistrationAction;
 import ch.systemsx.cisd.etlserver.IPreRegistrationAction;
@@ -348,7 +349,12 @@ public abstract class AbstractOmniscientTopLevelDataSetRegistrator<T extends Dat
 
     private File copyIncomingFileToPreStaging(File incomingDataSetFile)
     {
-        File preStagingDir = state.getGlobalState().getPreStagingDir();
+        TopLevelDataSetRegistratorGlobalState globalState = state.getGlobalState();
+        File preStagingRootDir = globalState.getPreStagingDir();
+        String incomingDirName = new DssUniqueFilenameGenerator(globalState.getThreadParameters().getThreadName(), incomingDataSetFile.getName(), null).generateFilename();
+        File preStagingDir = new File(preStagingRootDir, incomingDirName);
+        preStagingDir.mkdir();
+
         // Try to find a hardlink maker
         IImmutableCopier hardlinkMaker = FastRecursiveHardLinkMaker.tryCreate();
         boolean linkWasMade = false;
diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/DssComponentTest.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/DssComponentTest.java
index 1a2f7e4df8a..3e0855da530 100644
--- a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/DssComponentTest.java
+++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/DssComponentTest.java
@@ -139,7 +139,7 @@ public class DssComponentTest extends SystemTestCase
                     "^\\t\\d+-\\d+$",
                     "^\\d{2}:\\d{2}:\\d{2} Data has been moved to the store.$",
                     "^\\d{2}:\\d{2}:\\d{2} Data has been registered with the openBIS Application Server.$",
-                    "^\\d{2}:\\d{2}:\\d{2} Storage processors have committed.$"
+                    "^\\d{2}:\\d{2}:\\d{2} Storage processors have been committed.$"
         };
         checkLogFileContents(logFile, expectedContents);
 
@@ -158,7 +158,7 @@ public class DssComponentTest extends SystemTestCase
         int i = 0;
         for (String expected : expectedContents)
         {
-            assertTrue(logFileContents.get(i), Pattern.matches(expected, logFileContents.get(i++)));
+            assertTrue(expected + ".matches(" + logFileContents.get(i) + ")", Pattern.matches(expected, logFileContents.get(i++)));
         }
     }
 
-- 
GitLab