Allow ApplicationPid to write to a new file

Update `ApplicationPid` so that "canWrite" is only called for files
that already exist.

See gh-9922
Fixes gh-10784
This commit is contained in:
Phillip Webb 2017-11-03 23:54:31 -07:00
parent 144625022c
commit fabf14ff35
2 changed files with 15 additions and 2 deletions

View File

@ -89,7 +89,9 @@ public class ApplicationPid {
public void write(File file) throws IOException {
Assert.state(this.pid != null, "No PID available");
createParentFolder(file);
assertCanWrite(file);
if (file.exists()) {
assertCanOverwrite(file);
}
try (FileWriter writer = new FileWriter(file)) {
writer.append(this.pid);
}
@ -102,7 +104,7 @@ public class ApplicationPid {
}
}
private void assertCanWrite(File file) throws IOException {
private void assertCanOverwrite(File file) throws IOException {
if (!file.canWrite() || !canWritePosixFile(file)) {
throw new FileNotFoundException(file.toString() + " (permission denied)");
}

View File

@ -68,6 +68,17 @@ public class ApplicationPidTests {
assertThat(actual).isEqualTo("123");
}
@Test
public void writeNewPid() throws Exception {
// gh-10784
ApplicationPid pid = new ApplicationPid("123");
File file = this.temporaryFolder.newFile();
file.delete();
pid.write(file);
String actual = FileCopyUtils.copyToString(new FileReader(file));
assertThat(actual).isEqualTo("123");
}
@Test
public void getPidFromJvm() throws Exception {
assertThat(new ApplicationPid().toString()).isNotEmpty();