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