Skip to content

Commit

Permalink
cadc-vosi: implement server version in capabilities actions
Browse files Browse the repository at this point in the history
  • Loading branch information
pdowler committed Mar 20, 2024
1 parent 0e8c38e commit 312b5d7
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
5 changes: 5 additions & 0 deletions cadc-vosi/src/main/java/ca/nrc/cadc/vosi/CapGetAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ public CapGetAction() {
super();
}

@Override
protected String getServerImpl() {
return CapInitAction.getVersion(componentID);
}

@Override
protected InlineContentHandler getInlineContentHandler() {
return null;
Expand Down
6 changes: 6 additions & 0 deletions cadc-vosi/src/main/java/ca/nrc/cadc/vosi/CapHeadAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ public CapHeadAction() {
super();
}

@Override
protected String getServerImpl() {
return CapInitAction.getVersion(componentID);
}


@Override
protected InlineContentHandler getInlineContentHandler() {
return null;
Expand Down
68 changes: 66 additions & 2 deletions cadc-vosi/src/main/java/ca/nrc/cadc/vosi/CapInitAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
******************* CANADIAN ASTRONOMY DATA CENTRE *******************
************** CENTRE CANADIEN DE DONNÉES ASTRONOMIQUES **************
*
* (c) 2020. (c) 2020.
* (c) 2024. (c) 2024.
* Government of Canada Gouvernement du Canada
* National Research Council Conseil national de recherches
* Ottawa, Canada, K1A 0R6 Ottawa, Canada, K1A 0R6
Expand Down Expand Up @@ -111,7 +111,7 @@ static Capabilities getTemplate(String componentID) {
static boolean getAuthRequired(String componentID) {
String jndiKey = componentID + ".authRequired";
try {
log.debug("retrieving capabilities template via JNDI: " + jndiKey);
log.debug("retrieving authRequired via JNDI: " + jndiKey);
Context initContext = new InitialContext();
Boolean authRequired = (Boolean) initContext.lookup(jndiKey);
if (authRequired == null) {
Expand All @@ -123,6 +123,18 @@ static boolean getAuthRequired(String componentID) {
}
}

static String getVersion(String componentID) {
String jndiKey = componentID + ".version";
try {
log.debug("retrieving version via JNDI: " + jndiKey);
Context initContext = new InitialContext();
String version = (String) initContext.lookup(jndiKey);
return version;
} catch (Exception ex) {
throw new IllegalStateException("failed to find version via JNDI: init failed", ex);
}
}

@Override
public void doInit() {

Expand Down Expand Up @@ -175,5 +187,57 @@ public void doInit() {
} catch (Exception ex) {
throw new IllegalArgumentException("CONFIG: failed to set authRequired flag", ex);
}

try {
String version = findLibraryVersion(CapInitAction.class);

jndiKey = componentID + ".version";
try {
log.debug("unbinding possible version value");
initContext.unbind(jndiKey);
} catch (NamingException e) {
log.debug("no previously bound value, continuting");
}
initContext.bind(jndiKey, version);
log.info("doInit: version=" + version + " stored via JNDI: " + jndiKey);
} catch (Exception ex) {
throw new IllegalArgumentException("CONFIG: failed to set version flag", ex);
}
}

// TODO: move this code up to cadc-rest or cadc-util
private String findLibraryVersion(Class probe) {
String ret = "no-version-found";
String rname = probe.getSimpleName() + ".class";
try {

URL resURL = probe.getResource(rname);
log.debug("library URL: " + resURL);
// assume 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 ret;
}
}

0 comments on commit 312b5d7

Please sign in to comment.