From cc3608bf2b545116b67eba41d69225cae3247994 Mon Sep 17 00:00:00 2001 From: Alexander Marsalek Date: Wed, 16 Jun 2021 13:52:48 +0200 Subject: v4.2.0-RC1 workaround to get implementation and specification version from manifest --- .../at/gv/egiz/pdfas/lib/api/PdfAsFactory.java | 53 ++++++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) (limited to 'pdf-as-lib/src/main') diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsFactory.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsFactory.java index f917de64..0b465bc1 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsFactory.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsFactory.java @@ -42,14 +42,24 @@ import javax.activation.DataSource; import javax.crypto.Cipher; import java.awt.*; import java.awt.image.BufferedImage; -import java.io.*; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.lang.management.ManagementFactory; import java.lang.management.OperatingSystemMXBean; import java.lang.management.RuntimeMXBean; +import java.net.URL; +import java.net.URLClassLoader; import java.security.Provider; import java.security.Security; +import java.util.Enumeration; import java.util.Map; import java.util.Map.Entry; +import java.util.jar.Attributes; +import java.util.jar.Manifest; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -347,7 +357,12 @@ public class PdfAsFactory implements IConfigurationConstants { */ public static String getSCMRevision() { Package pack = PdfAsFactory.class.getPackage(); - return pack.getSpecificationVersion(); + String specificationVersion = pack.getSpecificationVersion(); + + if(specificationVersion != null) + return specificationVersion; + //fallback + return getJarAttributes().getValue("Specification-Version"); } /** @@ -356,8 +371,40 @@ public class PdfAsFactory implements IConfigurationConstants { * @return PDF-AS Verison string */ public static String getVersion() { + Package pack = PdfAsFactory.class.getPackage(); - return pack.getImplementationVersion(); + String version = pack.getImplementationVersion(); + if(version != null) + return version; + //fallback + return getJarAttributes().getValue("Implementation-Version"); + } + + private static Attributes jarAttributes = null; + private static Attributes getJarAttributes() { + if(jarAttributes != null) + return jarAttributes; + try { + URLClassLoader cl = (URLClassLoader) PdfAsFactory.class.getClassLoader(); + Enumeration urls = cl.findResources("META-INF/MANIFEST.MF"); + URL url = null; + while (urls.hasMoreElements()) { + URL tmp = urls.nextElement(); + if (tmp.getFile().contains("pdf-as-lib")) { + System.out.println("Found:" + tmp); + url = tmp; + } + + } + Manifest manifest = new Manifest(url.openStream()); + Attributes mainAttributes = manifest.getMainAttributes(); + jarAttributes = mainAttributes; + return mainAttributes; + } catch (Exception e) { + // handle + jarAttributes = new Attributes(); + } + return null; } /** -- cgit v1.2.3