From e22adfb633e6de819ce3f766461bbb5b6c0457a6 Mon Sep 17 00:00:00 2001
From: brinn <brinn>
Date: Thu, 5 Mar 2015 13:00:28 +0000
Subject: [PATCH] Add options to remove old resource temporary files on copying
 new ones and use it for removing old native library temp files (avoid filling
 up the temp directory under Windows where mandatory locks prevent file
 deletion of temp files on shutdown).

SVN: 33566
---
 .../utilities/NativeLibraryUtilities.java     |  4 +-
 .../base/utilities/ResourceUtilities.java     | 57 +++++++++++++++++++
 2 files changed, 60 insertions(+), 1 deletion(-)

diff --git a/base/source/java/ch/systemsx/cisd/base/utilities/NativeLibraryUtilities.java b/base/source/java/ch/systemsx/cisd/base/utilities/NativeLibraryUtilities.java
index 785e662d6c7..e336aeb6307 100644
--- a/base/source/java/ch/systemsx/cisd/base/utilities/NativeLibraryUtilities.java
+++ b/base/source/java/ch/systemsx/cisd/base/utilities/NativeLibraryUtilities.java
@@ -141,8 +141,10 @@ public final class NativeLibraryUtilities
      */
     public static String tryCopyNativeLibraryToTempFile(final String libraryName)
     {
+        // Request clean-up of old native library temp files as under Windows the files are locked and 
+        // cannot be deleted on regular shutdown. 
         return ResourceUtilities.tryCopyResourceToTempFile(getLibPath("/native", libraryName),
-                libraryName, ".so");
+                libraryName, ".so", true);
     }
 
     private static String getLibPath(final String prefix, final String libraryName)
diff --git a/base/source/java/ch/systemsx/cisd/base/utilities/ResourceUtilities.java b/base/source/java/ch/systemsx/cisd/base/utilities/ResourceUtilities.java
index 50b8c6e31b6..404c75464bd 100644
--- a/base/source/java/ch/systemsx/cisd/base/utilities/ResourceUtilities.java
+++ b/base/source/java/ch/systemsx/cisd/base/utilities/ResourceUtilities.java
@@ -18,11 +18,13 @@ package ch.systemsx.cisd.base.utilities;
 
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.FilenameFilter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.filefilter.WildcardFileFilter;
 
 import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel;
 import ch.systemsx.cisd.base.exceptions.IOExceptionUnchecked;
@@ -56,6 +58,29 @@ public class ResourceUtilities
         }
     }
 
+    /**
+     * Tries to copy the resource with the given name to a temporary file.
+     * 
+     * @param resource The name of the resource to copy.
+     * @param prefix The prefix to use for the temporary name.
+     * @param postfix The postfix to use for the temporary name.
+     * @param cleanUpOldResources If <code>true</code>, remove old leftover temporary files for this
+     *            <var>prefix</var> and <var>postfix</var>.
+     * @return The name of the temporary file, or <code>null</code>, if the resource could not be
+     *         copied.
+     */
+    public static String tryCopyResourceToTempFile(final String resource, final String prefix,
+            final String postfix, final boolean cleanUpOldResources)
+    {
+        try
+        {
+            return copyResourceToTempFile(resource, prefix, postfix, cleanUpOldResources);
+        } catch (final Exception ex)
+        {
+            return null;
+        }
+    }
+
     /**
      * Copies the resource with the given name to a temporary file. The file will be deleted on
      * program exit.
@@ -70,6 +95,29 @@ public class ResourceUtilities
     public static String copyResourceToTempFile(final String resource, final String prefix,
             final String postfix) throws IOExceptionUnchecked
     {
+        return copyResourceToTempFile(resource, prefix, postfix, false);
+    }
+
+    /**
+     * Copies the resource with the given name to a temporary file. The file will be deleted on
+     * program exit.
+     * 
+     * @param resource The name of the resource to copy.
+     * @param prefix The prefix to use for the temporary name.
+     * @param postfix The postfix to use for the temporary name.
+     * @param cleanUpOldResources If <code>true</code>, remove old leftover temporary files for this
+     *            <var>prefix</var> and <var>postfix</var>.
+     * @return The name of the temporary file.
+     * @throws IllegalArgumentException If the resource cannot be found in the class path.
+     * @throws IOExceptionUnchecked If an {@link IOException} occurs.
+     */
+    public static String copyResourceToTempFile(final String resource, final String prefix,
+            final String postfix, final boolean cleanUpOldResources) throws IOExceptionUnchecked
+    {
+        if (cleanUpOldResources)
+        {
+            deleteOldResourceTempFiles(prefix, postfix);
+        }
         final InputStream resourceStream = ResourceUtilities.class.getResourceAsStream(resource);
         if (resourceStream == null)
         {
@@ -98,4 +146,13 @@ public class ResourceUtilities
         }
     }
 
+    private static void deleteOldResourceTempFiles(final String prefix, final String postfix)
+    {
+        final FilenameFilter filter = new WildcardFileFilter(prefix + "*" + postfix);
+        for (File file : new File(System.getProperty("java.io.tmpdir")).listFiles(filter))
+        {
+            file.delete();
+        }
+    }
+
 }
-- 
GitLab