This is an automated email from the ASF dual-hosted git repository.

coheigea pushed a commit to branch 3.6.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit 4e46fb605beaf889d35f11cce60e6092d12b1694
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Fri Nov 27 15:27:04 2026 +0000

    Recreate CXF's default temp directory if it is deleted at runtime (#3796)
    
    (cherry picked from commit 0b51ed5d27c55733acf889dc924915f3d7636d28)
---
 .../java/org/apache/cxf/helpers/FileUtils.java     |   4 +-
 .../cxf/helpers/FileUtilsDefaultTempDirTest.java   | 108 +++++++++++++++++++++
 2 files changed, 111 insertions(+), 1 deletion(-)

diff --git a/core/src/main/java/org/apache/cxf/helpers/FileUtils.java 
b/core/src/main/java/org/apache/cxf/helpers/FileUtils.java
index 18a330da139..bcd0c78288b 100644
--- a/core/src/main/java/org/apache/cxf/helpers/FileUtils.java
+++ b/core/src/main/java/org/apache/cxf/helpers/FileUtils.java
@@ -84,7 +84,9 @@ public final class FileUtils {
                 defaultTempDir = f;
             }
         }
-        if (defaultTempDir == null) {
+        // also create a new directory if ours was removed, e.g. by a tmp 
cleaner, while the JVM was running.
+        // A fresh random directory is used rather than recreating the old, 
predictable path in a shared tmpdir
+        if (defaultTempDir == null || !exists(defaultTempDir)) {
             defaultTempDir = createTmpDir(false);
             if (shutdownHook != null) {
                 Runtime.getRuntime().removeShutdownHook(shutdownHook);
diff --git 
a/core/src/test/java/org/apache/cxf/helpers/FileUtilsDefaultTempDirTest.java 
b/core/src/test/java/org/apache/cxf/helpers/FileUtilsDefaultTempDirTest.java
new file mode 100644
index 00000000000..271a8528aa4
--- /dev/null
+++ b/core/src/test/java/org/apache/cxf/helpers/FileUtilsDefaultTempDirTest.java
@@ -0,0 +1,108 @@
+/**
+ * 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.
+ */
+package org.apache.cxf.helpers;
+
+import java.io.File;
+import java.lang.reflect.Field;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for the temp directory CXF creates itself when the TempDirectory 
property is not set.
+ */
+public class FileUtilsDefaultTempDirTest {
+
+    private static final String TEMP_DIR_PROPERTY = FileUtils.class.getName() 
+ ".TempDirectory";
+
+    private Object originalTempDir;
+    private Object originalShutdownHook;
+    private String originalProperty;
+
+    @Before
+    public void setUp() throws Exception {
+        // FileUtils keeps static state, so save it and start each test from a 
clean state
+        originalTempDir = getField("defaultTempDir");
+        originalShutdownHook = getField("shutdownHook");
+        originalProperty = System.getProperty(TEMP_DIR_PROPERTY);
+        System.clearProperty(TEMP_DIR_PROPERTY);
+        setField("defaultTempDir", null);
+        setField("shutdownHook", null);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        if (originalProperty != null) {
+            System.setProperty(TEMP_DIR_PROPERTY, originalProperty);
+        }
+        Thread hook = (Thread)getField("shutdownHook");
+        if (hook != null) {
+            Runtime.getRuntime().removeShutdownHook(hook);
+            hook.run();
+        }
+        setField("defaultTempDir", originalTempDir);
+        setField("shutdownHook", originalShutdownHook);
+    }
+
+    @Test
+    public void testDeletedTempDirIsReplaced() throws Exception {
+        File first = FileUtils.getDefaultTempDir();
+        assertTrue(first.isDirectory());
+        Thread firstHook = (Thread)getField("shutdownHook");
+
+        // simulate a tmp cleaner removing the directory while the JVM is 
running
+        FileUtils.removeDir(first);
+        assertFalse(first.exists());
+
+        File second = FileUtils.getDefaultTempDir();
+        assertTrue(second.isDirectory());
+        assertNotEquals(first, second);
+        // the old hook is replaced by one for the new directory
+        assertNotNull(getField("shutdownHook"));
+        assertFalse(Runtime.getRuntime().removeShutdownHook(firstHook));
+    }
+
+    @Test
+    public void testTempFileCanBeCreatedAfterTempDirIsDeleted() throws 
Exception {
+        File first = FileUtils.getDefaultTempDir();
+        FileUtils.removeDir(first);
+
+        File tempFile = FileUtils.createTempFile("cos", "tmp");
+        assertTrue(tempFile.isFile());
+        assertNotEquals(first, tempFile.getParentFile());
+    }
+
+    private static Object getField(String name) throws Exception {
+        Field f = FileUtils.class.getDeclaredField(name);
+        f.setAccessible(true);
+        return f.get(null);
+    }
+
+    private static void setField(String name, Object value) throws Exception {
+        Field f = FileUtils.class.getDeclaredField(name);
+        f.setAccessible(true);
+        f.set(null, value);
+    }
+}

Reply via email to