View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.api.util;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.UncheckedIOException;
24  import java.time.LocalDateTime;
25  import java.time.format.DateTimeFormatter;
26  import java.util.ArrayList;
27  import java.util.LinkedHashMap;
28  import java.util.List;
29  import java.util.Map;
30  import java.util.concurrent.atomic.AtomicInteger;
31  
32  /**
33   * Management of temporary files in surefire with support for sub directories of the system's directory
34   * of temporary files.<br>
35   * The {@link File#createTempFile(String, String)} API creates rather meaningless file names and
36   * only in the system's temp directory.<br>
37   * This class creates temp files from a prefix, a unique date/timestamp, a short file id and suffix.
38   * It also helps you organize temporary files into sub-directories,
39   * and thus avoid bloating the temp directory root.<br>
40   * Apart from that, this class works in much the same way as {@link File#createTempFile(String, String)}
41   * and {@link File#deleteOnExit()}, and can be used as a drop-in replacement.
42   *
43   * @author Markus Spann
44   */
45  public final class TempFileManager {
46  
47      private static final Map<File, TempFileManager> INSTANCES = new LinkedHashMap<>();
48      /** An id unique across all file managers used as part of the temporary file's base name. */
49      private static final AtomicInteger FILE_ID = new AtomicInteger(1);
50  
51      private static final String SUFFIX_TMP = ".tmp";
52  
53      private static Thread shutdownHook;
54  
55      /** The temporary directory or sub-directory under which temporary files are created. */
56      private final File tempDir;
57      /** The fixed base name used between prefix and suffix of temporary files created by this class. */
58      private final String baseName;
59  
60      /** List of successfully created temporary files. */
61      private final List<File> tempFiles;
62  
63      /** Temporary files are delete on JVM exit if true. */
64      private boolean deleteOnExit;
65  
66      private TempFileManager(File tempDir) {
67          this.tempDir = tempDir;
68          this.baseName = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS").format(LocalDateTime.now());
69          this.tempFiles = new ArrayList<>();
70      }
71  
72      private static File calcTempDir(String subDirName) {
73          String tempDirName = subDirName == null ? null : subDirName.trim().isEmpty() ? null : subDirName.trim();
74          File tempDirCandidate =
75                  tempDirName == null ? new File(getJavaIoTmpDir()) : new File(getJavaIoTmpDir(), tempDirName);
76          return tempDirCandidate;
77      }
78  
79      public static TempFileManager instance() {
80          return instance((File) null);
81      }
82  
83      /**
84       * Creates an instance using a subdirectory of the system's temporary directory.
85       *
86       * @param subDirName name of subdirectory
87       * @return instance
88       */
89      public static TempFileManager instance(String subDirName) {
90          return instance(calcTempDir(subDirName));
91      }
92  
93      public static synchronized TempFileManager instance(File tempDir) {
94  
95          // on creation of the first instance, register a shutdown hook to delete temporary files of all managers
96          if (shutdownHook == null) {
97              ThreadGroup tg = Thread.currentThread().getThreadGroup();
98              while (tg.getParent() != null) {
99                  tg = tg.getParent();
100             }
101             shutdownHook = new Thread(
102                     tg, TempFileManager::shutdownAll, TempFileManager.class.getSimpleName() + "-ShutdownHook");
103             Runtime.getRuntime().addShutdownHook(shutdownHook);
104         }
105 
106         return INSTANCES.computeIfAbsent(tempDir == null ? new File(getJavaIoTmpDir()) : tempDir, TempFileManager::new);
107     }
108 
109     public synchronized void deleteAll() {
110         tempFiles.forEach(File::delete);
111         tempFiles.clear();
112         tempDir.delete();
113     }
114 
115     static void shutdownAll() {
116         INSTANCES.values().stream().filter(TempFileManager::isDeleteOnExit).forEach(TempFileManager::deleteAll);
117     }
118 
119     /**
120      * Returns the temporary directory or sub-directory under which temporary files are created.
121      *
122      * @return temporary directory
123      */
124     public File getTempDir() {
125         return tempDir;
126     }
127 
128     /**
129      * Creates a new, uniquely-named temporary file in this object's {@link #tempDir}
130      * with user-defined prefix and suffix (both may be null or empty and won't be trimmed).
131      * <p>
132      * This method behaves similarly to {@link java.io.File#createTempFile(String, String)} and
133      * may be used as a drop-in replacement.<br>
134      * This method is {@code synchronized} to help ensure uniqueness of temporary files.
135      *
136      * @param prefix optional file name prefix
137      * @param suffix optional file name suffix
138      * @return file object
139      */
140     public synchronized File createTempFile(String prefix, String suffix) {
141         // use only the file name from the supplied prefix
142         prefix = prefix == null ? "" : (new File(prefix)).getName();
143         suffix = suffix == null ? "" : suffix;
144 
145         String name = String.join("-", prefix, baseName + "_" + FILE_ID.getAndIncrement()) + suffix;
146         File tempFile = new File(tempDir, name);
147         if (!name.equals(tempFile.getName())) {
148             throw new UncheckedIOException(new IOException("Unable to create temporary file " + tempFile));
149         }
150 
151         if (tempFile.exists() || tempFiles.contains(tempFile)) {
152             // temporary file already exists? Try another name
153             return createTempFile(prefix, suffix);
154         } else {
155             // create temporary directory
156             if (!tempDir.exists()) {
157                 if (!tempDir.mkdirs()) {
158                     throw new UncheckedIOException(
159                             new IOException("Unable to create temporary directory " + tempDir.getAbsolutePath()));
160                 }
161             }
162 
163             try {
164                 tempFile.createNewFile();
165             } catch (IOException ex) {
166                 throw new UncheckedIOException("Unable to create temporary file " + tempFile.getAbsolutePath(), ex);
167             }
168 
169             tempFiles.add(tempFile);
170             return tempFile;
171         }
172     }
173 
174     public File createTempFile(String prefix) {
175         return createTempFile(prefix, SUFFIX_TMP);
176     }
177 
178     public boolean isDeleteOnExit() {
179         return deleteOnExit;
180     }
181 
182     /**
183      * Instructs this file manager to delete its temporary files during JVM shutdown.<br>
184      * This status can be turned on and off unlike {@link File#deleteOnExit()}.
185      *
186      * @param deleteOnExit delete the file in a shutdown hook on JVM exit
187      */
188     public void setDeleteOnExit(boolean deleteOnExit) {
189         this.deleteOnExit = deleteOnExit;
190     }
191 
192     @Override
193     public String toString() {
194         return String.format(
195                 "%s[tempDir=%s, deleteOnExit=%s, baseName=%s, tempFiles=%d]",
196                 getClass().getSimpleName(), tempDir, deleteOnExit, baseName, tempFiles.size());
197     }
198 
199     /**
200      * Returns the value of system property {@code java.io.tmpdir}, the system's temp directory.
201      *
202      * @return path to system temp directory
203      */
204     public static String getJavaIoTmpDir() {
205         String tmpDir = System.getProperty("java.io.tmpdir");
206         if (!tmpDir.endsWith(File.separator)) {
207             tmpDir += File.separatorChar;
208         }
209         return tmpDir;
210     }
211 }