From 75cea8928a2d0007b31f850d5fcaba3c018cb6cd Mon Sep 17 00:00:00 2001 From: Greg DiCristofaro <gregd@basistech.com> Date: Tue, 29 Aug 2023 17:25:38 -0400 Subject: [PATCH] handle upload --- .../autopsy/apiupdate/ManifestLoader.java | 7 ++++- .../autopsy/apiupdate/ModuleUpdates.java | 27 ++++++++++++------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/release_scripts/APIUpdate/src/main/java/org/sleuthkit/autopsy/apiupdate/ManifestLoader.java b/release_scripts/APIUpdate/src/main/java/org/sleuthkit/autopsy/apiupdate/ManifestLoader.java index 2b97b3328e..ddc9b04741 100644 --- a/release_scripts/APIUpdate/src/main/java/org/sleuthkit/autopsy/apiupdate/ManifestLoader.java +++ b/release_scripts/APIUpdate/src/main/java/org/sleuthkit/autopsy/apiupdate/ManifestLoader.java @@ -19,13 +19,18 @@ * @author gregd */ public class ManifestLoader { + private static final String JAR_MANIFEST_REL_PATH = "META-INF/MANIFEST.MF"; public static Attributes loadInputStream(InputStream is) throws IOException { - Manifest manifest = new Manifest(is); + Manifest manifest = loadManifest(is); return manifest.getMainAttributes(); } + public static Manifest loadManifest(InputStream is) throws IOException { + return new Manifest(is); + } + public static Attributes loadFromJar(File jarFile) throws IOException { ZipFile zipFile = new ZipFile(jarFile); diff --git a/release_scripts/APIUpdate/src/main/java/org/sleuthkit/autopsy/apiupdate/ModuleUpdates.java b/release_scripts/APIUpdate/src/main/java/org/sleuthkit/autopsy/apiupdate/ModuleUpdates.java index d990167c96..dcd69abba7 100644 --- a/release_scripts/APIUpdate/src/main/java/org/sleuthkit/autopsy/apiupdate/ModuleUpdates.java +++ b/release_scripts/APIUpdate/src/main/java/org/sleuthkit/autopsy/apiupdate/ModuleUpdates.java @@ -14,6 +14,7 @@ import java.util.Map.Entry; import java.util.Properties; import java.util.jar.Attributes; +import java.util.jar.Manifest; import java.util.logging.Level; import java.util.logging.Logger; import java.util.logging.SimpleFormatter; @@ -45,6 +46,7 @@ public class ModuleUpdates { private static final Logger LOGGER = Logger.getLogger(ModuleUpdates.class.getName()); + static { LOGGER.addHandler(new StreamHandler(System.out, new SimpleFormatter())); } @@ -181,8 +183,9 @@ static void setVersions(File srcDir, Map<String, ModuleVersionNumbers> versNums) String moduleName = moduleNameDir.getKey(); File moduleDir = moduleNameDir.getValue(); ModuleVersionNumbers thisVersNums = versNums.get(moduleName); - + try { + LOGGER.log(Level.INFO, "Updating for module name: " + moduleName); updateProjXml(moduleDir, versNums); if (thisVersNums != null) { @@ -224,22 +227,29 @@ private static void updateManifest(File moduleDir, ModuleVersionNumbers thisVers return; } - Attributes attributes; + Manifest manifest; try (FileInputStream manifestIs = new FileInputStream(manifestFile)) { - attributes = ManifestLoader.loadInputStream(manifestIs); + manifest = ManifestLoader.loadManifest(manifestIs); } + Attributes attributes = manifest.getMainAttributes(); - updateAttr(attributes, IMPL_KEY, Integer.toString(thisVersNums.getImplementation()), true); - updateAttr(attributes, SPEC_KEY, thisVersNums.getSpec().getSemVerStr(), true); - updateAttr(attributes, RELEASE_KEY, thisVersNums.getRelease().getFullReleaseStr(), true); + boolean updated = updateAttr(attributes, IMPL_KEY, Integer.toString(thisVersNums.getImplementation()), true); + updated = updateAttr(attributes, SPEC_KEY, thisVersNums.getSpec().getSemVerStr(), true) || updated; + updated = updateAttr(attributes, RELEASE_KEY, thisVersNums.getRelease().getFullReleaseStr(), true) || updated; + if (updated) { + try (FileOutputStream manifestOut = new FileOutputStream(manifestFile)) { + manifest.write(manifestOut); + } + } } - private static void updateAttr(Attributes attributes, String key, String val, boolean updateOnlyIfPresent) { + private static boolean updateAttr(Attributes attributes, String key, String val, boolean updateOnlyIfPresent) { if (updateOnlyIfPresent && attributes.getValue(key) == null) { - return; + return false; } attributes.putValue(key, val); + return true; } private static void updateProjXml(File moduleDir, Map<String, ModuleVersionNumbers> versNums) @@ -282,7 +292,6 @@ private static void updateProjXml(File moduleDir, Map<String, ModuleVersionNumbe // pretty print XML //transformer.setOutputProperty(OutputKeys.INDENT, "yes"); - DOMSource source = new DOMSource(projectXmlDoc); try (FileOutputStream xmlOut = new FileOutputStream(projXmlFile)) { StreamResult result = new StreamResult(xmlOut); -- GitLab