| 1 | package com.reallifedeveloper.common.infrastructure.jmx; | |
| 2 | ||
| 3 | import static com.reallifedeveloper.common.domain.LogUtil.removeCRLF; | |
| 4 | ||
| 5 | import java.io.InputStream; | |
| 6 | import java.util.jar.Attributes; | |
| 7 | import java.util.jar.Manifest; | |
| 8 | ||
| 9 | import org.checkerframework.checker.nullness.qual.Nullable; | |
| 10 | import org.slf4j.Logger; | |
| 11 | import org.slf4j.LoggerFactory; | |
| 12 | import org.springframework.jmx.export.annotation.ManagedAttribute; | |
| 13 | import org.springframework.jmx.export.annotation.ManagedResource; | |
| 14 | import org.springframework.web.context.ServletContextAware; | |
| 15 | ||
| 16 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | |
| 17 | import jakarta.servlet.ServletContext; | |
| 18 | ||
| 19 | /** | |
| 20 | * An implementation of the JMX {@link SystemInfoMXBean} interface that reads the system information from a manifest file in a WAR file. | |
| 21 | * <p> | |
| 22 | * This class expects to be configured as a Spring bean in a web application so that the {@link #setServletContext(ServletContext)} method | |
| 23 | * is called automatically. If you want to use this class outside of Spring, you are responsible for calling this method. | |
| 24 | * | |
| 25 | * @author RealLifeDeveloper | |
| 26 | */ | |
| 27 | @ManagedResource(description = "General System Information") | |
| 28 | public class ServletContextSystemInfo implements SystemInfoMXBean, ServletContextAware { | |
| 29 | ||
| 30 | private static final String VERSION_MANIFEST_ENTRY = "Implementation-Version"; | |
| 31 | private static final String BUILD_TIME_MANIFEST_ENTRY = "Build-Time"; | |
| 32 | private static final String SCM_REVISION_MANIFEST_ENTRY = "SCM-Revision"; | |
| 33 | ||
| 34 | private static final Logger LOG = LoggerFactory.getLogger(ServletContextSystemInfo.class); | |
| 35 | ||
| 36 | private @Nullable String version; | |
| 37 | private @Nullable String buildTime; | |
| 38 | private @Nullable String scmRevision; | |
| 39 | ||
| 40 | @Override | |
| 41 | @ManagedAttribute(description = "System version") | |
| 42 | public @Nullable String getVersion() { | |
| 43 |
1
1. getVersion : replaced return value with "" for com/reallifedeveloper/common/infrastructure/jmx/ServletContextSystemInfo::getVersion → KILLED |
return version; |
| 44 | } | |
| 45 | ||
| 46 | @Override | |
| 47 | @ManagedAttribute(description = "Date and time that the system was built") | |
| 48 | public @Nullable String getBuildTime() { | |
| 49 |
1
1. getBuildTime : replaced return value with "" for com/reallifedeveloper/common/infrastructure/jmx/ServletContextSystemInfo::getBuildTime → KILLED |
return buildTime; |
| 50 | } | |
| 51 | ||
| 52 | @Override | |
| 53 | @ManagedAttribute(description = "System revision number in version control") | |
| 54 | public @Nullable String getScmRevision() { | |
| 55 |
1
1. getScmRevision : replaced return value with "" for com/reallifedeveloper/common/infrastructure/jmx/ServletContextSystemInfo::getScmRevision → KILLED |
return scmRevision; |
| 56 | } | |
| 57 | ||
| 58 | @Override | |
| 59 | @SuppressWarnings({ "PMD.AvoidCatchingGenericException", "PMD.LooseCoupling" }) | |
| 60 | @SuppressFBWarnings("REC_CATCH_EXCEPTION") | |
| 61 | public void setServletContext(ServletContext servletContext) { | |
| 62 | try (InputStream in = servletContext.getResourceAsStream("/META-INF/MANIFEST.MF")) { | |
| 63 | Manifest manifest = new Manifest(in); | |
| 64 | Attributes attributes = manifest.getMainAttributes(); | |
| 65 | version = attributes.getValue(VERSION_MANIFEST_ENTRY); | |
| 66 | LOG.info("version={}", removeCRLF(version)); | |
| 67 | buildTime = attributes.getValue(BUILD_TIME_MANIFEST_ENTRY); | |
| 68 | LOG.info("buildTime={}", removeCRLF(buildTime)); | |
| 69 | scmRevision = attributes.getValue(SCM_REVISION_MANIFEST_ENTRY); | |
| 70 | LOG.info("scmRevision={}", removeCRLF(scmRevision)); | |
| 71 | } catch (Exception e) { | |
| 72 | LOG.error("Failed to read META-INF/MANIFEST.MF: ", e); | |
| 73 | } | |
| 74 | } | |
| 75 | ||
| 76 | } | |
Mutations | ||
| 43 |
1.1 |
|
| 49 |
1.1 |
|
| 55 |
1.1 |