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:com.isomorphic.maven.util.ArchiveUtils.java

/**
 * Builds a JAR file from the contents of a directory on the filesystem (recursively).
 * Adapted from stackoverflow solution.  
 * //from  ww  w  .  ja  va2s .c o  m
 * @see http://stackoverflow.com/questions/1281229/how-to-use-jaroutputstream-to-create-a-jar-file
 * 
 * @param directory the directory containing the content to be xzipped up
 * @param output the zip file to be written to
 */
public static void jar(File directory, File output) throws IOException {
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    output.getParentFile().mkdirs();
    JarOutputStream target = new JarOutputStream(new FileOutputStream(output), manifest);
    zip(directory, directory, target);
    target.close();
}

From source file:org.talend.commons.utils.resource.BundleFileUtil.java

public static String[] getBundleClassPath(Manifest manifest) {
    if (manifest == null) {
        return new String[0];
    }/*  w ww  .  j  a v a  2 s .  c  o  m*/
    Attributes mainAttributes = manifest.getMainAttributes();
    if (mainAttributes == null) {
        return new String[0];
    }

    String value = mainAttributes.getValue("Bundle-ClassPath"); //$NON-NLS-1$
    if (value == null) {
        return new String[0];
    }

    List<String> bundleCPs = new ArrayList<String>();
    final String[] entries = value.split(","); //$NON-NLS-1$
    for (String entry : entries) {
        bundleCPs.add(entry);
    }
    return bundleCPs.toArray(new String[0]);
}

From source file:adalid.util.info.JavaInfo.java

private static boolean extensionNameMatches(Manifest manifest, String regex) {
    if (StringUtils.isBlank(regex)) {
        return true;
    }//ww w. j  a  v  a 2s .  c o  m
    Attributes attributes = manifest.getMainAttributes();
    Object object = attributes.get(Attributes.Name.EXTENSION_NAME);
    if (object instanceof String) {
        String string = (String) object;
        return string.matches(regex);
    }
    return false;
}

From source file:info.dolezel.fatrat.plugins.helpers.JarClassLoader.java

private static void getPackageVersion(File fo, Map<String, String> rv) throws IOException {
    if (!fo.exists())
        return;//from w  ww  . j  a v a2s.c o m

    JarFile file = new JarFile(fo);
    Manifest manifest = file.getManifest();
    Attributes attr = manifest.getMainAttributes();

    rv.put(fo.getName(), attr.getValue("Implementation-Version"));
}

From source file:org.jahia.services.modulemanager.persistence.PersistentBundleInfoBuilder.java

/**
 * Parses the supplied resource and builds the information for the bundle.
 *
 * @param resource The bundle resource// w  w w. j a v  a 2 s . co m
 * @param calculateChecksum should we calculate the resource checksum?
 * @param checkTransformationRequired should we check if the module dependency capability headers has to be added
 * @return The information about the supplied bundle
 * @throws IOException In case of resource read errors
 */
public static PersistentBundle build(Resource resource, boolean calculateChecksum,
        boolean checkTransformationRequired) throws IOException {

    // populate data from manifest
    String groupId = null;
    String symbolicName = null;
    String version = null;
    String displayName = null;
    try (JarInputStream jarIs = new JarInputStream(resource.getInputStream())) {
        Manifest mf = jarIs.getManifest();
        if (mf != null) {
            Attributes attrs = mf.getMainAttributes();
            groupId = attrs.getValue(ATTR_NAME_GROUP_ID);
            symbolicName = attrs.getValue(ATTR_NAME_BUNDLE_SYMBOLIC_NAME);
            version = StringUtils.defaultIfBlank(attrs.getValue(ATTR_NAME_BUNDLE_VERSION),
                    attrs.getValue(ATTR_NAME_IMPL_VERSION));
            displayName = StringUtils.defaultIfBlank(attrs.getValue(ATTR_NAME_IMPL_TITLE),
                    attrs.getValue(ATTR_NAME_BUNDLE_NAME));
        }
    }

    if (StringUtils.isBlank(symbolicName) || StringUtils.isBlank(version)) {
        // not a valid JAR or bundle information is missing -> we stop here
        logger.warn("Not a valid JAR or bundle information is missing for resource " + resource);
        return null;
    }

    PersistentBundle bundleInfo = new PersistentBundle(groupId, symbolicName, version);
    bundleInfo.setDisplayName(displayName);
    if (calculateChecksum) {
        bundleInfo.setChecksum(calculateDigest(resource));
    }
    if (checkTransformationRequired) {
        bundleInfo.setTransformationRequired(isTransformationRequired(resource));
    }
    bundleInfo.setResource(resource);
    return bundleInfo;
}

