this.dir = dir;
}
- // Create our temp dir only once by double-checked locking
- // The directory is not deleted, even if it was created by this TempFileCreationStrategy
- private void createPOIFilesDirectoryIfNecessary() throws IOException {
- // First make sure we recreate the directory if it was not somehow removed by a third party
- if (dir != null && !dir.exists()) {
- dir = null;
- }
- if (dir == null) {
- final String tmpDir = System.getProperty(JAVA_IO_TMPDIR);
- if (tmpDir == null) {
- throw new IOException("System's temporary directory not defined - set the -D" + JAVA_IO_TMPDIR + " jvm property!");
- }
- dirLock.lock();
- try {
- if (dir == null) {
- Path dirPath = Paths.get(tmpDir, POIFILES);
- File fileDir = dirPath.toFile();
- if (fileDir.exists()) {
- if (!fileDir.isDirectory()) {
- throw new IOException("Could not create temporary directory. '" + fileDir + "' exists but is not a directory.");
- }
- dir = fileDir;
- } else {
- dir = Files.createDirectories(dirPath).toFile();
- }
- }
- } finally {
- dirLock.unlock();
- }
- }
- }
-
@Override
public File createTempFile(String prefix, String suffix) throws IOException {
// Identify and create our temp dir, if needed
// All done
return newDirectory;
}
+
+ protected String getJavaIoTmpDir() throws IOException {
+ final String tmpDir = System.getProperty(JAVA_IO_TMPDIR);
+ if (tmpDir == null) {
+ throw new IOException("System's temporary directory not defined - set the -D" + JAVA_IO_TMPDIR + " jvm property!");
+ }
+ return tmpDir;
+ }
+
+ protected Path getPOIFilesDirectoryPath() throws IOException {
+ return Paths.get(getJavaIoTmpDir(), POIFILES);
+ }
+
+ // Create our temp dir only once by double-checked locking
+ // The directory is not deleted, even if it was created by this TempFileCreationStrategy
+ private void createPOIFilesDirectoryIfNecessary() throws IOException {
+ // First make sure we recreate the directory if it was not somehow removed by a third party
+ if (dir != null && !dir.exists()) {
+ dir = null;
+ }
+ if (dir == null) {
+ dirLock.lock();
+ try {
+ if (dir == null) {
+ final Path dirPath = getPOIFilesDirectoryPath();
+ File fileDir = dirPath.toFile();
+ if (fileDir.exists()) {
+ if (!fileDir.isDirectory()) {
+ throw new IOException("Could not create temporary directory. '" + fileDir + "' exists but is not a directory.");
+ }
+ dir = fileDir;
+ } else {
+ dir = Files.createDirectories(dirPath).toFile();
+ }
+ }
+ } finally {
+ dirLock.unlock();
+ }
+ }
+ }
+
}
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.util;\r
+\r
+import java.io.IOException;\r
+import java.nio.file.Path;\r
+import java.nio.file.Paths;\r
+\r
+/**\r
+ * Username-aware subclass of {@link DefaultTempFileCreationStrategy}\r
+ * that avoids permission issues when deploying applications with multiple users on the same server.\r
+ * Other than adding the username to the temporary directory, all other behavior is the same as the superclass.\r
+ *\r
+ * @since POI 5.3.1\r
+ */\r
+public class UserNameAwareTempFileCreationStrategy extends DefaultTempFileCreationStrategy {\r
+\r
+ /**\r
+ * JVM property for the current username.\r
+ */\r
+ private static final String JAVA_PROP_USER_NAME = "user.name";\r
+\r
+ @Override\r
+ protected Path getPOIFilesDirectoryPath() throws IOException {\r
+ final String tmpDir = getJavaIoTmpDir();\r
+ String poifilesDir = POIFILES;\r
+ // Make the default temporary directory contain the username to avoid permission issues\r
+ // when deploying applications on the same server with multiple users\r
+ String username = System.getProperty(JAVA_PROP_USER_NAME);\r
+ if (null != username && !username.isEmpty()) {\r
+ poifilesDir += "_" + username;\r
+ }\r
+ return Paths.get(tmpDir, poifilesDir);\r
+ }\r
+\r
+}\r
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.util;\r
+\r
+import org.junit.jupiter.api.Test;\r
+\r
+import java.io.IOException;\r
+import java.nio.file.Path;\r
+import java.nio.file.Paths;\r
+\r
+import static org.apache.poi.util.DefaultTempFileCreationStrategy.POIFILES;\r
+import static org.junit.jupiter.api.Assertions.assertEquals;\r
+\r
+class UserNameAwareTempFileCreationStrategyTest {\r
+\r
+ @Test\r
+ void getPOIFilesDirectoryPath() throws IOException {\r
+ UserNameAwareTempFileCreationStrategy strategy = new UserNameAwareTempFileCreationStrategy();\r
+ String tmpDir = System.getProperty(TempFile.JAVA_IO_TMPDIR);\r
+ String username = System.getProperty("user.name");\r
+ String expectedPath = Paths.get(tmpDir, POIFILES + "_" + username).toString();\r
+\r
+ Path actualPath = strategy.getPOIFilesDirectoryPath();\r
+\r
+ assertEquals(expectedPath, actualPath.toString());\r
+ }\r
+\r
+}\r