Refine ApplicationPid for structured logging

Update `ApplicationPid` with `toLong()` and `isAvailable()` methods to
make it easier to use with structured logging.

See gh-41491
This commit is contained in:
Phillip Webb
2024-07-24 17:58:43 +01:00
parent 09fdb9d36c
commit 700c1e8f82
3 changed files with 85 additions and 10 deletions

View File

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

View File

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

View File

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