From source file:com.stgmastek.core.comm.main.StartCoreCommunication.java

static void getBundleDetails() {
    strBundleDetailsArray_ = new String[] { "Unknown", "Unknown", "Unknown", Integer.MIN_VALUE + "",
            Integer.MIN_VALUE + "", Integer.MIN_VALUE + "" };
    String localFile = StartCoreCommunication.class.getProtectionDomain().getCodeSource().getLocation()
            .toString();/*w  w  w .ja  v  a2s .c  om*/
    localFile = localFile.concat("!/");
    String tmpString = "jar:";
    String localJarFileString = tmpString.concat(localFile);
    URL localJarFileURL;
    try {
        localJarFileURL = new URL(localJarFileString);
        JarURLConnection localJarFile = (JarURLConnection) localJarFileURL.openConnection();
        Manifest mf = localJarFile.getManifest();
        Attributes attributes = mf.getMainAttributes();
        strBundleDetailsArray_[0] = (String) attributes.getValue("Bundle-Name");
        strBundleDetailsArray_[1] = (String) attributes.getValue("Bundle-Version");
        strBundleDetailsArray_[2] = (String) attributes.getValue("Bundled-On");
        strBundleDetailsArray_[3] = (String) attributes.getValue("Major-Version");
        strBundleDetailsArray_[4] = (String) attributes.getValue("Minor-Version");
        strBundleDetailsArray_[5] = (String) attributes.getValue("Build-Number");
    } catch (MalformedURLException e) {
        // do nothing
    } catch (FileNotFoundException fnfe) {
        // do nothing
    } catch (IOException ioe) {
        // do nothing
    }
}

From source file:org.nuxeo.osgi.BundleManifestReader.java

public static Dictionary<String, String> getHeaders(Manifest mf) throws BundleException {
    Attributes attrs = mf.getMainAttributes();
    String symbolicName = attrs.getValue(Constants.BUNDLE_SYMBOLICNAME);
    if (symbolicName == null) {
        throw new BundleException("Missing " + Constants.BUNDLE_SYMBOLICNAME);
    }/*  ww w  . j  a  v a  2 s .c  o m*/
    Hashtable<String, String> headers = new Hashtable<String, String>();
    parseSymbolicName(headers, symbolicName);
    String val = attrs.getValue(Constants.BUNDLE_ACTIVATOR);
    if (val != null) {
        headers.put(Constants.BUNDLE_ACTIVATOR, val.trim());
    }
    val = attrs.getValue(Constants.BUNDLE_CLASSPATH);
    if (val != null) {
        headers.put(Constants.BUNDLE_CLASSPATH, val.trim());
    }
    val = attrs.getValue(Constants.BUNDLE_NAME);
    if (val != null) {
        headers.put(Constants.BUNDLE_NAME, val);
    }
    val = attrs.getValue(Constants.BUNDLE_VENDOR);
    if (val != null) {
        headers.put(Constants.BUNDLE_VENDOR, val);
    }
    val = attrs.getValue(Constants.BUNDLE_VERSION);
    if (val != null) {
        headers.put(Constants.BUNDLE_VERSION, val);
    }
    val = attrs.getValue(Constants.BUNDLE_DESCRIPTION);
    if (val != null) {
        headers.put(Constants.BUNDLE_DESCRIPTION, val);
    }
    val = attrs.getValue(Constants.BUNDLE_DOCURL);
    if (val != null) {
        headers.put(Constants.BUNDLE_DOCURL, val);
    }
    val = attrs.getValue(Constants.BUNDLE_COPYRIGHT);
    if (val != null) {
        headers.put(Constants.BUNDLE_COPYRIGHT, val);
    }
    val = attrs.getValue(Constants.BUNDLE_LOCALIZATION);
    if (val != null) {
        headers.put(Constants.BUNDLE_LOCALIZATION, val);
    }
    val = attrs.getValue(Constants.REQUIRE_BUNDLE);
    if (val != null) {
        headers.put(Constants.REQUIRE_BUNDLE, val);
    }
    val = attrs.getValue(Constants.FRAGMENT_HOST);
    if (val != null) {
        headers.put(Constants.FRAGMENT_HOST, val);
    }
    // Nuxeo headers
    for (String key : CUSTOM_HEADERS) {
        val = attrs.getValue(key);
        if (val != null) {
            headers.put(key, val);
        }
    }
    return headers;
}

