Skip to content

Commit 700c1e8

Browse files
committed
Refine ApplicationPid for structured logging
Update `ApplicationPid` with `toLong()` and `isAvailable()` methods to make it easier to use with structured logging. See gh-41491
1 parent 09fdb9d commit 700c1e8

File tree

3 files changed

+85
-10
lines changed

3 files changed

+85
-10
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationPid.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,43 @@ public class ApplicationPid {
3838
private static final PosixFilePermission[] WRITE_PERMISSIONS = { PosixFilePermission.OWNER_WRITE,
3939
PosixFilePermission.GROUP_WRITE, PosixFilePermission.OTHERS_WRITE };
4040

41-
private final String pid;
41+
private final Long pid;
4242

4343
public ApplicationPid() {
44-
this.pid = getPid();
44+
this.pid = currentProcessPid();
4545
}
4646

47-
protected ApplicationPid(String pid) {
47+
protected ApplicationPid(Long pid) {
4848
this.pid = pid;
4949
}
5050

51-
private String getPid() {
51+
private Long currentProcessPid() {
5252
try {
53-
return Long.toString(ProcessHandle.current().pid());
53+
return Long.valueOf(ProcessHandle.current().pid());
5454
}
5555
catch (Throwable ex) {
5656
return null;
5757
}
5858
}
5959

60+
/**
61+
* Return if the application PID is available.
62+
* @return {@code true} if the PID is available
63+
* @since 3.4.0
64+
*/
65+
public boolean isAvailable() {
66+
return this.pid != null;
67+
}
68+
69+
/**
70+
* Return the application PID as a {@link Long}.
71+
* @return the application PID or {@code null}
72+
* @since 3.4.0
73+
*/
74+
public Long toLong() {
75+
return this.pid;
76+
}
77+
6078
@Override
6179
public boolean equals(Object obj) {
6280
if (obj == this) {
@@ -75,7 +93,7 @@ public int hashCode() {
7593

7694
@Override
7795
public String toString() {
78-
return (this.pid != null) ? this.pid : "???";
96+
return (this.pid != null) ? String.valueOf(this.pid) : "???";
7997
}
8098

8199
/**
@@ -91,7 +109,7 @@ public void write(File file) throws IOException {
91109
assertCanOverwrite(file);
92110
}
93111
try (FileWriter writer = new FileWriter(file)) {
94-
writer.append(this.pid);
112+
writer.append(String.valueOf(this.pid));
95113
}
96114
}
97115

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/system/ApplicationPidTests.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ApplicationPidTests {
3737

3838
@Test
3939
void toStringWithPid() {
40-
assertThat(new ApplicationPid("123")).hasToString("123");
40+
assertThat(new ApplicationPid(123L)).hasToString("123");
4141
}
4242

4343
@Test
@@ -54,7 +54,7 @@ void throwIllegalStateWritingMissingPid() {
5454

5555
@Test
5656
void writePid() throws Exception {
57-
ApplicationPid pid = new ApplicationPid("123");
57+
ApplicationPid pid = new ApplicationPid(123L);
5858
File file = new File(this.tempDir, "pid");
5959
pid.write(file);
6060
assertThat(contentOf(file)).isEqualTo("123");
@@ -63,13 +63,37 @@ void writePid() throws Exception {
6363
@Test
6464
void writeNewPid() throws Exception {
6565
// gh-10784
66-
ApplicationPid pid = new ApplicationPid("123");
66+
ApplicationPid pid = new ApplicationPid(123L);
6767
File file = new File(this.tempDir, "pid");
6868
file.delete();
6969
pid.write(file);
7070
assertThat(contentOf(file)).isEqualTo("123");
7171
}
7272

73+
@Test
74+
void toLong() {
75+
ApplicationPid pid = new ApplicationPid(123L);
76+
assertThat(pid.toLong()).isEqualTo(123L);
77+
}
78+
79+
@Test
80+
void toLongWhenNotAvailable() {
81+
ApplicationPid pid = new ApplicationPid(null);
82+
assertThat(pid.toLong()).isNull();
83+
}
84+
85+
@Test
86+
void isAvailableWhenAvailable() {
87+
ApplicationPid pid = new ApplicationPid(123L);
88+
assertThat(pid.isAvailable()).isTrue();
89+
}
90+
91+
@Test
92+
void isAvailableWhenNotAvailable() {
93+
ApplicationPid pid = new ApplicationPid(null);
94+
assertThat(pid.isAvailable()).isFalse();
95+
}
96+
7397
@Test
7498
void getPidFromJvm() {
7599
assertThat(new ApplicationPid().toString()).isNotEmpty();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.system;
18+
19+
/**
20+
* Factory to create mock {@link ApplicationPid} instances for testing.
21+
*
22+
* @author Phillip Webb
23+
*/
24+
public final class MockApplicationPid {
25+
26+
private MockApplicationPid() {
27+
}
28+
29+
public static ApplicationPid of(long value) {
30+
return new ApplicationPid(value);
31+
}
32+
33+
}

0 commit comments

Comments
 (0)