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 Jun 17, 2024
1 parent 87fa357 commit 9a6d2d0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 37 deletions.
17 changes: 14 additions & 3 deletions Kitodo/src/main/java/org/kitodo/production/KitodoProduction.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,25 @@ 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
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();
}

Expand Down Expand Up @@ -117,10 +119,19 @@ 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();
if(Objects.nonNull(activeMQDirector)) {
if (Objects.nonNull(activeMQDirector)) {
activeMQDirector.shutDown();
}
}
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 9a6d2d0

Please sign in to comment.