package io.github.greyp9.arwo.core.vm.app;

import io.github.greyp9.arwo.core.url.URLCodec;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

public final class ManifestU {

    private ManifestU() {
    }

    @SuppressWarnings("PMD.OnlyOneReturn")
    public static Manifest getManifestSafe(final Class<?> c) {
        try {
            return getManifest(c);
        } catch (IOException e) {
            return new Manifest();
        }
    }

    public static Manifest getManifest(final Class<?> c) throws IOException {
        final URL url = c.getProtectionDomain().getCodeSource().getLocation();
        final File file = URLCodec.toFile(url);
        return (file.isFile() ? getManifest(file) : new Manifest());
    }

    public static Manifest getManifest(final File file) throws IOException {
        final JarFile jarFile = new JarFile(file);
        try {
            return jarFile.getManifest();
        } finally {
            jarFile.close();
        }
    }

    public static String getAttribute(final Manifest manifest, final String name) {
        return manifest.getMainAttributes().getValue(name);
    }

    public static String getImplementationBuild(final Manifest manifest) {
        return getAttribute(manifest, Const.BUILD);
    }

    public static String getImplementationVersion(final Manifest manifest) {
        return getAttribute(manifest, Const.VERSION);
    }

    public static String getVersion(final Class<?> c) {
        final Manifest manifest = getManifestSafe(c);
        return String.format("[v%s %s]",
                getImplementationVersion(manifest),
                getImplementationBuild(manifest));
    }

    private static class Const {
        public static final String BUILD = "Implementation-Build";  // i18n JRE
        public static final String VERSION = "Implementation-Version";  // i18n JRE
    }
}

@@ -151,19 +151,84 @@
 ava.net.
-URL
+JarURLConnection;%0Aimport java.net.URL;%0Aimport java.net.URLConnection
 ;%0Aimport
@@ -659,32 +659,59 @@
 s IOException %7B%0A
+        Manifest manifest;%0A
         final UR
@@ -769,24 +769,89 @@
 Location();%0A
+        if (%22file%22.equals(url.getProtocol())) %7B  // i18n JRE%0A    
         fina
@@ -894,22 +894,30 @@
         
-return
+    manifest =
  (file.i
@@ -959,24 +959,395 @@
 anifest());%0A
+        %7D else if (%22jar%22.equals(url.getProtocol())) %7B  // i18n JRE%0A            final URLConnection urlConnection = url.openConnection();%0A            final JarURLConnection jarURLConnection = (JarURLConnection) urlConnection;%0A            manifest = jarURLConnection.getManifest();%0A        %7D else %7B%0A            manifest = new Manifest();%0A        %7D%0A        return manifest;%0A
     %7D%0A%0A    p
@@ -2045,24 +2045,25 @@
 g getVersion
+2
 (final Class
@@ -2218,16 +2218,16 @@
 ifest),%0A
-
         
@@ -2272,24 +2272,169 @@
 t));%0A    %7D%0A%0A
+    public static String getVersion(final Class%3C?%3E c) %7B%0A        return String.format(%22%5Bv%25s%5D%22, c.getPackage().getImplementationVersion());%0A    %7D%0A%0A
     private 

handle unexploded WAR classloader issue (default Tomcat8 behavior?)