From source file:org.sonar.updatecenter.deprecated.Plugin.java

public static Plugin extractMetadata(File file) throws IOException {
    JarFile jar = new JarFile(file);
    ZipEntry entry = jar.getEntry("META-INF/MANIFEST.MF");
    long timestamp = entry.getTime();
    Manifest manifest = jar.getManifest();
    jar.close();//  w w  w . ja  v  a 2 s.  c o  m

    Attributes attributes = manifest.getMainAttributes();
    String pluginKey = attributes.getValue("Plugin-Key");
    Plugin plugin = new Plugin(pluginKey);
    plugin.setName(attributes.getValue("Plugin-Name"));
    plugin.setVersion(attributes.getValue("Plugin-Version"));
    plugin.setRequiredSonarVersion(attributes.getValue("Sonar-Version"));
    plugin.setHomepage(attributes.getValue("Plugin-Homepage"));
    plugin.setDate(timestamp);
    return plugin;
}

From source file:org.apache.hadoop.util.TestClasspath.java

/**
 * Asserts that the specified file is a jar file with a manifest containing a
 * non-empty classpath attribute./*from w  ww .j  av  a2 s.  c o m*/
 *
 * @param file File to check
 * @throws IOException if there is an I/O error
 */
private static void assertJar(File file) throws IOException {
    JarFile jarFile = null;
    try {
        jarFile = new JarFile(file);
        Manifest manifest = jarFile.getManifest();
        assertNotNull(manifest);
        Attributes mainAttributes = manifest.getMainAttributes();
        assertNotNull(mainAttributes);
        assertTrue(mainAttributes.containsKey(Attributes.Name.CLASS_PATH));
        String classPathAttr = mainAttributes.getValue(Attributes.Name.CLASS_PATH);
        assertNotNull(classPathAttr);
        assertFalse(classPathAttr.isEmpty());
    } finally {
        // It's too bad JarFile doesn't implement Closeable.
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (IOException e) {
                LOG.warn("exception closing jarFile: " + jarFile, e);
            }
        }
    }
}

From source file:com.hurence.logisland.classloading.PluginLoader.java

/**
 * Scan for plugins./*from www  .ja  v  a 2s.c o m*/
 */
private static void scanAndRegisterPlugins() {
    Set<URL> urls = new HashSet<>();
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    while (cl != null) {
        if (cl instanceof URLClassLoader) {
            urls.addAll(Arrays.asList(((URLClassLoader) cl).getURLs()));
            cl = cl.getParent();
        }
    }

    for (URL url : urls) {
        try {
            Archive archive = null;
            try {
                archive = new JarFileArchive(
                        new File(URLDecoder.decode(url.getFile(), Charset.defaultCharset().name())), url);
            } catch (Exception e) {
                //silently swallowing exception. just skip the archive since not an archive
            }
            if (archive == null) {
                continue;
            }
            Manifest manifest = archive.getManifest();
            if (manifest != null) {
                String exportedPlugins = manifest.getMainAttributes()
                        .getValue(ManifestAttributes.MODULE_EXPORTS);
                if (exportedPlugins != null) {
                    String version = StringUtils.defaultIfEmpty(
                            manifest.getMainAttributes().getValue(ManifestAttributes.MODULE_VERSION),
                            "UNKNOWN");

                    logger.info("Loading components from module {}", archive.getUrl().toExternalForm());

                    final Archive arc = archive;

                    if (StringUtils.isNotBlank(exportedPlugins)) {
                        Arrays.stream(exportedPlugins.split(",")).map(String::trim).forEach(s -> {
                            if (registry.putIfAbsent(s, PluginClassloaderBuilder.build(arc)) == null) {
                                logger.info("Registered component '{}' version '{}'", s, version);
                            }

                        });
                    }
                }
            }

        } catch (Exception e) {
            logger.error("Unable to load components from " + url.toExternalForm(), e);
        }
    }
}