From 9a6d2d0aa8cb405644cc956922f0e3f62c158050 Mon Sep 17 00:00:00 2001 From: Matthias Ronge Date: Wed, 24 Apr 2024 13:21:52 +0200 Subject: [PATCH] Handle version information as part of the application main --- .../kitodo/production/KitodoProduction.java | 17 +++++++++++--- .../org/kitodo/production/KitodoVersion.java | 21 ++++-------------- .../kitodo/production/forms/HelperForm.java | 4 ++-- .../kitodo/production/KitodoVersionTest.java | 22 ++++++------------- 4 files changed, 27 insertions(+), 37 deletions(-) diff --git a/Kitodo/src/main/java/org/kitodo/production/KitodoProduction.java b/Kitodo/src/main/java/org/kitodo/production/KitodoProduction.java index 01455d5e4b1..3c6e2d5900f 100644 --- a/Kitodo/src/main/java/org/kitodo/production/KitodoProduction.java +++ b/Kitodo/src/main/java/org/kitodo/production/KitodoProduction.java @@ -49,23 +49,25 @@ public class KitodoProduction implements ServletContextListener, HttpSessionList private static CompletableFuture instance = new CompletableFuture<>(); private ServletContext context; private Optional manifest = Optional.empty(); + private KitodoVersion version = new KitodoVersion(); private ActiveMQDirector activeMQDirector; @Override public void contextInitialized(ServletContextEvent sce) { // Retrieve Manifest file as Stream context = sce.getServletContext(); - try (InputStream rs = context.getResourceAsStream("/META-INF/MANIFEST.MF")){ + try (InputStream rs = context.getResourceAsStream("/META-INF/MANIFEST.MF")) { // Use Manifest to setup version information if (Objects.nonNull(rs)) { Manifest m = new Manifest(rs); manifest = Optional.of(m); - KitodoVersion.setupFromManifest(m); + version.setupFromManifest(m); } } catch (IOException e) { logger.error(e.getMessage(), e); context.log(e.getMessage()); } + instance.complete(this); startActiveMQ(); } @@ -117,10 +119,19 @@ public Optional getManifest() { return manifest; } + /** + * Returns application version information. + * + * @return version information + */ + public KitodoVersion getVersionInformation() { + return version; + } + @Override public void contextDestroyed(ServletContextEvent sce) { TaskManager.shutdownNow(); - if(Objects.nonNull(activeMQDirector)) { + if (Objects.nonNull(activeMQDirector)) { activeMQDirector.shutDown(); } } diff --git a/Kitodo/src/main/java/org/kitodo/production/KitodoVersion.java b/Kitodo/src/main/java/org/kitodo/production/KitodoVersion.java index e0e955d7d20..d64a81520fe 100644 --- a/Kitodo/src/main/java/org/kitodo/production/KitodoVersion.java +++ b/Kitodo/src/main/java/org/kitodo/production/KitodoVersion.java @@ -17,30 +17,21 @@ public class KitodoVersion { private static String version = "N/A"; - private static String buildVersion = "N/A"; private static String buildDate = "N/A"; - /** - * Private constructor to hide the implicit public one. - */ - private KitodoVersion() { - - } - /** * Setup KitodoVersion form manifest. * * @param manifest as Manifest */ - public static void setupFromManifest(Manifest manifest) { + void setupFromManifest(Manifest manifest) { Attributes mainAttributes = manifest.getMainAttributes(); version = getValueOrThrowException(mainAttributes, "Implementation-Version"); - buildVersion = version; buildDate = getValueOrThrowException(mainAttributes, "Implementation-Build-Date"); } - private static String getValueOrThrowException(Attributes attributes, String attributeName) { + private String getValueOrThrowException(Attributes attributes, String attributeName) { String value = attributes.getValue(attributeName); if (null == value) { throw new IllegalArgumentException( @@ -49,15 +40,11 @@ private static String getValueOrThrowException(Attributes attributes, String att return value; } - public static String getVersion() { + public String getVersion() { return version; } - public static String getBuildVersion() { - return buildVersion; - } - - public static String getBuildDate() { + public String getBuildDate() { return buildDate; } } diff --git a/Kitodo/src/main/java/org/kitodo/production/forms/HelperForm.java b/Kitodo/src/main/java/org/kitodo/production/forms/HelperForm.java index b6c3b2678e6..39d6d6b6026 100644 --- a/Kitodo/src/main/java/org/kitodo/production/forms/HelperForm.java +++ b/Kitodo/src/main/java/org/kitodo/production/forms/HelperForm.java @@ -18,7 +18,7 @@ import org.kitodo.config.ConfigCore; import org.kitodo.config.enums.ParameterCore; -import org.kitodo.production.KitodoVersion; +import org.kitodo.production.KitodoProduction; import org.kitodo.production.helper.Helper; /** @@ -30,7 +30,7 @@ public class HelperForm implements Serializable { public String getVersion() { - return KitodoVersion.getBuildVersion(); + return KitodoProduction.getInstance().getVersionInformation().getVersion(); } public boolean getAnonymized() { diff --git a/Kitodo/src/test/java/org/kitodo/production/KitodoVersionTest.java b/Kitodo/src/test/java/org/kitodo/production/KitodoVersionTest.java index ec0d2bda931..b9f9ad09977 100644 --- a/Kitodo/src/test/java/org/kitodo/production/KitodoVersionTest.java +++ b/Kitodo/src/test/java/org/kitodo/production/KitodoVersionTest.java @@ -24,36 +24,28 @@ public class KitodoVersionTest { @Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionIfKitodoSectionIsMissingInManifest() { - KitodoVersion.setupFromManifest(new Manifest()); + new KitodoVersion().setupFromManifest(new Manifest()); } @Test public void attributeVersionShouldBeEqualToImplementationVersion() { Manifest manifest = createManifestWithValues(); - KitodoVersion.setupFromManifest(manifest); + KitodoVersion version = new KitodoVersion(); + version.setupFromManifest(manifest); assertEquals("Version attribute should be equal to Implementation-Version as specified in the given Manifest.", - VERSION, KitodoVersion.getVersion()); - } - - @Test - public void attributeBuildVersionShouldBeEqualToImplementationVersion() { - Manifest manifest = createManifestWithValues(); - KitodoVersion.setupFromManifest(manifest); - - assertEquals( - "BuildVersion attribute should be equal to Implementation-Version as specified in the given Manifest.", - VERSION, KitodoVersion.getBuildVersion()); + VERSION, version.getVersion()); } @Test public void attributeBuildDateShouldBeEqualToImplementationBuildDate() { Manifest manifest = createManifestWithValues(); - KitodoVersion.setupFromManifest(manifest); + KitodoVersion version = new KitodoVersion(); + version.setupFromManifest(manifest); assertEquals( "BuildDate attribute should be equal to Implementation-Build-Date as specified in the given Manifest.", - BUILD_DATE, KitodoVersion.getBuildDate()); + BUILD_DATE, version.getBuildDate()); } private Manifest createManifestWithValues() {