diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationPid.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationPid.java index 9da813fa1a..0f8a70af5c 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationPid.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationPid.java @@ -38,25 +38,43 @@ public class ApplicationPid { private static final PosixFilePermission[] WRITE_PERMISSIONS = { PosixFilePermission.OWNER_WRITE, PosixFilePermission.GROUP_WRITE, PosixFilePermission.OTHERS_WRITE }; - private final String pid; + private final Long pid; public ApplicationPid() { - this.pid = getPid(); + this.pid = currentProcessPid(); } - protected ApplicationPid(String pid) { + protected ApplicationPid(Long pid) { this.pid = pid; } - private String getPid() { + private Long currentProcessPid() { try { - return Long.toString(ProcessHandle.current().pid()); + return Long.valueOf(ProcessHandle.current().pid()); } catch (Throwable ex) { return null; } } + /** + * Return if the application PID is available. + * @return {@code true} if the PID is available + * @since 3.4.0 + */ + public boolean isAvailable() { + return this.pid != null; + } + + /** + * Return the application PID as a {@link Long}. + * @return the application PID or {@code null} + * @since 3.4.0 + */ + public Long toLong() { + return this.pid; + } + @Override public boolean equals(Object obj) { if (obj == this) { @@ -75,7 +93,7 @@ public class ApplicationPid { @Override public String toString() { - return (this.pid != null) ? this.pid : "???"; + return (this.pid != null) ? String.valueOf(this.pid) : "???"; } /** @@ -91,7 +109,7 @@ public class ApplicationPid { assertCanOverwrite(file); } try (FileWriter writer = new FileWriter(file)) { - writer.append(this.pid); + writer.append(String.valueOf(this.pid)); } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/system/ApplicationPidTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/system/ApplicationPidTests.java index 3c72660ff9..7f1a783efd 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/system/ApplicationPidTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/system/ApplicationPidTests.java @@ -37,7 +37,7 @@ class ApplicationPidTests { @Test void toStringWithPid() { - assertThat(new ApplicationPid("123")).hasToString("123"); + assertThat(new ApplicationPid(123L)).hasToString("123"); } @Test @@ -54,7 +54,7 @@ class ApplicationPidTests { @Test void writePid() throws Exception { - ApplicationPid pid = new ApplicationPid("123"); + ApplicationPid pid = new ApplicationPid(123L); File file = new File(this.tempDir, "pid"); pid.write(file); assertThat(contentOf(file)).isEqualTo("123"); @@ -63,13 +63,37 @@ class ApplicationPidTests { @Test void writeNewPid() throws Exception { // gh-10784 - ApplicationPid pid = new ApplicationPid("123"); + ApplicationPid pid = new ApplicationPid(123L); File file = new File(this.tempDir, "pid"); file.delete(); pid.write(file); assertThat(contentOf(file)).isEqualTo("123"); } + @Test + void toLong() { + ApplicationPid pid = new ApplicationPid(123L); + assertThat(pid.toLong()).isEqualTo(123L); + } + + @Test + void toLongWhenNotAvailable() { + ApplicationPid pid = new ApplicationPid(null); + assertThat(pid.toLong()).isNull(); + } + + @Test + void isAvailableWhenAvailable() { + ApplicationPid pid = new ApplicationPid(123L); + assertThat(pid.isAvailable()).isTrue(); + } + + @Test + void isAvailableWhenNotAvailable() { + ApplicationPid pid = new ApplicationPid(null); + assertThat(pid.isAvailable()).isFalse(); + } + @Test void getPidFromJvm() { assertThat(new ApplicationPid().toString()).isNotEmpty(); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/system/MockApplicationPid.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/system/MockApplicationPid.java new file mode 100644 index 0000000000..a12619cc85 --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/system/MockApplicationPid.java @@ -0,0 +1,33 @@ +/* + * Copyright 2012-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.system; + +/** + * Factory to create mock {@link ApplicationPid} instances for testing. + * + * @author Phillip Webb + */ +public final class MockApplicationPid { + + private MockApplicationPid() { + } + + public static ApplicationPid of(long value) { + return new ApplicationPid(value); + } + +}