From 30f6511b9edc815c2f4d4e28f0036e098d8b67cf Mon Sep 17 00:00:00 2001
From: ribeaudc <ribeaudc>
Date: Sat, 20 Oct 2007 09:48:15 +0000
Subject: [PATCH] change: - RenamingProcess moved to common/FileRenamingProcess
 - datamover uses FileRenamingProcess

SVN: 2219
---
 .../common/utilities/FileRenamingProcess.java | 111 ++++++++++++++++++
 .../filesystem/RetryingPathMover.java         |  39 +-----
 2 files changed, 117 insertions(+), 33 deletions(-)
 create mode 100644 common/source/java/ch/systemsx/cisd/common/utilities/FileRenamingProcess.java

diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/FileRenamingProcess.java b/common/source/java/ch/systemsx/cisd/common/utilities/FileRenamingProcess.java
new file mode 100644
index 00000000000..c2f12f3bc5c
--- /dev/null
+++ b/common/source/java/ch/systemsx/cisd/common/utilities/FileRenamingProcess.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright 2007 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.common.utilities;
+
+import java.io.File;
+
+import org.apache.log4j.Logger;
+
+import ch.systemsx.cisd.common.logging.LogCategory;
+import ch.systemsx.cisd.common.logging.LogFactory;
+
+/**
+ * A <code>IProcess</code> implementation for file renaming.
+ * 
+ * @author Christian Ribeaud
+ */
+public final class FileRenamingProcess implements IProcess
+{
+    private static final Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION, FileRenamingProcess.class);
+
+    public static final long DEFAULT_MILLIS_TO_SLEEP = 5000L;
+
+    public static final int DEFAULT_MAX_RETRIES = 12;
+
+    private final File sourceFile;
+
+    private final File destinationFile;
+
+    private final int maxRetries;
+
+    private final long millisToSleep;
+
+    private int failures;
+
+    private boolean renamed;
+
+    public FileRenamingProcess(final File sourceFile, final File destinationFile)
+    {
+        this(DEFAULT_MAX_RETRIES, DEFAULT_MILLIS_TO_SLEEP, sourceFile, destinationFile);
+    }
+
+    public FileRenamingProcess(final int maxRetries, final long millisToSleep, final File sourceFile,
+            final File destinationFile)
+    {
+        this.sourceFile = sourceFile;
+        this.maxRetries = maxRetries;
+        this.millisToSleep = millisToSleep;
+        this.destinationFile = destinationFile;
+    }
+
+    /**
+     * Whether the file has been renamed.
+     * <p>
+     * This is the return value of {@link File#renameTo(File)}.
+     * </p>
+     */
+    public final boolean isRenamed()
+    {
+        return renamed;
+    }
+
+    //
+    // IProcess
+    //
+
+    public final int getMaxRetryOnFailure()
+    {
+        return maxRetries;
+    }
+
+    public final long getMillisToSleepOnFailure()
+    {
+        return millisToSleep;
+    }
+
+    public final void run()
+    {
+        renamed = sourceFile.renameTo(destinationFile);
+    }
+
+    public final boolean succeeded()
+    {
+        if (renamed == false)
+        {
+            if (sourceFile.exists() == false)
+            {
+                operationLog.error(String.format("Path '%s' doesn't exist, so it can't be moved to '%s'.", sourceFile,
+                        destinationFile));
+                // Nothing to do here. So exit the looping by returning true.
+                return true;
+            }
+            operationLog.warn(String.format("Moving path '%s' to directory '%s' failed (attempt %d).", sourceFile,
+                    destinationFile, ++failures));
+        }
+        return true;
+    }
+}
\ No newline at end of file
diff --git a/datamover/source/java/ch/systemsx/cisd/datamover/filesystem/RetryingPathMover.java b/datamover/source/java/ch/systemsx/cisd/datamover/filesystem/RetryingPathMover.java
index edcc3d6f649..55d18a50a18 100644
--- a/datamover/source/java/ch/systemsx/cisd/datamover/filesystem/RetryingPathMover.java
+++ b/datamover/source/java/ch/systemsx/cisd/datamover/filesystem/RetryingPathMover.java
@@ -24,7 +24,9 @@ import org.apache.log4j.Logger;
 
 import ch.systemsx.cisd.common.logging.LogCategory;
 import ch.systemsx.cisd.common.logging.LogFactory;
+import ch.systemsx.cisd.common.utilities.FileRenamingProcess;
 import ch.systemsx.cisd.common.utilities.FileUtilities;
+import ch.systemsx.cisd.common.utilities.ProcessRunner;
 import ch.systemsx.cisd.datamover.filesystem.intf.IPathMover;
 
 /**
@@ -67,39 +69,10 @@ class RetryingPathMover implements IPathMover
             operationLog.info(String.format("Moving path '%s' to '%s'", sourcePath.getPath(), destinationPath));
         }
         File destFile = new File(destinationPath);
-        int failures = 0;
-        boolean movedOK = false;
-        while (true)
-        {
-            movedOK = sourcePath.renameTo(destFile);
-            if (movedOK)
-            {
-                break;
-            } else
-            {
-                if (sourcePath.exists() == false)
-                {
-                    operationLog.error(String.format("Path '%s' doesn't exist, so it can't be moved to '%s'.",
-                            sourcePath, destinationDirectory));
-                    break;
-                }
-                ++failures;
-                operationLog.warn(String.format("Moving path '%s' to directory '%s' failed (attempt %d).", sourcePath,
-                        destinationDirectory, failures));
-                if (failures >= maxRetriesOnFailure)
-                {
-                    break;
-                }
-                try
-                {
-                    Thread.sleep(millisToSleepOnFailure);
-                } catch (InterruptedException ex)
-                {
-                    break;
-                }
-            }
-        }
-        if (movedOK == false)
+        final FileRenamingProcess process =
+                new FileRenamingProcess(maxRetriesOnFailure, millisToSleepOnFailure, sourcePath, destFile);
+        new ProcessRunner(process);
+        if (process.isRenamed() == false)
         {
             notificationLog.error(String.format("Moving path '%s' to directory '%s' failed, giving up.", sourcePath,
                     destinationDirectory));
-- 
GitLab