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
pull/10921/merge
Phillip Webb 7 years ago
parent 144625022c
commit fabf14ff35

@ -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)");
}

@ -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();

Loading…
Cancel
Save