diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfiguration.java index 92ffca446b4e..f50f73f56cd7 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfiguration.java @@ -20,6 +20,7 @@ import org.springframework.boot.actuate.info.EnvironmentInfoContributor; import org.springframework.boot.actuate.info.GitInfoContributor; import org.springframework.boot.actuate.info.InfoContributor; +import org.springframework.boot.actuate.info.java.JavaInfoContributor; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -40,6 +41,7 @@ * * @author Meang Akira Tanaka * @author Stephane Nicoll + * @author Jonatan Ivanov * @since 2.0.0 */ @Configuration(proxyBeanMethods = false) @@ -77,4 +79,11 @@ public InfoContributor buildInfoContributor(BuildProperties buildProperties) { return new BuildInfoContributor(buildProperties); } + @Bean + @ConditionalOnEnabledInfoContributor("java") + @Order(DEFAULT_ORDER) + public InfoContributor javaInfoContributor() { + return new JavaInfoContributor(); + } + } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfigurationTests.java index efa37a54b0cd..40b7390d4042 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfigurationTests.java @@ -23,9 +23,12 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.actuate.info.BuildInfoContributor; +import org.springframework.boot.actuate.info.EnvironmentInfoContributor; import org.springframework.boot.actuate.info.GitInfoContributor; import org.springframework.boot.actuate.info.Info; import org.springframework.boot.actuate.info.InfoContributor; +import org.springframework.boot.actuate.info.java.JavaInfo; +import org.springframework.boot.actuate.info.java.JavaInfoContributor; import org.springframework.boot.info.BuildProperties; import org.springframework.boot.info.GitProperties; import org.springframework.boot.test.util.TestPropertyValues; @@ -39,6 +42,7 @@ * Tests for {@link InfoContributorAutoConfiguration}. * * @author Stephane Nicoll + * @author Jonatan Ivanov */ class InfoContributorAutoConfigurationTests { @@ -55,7 +59,26 @@ void close() { void disableEnvContributor() { load("management.info.env.enabled:false"); Map beans = this.context.getBeansOfType(InfoContributor.class); - assertThat(beans).hasSize(0); + assertThat(beans).extractingFromEntries(Map.Entry::getValue).allSatisfy( + (infoContributor) -> assertThat(infoContributor).isNotInstanceOf(EnvironmentInfoContributor.class)); + } + + @Test + void disableJavaContributor() { + load("management.info.java.enabled:false"); + Map beans = this.context.getBeansOfType(InfoContributor.class); + assertThat(beans).extractingFromEntries(Map.Entry::getValue).allSatisfy( + (infoContributor) -> assertThat(infoContributor).isNotInstanceOf(JavaInfoContributor.class)); + } + + @Test + void defaultInfoContributorsEnabled() { + load(); + Map beans = this.context.getBeansOfType(InfoContributor.class); + assertThat(beans).hasSize(2).extractingFromEntries(Map.Entry::getValue) + .anySatisfy( + (infoContributor) -> assertThat(infoContributor).isInstanceOf(EnvironmentInfoContributor.class)) + .anySatisfy((infoContributor) -> assertThat(infoContributor).isInstanceOf(JavaInfoContributor.class)); } @Test @@ -129,6 +152,17 @@ void customBuildInfoContributor() { .isSameAs(this.context.getBean("customBuildInfoContributor")); } + @Test + void javaInfoContributor() { + load(); + Map beans = this.context.getBeansOfType(InfoContributor.class); + assertThat(beans).containsKeys("javaInfoContributor"); + Map content = invokeContributor( + this.context.getBean("javaInfoContributor", InfoContributor.class)); + Object javaInfo = content.get("java"); + assertThat(javaInfo).isInstanceOf(JavaInfo.class); + } + private Map invokeContributor(InfoContributor contributor) { Info.Builder builder = new Info.Builder(); contributor.contribute(builder); diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/java/JavaInfo.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/java/JavaInfo.java new file mode 100644 index 000000000000..b9898f155ba5 --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/java/JavaInfo.java @@ -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; + } + +} diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/java/JavaInfoContributor.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/java/JavaInfoContributor.java new file mode 100644 index 000000000000..5c44d9380cbb --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/java/JavaInfoContributor.java @@ -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); + } + +} diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/java/JreInfo.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/java/JreInfo.java new file mode 100644 index 000000000000..1f8dd8132090 --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/java/JreInfo.java @@ -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; + } + +} diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/java/VmInfo.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/java/VmInfo.java new file mode 100644 index 000000000000..76be01f4c37e --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/java/VmInfo.java @@ -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; + } + +} diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/java/package-info.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/java/package-info.java new file mode 100644 index 000000000000..3d53a37834d9 --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/java/package-info.java @@ -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; diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/info/java/JavaInfoContributorTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/info/java/JavaInfoContributorTests.java new file mode 100644 index 000000000000..f17638817cb3 --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/info/java/JavaInfoContributorTests.java @@ -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")); + } + +}