mirror of https://github.com/apache/poi.git
bug 59788: move DefaultTempFileCreationStrategy from TempFile inner class to its own class
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1751185 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
25aebedc69
commit
a2d96fe7e3
|
@ -0,0 +1,99 @@
|
|||
package org.apache.poi.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
/**
|
||||
* Default implementation of the {@link TempFileCreationStrategy} used by {@link TempFile}:
|
||||
* Files are collected into one directory and by default are deleted on exit from the VM.
|
||||
* Files may be manually deleted by user prior to JVM exit.
|
||||
* Files can be kept by defining the system property {@link #KEEP_FILES}.
|
||||
*/
|
||||
public class DefaultTempFileCreationStrategy implements TempFileCreationStrategy {
|
||||
/** Define a constant for this property as it is sometimes mistypes as "tempdir" otherwise */
|
||||
private static final String JAVA_IO_TMPDIR = TempFile.JAVA_IO_TMPDIR;
|
||||
/** To keep files after JVM exit, set the <code>-Dpoi.keep.tmp.files</code> JVM property */
|
||||
public static final String KEEP_FILES = "poi.keep.tmp.files";
|
||||
|
||||
/** The directory where the temporary files will be created (<code>null</code> to use the default directory). */
|
||||
private File dir;
|
||||
|
||||
/**
|
||||
* Creates the strategy so that it creates the temporary files in the default directory.
|
||||
*
|
||||
* @see File#createTempFile(String, String)
|
||||
*/
|
||||
public DefaultTempFileCreationStrategy() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the strategy allowing to set the
|
||||
*
|
||||
* @param dir The directory where the temporary files will be created (<code>null</code> to use the default directory).
|
||||
*
|
||||
* @see File#createTempFile(String, String, File)
|
||||
*/
|
||||
public DefaultTempFileCreationStrategy(File dir) {
|
||||
this.dir = dir;
|
||||
}
|
||||
|
||||
private void createPOIFilesDirectory() throws IOException {
|
||||
// Identify and create our temp dir, if needed
|
||||
// The directory is not deleted, even if it was created by this TempFileCreationStrategy
|
||||
if (dir == null) {
|
||||
String tmpDir = System.getProperty(JAVA_IO_TMPDIR);
|
||||
if (tmpDir == null) {
|
||||
throw new IOException("Systems temporary directory not defined - set the -D"+JAVA_IO_TMPDIR+" jvm property!");
|
||||
}
|
||||
dir = new File(tmpDir, "poifiles");
|
||||
}
|
||||
|
||||
createTempDirectory(dir);
|
||||
}
|
||||
|
||||
private void createTempDirectory(File directory) throws IOException {
|
||||
if (!(directory.exists() || directory.mkdirs()) || !directory.isDirectory()) {
|
||||
throw new IOException("Could not create temporary directory '" + directory + "'");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public File createTempFile(String prefix, String suffix) throws IOException {
|
||||
// Identify and create our temp dir, if needed
|
||||
createPOIFilesDirectory();
|
||||
|
||||
// Generate a unique new filename
|
||||
File newFile = File.createTempFile(prefix, suffix, dir);
|
||||
|
||||
// Set the delete on exit flag, unless explicitly disabled
|
||||
if (System.getProperty(KEEP_FILES) == null) {
|
||||
newFile.deleteOnExit();
|
||||
}
|
||||
|
||||
// All done
|
||||
return newFile;
|
||||
}
|
||||
|
||||
private static final SecureRandom random = new SecureRandom();
|
||||
@Override
|
||||
public File createTempDirectory(String prefix) throws IOException {
|
||||
// Identify and create our temp dir, if needed
|
||||
createPOIFilesDirectory();
|
||||
|
||||
// Generate a unique new filename
|
||||
// FIXME: Java 7+: use java.nio.Files#createTempDirectory
|
||||
final long n = random.nextLong();
|
||||
File newDirectory = new File(dir, prefix + Long.toString(n));
|
||||
createTempDirectory(newDirectory);
|
||||
|
||||
// Set the delete on exit flag, unless explicitly disabled
|
||||
if (System.getProperty(KEEP_FILES) == null) {
|
||||
newDirectory.deleteOnExit();
|
||||
}
|
||||
|
||||
// All done
|
||||
return newDirectory;
|
||||
}
|
||||
}
|
|
@ -19,7 +19,6 @@ package org.apache.poi.util;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
/**
|
||||
* Interface for creating temporary files. Collects them all into one directory by default.
|
||||
|
@ -72,91 +71,7 @@ public final class TempFile {
|
|||
}
|
||||
|
||||
/**
|
||||
* Default implementation of the {@link TempFileCreationStrategy} used by {@link TempFile}:
|
||||
* Files are collected into one directory and by default are deleted on exit from the VM.
|
||||
* Files can be kept by defining the system property <code>poi.keep.tmp.files</code>.
|
||||
* @deprecated POI 3.15 beta 3. Moved to {@link org.apache.poi.util.DefaultTempFileCreationStrategy}.
|
||||
*/
|
||||
public static class DefaultTempFileCreationStrategy implements TempFileCreationStrategy {
|
||||
|
||||
/** The directory where the temporary files will be created (<code>null</code> to use the default directory). */
|
||||
private File dir;
|
||||
|
||||
/**
|
||||
* Creates the strategy so that it creates the temporary files in the default directory.
|
||||
*
|
||||
* @see File#createTempFile(String, String)
|
||||
*/
|
||||
public DefaultTempFileCreationStrategy() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the strategy allowing to set the
|
||||
*
|
||||
* @param dir The directory where the temporary files will be created (<code>null</code> to use the default directory).
|
||||
*
|
||||
* @see File#createTempFile(String, String, File)
|
||||
*/
|
||||
public DefaultTempFileCreationStrategy(File dir) {
|
||||
this.dir = dir;
|
||||
}
|
||||
|
||||
private void createPOIFilesDirectory() throws IOException {
|
||||
// Identify and create our temp dir, if needed
|
||||
// The directory is not deleted, even if it was created by this TempFleCreationStrategy
|
||||
if (dir == null) {
|
||||
String tmpDir = System.getProperty(JAVA_IO_TMPDIR);
|
||||
if (tmpDir == null) {
|
||||
throw new IOException("Systems temporary directory not defined - set the -D"+JAVA_IO_TMPDIR+" jvm property!");
|
||||
}
|
||||
dir = new File(tmpDir, "poifiles");
|
||||
}
|
||||
|
||||
createTempDirectory(dir);
|
||||
}
|
||||
|
||||
private void createTempDirectory(File directory) throws IOException {
|
||||
if (!(directory.exists() || directory.mkdirs()) || !directory.isDirectory()) {
|
||||
throw new IOException("Could not create temporary directory '" + directory + "'");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public File createTempFile(String prefix, String suffix) throws IOException {
|
||||
// Identify and create our temp dir, if needed
|
||||
createPOIFilesDirectory();
|
||||
|
||||
// Generate a unique new filename
|
||||
File newFile = File.createTempFile(prefix, suffix, dir);
|
||||
|
||||
// Set the delete on exit flag, unless explicitly disabled
|
||||
if (System.getProperty("poi.keep.tmp.files") == null) {
|
||||
newFile.deleteOnExit();
|
||||
}
|
||||
|
||||
// All done
|
||||
return newFile;
|
||||
}
|
||||
|
||||
private static final SecureRandom random = new SecureRandom();
|
||||
@Override
|
||||
public File createTempDirectory(String prefix) throws IOException {
|
||||
// Identify and create our temp dir, if needed
|
||||
createPOIFilesDirectory();
|
||||
|
||||
// Generate a unique new filename
|
||||
// FIXME: Java 7+: use java.nio.Files#createTempDirectory
|
||||
final long n = random.nextLong();
|
||||
File newDirectory = new File(dir, prefix + Long.toString(n));
|
||||
createTempDirectory(newDirectory);
|
||||
|
||||
// Set the delete on exit flag, unless explicitly disabled
|
||||
if (System.getProperty("poi.keep.tmp.files") == null) {
|
||||
newDirectory.deleteOnExit();
|
||||
}
|
||||
|
||||
// All done
|
||||
return newDirectory;
|
||||
}
|
||||
}
|
||||
public static class DefaultTempFileCreationStrategy extends org.apache.poi.util.DefaultTempFileCreationStrategy {}
|
||||
}
|
||||
|
|
|
@ -74,7 +74,7 @@ public class TestTempFile {
|
|||
}
|
||||
|
||||
// reset strategy to re-create the directory
|
||||
TempFile.setTempFileCreationStrategy(new TempFile.DefaultTempFileCreationStrategy());
|
||||
TempFile.setTempFileCreationStrategy(new DefaultTempFileCreationStrategy());
|
||||
|
||||
// check that we can still create a tempfile
|
||||
File testFile = TempFile.createTempFile("test", ".tst");
|
||||
|
@ -128,7 +128,7 @@ public class TestTempFile {
|
|||
|
||||
@Test
|
||||
public void testSetTempFileCreationStrategy() throws IOException {
|
||||
TempFile.setTempFileCreationStrategy(new TempFile.DefaultTempFileCreationStrategy());
|
||||
TempFile.setTempFileCreationStrategy(new DefaultTempFileCreationStrategy());
|
||||
|
||||
// Should be able to create two tempfiles with same prefix and suffix
|
||||
File file1 = TempFile.createTempFile("TestTempFile", ".tst");
|
||||
|
|
Loading…
Reference in New Issue