You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultTempFileCreationStrategy.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.util;
  16. import static org.apache.poi.util.TempFile.JAVA_IO_TMPDIR;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.security.SecureRandom;
  20. /**
  21. * Default implementation of the {@link TempFileCreationStrategy} used by {@link TempFile}:
  22. * Files are collected into one directory and by default are deleted on exit from the VM.
  23. * Files may be manually deleted by user prior to JVM exit.
  24. * Files can be kept by defining the system property {@link #KEEP_FILES}.
  25. *
  26. * Each file is registered for deletion with the JVM and the temporary directory is not deleted
  27. * after the JVM exits. Files that are created in the poifiles directory outside
  28. * the control of DefaultTempFileCreationStrategy are not deleted.
  29. * See {@link TempFileCreationStrategy} for better strategies for long-running
  30. * processes or limited temporary storage.
  31. */
  32. public class DefaultTempFileCreationStrategy implements TempFileCreationStrategy {
  33. public static final String POIFILES = "poifiles";
  34. /** To keep files after JVM exit, set the <code>-Dpoi.keep.tmp.files</code> JVM property */
  35. public static final String KEEP_FILES = "poi.keep.tmp.files";
  36. /** random number generator to generate unique filenames */
  37. private static final SecureRandom random = new SecureRandom();
  38. /** The directory where the temporary files will be created (<code>null</code> to use the default directory). */
  39. private File dir;
  40. /**
  41. * Creates the strategy so that it creates the temporary files in the default directory.
  42. *
  43. * @see File#createTempFile(String, String)
  44. */
  45. public DefaultTempFileCreationStrategy() {
  46. this(null);
  47. }
  48. /**
  49. * Creates the strategy allowing to set the
  50. *
  51. * @param dir The directory where the temporary files will be created (<code>null</code> to use the default directory).
  52. *
  53. * @see File#createTempFile(String, String, File)
  54. */
  55. public DefaultTempFileCreationStrategy(File dir) {
  56. this.dir = dir;
  57. }
  58. private void createPOIFilesDirectory() throws IOException {
  59. // Identify and create our temp dir, if needed
  60. // The directory is not deleted, even if it was created by this TempFileCreationStrategy
  61. if (dir == null) {
  62. String tmpDir = System.getProperty(JAVA_IO_TMPDIR);
  63. if (tmpDir == null) {
  64. throw new IOException("Systems temporary directory not defined - set the -D"+JAVA_IO_TMPDIR+" jvm property!");
  65. }
  66. dir = new File(tmpDir, POIFILES);
  67. }
  68. createTempDirectory(dir);
  69. }
  70. /**
  71. * Attempt to create a directory, including any necessary parent directories.
  72. * Does nothing if directory already exists.
  73. * The method is synchronized to ensure that multiple threads don't try to create the directory at the same time.
  74. *
  75. * @param directory the directory to create
  76. * @throws IOException if unable to create temporary directory or it is not a directory
  77. */
  78. private synchronized void createTempDirectory(File directory) throws IOException {
  79. // create directory if it doesn't exist
  80. final boolean dirExists = (directory.exists() || directory.mkdirs());
  81. if (!dirExists) {
  82. throw new IOException("Could not create temporary directory '" + directory + "'");
  83. }
  84. else if (!directory.isDirectory()) {
  85. throw new IOException("Could not create temporary directory. '" + directory + "' exists but is not a directory.");
  86. }
  87. }
  88. @Override
  89. public File createTempFile(String prefix, String suffix) throws IOException {
  90. // Identify and create our temp dir, if needed
  91. createPOIFilesDirectory();
  92. // Generate a unique new filename
  93. File newFile = File.createTempFile(prefix, suffix, dir);
  94. // Set the delete on exit flag, unless explicitly disabled
  95. if (System.getProperty(KEEP_FILES) == null) {
  96. newFile.deleteOnExit();
  97. }
  98. // All done
  99. return newFile;
  100. }
  101. /* (non-JavaDoc) Created directory path is <JAVA_IO_TMPDIR>/poifiles/prefix0123456789 */
  102. @Override
  103. public File createTempDirectory(String prefix) throws IOException {
  104. // Identify and create our temp dir, if needed
  105. createPOIFilesDirectory();
  106. // Generate a unique new filename
  107. // FIXME: Java 7+: use java.nio.Files#createTempDirectory
  108. final long n = random.nextLong();
  109. File newDirectory = new File(dir, prefix + Long.toString(n));
  110. createTempDirectory(newDirectory);
  111. // Set the delete on exit flag, unless explicitly disabled
  112. if (System.getProperty(KEEP_FILES) == null) {
  113. newDirectory.deleteOnExit();
  114. }
  115. // All done
  116. return newDirectory;
  117. }
  118. }