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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /**
  20. * Default implementation of the {@link TempFileCreationStrategy} used by {@link TempFile}:
  21. * Files are collected into one directory and by default are deleted on exit from the VM.
  22. * Files may be manually deleted by user prior to JVM exit.
  23. * Files can be kept by defining the system property {@link #DELETE_FILES_ON_EXIT}.
  24. *
  25. * Each file is registered for deletion with the JVM and the temporary directory is not deleted
  26. * after the JVM exits. Files that are created in the poifiles directory outside
  27. * the control of DefaultTempFileCreationStrategy are not deleted.
  28. * See {@link TempFileCreationStrategy} for better strategies for long-running
  29. * processes or limited temporary storage.
  30. */
  31. public class DefaultTempFileCreationStrategy implements TempFileCreationStrategy {
  32. public static final String POIFILES = "poifiles";
  33. /** To use files.deleteOnExit after clean JVM exit, set the <code>-Dpoi.delete.tmp.files.on.exit</code> JVM property */
  34. public static final String DELETE_FILES_ON_EXIT = "poi.delete.tmp.files.on.exit";
  35. /** The directory where the temporary files will be created (<code>null</code> to use the default directory). */
  36. private File dir;
  37. /**
  38. * Creates the strategy so that it creates the temporary files in the default directory.
  39. *
  40. * @see File#createTempFile(String, String)
  41. */
  42. public DefaultTempFileCreationStrategy() {
  43. this(null);
  44. }
  45. /**
  46. * Creates the strategy allowing to set the
  47. *
  48. * @param dir The directory where the temporary files will be created (<code>null</code> to use the default directory).
  49. *
  50. * @see File#createTempFile(String, String, File)
  51. */
  52. public DefaultTempFileCreationStrategy(File dir) {
  53. this.dir = dir;
  54. }
  55. private void createPOIFilesDirectory() throws IOException {
  56. // Identify and create our temp dir, if needed
  57. // The directory is not deleted, even if it was created by this TempFileCreationStrategy
  58. if (dir == null) {
  59. String tmpDir = System.getProperty(JAVA_IO_TMPDIR);
  60. if (tmpDir == null) {
  61. throw new IOException("Systems temporary directory not defined - set the -D"+JAVA_IO_TMPDIR+" jvm property!");
  62. }
  63. dir = new File(tmpDir, POIFILES);
  64. }
  65. createTempDirectory(dir);
  66. }
  67. /**
  68. * Attempt to create a directory, including any necessary parent directories.
  69. * Does nothing if directory already exists.
  70. * The method is synchronized to ensure that multiple threads don't try to create the directory at the same time.
  71. *
  72. * @param directory the directory to create
  73. * @throws IOException if unable to create temporary directory or it is not a directory
  74. */
  75. private synchronized void createTempDirectory(File directory) throws IOException {
  76. // create directory if it doesn't exist
  77. final boolean dirExists = (directory.exists() || directory.mkdirs());
  78. if (!dirExists) {
  79. throw new IOException("Could not create temporary directory '" + directory + "'");
  80. }
  81. else if (!directory.isDirectory()) {
  82. throw new IOException("Could not create temporary directory. '" + directory + "' exists but is not a directory.");
  83. }
  84. }
  85. @Override
  86. public File createTempFile(String prefix, String suffix) throws IOException {
  87. // Identify and create our temp dir, if needed
  88. createPOIFilesDirectory();
  89. // Generate a unique new filename
  90. File newFile = File.createTempFile(prefix, suffix, dir);
  91. // Set the delete on exit flag, but only when explicitly disabled
  92. if (System.getProperty(DELETE_FILES_ON_EXIT) != null) {
  93. newFile.deleteOnExit();
  94. }
  95. // All done
  96. return newFile;
  97. }
  98. /* (non-JavaDoc) Created directory path is <JAVA_IO_TMPDIR>/poifiles/prefix0123456789 */
  99. @Override
  100. public File createTempDirectory(String prefix) throws IOException {
  101. // Identify and create our temp dir, if needed
  102. createPOIFilesDirectory();
  103. // Generate a unique new filename
  104. // FIXME: Java 7+: use java.nio.Files#createTempDirectory
  105. final long n = RandomSingleton.getInstance().nextLong();
  106. File newDirectory = new File(dir, prefix + Long.toString(n));
  107. createTempDirectory(newDirectory);
  108. //this method appears to be only used in tests, so it is probably ok to use deleteOnExit
  109. newDirectory.deleteOnExit();
  110. // All done
  111. return newDirectory;
  112. }
  113. }