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:org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer.java

private static Class<?> getStartClass(List<URL> list) {
    logger.info("Searching manifests: " + list);
    for (URL url : list) {
        try {//from  w ww  . ja v a 2s. co  m
            logger.info("Searching manifest: " + url);
            InputStream inputStream = url.openStream();
            try {
                Manifest manifest = new Manifest(inputStream);
                String startClass = manifest.getMainAttributes().getValue("Start-Class");
                if (startClass != null) {
                    return ClassUtils.forName(startClass,
                            AbstractSpringFunctionAdapterInitializer.class.getClassLoader());
                }
            } finally {
                inputStream.close();
            }
        } catch (Exception ex) {
        }
    }
    return null;
}

From source file:com.arcusys.liferay.vaadinplugin.util.ControlPanelPortletUtil.java

private static String getManifestAttribute(JarFile jarFile, String versionAttribute) throws IOException {
    Manifest manifest = jarFile.getManifest();
    if (manifest == null) {
        return null;
    }// ww w . ja  va2  s. c om
    Attributes attr = manifest.getMainAttributes();
    String bundleName = attr.getValue("Bundle-Name");
    if (bundleName != null) {
        return attr.getValue(versionAttribute);
    }

    return null;
}

From source file:com.arcusys.liferay.vaadinplugin.util.ControlPanelPortletUtil.java

private static String getManifestAttributeForVaadin6(JarFile jarFile, String versionAttribute)
        throws IOException {
    Manifest manifest = jarFile.getManifest();
    if (manifest == null) {
        return null;
    }//  w  w w  .j  av  a2s  .c o  m
    Attributes attr = manifest.getMainAttributes();
    String bundleName = attr.getValue("Bundle-Name");
    if (bundleName != null && bundleName.equals("Vaadin")) {
        return attr.getValue(versionAttribute);
    }

    return null;
}

From source file:org.rhq.enterprise.server.xmlschema.ServerPluginDescriptorUtil.java

/**
 * Returns the version for the plugin represented by the given descriptor/file.
 * If the descriptor defines a version, that is considered the version of the plugin.
 * However, if the plugin descriptor does not define a version, the plugin jar's manifest
 * is searched for an implementation version string and if one is found that is the version
 * of the plugin. If the manifest entry is also not found, the plugin does not have a version
 * associated with it, which causes this method to throw an exception.
 * //  w  w  w.  j  a v a 2 s .  c  o  m
 * @param pluginFile the plugin jar
 * @param descriptor the plugin descriptor as found in the plugin jar (if <code>null</code>,
 *                   the plugin file will be read and the descriptor parsed from it)
 * @return the version of the plugin
 * @throws Exception if the plugin is invalid, there is no version for the plugin or the version string is invalid
 */
public static ComparableVersion getPluginVersion(File pluginFile, ServerPluginDescriptorType descriptor)
        throws Exception {

    if (descriptor == null) {
        descriptor = loadPluginDescriptorFromUrl(pluginFile.toURI().toURL());
        if (descriptor == null) {
            throw new Exception("Plugin is missing a descriptor: " + pluginFile);
        }
    }

    String version = descriptor.getVersion();
    if (version == null) {
        Manifest manifest = getManifest(pluginFile);
        if (manifest != null) {
            version = manifest.getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_VERSION);
        }
    }

    if (version == null) {
        throw new Exception("No version is defined for plugin jar [" + pluginFile
                + "]. A version must be defined either via the MANIFEST.MF ["
                + Attributes.Name.IMPLEMENTATION_VERSION
                + "] attribute or via the plugin descriptor 'version' attribute.");
    }

    try {
        return new ComparableVersion(version);
    } catch (RuntimeException e) {
        throw new Exception("Version [" + version + "] for [" + pluginFile + "] did not parse", e);
    }
}

From source file:tilda.db.ConnectionPool.java

