From add8bfad91cf6608d133d01673b822ac2e7e4b7f Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 14 Feb 2022 15:02:24 +0100 Subject: AppUtil: Fix opening jar file if running from network share Turning the URL of the jar into either an URI, or the URI into a string results in a representation that the File constructor cannot turn back into a meaningful path to the file. Fix this by directly opening an InputStream from the URL instance, without the File detour. --- src/main/java/org/openslx/util/AppUtil.java | 47 +++++++++++++++-------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/openslx/util/AppUtil.java b/src/main/java/org/openslx/util/AppUtil.java index 0ef4d8e..22fa331 100644 --- a/src/main/java/org/openslx/util/AppUtil.java +++ b/src/main/java/org/openslx/util/AppUtil.java @@ -1,20 +1,21 @@ package org.openslx.util; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; import java.io.InputStream; -import java.net.URISyntaxException; import java.time.Instant; import java.time.ZoneId; import java.time.format.DateTimeFormatter; +import java.util.jar.Attributes; import java.util.jar.JarInputStream; import java.util.jar.Manifest; +import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class AppUtil { + + private final static Logger LOGGER = LogManager.getLogger( AppUtil.class ); + private static final int PROPERTY_MAX_WIDTH = 30; private static final String MANIFEST_REVISION_VERSION = "Revision-Version"; @@ -38,30 +39,30 @@ public class AppUtil private static final String PROPERTY_JAVA_VERSION = "java.version"; private static final String PROPERTY_JAVA_VERSION_VM = "java.vm.version"; private static final String PROPERTY_JAVA_VERSION_RUNTIME = "java.runtime.version"; + + private static Attributes manifestAttributes = null; private static String getManifestValue( final String entry ) { - File jarFile = null; - InputStream jarFileStream = null; - JarInputStream jarStream = null; - String value = null; - - try { - final String jarFilename = AppUtil.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath(); - jarFile = new File( jarFilename ); - jarFileStream = new FileInputStream( jarFile ); - jarStream = new JarInputStream( jarFileStream ); - - final Manifest mf = jarStream.getManifest(); - value = mf.getMainAttributes().getValue( entry ); - } catch ( URISyntaxException | IOException e ) { - return value; - } finally { - Util.safeClose( jarStream ); - Util.safeClose( jarFileStream ); + if ( manifestAttributes == null ) { + InputStream jarFileStream = null; + JarInputStream jarStream = null; + // Do this so in case of failure, we won't open the jar again and again and spam exceptions to the log + manifestAttributes = new Attributes(); + + try { + jarFileStream = AppUtil.class.getProtectionDomain().getCodeSource().getLocation().openStream(); + jarStream = new JarInputStream( jarFileStream ); + final Manifest mf = jarStream.getManifest(); + manifestAttributes = mf.getMainAttributes(); + } catch ( Exception e ) { + LOGGER.warn( "Cannot read jar/manifest attributes", e ); + } finally { + Util.safeClose( jarStream, jarFileStream ); + } } - return value; + return manifestAttributes.getValue( entry ); } public static String getRevisionVersion() -- cgit v1.2.3-55-g7522