poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From one...@apache.org
Subject svn commit: r1751185 - in /poi/trunk/src: java/org/apache/poi/util/DefaultTempFileCreationStrategy.java java/org/apache/poi/util/TempFile.java testcases/org/apache/poi/util/TestTempFile.java
Date Mon, 04 Jul 2016 00:00:20 GMT
Author: onealj
Date: Mon Jul  4 00:00:20 2016
New Revision: 1751185

URL: http://svn.apache.org/viewvc?rev=1751185&view=rev
Log:
bug 59788: move DefaultTempFileCreationStrategy from TempFile inner class to its own class

Added:
    poi/trunk/src/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java   (with props)
Modified:
    poi/trunk/src/java/org/apache/poi/util/TempFile.java
    poi/trunk/src/testcases/org/apache/poi/util/TestTempFile.java

Added: poi/trunk/src/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java?rev=1751185&view=auto
==============================================================================
--- poi/trunk/src/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java (added)
+++ poi/trunk/src/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java Mon Jul  4
00:00:20 2016
@@ -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;
+    }
+}
\ No newline at end of file

Propchange: poi/trunk/src/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: poi/trunk/src/java/org/apache/poi/util/TempFile.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/util/TempFile.java?rev=1751185&r1=1751184&r2=1751185&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/util/TempFile.java (original)
+++ poi/trunk/src/java/org/apache/poi/util/TempFile.java Mon Jul  4 00:00:20 2016
@@ -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
{}
 }

Modified: poi/trunk/src/testcases/org/apache/poi/util/TestTempFile.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/util/TestTempFile.java?rev=1751185&r1=1751184&r2=1751185&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/util/TestTempFile.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/util/TestTempFile.java Mon Jul  4 00:00:20 2016
@@ -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");



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message