-
Notifications
You must be signed in to change notification settings - Fork 766
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[github-704] Add UserNameAwareTempFileCreationStrategy. Thanks to Tig…
…erZCoder. This closes #704 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1921154 13f79535-47bb-0310-9956-ffa450edef68
- Loading branch information
Showing
3 changed files
with
135 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
poi/src/main/java/org/apache/poi/util/UserNameAwareTempFileCreationStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
poi/src/test/java/org/apache/poi/util/UserNameAwareTempFileCreationStrategyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
||
} |