Rename AppliationTemp.getFolder() to getDir()

Update ApplicationTemp so that `dir` is used in preference to `folder`.
This better aligns with the existing ApplicationHome class.

Fixes gh-4192
This commit is contained in:
Phillip Webb 2015-10-14 23:01:49 -07:00
parent 922f8b6ba6
commit eef5e18eec
4 changed files with 28 additions and 28 deletions

View File

@ -23,7 +23,7 @@ import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Provides access to an application specific temporary folder. Generally speaking
* Provides access to an application specific temporary directory. Generally speaking
* different Spring Boot applications will get different locations, however, simply
* restarting an application will give the same location.
*
@ -36,7 +36,7 @@ public class ApplicationTemp {
private final Class<?> sourceClass;
private volatile File folder;
private volatile File dir;
/**
* Create a new {@link ApplicationTemp} instance.
@ -55,43 +55,43 @@ public class ApplicationTemp {
@Override
public String toString() {
return getFolder().getAbsolutePath();
return getDir().getAbsolutePath();
}
/**
* Return a subfolder of the application temp.
* @param subFolder the subfolder name
* @return a subfolder
* Return a sub-directory of the application temp.
* @param subDir the sub-directory name
* @return a sub-directory
*/
public File getFolder(String subFolder) {
File folder = new File(getFolder(), subFolder);
folder.mkdirs();
return folder;
public File getDir(String subDir) {
File dir = new File(getDir(), subDir);
dir.mkdirs();
return dir;
}
/**
* Return the folder to be used for application specific temp files.
* @return the application temp folder
* Return the directory to be used for application specific temp files.
* @return the application temp directory
*/
public File getFolder() {
if (this.folder == null) {
public File getDir() {
if (this.dir == null) {
synchronized (this) {
byte[] hash = generateHash(this.sourceClass);
this.folder = new File(getTempDirectory(), toHexString(hash));
this.folder.mkdirs();
Assert.state(this.folder.exists(),
"Unable to create temp folder " + this.folder);
this.dir = new File(getTempDirectory(), toHexString(hash));
this.dir.mkdirs();
Assert.state(this.dir.exists(),
"Unable to create temp directory " + this.dir);
}
}
return this.folder;
return this.dir;
}
private File getTempDirectory() {
String property = System.getProperty("java.io.tmpdir");
Assert.state(StringUtils.hasLength(property), "No 'java.io.tmpdir' property set");
File file = new File(property);
Assert.state(file.exists(), "Temp folder " + file + " does not exist");
Assert.state(file.isDirectory(), "Temp location " + file + " is not a folder");
Assert.state(file.exists(), "Temp directory" + file + " does not exist");
Assert.state(file.isDirectory(), "Temp location " + file + " is not a directory");
return file;
}

View File

@ -150,7 +150,7 @@ public abstract class AbstractEmbeddedServletContainerFactory
protected final File getValidSessionStoreDir(boolean mkdirs) {
File dir = getSessionStoreDir();
if (dir == null) {
return new ApplicationTemp().getFolder("servlet-sessions");
return new ApplicationTemp().getDir("servlet-sessions");
}
if (!dir.isAbsolute()) {
dir = new File(new ApplicationHome().getDir(), dir.getPath());

View File

@ -36,17 +36,17 @@ public class ApplicationTempTests {
public void generatesConsistentTemp() throws Exception {
ApplicationTemp t1 = new ApplicationTemp();
ApplicationTemp t2 = new ApplicationTemp();
assertThat(t1.getFolder(), notNullValue());
assertThat(t1.getFolder(), equalTo(t2.getFolder()));
assertThat(t1.getDir(), notNullValue());
assertThat(t1.getDir(), equalTo(t2.getDir()));
}
@Test
public void differentBasedOnUserDir() throws Exception {
String userDir = System.getProperty("user.dir");
try {
File t1 = new ApplicationTemp().getFolder();
File t1 = new ApplicationTemp().getDir();
System.setProperty("user.dir", "abc");
File t2 = new ApplicationTemp().getFolder();
File t2 = new ApplicationTemp().getDir();
assertThat(t1, not(equalTo(t2)));
}
finally {
@ -57,7 +57,7 @@ public class ApplicationTempTests {
@Test
public void getSubFolder() throws Exception {
ApplicationTemp temp = new ApplicationTemp();
assertThat(temp.getFolder("abc"), equalTo(new File(temp.getFolder(), "abc")));
assertThat(temp.getDir("abc"), equalTo(new File(temp.getDir(), "abc")));
}
}

View File

@ -595,7 +595,7 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
AbstractEmbeddedServletContainerFactory factory = getFactory();
File dir = factory.getValidSessionStoreDir(false);
assertThat(dir.getName(), equalTo("servlet-sessions"));
assertThat(dir.getParentFile(), equalTo(new ApplicationTemp().getFolder()));
assertThat(dir.getParentFile(), equalTo(new ApplicationTemp().getDir()));
}
@Test