All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.redislabs.picocliredis.ManifestVersionProvider Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package com.redislabs.picocliredis;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import picocli.CommandLine.IVersionProvider;

/**
 * {@link IVersionProvider} implementation that returns version information from
 * the jar file's {@code /META-INF/MANIFEST.MF} file.
 */
public class ManifestVersionProvider implements IVersionProvider {

	public String[] getVersion() throws Exception {
		Enumeration resources = getClass().getClassLoader().getResources("META-INF/MANIFEST.MF");
		while (resources.hasMoreElements()) {
			URL url = resources.nextElement();
			try {
				Manifest manifest = new Manifest(url.openStream());
				if (isApplicableManifest(manifest)) {
					Attributes attr = manifest.getMainAttributes();
					return new String[] { get(attr, "Implementation-Title") + " version \""
							+ get(attr, "Implementation-Version") + "\"" };
				}
			} catch (IOException ex) {
				return new String[] { "Unable to read from " + url + ": " + ex };
			}
		}
		return new String[0];
	}

	private boolean isApplicableManifest(Manifest manifest) {
		Attributes attributes = manifest.getMainAttributes();
		return "RIOT".equals(get(attributes, "Implementation-Title"));
	}

	private static Object get(Attributes attributes, String key) {
		return attributes.get(new Attributes.Name(key));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy