Skip to content
Snippets Groups Projects
Commit e22adfb6 authored by brinn's avatar brinn
Browse files

Add options to remove old resource temporary files on copying new ones and use...

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
parent 18d880dd
No related merge requests found
...@@ -141,8 +141,10 @@ public final class NativeLibraryUtilities ...@@ -141,8 +141,10 @@ public final class NativeLibraryUtilities
*/ */
public static String tryCopyNativeLibraryToTempFile(final String libraryName) 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), return ResourceUtilities.tryCopyResourceToTempFile(getLibPath("/native", libraryName),
libraryName, ".so"); libraryName, ".so", true);
} }
private static String getLibPath(final String prefix, final String libraryName) private static String getLibPath(final String prefix, final String libraryName)
......
...@@ -18,11 +18,13 @@ package ch.systemsx.cisd.base.utilities; ...@@ -18,11 +18,13 @@ package ch.systemsx.cisd.base.utilities;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import org.apache.commons.io.IOUtils; 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.CheckedExceptionTunnel;
import ch.systemsx.cisd.base.exceptions.IOExceptionUnchecked; import ch.systemsx.cisd.base.exceptions.IOExceptionUnchecked;
...@@ -56,6 +58,29 @@ public class ResourceUtilities ...@@ -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 * Copies the resource with the given name to a temporary file. The file will be deleted on
* program exit. * program exit.
...@@ -70,6 +95,29 @@ public class ResourceUtilities ...@@ -70,6 +95,29 @@ public class ResourceUtilities
public static String copyResourceToTempFile(final String resource, final String prefix, public static String copyResourceToTempFile(final String resource, final String prefix,
final String postfix) throws IOExceptionUnchecked 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); final InputStream resourceStream = ResourceUtilities.class.getResourceAsStream(resource);
if (resourceStream == null) if (resourceStream == null)
{ {
...@@ -98,4 +146,13 @@ public class ResourceUtilities ...@@ -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();
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment