Add Java InfoContributor

See gh-28136
This commit is contained in:
Jonatan Ivanov
2021-09-24 15:33:14 -07:00
committed by Stephane Nicoll
parent 0b58d4858d
commit fc87da7ef3
8 changed files with 311 additions and 1 deletions

View File

@@ -0,0 +1,59 @@
/*
* Copyright 2021-2021 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.actuate.info.java;
/**
* A simple DTO that holds information about the Java environment the application is
* running in.
*
* @author Jonatan Ivanov
* @since 2.6.0
*/
public class JavaInfo {
private final String vendor;
private final String version;
private final JreInfo runtime;
private final VmInfo vm;
public JavaInfo() {
this.vendor = System.getProperty("java.vendor");
this.version = System.getProperty("java.version");
this.runtime = new JreInfo();
this.vm = new VmInfo();
}
public String getVendor() {
return this.vendor;
}
public String getVersion() {
return this.version;
}
public JreInfo getRuntime() {
return this.runtime;
}
public VmInfo getVm() {
return this.vm;
}
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2021-2021 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.actuate.info.java;
import org.springframework.boot.actuate.info.Info.Builder;
import org.springframework.boot.actuate.info.InfoContributor;
/**
* An {@link InfoContributor} that exposes {@link JavaInfo}.
*
* @author Jonatan Ivanov
* @since 2.6.0
*/
public class JavaInfoContributor implements InfoContributor {
private final JavaInfo javaInfo;
public JavaInfoContributor() {
this.javaInfo = new JavaInfo();
}
@Override
public void contribute(Builder builder) {
builder.withDetail("java", this.javaInfo);
}
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright 2021-2021 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.actuate.info.java;
/**
* A simple DTO that holds information about the JRE (Java Runtime Environment) the
* application is running in.
*
* @author Jonatan Ivanov
* @since 2.6.0
*/
public class JreInfo {
private final String name;
private final String version;
public JreInfo() {
this.name = System.getProperty("java.runtime.name");
this.version = System.getProperty("java.runtime.version");
}
public String getName() {
return this.name;
}
public String getVersion() {
return this.version;
}
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2021-2021 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.actuate.info.java;
/**
* A simple DTO that holds information about the JVM (Java Virtual Machine) the
* application is running in.
*
* @author Jonatan Ivanov
* @since 2.6.0
*/
public class VmInfo {
private final String name;
private final String vendor;
private final String version;
public VmInfo() {
this.name = System.getProperty("java.vm.name");
this.vendor = System.getProperty("java.vm.vendor");
this.version = System.getProperty("java.vm.version");
}
public String getName() {
return this.name;
}
public String getVendor() {
return this.vendor;
}
public String getVersion() {
return this.version;
}
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright 2021-2021 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.
*/
/**
* Classes for java info.
*/
package org.springframework.boot.actuate.info.java;

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2021-2021 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.actuate.info.java;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.info.Info;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link JavaInfoContributor}
*
* @author Jonatan Ivanov
*/
class JavaInfoContributorTests {
@Test
void javaInfoShouldBeAdded() {
JavaInfoContributor javaInfoContributor = new JavaInfoContributor();
Info.Builder builder = new Info.Builder();
javaInfoContributor.contribute(builder);
assertThat(builder.build().getDetails().get("java")).isInstanceOf(JavaInfo.class);
JavaInfo javaInfo = (JavaInfo) builder.build().getDetails().get("java");
assertThat(javaInfo.getVendor()).isEqualTo(System.getProperty("java.vendor"));
assertThat(javaInfo.getVersion()).isEqualTo(System.getProperty("java.version"));
assertThat(javaInfo.getRuntime().getName()).isEqualTo(System.getProperty("java.runtime.name"));
assertThat(javaInfo.getRuntime().getVersion()).isEqualTo(System.getProperty("java.runtime.version"));
assertThat(javaInfo.getVm().getName()).isEqualTo(System.getProperty("java.vm.name"));
assertThat(javaInfo.getVm().getVendor()).isEqualTo(System.getProperty("java.vm.vendor"));
assertThat(javaInfo.getVm().getVersion()).isEqualTo(System.getProperty("java.vm.version"));
}
}