From fabf14ff35cf97194859ad3bc939dc9486b49eef Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 3 Nov 2017 23:54:31 -0700 Subject: [PATCH] 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 --- .../java/org/springframework/boot/ApplicationPid.java | 6 ++++-- .../org/springframework/boot/ApplicationPidTests.java | 11 +++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationPid.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationPid.java index bc8bf44f60..287ba63129 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationPid.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationPid.java @@ -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)"); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ApplicationPidTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ApplicationPidTests.java index ced285f490..00cdc37ee5 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ApplicationPidTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ApplicationPidTests.java @@ -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();