From 4b46aaca92d2a33f051e30b3e1a4a5563bdb5565 Mon Sep 17 00:00:00 2001 From: Patrick Dowler Date: Tue, 26 Mar 2024 12:55:52 -0700 Subject: [PATCH] add getLibraryVersion and refactor --- cadc-rest/build.gradle | 2 +- .../java/ca/nrc/cadc/rest/InitAction.java | 73 +++++++++++++++++++ .../java/ca/nrc/cadc/rest/RestAction.java | 20 ++--- 3 files changed, 80 insertions(+), 15 deletions(-) diff --git a/cadc-rest/build.gradle b/cadc-rest/build.gradle index c948a6d2..d83e66e6 100644 --- a/cadc-rest/build.gradle +++ b/cadc-rest/build.gradle @@ -16,7 +16,7 @@ sourceCompatibility = 1.8 group = 'org.opencadc' -version = '1.3.19' +version = '1.3.20' description = 'OpenCADC REST server library' def git_url = 'https://github.com/opencadc/core' diff --git a/cadc-rest/src/main/java/ca/nrc/cadc/rest/InitAction.java b/cadc-rest/src/main/java/ca/nrc/cadc/rest/InitAction.java index cc14fa6e..4a0b98d1 100644 --- a/cadc-rest/src/main/java/ca/nrc/cadc/rest/InitAction.java +++ b/cadc-rest/src/main/java/ca/nrc/cadc/rest/InitAction.java @@ -67,6 +67,8 @@ package ca.nrc.cadc.rest; +import ca.nrc.cadc.util.StringUtil; +import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.Map; @@ -135,6 +137,77 @@ protected URL getResource(String resource) throws MalformedURLException { return servletContext.getResource(resource); } + /** + * Read the VERSION file and extract the semantic version number from a line + * with VER=1.2.3 + * + * @return version or null if VERSION file not found or does not have expected content + */ + protected Version getVersionFromResource() { + try { + URL resURL = getResource("VERSION"); + return getVersionFromResource(resURL); + } catch (Exception ex) { + log.warn("failed to extract version from VERSION file: " + ex); + } + return null; + } + + static Version getVersionFromResource(URL resURL) throws IOException { + if (resURL != null) { + String versionFileContent = StringUtil.readFromInputStream(resURL.openStream(), "UTF-8"); + String[] lines = versionFileContent.split("\n"); + for (String s : lines) { + if (s.startsWith("VER=")) { + String ver = s.substring(4); + return new Version(ver); + } + } + } + return null; + } + + /** + * Get the library version that provides the specified class. + * + * @param probe class to search for + * @return library version in {name}-{major}.{minor} form + */ + protected static Version getLibraryVersion(Class probe) { + String ret = "no-version-found"; + String rname = probe.getSimpleName() + ".class"; + try { + + URL resURL = probe.getResource(rname); + log.debug("library URL: " + resURL); + // assume maven-central naming conventions for jar + // jar:file:/path/to/{library}-{ver}.jar!/package/subpackage/{rname} + if (resURL != null) { + String[] parts = resURL.toExternalForm().split("[:!]"); + int i = 0; + for (String p : parts) { + if (p.endsWith(".jar")) { + int s = p.lastIndexOf('/'); + String ver = p.substring(s + 1); // {library}-{ver}.jar + ver = ver.replace(".jar", ""); // {library}-{ver} + + // extract {major}.{minor} only + String[] mmp = ver.split("\\."); + if (mmp.length > 2) { + ret = mmp[0] + "." + mmp[1]; + } else { + ret = ver; + } + } + } + } + } catch (Exception ex) { + log.error("failed to find version for " + rname + " from classpath", ex); + } + + return new Version(ret); + } + /** * Initialisation implemented by subclass. This method gets called once during startup/deployment. */ diff --git a/cadc-rest/src/main/java/ca/nrc/cadc/rest/RestAction.java b/cadc-rest/src/main/java/ca/nrc/cadc/rest/RestAction.java index f191a383..abccc778 100644 --- a/cadc-rest/src/main/java/ca/nrc/cadc/rest/RestAction.java +++ b/cadc-rest/src/main/java/ca/nrc/cadc/rest/RestAction.java @@ -83,7 +83,6 @@ import ca.nrc.cadc.net.TransientException; import ca.nrc.cadc.reg.client.RegistryClient; import ca.nrc.cadc.util.StringUtil; - import java.io.IOException; import java.io.OutputStream; import java.net.MalformedURLException; @@ -92,9 +91,7 @@ import java.security.PrivilegedExceptionAction; import java.security.cert.CertificateException; import java.util.Map; - import javax.servlet.ServletContext; - import org.apache.log4j.Logger; /** @@ -270,22 +267,17 @@ protected String getServerImpl() { protected Version getVersionFromResource() { try { URL resURL = getResource("VERSION"); - if (resURL != null) { - String versionFileContent = StringUtil.readFromInputStream(resURL.openStream(), "UTF-8"); - String[] lines = versionFileContent.split("\n"); - for (String s : lines) { - if (s.startsWith("VER=")) { - String ver = s.substring(4); - return new Version(ver); - } - } - } - } catch (Exception ex) { + return InitAction.getVersionFromResource(resURL); + } catch (Exception ex) { log.warn("failed to extract version from VERSION file: " + ex); } return null; } + protected static Version getLibraryVersion(Class probe) { + return InitAction.getLibraryVersion(probe); + } + /** * Create inline content handler to process non-form data. Non-form data could * be a document or part of a multi-part request). Null return value is allowed