Example usage for java.util.jar Manifest getMainAttributes

List of usage examples for java.util.jar Manifest getMainAttributes

Introduction

In this page you can find the example usage for java.util.jar Manifest getMainAttributes.

Prototype

public Attributes getMainAttributes() 

Source Link

Document

Returns the main Attributes for the Manifest.

Usage

From source file:edu.cmu.tetrad.cli.TetradCliApp.java

private static void showHelp() {
    String cmdLineSyntax = "java -jar ";
    try {//from w  w w  .j  a  v a2  s .c  o m
        JarFile jarFile = new JarFile(
                TetradCliApp.class.getProtectionDomain().getCodeSource().getLocation().getPath(), true);
        Manifest manifest = jarFile.getManifest();
        Attributes attributes = manifest.getMainAttributes();
        String artifactId = attributes.getValue("Implementation-Title");
        String version = attributes.getValue("Implementation-Version");
        cmdLineSyntax += String.format("%s-%s.jar", artifactId, version);
    } catch (IOException exception) {
        cmdLineSyntax += "causal-cmd.jar";
    }

    HelpFormatter formatter = new HelpFormatter();
    formatter.printHelp(cmdLineSyntax, MAIN_OPTIONS, true);
}

From source file:com.textocat.textokit.morph.opencorpora.OpencorporaMorphDictionaryAPI.java

public static String locateDictionaryClassPath() {
    List<Manifest> jarManifests = ManifestUtils.searchByAttributeKey(ME_OPENCORPORA_DICTIONARY_VERSION);
    if (jarManifests.isEmpty())
        throw new IllegalStateException("Can't find an OpenCorpora dictionary in classpath");
    if (jarManifests.size() > 1)
        throw new UnsupportedOperationException("Found several OpenCorpora dictionaries in classpath");
    Manifest ocJarManifest = jarManifests.get(0);
    String version = ocJarManifest.getMainAttributes().getValue(ME_OPENCORPORA_DICTIONARY_VERSION);
    String revision = ocJarManifest.getMainAttributes().getValue(ME_OPENCORPORA_DICTIONARY_REVISION);
    String variant = ocJarManifest.getMainAttributes().getValue(ME_OPENCORPORA_DICTIONARY_VARIANT);
    return String.format(OpencorporaMorphDictionaryAPI.FILENAME_PATTERN_OPENCORPORA_SERIALIZED_DICT, version,
            revision, variant);/*ww  w.  j a v  a 2  s  .c  o  m*/
}

From source file:pl.otros.logview.VersionUtil.java

/**
 * Check version of running application. Version is read from /META-INF/MANIFEST.MF file
 *
 * @return currently running version//from  w  ww . j  a v  a  2s .c  o  m
 * @throws IOException
 */
public static String getRunningVersion() throws IOException {
    LOGGER.info("Checking running version");
    String result = "";
    Enumeration<URL> resources = VersionUtil.class.getClassLoader().getResources("META-INF/MANIFEST.MF");
    while (resources.hasMoreElements()) {
        URL url = resources.nextElement();
        if (url.toString().contains("OtrosLogViewer")) {
            InputStream inputStream = url.openStream();
            try {
                Manifest manifest = new Manifest(inputStream);
                result = manifest.getMainAttributes().getValue(IMPLEMENTATION_VERSION);
                LOGGER.info("Running version is " + result);

            } finally {
                IOUtils.closeQuietly(inputStream);
            }
        }
    }
    return result;
}

From source file:oz.hadoop.yarn.api.utils.JarUtils.java

/**
 * Will create a JAR file frombase dir/*from  w  w  w  .  ja v  a 2s .  co  m*/
 *
 * @param source
 * @param jarName
 * @return
 */
public static File toJar(File source, String jarName) {
    if (!source.isAbsolute()) {
        throw new IllegalArgumentException("Source must be expressed through absolute path");
    }
    StringAssertUtils.assertNotEmptyAndNoSpacesAndEndsWith(jarName, ".jar");
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    File jarFile = new File(jarName);
    try {
        JarOutputStream target = new JarOutputStream(new FileOutputStream(jarFile), manifest);
        add(source, source.getAbsolutePath().length(), target);
        target.close();
    } catch (Exception e) {
        throw new IllegalStateException(
                "Failed to create JAR file '" + jarName + "' from " + source.getAbsolutePath(), e);
    }
    return jarFile;
}

From source file:edu.cornell.med.icb.util.VersionUtils.java

/**
 * Gets the Implementation-Version attribute from the manifest of the jar file a class
 * is loaded from.//from   ww  w. j  a va 2 s .c o m
 * @param clazz The class to get the version for
 * @return The value of the Implementation-Version attribute or "UNKNOWN" if the
 * jar file cannot be read.
 */
