package com.armundia.app;

import java.net.URL;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

public class AppInfo {
	private static String version = "";
	
	private AppInfo() {
		
	}
	
	public static String getVersion() {
		if (version == null || "".equals(version)) {
			try {
	            Class<?> clazz = AppInfo.class;
	            String className = clazz.getSimpleName() + ".class";
	            String classPath = clazz.getResource(className).toString();
	            String manifestPath = classPath.substring(0, classPath.lastIndexOf(".war") + 4) + "/META-INF/MANIFEST.MF";
	            Manifest manifest = new Manifest(new URL(manifestPath).openStream());
	            Attributes attributes = manifest.getMainAttributes();
	            version = attributes.getValue("Implementation-Version");
	        } catch (Exception e) {
	            e.printStackTrace();
	            return "Not available (error occurred)";
	        }
		}
		
		return version;
	}
}