Skip to content

Commit

Permalink
add getLibraryVersion and refactor
Browse files Browse the repository at this point in the history
pdowler committed Mar 26, 2024
1 parent fc90af2 commit 4b46aac
Showing 3 changed files with 80 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cadc-rest/build.gradle
Original file line number Diff line number Diff line change
@@ -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'
73 changes: 73 additions & 0 deletions cadc-rest/src/main/java/ca/nrc/cadc/rest/InitAction.java
Original file line number Diff line number Diff line change
@@ -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.
*/
20 changes: 6 additions & 14 deletions cadc-rest/src/main/java/ca/nrc/cadc/rest/RestAction.java
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4b46aac

Please sign in to comment.