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:
parent
144625022c
commit
fabf14ff35
|
@ -89,7 +89,9 @@ public class ApplicationPid {
|
||||||
public void write(File file) throws IOException {
|
public void write(File file) throws IOException {
|
||||||
Assert.state(this.pid != null, "No PID available");
|
Assert.state(this.pid != null, "No PID available");
|
||||||
createParentFolder(file);
|
createParentFolder(file);
|
||||||
assertCanWrite(file);
|
if (file.exists()) {
|
||||||
|
assertCanOverwrite(file);
|
||||||
|
}
|
||||||
try (FileWriter writer = new FileWriter(file)) {
|
try (FileWriter writer = new FileWriter(file)) {
|
||||||
writer.append(this.pid);
|
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)) {
|
if (!file.canWrite() || !canWritePosixFile(file)) {
|
||||||
throw new FileNotFoundException(file.toString() + " (permission denied)");
|
throw new FileNotFoundException(file.toString() + " (permission denied)");
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,6 +68,17 @@ public class ApplicationPidTests {
|
||||||
assertThat(actual).isEqualTo("123");
|
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
|
@Test
|
||||||
public void getPidFromJvm() throws Exception {
|
public void getPidFromJvm() throws Exception {
|
||||||
assertThat(new ApplicationPid().toString()).isNotEmpty();
|
assertThat(new ApplicationPid().toString()).isNotEmpty();
|
||||||
|
|
Loading…
Reference in New Issue