Skip to content

Commit

Permalink
Handle version information as part of the application main
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-ronge committed Apr 24, 2024
1 parent f90b486 commit f93782c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 35 deletions.
11 changes: 10 additions & 1 deletion Kitodo/src/main/java/org/kitodo/production/KitodoProduction.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class KitodoProduction implements ServletContextListener, HttpSessionList
private static CompletableFuture<KitodoProduction> instance = new CompletableFuture<>();
private ServletContext context;
private Optional<Manifest> manifest = Optional.empty();
private KitodoVersion version = new KitodoVersion();
private ActiveMQDirector activeMQDirector;

@Override
Expand All @@ -60,7 +61,7 @@ public void contextInitialized(ServletContextEvent sce) {
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);
Expand Down Expand Up @@ -117,6 +118,14 @@ public Optional<Manifest> getManifest() {
return manifest;
}

/**
* Returns application version information.
* @return version information
*/
public KitodoVersion getVersionInformation() {
return version;
}

@Override
public void contextDestroyed(ServletContextEvent sce) {
TaskManager.shutdownNow();
Expand Down
21 changes: 4 additions & 17 deletions Kitodo/src/main/java/org/kitodo/production/KitodoVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -30,7 +30,7 @@
public class HelperForm implements Serializable {

public String getVersion() {
return KitodoVersion.getBuildVersion();
return KitodoProduction.getInstance().getVersionInformation().getVersion();
}

public boolean getAnonymized() {
Expand Down
22 changes: 7 additions & 15 deletions Kitodo/src/test/java/org/kitodo/production/KitodoVersionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit f93782c

Please sign in to comment.