Author: fanningpj
Date: Mon Jun 16 10:54:41 2025
New Revision: 1926465

URL: http://svn.apache.org/viewvc?rev=1926465&view=rev
Log:
[bug-69714] add thread local support for overriding TempFile strategy

Modified:
    poi/trunk/poi/src/main/java/org/apache/poi/util/TempFile.java

Modified: poi/trunk/poi/src/main/java/org/apache/poi/util/TempFile.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/util/TempFile.java?rev=1926465&r1=1926464&r2=1926465&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/util/TempFile.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/util/TempFile.java Mon Jun 16 
10:54:41 2025
@@ -27,6 +27,13 @@ public final class TempFile {
     /** The strategy used by {@link #createTempFile(String, String)} to create 
the temporary files. */
     private static TempFileCreationStrategy strategy = new 
DefaultTempFileCreationStrategy();
 
+    /** If set for the thread, this is used instead of the strategy variable 
above. */
+    private static final ThreadLocal<TempFileCreationStrategy> 
threadLocalStrategy = new ThreadLocal<>();
+    static {
+        // allow to clear all thread-locals via ThreadLocalUtil
+        ThreadLocalUtil.registerCleaner(threadLocalStrategy::remove);
+    }
+
     /** Define a constant for this property as it is sometimes mistyped as 
"tempdir" otherwise */
     public static final String JAVA_IO_TMPDIR = "java.io.tmpdir";
     
@@ -47,6 +54,21 @@ public final class TempFile {
         }
         TempFile.strategy = strategy;
     }
+
+    /**
+     * Configures the strategy used by {@link #createTempFile(String, String)} 
to create the temporary files.
+     *
+     * @param strategy The new strategy to be used to create the temporary 
files.
+     *
+     * @throws IllegalArgumentException When the given strategy is 
<code>null</code>.
+     * @since POI 5.4.2
+     */
+    public static void 
setThreadLocalTempFileCreationStrategy(TempFileCreationStrategy strategy) {
+        if (strategy == null) {
+            throw new IllegalArgumentException("strategy == null");
+        }
+        threadLocalStrategy.set(strategy);
+    }
     
     /**
      * Creates a new and empty temporary file. By default, files are collected 
into one directory and are not
@@ -64,10 +86,15 @@ public final class TempFile {
      * @throws IOException If no temporary file could be created.
      */
     public static File createTempFile(String prefix, String suffix) throws 
IOException {
-        return strategy.createTempFile(prefix, suffix);
+        return getStrategy().createTempFile(prefix, suffix);
     }
     
     public static File createTempDirectory(String name) throws IOException {
-        return strategy.createTempDirectory(name);
+        return getStrategy().createTempDirectory(name);
+    }
+
+    private static TempFileCreationStrategy getStrategy() {
+        TempFileCreationStrategy s = threadLocalStrategy.get();
+        return s == null ? strategy: s;
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to