sman-81 commented on code in PR #535:
URL: https://github.com/apache/maven-surefire/pull/535#discussion_r872279362


##########
surefire-api/src/main/java/org/apache/maven/surefire/api/util/TempFileManager.java:
##########
@@ -0,0 +1,242 @@
+package org.apache.maven.surefire.api.util;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.File;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Management of temporary files in surefire with support for sub directories 
of the system's directory
+ * of temporary files.<br>
+ * The {@link File#createTempFile(String, String)} API creates rather 
meaningless file names and
+ * only in the system's temp directory.<br>
+ * This class creates temp files from a prefix, a unique date/timestamp, a 
short file id and suffix.
+ * It also helps you organize temporary files into sub-directories,
+ * and thus avoid bloating the temp directory root.<br>
+ * Apart from that, this class works in much the same way as {@link 
File#createTempFile(String, String)}
+ * and {@link File#deleteOnExit()}, and can be used as a drop-in replacement.
+ *
+ * @author Markus Spann
+ */
+public final class TempFileManager
+{
+
+    private static final Map<File, TempFileManager> INSTANCES  = new 
LinkedHashMap<>();
+    /** An id unique across all file managers used as part of the temporary 
file's base name. */
+    private static final AtomicInteger              FILE_ID    = new 
AtomicInteger( 1 );
+    private static final String                     SUFFIX_TMP = ".tmp";
+
+    private static Thread                           shutdownHook;
+
+    /** The temporary directory or sub-directory under which temporary files 
are created. */
+    private final File                              tempDir;
+    /** The fixed base name used between prefix and suffix of temporary files 
created by this class. */
+    private final String                            baseName;
+
+    /** List of successfully created temporary files. */
+    private final List<File>                        tempFiles;
+
+    /** Temporary files are delete on JVM exit if true. */
+    private boolean                                 deleteOnExit;
+
+    private TempFileManager( File tempDir )
+    {
+        this.tempDir = tempDir;
+        this.baseName = DateTimeFormatter.ofPattern( "yyyyMMddHHmmssSSS" 
).format( LocalDateTime.now() );
+        this.tempFiles = new ArrayList<>();
+    }
+
+    private static File calcTempDir( String subDirName )
+    {
+        String tempDirName = subDirName == null ? null
+                        : subDirName.trim().isEmpty() ? null : 
subDirName.trim();
+        File tempDirCandidate = tempDirName == null ? new File( 
getJavaIoTmpDir() )
+                        : new File( getJavaIoTmpDir(), tempDirName );
+        return tempDirCandidate;
+    }
+
+    public static TempFileManager instance( )
+    {
+        return instance( ( File ) null );
+    }
+
+    /**
+     * Creates an instance using a subdirectory of the system's temporary 
directory.
+     * @param subDirName name of subdirectory
+     * @return instance
+     */
+    public static TempFileManager instance( String subDirName )
+    {
+        return instance( calcTempDir( subDirName ) );
+    }
+
+    public static synchronized TempFileManager instance( File tempDir )
+    {
+
+        // on creation of the first instance, register a shutdown hook to 
delete temporary files of all managers
+        if ( shutdownHook == null )
+        {
+            ThreadGroup tg = Thread.currentThread().getThreadGroup();

Review Comment:
   Do you mean `ShutdownHook` or `ThreadGroup`?
   Both are still part of moderns JDKs such as Java 17 (LTS) and neither marked 
deprecated nor discouraged.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@maven.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to