From 3cea024119fcbe5853e4ab302ee305e2c444c9e7 Mon Sep 17 00:00:00 2001 From: Michael Ritter Date: Thu, 23 Feb 2023 17:27:04 -0700 Subject: [PATCH] DRYD-1145: Get version info from implementation version (#311) * Reformatting * DRYD-1145: Add build info to manifest for jar and war files * DRYD-1145: Use implementation version for systeminfo endpoint * Update version display name * Add git commit id --- pom.xml | 37 +++++ .../services/common/ServiceMain.java | 7 +- .../systeminfo/SystemInfoResource.java | 146 ++++++++++-------- 3 files changed, 121 insertions(+), 69 deletions(-) diff --git a/pom.xml b/pom.xml index 1d7691be5..e3b6626f3 100644 --- a/pom.xml +++ b/pom.xml @@ -188,6 +188,8 @@ true + true + true @@ -197,6 +199,36 @@ org.apache.maven.plugins maven-jar-plugin 2.2 + + + + true + true + + + + + + + pl.project13.maven + git-commit-id-plugin + 4.9.10 + + + + revision + + initialize + + + + true + ${project.build.outputDirectory}/git.properties + + ^git.commit.id.abbrev$ + + full + @@ -360,6 +392,11 @@ + + + pl.project13.maven + git-commit-id-plugin + diff --git a/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java b/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java index d7f54fe42..d11f79681 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java +++ b/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java @@ -74,11 +74,6 @@ import org.slf4j.LoggerFactory; public class ServiceMain { final static Logger logger = LoggerFactory.getLogger(ServiceMain.class); - public static final String VER_DISPLAY_NAME = "CollectionSpace Services v7.1"; - public static final String VER_MAJOR = "7"; - public static final String VER_MINOR = "1"; - public static final String VER_PATCH = "0"; - public static final String VER_BUILD = "1"; private static final int PRIMARY_REPOSITORY_DOMAIN = 0; @@ -1045,7 +1040,7 @@ public class ServiceMain { } private void initRepositoryDatabaseVersion(String dataSourceName, String repositoryName, String cspaceInstanceId) throws Exception { - String version = ServiceMain.VER_MAJOR + "." + ServiceMain.VER_MINOR + "." + ServiceMain.VER_PATCH; + String version = getClass().getPackage().getImplementationVersion(); Connection conn = null; try { diff --git a/services/systeminfo/service/src/main/java/org/collectionspace/services/systeminfo/SystemInfoResource.java b/services/systeminfo/service/src/main/java/org/collectionspace/services/systeminfo/SystemInfoResource.java index 24e45547a..d7dfc6c1e 100644 --- a/services/systeminfo/service/src/main/java/org/collectionspace/services/systeminfo/SystemInfoResource.java +++ b/services/systeminfo/service/src/main/java/org/collectionspace/services/systeminfo/SystemInfoResource.java @@ -1,26 +1,25 @@ package org.collectionspace.services.systeminfo; +import java.io.IOException; import java.nio.charset.Charset; import java.util.Locale; +import java.util.Properties; import java.util.TimeZone; - import javax.ws.rs.Consumes; import javax.ws.rs.GET; +import javax.ws.rs.HttpMethod; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; -import javax.ws.rs.HttpMethod; import org.collectionspace.services.authorization.AuthZ; -import org.collectionspace.services.authorization.CSpaceAction; import org.collectionspace.services.authorization.CSpaceResource; import org.collectionspace.services.authorization.URIResourceImpl; import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl; import org.collectionspace.services.common.CSWebApplicationException; import org.collectionspace.services.common.ServiceMain; -import org.collectionspace.services.common.UriInfoWrapper; import org.collectionspace.services.common.context.RemoteServiceContextFactory; import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.context.ServiceContextFactory; @@ -31,74 +30,95 @@ import org.collectionspace.services.common.security.UnauthorizedException; @Consumes({"application/xml"}) public class SystemInfoResource extends AbstractCollectionSpaceResourceImpl { - @Override - public Class getCommonPartClass() { - // TODO Auto-generated method stub - return null; - } + private static final String VER_DISPLAY_NAME = "CollectionSpace Services v%s.%s"; + private static final int MAJOR_IDX = 0; + private static final int MINOR_IDX = 1; + private static final int PATCH_IDX = 2; + + @Override + public Class getCommonPartClass() { + // TODO Auto-generated method stub + return null; + } - @Override - public String getServiceName() { - return SystemInfoClient.SERVICE_NAME; - } + @Override + public String getServiceName() { + return SystemInfoClient.SERVICE_NAME; + } - @Override - protected String getVersionString() { - // TODO Auto-generated method stub - return null; - } + @Override + protected String getVersionString() { + // TODO Auto-generated method stub + return null; + } - // - // API Endpoints - // + // + // API Endpoints + // @GET public SystemInfoCommon get(@Context UriInfo ui) { - SystemInfoCommon result = null; - - try { - result = new SystemInfoCommon(); - result.setInstanceId(ServiceMain.getInstance().getCspaceInstanceId()); - result.setDisplayName(ServiceMain.VER_DISPLAY_NAME); - Version ver = new Version(); - ver.setMajor(ServiceMain.VER_MAJOR); - ver.setMinor(ServiceMain.VER_MINOR); - ver.setPatch(ServiceMain.VER_PATCH); - ver.setBuild(ServiceMain.VER_BUILD); - result.setVersion(ver); - - result.setHostTimezone(TimeZone.getDefault().getID()); - result.setHostLocale(Locale.getDefault().toLanguageTag()); - result.setHostCharset(Charset.defaultCharset().name()); - // - // To get the full set of the system information, we required the user be authenticated *and* have "DELETE" privs on the "systeminfo" resource - // - try { - ServiceContext ctx = createServiceContext(getServiceName(), ui); - CSpaceResource res = new URIResourceImpl(ctx.getTenantId(), SystemInfoClient.SERVICE_NAME, HttpMethod.DELETE); - if (AuthZ.get().isAccessAllowed(res)) { - // TODO: Stop hardcoding this! - // result.setNuxeoVersionString("7.10-HF17"); - result.setHost(String.format("Architecture:%s Name:%s Version:%s", - System.getProperty("os.arch"), System.getProperty("os.name"), System.getProperty("os.version"))); - result.setJavaVersionString(System.getProperty("java.version")); - // TODO: Stop hardcoding this! - // result.setPostgresVersionString("9.5.7"); - } - } catch (UnauthorizedException e) { - logger.trace(e.getMessage(), e); - } - - } catch(Exception e) { - Response response = Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type("text/plain").build(); + SystemInfoCommon result; + + try { + String packageVersion = SystemInfoResource.class.getPackage().getImplementationVersion(); + String[] versionParts = packageVersion.split("\\."); + + result = new SystemInfoCommon(); + result.setInstanceId(ServiceMain.getInstance().getCspaceInstanceId()); + result.setDisplayName(String.format(VER_DISPLAY_NAME, versionParts[MAJOR_IDX], versionParts[MINOR_IDX])); + + Version ver = new Version(); + ver.setMajor(versionParts[MAJOR_IDX]); + ver.setMinor(versionParts[MINOR_IDX]); + ver.setPatch(versionParts[PATCH_IDX]); + ver.setBuild(readGitCommitId()); + result.setVersion(ver); + + result.setHostTimezone(TimeZone.getDefault().getID()); + result.setHostLocale(Locale.getDefault().toLanguageTag()); + result.setHostCharset(Charset.defaultCharset().name()); + + // To get the full set of the system information, we required the user be authenticated *and* have "DELETE" + // privs on the "systeminfo" resource + try { + ServiceContext ctx = createServiceContext(getServiceName(), ui); + CSpaceResource res = new URIResourceImpl(ctx.getTenantId(), SystemInfoClient.SERVICE_NAME, + HttpMethod.DELETE); + if (AuthZ.get().isAccessAllowed(res)) { + // TODO: Stop hardcoding this! + // result.setNuxeoVersionString("7.10-HF17"); + result.setHost(String.format("Architecture:%s Name:%s Version:%s", + System.getProperty("os.arch"), + System.getProperty("os.name"), + System.getProperty("os.version"))); + result.setJavaVersionString(System.getProperty("java.version")); + // TODO: Stop hardcoding this! + // result.setPostgresVersionString("9.5.7"); + } + } catch (UnauthorizedException e) { + logger.trace(e.getMessage(), e); + } + + } catch (Exception e) { + logger.error("Error generating system info", e); + Response response = Response.status(Response.Status.BAD_REQUEST) + .entity(e.getMessage()) + .type("text/plain").build(); throw new CSWebApplicationException(response); - } + } + + return result; + } - return result; + private String readGitCommitId() throws IOException { + Properties properties = new Properties(); + properties.load(getClass().getClassLoader().getResourceAsStream("git.properties")); + return properties.getProperty("git.commit.id.abbrev"); } - @Override - public ServiceContextFactory getServiceContextFactory() { + @Override + public ServiceContextFactory getServiceContextFactory() { return (ServiceContextFactory) RemoteServiceContextFactory.get(); - } + } } -- 2.47.3