public static String getImplementationVersion(final Class<?> clazz) {
    String version;
    try {
        final String classContainer = clazz.getProtectionDomain().getCodeSource().getLocation().toString();
        final URL manifestUrl = new URL("jar:" + classContainer + "!/" + JarFile.MANIFEST_NAME);
        final Manifest manifest = new Manifest(manifestUrl.openStream());
        final Attributes attributes = manifest.getMainAttributes();
        version = attributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
    } catch (Exception e) {
        // pretty much any error here is ok since we may not even have a jar to read from
        version = "UNKNOWN";
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug(Attributes.Name.IMPLEMENTATION_VERSION + ": " + version);
    }
    return StringUtils.defaultString(version);
}

From source file:io.zatarox.satellite.impl.BackgroundWrapperTest.java

@BeforeClass
public static void setBefore() throws Exception {
    file = File.createTempFile("archive", ".jar");
    file.deleteOnExit();// w  ww.j  a  v  a 2  s.c  o  m
    final FileOutputStream out = new FileOutputStream(file);
    ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP,
            out);
    ZipArchiveEntry entry = new ZipArchiveEntry("META-INF/MANIFEST.MF");
    archive.putArchiveEntry(entry);
    final Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    manifest.getMainAttributes().putValue("Background-Process-Class",
            FakeBackgroundProcessImpl.class.getName());
    manifest.write(archive);
    archive.closeArchiveEntry();
    archive.close();
}

From source file:org.apache.hama.monitor.Configurator.java

/**
 * Load jar from specified path./*from  ww  w.  j a v a 2 s . c om*/
 * 
 * @param path to the jar file.
 * @param loader of the current thread.
 * @return task to be run.
 */
private static Task load(File path, ClassLoader loader) throws IOException {
    JarFile jar = new JarFile(path);
    Manifest manifest = jar.getManifest();
    String pkg = manifest.getMainAttributes().getValue("Package");
    String main = manifest.getMainAttributes().getValue("Main-Class");
    if (null == pkg || null == main)
        throw new NullPointerException("Package or main class not found " + "in menifest file.");
    String namespace = pkg + File.separator + main;
    namespace = namespace.replaceAll(File.separator, ".");
    LOG.debug("Task class to be loaded: " + namespace);
    URLClassLoader child = new URLClassLoader(new URL[] { path.toURI().toURL() }, loader);
    Thread.currentThread().setContextClassLoader(child);
    Class<?> taskClass = null;
    try {
        taskClass = Class.forName(namespace, true, child); // task class
    } catch (ClassNotFoundException cnfe) {
        LOG.warn("Task class is not found.", cnfe);
    }
    if (null == taskClass)
        return null;

    try {
        return (Task) taskClass.newInstance();
    } catch (InstantiationException ie) {
        LOG.warn("Unable to instantiate task class." + namespace, ie);
    } catch (IllegalAccessException iae) {
        LOG.warn(iae);
    }
    return null;
}

From source file:tk.tomby.tedit.plugins.PluginDriver.java

private static JarEntry getDescriptor(JarFile jar) throws IOException {
    JarEntry entry = null;/*from w  ww . j a va 2 s . co m*/

    Manifest manifest = jar.getManifest();
    Attributes attrs = manifest.getMainAttributes();
    String pluginAttr = attrs.getValue("Plugin-Descriptor");

    if (pluginAttr != null) {
        entry = jar.getJarEntry(pluginAttr);
    } else {
        entry = jar.getJarEntry("META-INF/plugin.xml");
    }

    return entry;
}

From source file:org.jcurl.core.helpers.Version.java

public static final Version find(final ClassLoader clz) {
    try {/*from   w ww . ja v  a2s . c o m*/
        final Manifest mf = findManifest(clz, "jcurl-");
        final Attributes main = mf.getMainAttributes();
        return create(main.getValue("Bundle-Version"), main.getValue("Built-Time"));
    } catch (final Exception e) {
        return null;
    }
}

From source file:org.ow2.chameleon.core.utils.BundleHelper.java

private static boolean isExplodedBundle(File directory) {
    if (!directory.isDirectory()) {
        return false;
    }/*  ww  w  . j  a v  a2  s .co  m*/
    File manifestFile = new File(directory, MANIFEST);
    if (!manifestFile.exists()) {
        return false;
    }

    FileInputStream stream = null;
    try {
        stream = new FileInputStream(manifestFile);
        Manifest manifest = new Manifest(stream);
        return manifest.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME) != null;
    } catch (IOException e) {
        LoggerFactory.getLogger(BundleHelper.class).error(
                "Cannot check if the directory {} is a bundle, " + "cannot read the manifest file",
                directory.getName(), e);
        return false;
    } finally {
        IOUtils.closeQuietly(stream);
    }
}