Java Code Examples for java.net.JarURLConnection#getManifest()

The following examples show how to use java.net.JarURLConnection#getManifest() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SplashLabel.java    From sciview with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private String getGitHashFor(Class<?> clazz) {
    final String sciviewBaseClassName = clazz.getSimpleName() + ".class";
    final String sciviewClassPath = clazz.getResource(sciviewBaseClassName).toString();
    String gitHash = "";
    if(!sciviewClassPath.startsWith("jar")) {
        return gitHash;
    }

    try {
        URL url = new URL(sciviewClassPath);
        JarURLConnection jarConnection = (JarURLConnection) url.openConnection();
        Manifest manifest = jarConnection.getManifest();
        Attributes attributes = manifest.getMainAttributes();
        gitHash = attributes.getValue("Implementation-Build").substring(0, 8);
    } catch (IOException ioe){
        gitHash = "";
    }

    return gitHash;
}
 
Example 2
Source File: RyaBannerProvider.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Loads the version number from the Rya Shell's MANIFEST.MF file.
 *
 * @return The version number of the Rya Shell.
 */
private String loadVersion() {
    final String className = getClass().getSimpleName() + ".class";
    final String classPath = getClass().getResource( className ).toString();

    try {
        final URL classUrl = new URL(classPath);
        final JarURLConnection jarConnection = (JarURLConnection) classUrl.openConnection();
        final Manifest manifest = jarConnection.getManifest();
        final Attributes attributes = manifest.getMainAttributes();
        return attributes.getValue("Implementation-Version");
    } catch (final IOException e) {
        log.error("Could not load the application's version from it's manifest.", e);
    }

    return "UNKNOWN";
}
 
Example 3
Source File: VersionProvider.java    From recheck with GNU Affero General Public License v3.0 5 votes vote down vote up
private static String readVersionFromManifest() {
	try {
		final URLConnection jarConnection = getLocationOfClass().openConnection();
		if ( !(jarConnection instanceof JarURLConnection) ) {
			return null;
		}
		final JarURLConnection conn = (JarURLConnection) jarConnection;
		final Manifest mf = conn.getManifest();
		final Attributes atts = mf.getMainAttributes();
		return atts.getValue( "Implementation-Version" );
	} catch ( final IOException e ) {
		return null;
	}
}
 
Example 4
Source File: VersionProvider.java    From recheck with GNU Affero General Public License v3.0 5 votes vote down vote up
private static String readBuildDateFromManifest() {
	try {
		final URLConnection jarConnection = getLocationOfClass().openConnection();
		if ( !(jarConnection instanceof JarURLConnection) ) {
			return null;
		}
		final JarURLConnection conn = (JarURLConnection) jarConnection;
		final Manifest mf = conn.getManifest();
		final Attributes atts = mf.getMainAttributes();
		return atts.getValue( "Build-Time" );
	} catch ( final IOException e ) {
		return null;
	}
}
 
Example 5
Source File: FileHelper.java    From mcaselector with MIT License 5 votes vote down vote up
public static Attributes getManifestAttributes() throws IOException {
	String className = FileHelper.class.getSimpleName() + ".class";
	String classPath = FileHelper.class.getResource(className).toString();
	if (!classPath.startsWith("jar")) {
		throw new IOException("application not running in jar file");
	}
	URL url = new URL(classPath);
	JarURLConnection jarConnection = (JarURLConnection) url.openConnection();
	Manifest manifest = jarConnection.getManifest();
	return manifest.getMainAttributes();
}
 
Example 6
Source File: ManifestGetter.java    From hop with Apache License 2.0 4 votes vote down vote up
public Manifest getManifest() throws Exception {
  URL url = this.getClass().getResource( "/META-INF/MANIFEST.MF" );
  JarURLConnection jarConnection = (JarURLConnection) url.openConnection();
  return jarConnection.getManifest();
}
 
Example 7
Source File: TldScanner.java    From packagedrone with Eclipse Public License 1.0 4 votes vote down vote up
private boolean addManifestClassPath(List<String> scannedJars,
                                     List<String> newJars,
                                     JarURLConnection jconn){

    Manifest manifest;
    try {
        manifest = jconn.getManifest();
    } catch (IOException ex) {
        // Maybe non existing jar, ignored
        return false;
    }

    String file = jconn.getJarFileURL().toString();
    if (! file.contains("WEB-INF")) {
        // Only jar in WEB-INF is considered here
        return true;
    }

    if (manifest == null)
        return true;

    java.util.jar.Attributes attrs = manifest.getMainAttributes();
    String cp = (String) attrs.getValue("Class-Path");
    if (cp == null)
        return true;

    String[] paths = cp.split(" ");
    int lastIndex = file.lastIndexOf('/');
    if (lastIndex < 0) {
        lastIndex = file.lastIndexOf('\\');
    }
    String baseDir = "";
    if (lastIndex > 0) {
        baseDir = file.substring(0, lastIndex+1);
    }
    for (String path: paths) {
        String p;
        if (path.startsWith("/") || path.startsWith("\\")){
            p = "file:" + path;
        } else {
            p = baseDir + path;
        }
        if ((scannedJars == null || !scannedJars.contains(p)) &&
            !newJars.contains(p) ){
                 newJars.add(p);
        }
    }
    return true;
}
 
Example 8
Source File: ManifestGetter.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public Manifest getManifest() throws Exception {
  URL url = this.getClass().getResource( BuildVersion.REFERENCE_FILE );
  JarURLConnection jarConnection = (JarURLConnection) url.openConnection();
  return jarConnection.getManifest();
}
 
Example 9
Source File: FreeCol.java    From freecol with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Extract the package version from the class.
 *
 * @param juc The {@code JarURLConnection} to extract from.
 * @return A value of the package version attribute.
 * @exception IOException if the manifest is not available.
 */
private static String readVersion(JarURLConnection juc) throws IOException {
    Manifest mf = juc.getManifest();
    return (mf == null) ? null
        : mf.getMainAttributes().getValue("Package-Version");
}