private static void LoadTildaResources(Connection C, boolean Migrate) throws Exception {
    Reader R = null;//w  w  w  .  j av a2s.co  m
    InputStream In = null;
    Enumeration<URL> resEnum = ConnectionPool.class.getClassLoader().getResources(JarFile.MANIFEST_NAME);
    List<Schema> TildaList = new ArrayList<Schema>();
    while (resEnum.hasMoreElements()) {
        URL url = (URL) resEnum.nextElement();
        In = url.openStream();
        if (In != null) {
            Manifest Man = new Manifest();
            Man.read(In);
            In.close();
            String Tildas = Man.getMainAttributes().getValue("Tilda");
            if (TextUtil.isNullOrEmpty(Tildas) == false) {
                LOG.debug("Found Tilda(s) " + Tildas + " in " + url.toString());
                String[] parts = Tildas.split(";");
                if (parts != null)
                    for (String p : parts) {
                        if (TextUtil.isNullOrEmpty(p) == true)
                            continue;
                        p = p.trim();
                        In = FileUtil.getResourceAsStream(p);
                        if (In == null)
                            throw new Exception(
                                    "Tilda schema definition '" + p + "' could not be found in the classpath.");
                        LOG.info("Inspecting " + p);
                        R = new BufferedReader(new InputStreamReader(In));
                        Gson gson = new GsonBuilder().setPrettyPrinting().create();
                        Schema S = gson.fromJson(R, Schema.class);
                        S.setOrigin(p);
                        TildaList.add(S);
                        In.close();
                    }
            }
        }
    }
    ReorderTildaListWithDependencies(TildaList);
    // LOG.debug("All Tildas in order of dependencies:");
    // for (Schema S : TildaList)
    // LOG.debug(" "+S._ResourceNameShort);
    if (Migrate == false)
        Migrator.logMigrationWarning();
    int warnings = 0;
    for (Schema S : TildaList) {
        int w = Migrator.migrate(C, S, Migrate);
        if (w != 0 && Migrate == false) {
            warnings += w;
            LOG.warn("There were " + w
                    + " warning(s) issued because schema discrepencies were found but not fixed.");
            Migrator.logMigrationWarning();
            continue;
        }
        LOG.debug("Initializing Schema objects");
        Method M = Class.forName(tilda.generation.java8.Helper.getSupportClassFullName(S))
                .getMethod("initSchema", Connection.class);
        M.invoke(null, C);
        _SchemaPackage.put(S._Name, S._Package);
        C.commit();
    }
    LOG.debug("");
    LOG.debug("Creating/updating Tilda helper stored procedures.");
    if (Migrate == false)
        Migrator.logMigrationWarning();
    else if (C.addHelperFunctions() == false)
        throw new Exception("Cannot upgrade schema by adding the Tilda helper functions.");

    if (warnings != 0 && Migrate == false) {
        LOG.warn("");
        LOG.warn("");
        LOG.warn("There were a total of " + warnings
                + " warning(s) issued because schema discrepencies were found but not fixed.");
        Migrator.logMigrationWarning();
    }

    C.commit();
}

From source file:io.selendroid.standalone.builder.SelendroidServerBuilder.java

