This is an automated email from the ASF dual-hosted git repository.
coheigea pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/main by this push:
new 0b51ed5d27c Recreate CXF's default temp directory if it is deleted at
runtime (#3796)
0b51ed5d27c is described below
commit 0b51ed5d27c55733acf889dc924915f3d7636d28
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)
---
.../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 6795baf1b71..44894814f1c 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);
+ }
+}