Author: fanningpj
Date: Sun Oct  6 17:30:49 2024
New Revision: 1921154

URL: http://svn.apache.org/viewvc?rev=1921154&view=rev
Log:
[github-704] Add UserNameAwareTempFileCreationStrategy. Thanks to TigerZCoder. 
This closes #704

Added:
    
poi/trunk/poi/src/main/java/org/apache/poi/util/UserNameAwareTempFileCreationStrategy.java
    
poi/trunk/poi/src/test/java/org/apache/poi/util/UserNameAwareTempFileCreationStrategyTest.java
Modified:
    
poi/trunk/poi/src/main/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java

Modified: 
poi/trunk/poi/src/main/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java?rev=1921154&r1=1921153&r2=1921154&view=diff
==============================================================================
--- 
poi/trunk/poi/src/main/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java
 (original)
+++ 
poi/trunk/poi/src/main/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java
 Sun Oct  6 17:30:49 2024
@@ -73,38 +73,6 @@ public class DefaultTempFileCreationStra
         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
@@ -137,4 +105,45 @@ public class DefaultTempFileCreationStra
         // 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();
+            }
+        }
+    }
+
 }

Added: 
poi/trunk/poi/src/main/java/org/apache/poi/util/UserNameAwareTempFileCreationStrategy.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/util/UserNameAwareTempFileCreationStrategy.java?rev=1921154&view=auto
==============================================================================
--- 
poi/trunk/poi/src/main/java/org/apache/poi/util/UserNameAwareTempFileCreationStrategy.java
 (added)
+++ 
poi/trunk/poi/src/main/java/org/apache/poi/util/UserNameAwareTempFileCreationStrategy.java
 Sun Oct  6 17:30:49 2024
@@ -0,0 +1,51 @@
+/* ====================================================================
+   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.poi.util;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+/**
+ * Username-aware subclass of {@link DefaultTempFileCreationStrategy}
+ * that avoids permission issues when deploying applications with multiple 
users on the same server.
+ * Other than adding the username to the temporary directory, all other 
behavior is the same as the superclass.
+ *
+ * @since POI 5.3.1
+ */
+public class UserNameAwareTempFileCreationStrategy extends 
DefaultTempFileCreationStrategy {
+
+    /**
+     * JVM property for the current username.
+     */
+    private static final String JAVA_PROP_USER_NAME = "user.name";
+
+    @Override
+    protected Path getPOIFilesDirectoryPath() throws IOException {
+        final String tmpDir = getJavaIoTmpDir();
+        String poifilesDir = POIFILES;
+        // Make the default temporary directory contain the username to avoid 
permission issues
+        // when deploying applications on the same server with multiple users
+        String username = System.getProperty(JAVA_PROP_USER_NAME);
+        if (null != username && !username.isEmpty()) {
+            poifilesDir += "_" + username;
+        }
+        return Paths.get(tmpDir, poifilesDir);
+    }
+
+}

Added: 
poi/trunk/poi/src/test/java/org/apache/poi/util/UserNameAwareTempFileCreationStrategyTest.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/util/UserNameAwareTempFileCreationStrategyTest.java?rev=1921154&view=auto
==============================================================================
--- 
poi/trunk/poi/src/test/java/org/apache/poi/util/UserNameAwareTempFileCreationStrategyTest.java
 (added)
+++ 
poi/trunk/poi/src/test/java/org/apache/poi/util/UserNameAwareTempFileCreationStrategyTest.java
 Sun Oct  6 17:30:49 2024
@@ -0,0 +1,43 @@
+/* ====================================================================
+   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.poi.util;
+
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import static org.apache.poi.util.DefaultTempFileCreationStrategy.POIFILES;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class UserNameAwareTempFileCreationStrategyTest {
+
+    @Test
+    void getPOIFilesDirectoryPath() throws IOException {
+        UserNameAwareTempFileCreationStrategy strategy = new 
UserNameAwareTempFileCreationStrategy();
+        String tmpDir = System.getProperty(TempFile.JAVA_IO_TMPDIR);
+        String username = System.getProperty("user.name");
+        String expectedPath = Paths.get(tmpDir, POIFILES + "_" + 
username).toString();
+
+        Path actualPath = strategy.getPOIFilesDirectoryPath();
+
+        assertEquals(expectedPath, actualPath.toString());
+    }
+
+}



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

Reply via email to