private static String getVersionFromManifest(String path) throws IOException {
    String manifestPath = path.substring(0, path.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF";
    Manifest manifest = new Manifest(new URL(manifestPath).openStream());
    Attributes attr = manifest.getMainAttributes();
    return attr.getValue("version");
}

From source file:org.wso2.carbon.wsdl2code.POMGenerator.java

public static String getVersion(String artifact) throws Exception {

    java.io.File file = new java.io.File(artifact);
    java.util.jar.JarFile jar = new java.util.jar.JarFile(file);
    java.util.jar.Manifest manifest = jar.getManifest();

    String versionNumber = "";
    java.util.jar.Attributes attributes = manifest.getMainAttributes();
    if (attributes != null) {
        java.util.Iterator it = attributes.keySet().iterator();
        while (it.hasNext()) {
            java.util.jar.Attributes.Name key = (java.util.jar.Attributes.Name) it.next();
            String keyword = key.toString();
            if (keyword.equals("Implementation-Version") || keyword.equals("Bundle-Version")) {
                versionNumber = (String) attributes.get(key);
                break;
            }//from   w  w  w  . jav a  2s  . com
        }
    }
    jar.close();

    if (versionNumber != null && !versionNumber.equals("")) {
        return versionNumber;
    }

    //if manifest does not contain version number it had to be extracted from the file name
    String fileName = file.getName().substring(0, file.getName().lastIndexOf("."));
    if (fileName.contains(".")) {
        String majorVersion = fileName.substring(0, fileName.indexOf("."));
        String minorVersion = fileName.substring(fileName.indexOf("."));
        int delimiter = majorVersion.lastIndexOf("-");
        if (majorVersion.indexOf("_") > delimiter)
            delimiter = majorVersion.indexOf("_");
        majorVersion = majorVersion.substring(delimiter + 1, fileName.indexOf("."));
        versionNumber = majorVersion + minorVersion;
    }

    return versionNumber;

}

From source file:pxb.android.tinysign.TinySign.java

private static String writeMF(File dir, Manifest manifest, ZipOutputStream zos)
        throws NoSuchAlgorithmException, IOException {
    MessageDigest md = MessageDigest.getInstance("SHA1");
    DigestOutputStream dos = new DigestOutputStream(zos, md);
    zipAndSha1(dir, zos, dos, manifest);
    Attributes main = manifest.getMainAttributes();
    main.putValue("Manifest-Version", "1.0");
    main.putValue("Created-By", "tiny-sign-" + TinySign.class.getPackage().getImplementationVersion());
    zos.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF"));
    manifest.write(dos);/* w  ww .j  a  v a2s  .  c om*/
    zos.closeEntry();
    return eBase64(md.digest());
}

From source file:org.orbisgis.core.plugin.BundleTools.java

private static List<String> getClassPath() {
    List<String> classPath = new LinkedList<String>();
    // Read declared class in the manifest
    try {//from ww  w .  ja  v a  2s  .c  o m
        Enumeration<URL> resources = BundleTools.class.getClassLoader().getResources("META-INF/MANIFEST.MF");
        while (resources.hasMoreElements()) {
            URL url = resources.nextElement();
            try {
                Manifest manifest = new Manifest(url.openStream());
                String value = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
                if (value != null) {
                    String[] pathElements = value.split(" ");
                    if (pathElements == null) {
                        pathElements = new String[] { value };
                    }
                    classPath.addAll(Arrays.asList(pathElements));
                }
            } catch (IOException ex) {
                LOGGER.warn("Unable to retrieve Jar MANIFEST " + url, ex);
            }
        }
    } catch (IOException ex) {
        LOGGER.warn("Unable to retrieve Jar MANIFEST", ex);
    }
    // Read packages in the class path
    String javaClasspath = System.getProperty("java.class.path");
    String[] pathElements = javaClasspath.split(":");
    if (pathElements == null) {
        pathElements = new String[] { javaClasspath };
    }
    classPath.addAll(Arrays.asList(pathElements));
    return classPath;
}

From source file:org.jahia.services.modulemanager.util.ModuleUtils.java

/**
 * Performs the transformation of the capability attributes in the MANIFEST.MF file of the supplied stream.
 *
 * @param sourceStream the source stream for the bundle, which manifest has to be adjusted w.r.t. module dependencies; the stream is
 *            closed after returning from this method
 * @return the transformed stream for the bundle with adjusted manifest
 * @throws IOException in case of I/O errors
 */// w w w  . ja v  a 2 s .c om
public static InputStream addModuleDependencies(InputStream sourceStream) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    try (ZipInputStream zis = new ZipInputStream(sourceStream);
            ZipOutputStream zos = new ZipOutputStream(out);) {
        ZipEntry zipEntry = zis.getNextEntry();
        while (zipEntry != null) {
            zos.putNextEntry(new ZipEntry(zipEntry.getName()));
            if (JarFile.MANIFEST_NAME.equals(zipEntry.getName())) {
                // we read the manifest from the source stream
                Manifest mf = new Manifest();
                mf.read(zis);

                addCapabilities(mf.getMainAttributes());

                // write the manifest entry into the target output stream
                mf.write(zos);
            } else {
                IOUtils.copy(zis, zos);
            }
            zis.closeEntry();
            zipEntry = zis.getNextEntry();
        }
    }

    return new ByteArrayInputStream(out.toByteArray());
}