From a2d96fe7e3225a0ae843738c45c82412094ad1af Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Mon, 4 Jul 2016 00:00:20 +0000 Subject: [PATCH] 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 --- .../util/DefaultTempFileCreationStrategy.java | 99 +++++++++++++++++++ src/java/org/apache/poi/util/TempFile.java | 89 +---------------- .../org/apache/poi/util/TestTempFile.java | 4 +- 3 files changed, 103 insertions(+), 89 deletions(-) create mode 100644 src/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java diff --git a/src/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java b/src/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java new file mode 100644 index 0000000000..0ed08c15d7 --- /dev/null +++ b/src/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java @@ -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 -Dpoi.keep.tmp.files JVM property */ + public static final String KEEP_FILES = "poi.keep.tmp.files"; + + /** The directory where the temporary files will be created (null 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 (null 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; + } +} \ No newline at end of file diff --git a/src/java/org/apache/poi/util/TempFile.java b/src/java/org/apache/poi/util/TempFile.java index b2b9c56bd4..ceb7c1d1c6 100644 --- a/src/java/org/apache/poi/util/TempFile.java +++ b/src/java/org/apache/poi/util/TempFile.java @@ -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 poi.keep.tmp.files. + * @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 (null 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 (null 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 {} } diff --git a/src/testcases/org/apache/poi/util/TestTempFile.java b/src/testcases/org/apache/poi/util/TestTempFile.java index aff7e2d6db..498b1daf8c 100644 --- a/src/testcases/org/apache/poi/util/TestTempFile.java +++ b/src/testcases/org/apache/poi/util/TestTempFile.java @@ -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"); -- 2.39.5