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.reficio.p2.utils.JarUtils.java

public static void adjustSnapshotOutputVersion(File inputFile, File outputFile, String version) {
    Jar jar = null;//from w  ww. ja  va  2 s  .  c  om
    try {
        jar = new Jar(inputFile);
        Manifest manifest = jar.getManifest();
        Attributes attributes = manifest.getMainAttributes();
        attributes.putValue(Analyzer.BUNDLE_VERSION, version);
        jar.write(outputFile);
    } catch (IOException e) {
        throw new RuntimeException("Cannot open jar " + outputFile);
    } catch (Exception e) {
        throw new RuntimeException("Cannot open jar " + outputFile);
    } finally {
        if (jar != null) {
            jar.close();
        }
    }
}

From source file:org.kuali.kra.test.infrastructure.ApplicationServer.java

/**
 * The jetty server's jsp compiler does not have access to the classpath artifacts to compile the jsps.
 * This method takes the current webapp classloader and creates one containing all of the
 * classpath artifacts on the test's classpath.
 *
 * See http://stackoverflow.com/questions/17685330/how-do-you-get-embedded-jetty-9-to-successfully-resolve-the-jstl-uri
 *
 * @param current the current webapp classpath
 * @return a classloader to replace it with
 * @throws IOException if an error occurs creating the classloader
 *//*from  w  w w .  j  av  a  2  s.c o  m*/
private static ClassLoader createClassLoaderForJasper(ClassLoader current) throws IOException {
    // Replace classloader with a new classloader with all URLs in manifests
    // from the parent loader bubbled up so Jasper looks at them.
    final ClassLoader parentLoader = current.getParent();
    if (current instanceof WebAppClassLoader && parentLoader instanceof URLClassLoader) {
        final LinkedList<URL> allURLs = new LinkedList<URL>(
                Arrays.asList(((URLClassLoader) parentLoader).getURLs()));

        for (URL url : ((LinkedList<URL>) allURLs.clone())) {
            try {
                final URLConnection conn = new URL("jar:" + url.toString() + "!/").openConnection();
                if (conn instanceof JarURLConnection) {
                    final JarURLConnection jconn = (JarURLConnection) conn;
                    final Manifest jarManifest = jconn.getManifest();
                    final String[] classPath = ((String) jarManifest.getMainAttributes().getValue("Class-Path"))
                            .split(" ");

                    for (String cpurl : classPath) {
                        allURLs.add(new URL(url, cpurl));
                    }
                }
            } catch (IOException | NullPointerException e) {
                //do nothing
            }
        }
        LOG.info("Creating new classloader for Application Server");
        return new WebAppClassLoader(new URLClassLoader(allURLs.toArray(new URL[] {}), parentLoader),
                ((WebAppClassLoader) current).getContext());
    }
    LOG.warn("Cannot create new classloader for app server " + current);
    return current;
}

From source file:org.eclipse.jdt.ls.core.internal.handlers.BundleUtils.java

private static BundleInfo getBundleInfo(String bundleLocation) throws IOException {
    try (JarFile jarFile = new JarFile(bundleLocation)) {
        Manifest manifest = jarFile.getManifest();
        if (manifest != null) {
            Attributes mainAttributes = manifest.getMainAttributes();
            if (mainAttributes != null) {
                String bundleVersion = mainAttributes.getValue(Constants.BUNDLE_VERSION);
                if (StringUtils.isBlank(bundleVersion)) {
                    return null;
                }/*from w w  w .jav  a 2  s  . c om*/
                String symbolicName = mainAttributes.getValue(Constants.BUNDLE_SYMBOLICNAME);
                if (StringUtils.isNotBlank(symbolicName) && symbolicName.indexOf(';') >= 0) {
                    symbolicName = symbolicName.substring(0, symbolicName.indexOf(';'));
                }
                return new BundleInfo(bundleVersion, symbolicName);
            }
        }
    }
    return null;
}

From source file:net.sf.keystore_explorer.crypto.jcepolicy.JcePolicyUtil.java

/**
 * Get a JCE policy's crypto strength.//from   w w w . j  ava 2 s  . c  o  m
 *
 * @param jcePolicy
 *            JCE policy
 * @return Crypto strength
 * @throws CryptoException
 *             If there was a problem getting the crypto strength
 */
public static CryptoStrength getCryptoStrength(JcePolicy jcePolicy) throws CryptoException {
    try {
        File file = getJarFile(jcePolicy);

        // if there is no policy file at all, we assume that we are running under OpenJDK
        if (!file.exists()) {
            return UNLIMITED;
        }

        JarFile jar = new JarFile(file);

        Manifest jarManifest = jar.getManifest();
        String strength = jarManifest.getMainAttributes().getValue("Crypto-Strength");

        // workaround for IBM JDK: test for maximum key size
        if (strength == null) {
            return unlimitedStrengthTest();
        }

        if (strength.equals(LIMITED.manifestValue())) {
            return LIMITED;
        } else {
            return UNLIMITED;
        }
    } catch (IOException ex) {
        throw new CryptoException(
                MessageFormat.format(res.getString("NoGetCryptoStrength.exception.message"), jcePolicy), ex);
    }
}

From source file:org.kse.crypto.jcepolicy.JcePolicyUtil.java

/**
 * Get a JCE policy's crypto strength.//from w ww .ja v a2 s  . c  o m
 *
 * @param jcePolicy
 *            JCE policy
 * @return Crypto strength
 * @throws CryptoException
 *             If there was a problem getting the crypto strength
 */
public static CryptoStrength getCryptoStrength(JcePolicy jcePolicy) throws CryptoException {
    JarFile jarFile = null;
    try {
        File file = getJarFile(jcePolicy);

        // if there is no policy file at all, we assume that we are running under OpenJDK
        if (!file.exists()) {
            return UNLIMITED;
        }

        jarFile = new JarFile(file);

        Manifest jarManifest = jarFile.getManifest();
        String strength = jarManifest.getMainAttributes().getValue("Crypto-Strength");

        // workaround for IBM JDK: test for maximum key size
        if (strength == null) {
            return unlimitedStrengthTest();
        }

        if (strength.equals(LIMITED.manifestValue())) {
            return LIMITED;
        } else {
            return UNLIMITED;
        }
    } catch (IOException ex) {
        throw new CryptoException(
                MessageFormat.format(res.getString("NoGetCryptoStrength.exception.message"), jcePolicy), ex);
    } finally {
        IOUtils.closeQuietly(jarFile);
    }
}

From source file:org.springframework.boot.devtools.restart.ChangeableUrls.java

private static List<URL> getUrlsFromManifestClassPathAttribute(URL jarUrl, JarFile jarFile) throws IOException {
    Manifest manifest = jarFile.getManifest();
    if (manifest == null) {
        return Collections.emptyList();
    }/*from w  w w . j  a v  a  2s  .  com*/
    String classPath = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
    if (!StringUtils.hasText(classPath)) {
        return Collections.emptyList();
    }
    String[] entries = StringUtils.delimitedListToStringArray(classPath, " ");
    List<URL> urls = new ArrayList<>(entries.length);
    List<URL> nonExistentEntries = new ArrayList<>();
    for (String entry : entries) {
        try {
            URL referenced = new URL(jarUrl, entry);
            if (new File(referenced.getFile()).exists()) {
                urls.add(referenced);
            } else {
                nonExistentEntries.add(referenced);
            }
        } catch (MalformedURLException ex) {
            throw new IllegalStateException("Class-Path attribute contains malformed URL", ex);
        }
    }
    if (!nonExistentEntries.isEmpty()) {
        System.out.println("The Class-Path manifest attribute in " + jarFile.getName()
                + " referenced one or more files that do not exist: "
                + StringUtils.collectionToCommaDelimitedString(nonExistentEntries));
    }
    return urls;
}

From source file:org.gatein.integration.jboss.as7.deployment.PortletBridgeDependencyProcessor.java

private static String getCdiPortletIntegrationVersion() {
    try {/*from ww w.  j av  a2  s.c  o  m*/
        Module module = Module.getBootModuleLoader().loadModule(CDI_PORTLET_INTEGRATION);
        Manifest mf = new Manifest(module.getClassLoader().getResourceAsStream("/META-INF/MANIFEST.MF"));
        return mf.getMainAttributes().getValue("Implementation-Version");
    } catch (Exception e) {
        return "";
    }
}

From source file:org.gatein.integration.jboss.as7.deployment.PortletBridgeDependencyProcessor.java

private static String getPortletBridgeVersion() {
    try {/*from  w w w  .j  a  v a2 s . co m*/
        Module module = Module.getBootModuleLoader().loadModule(PORTLETBRIDGE_IMPL);
        Manifest mf = new Manifest(module.getClassLoader().getResourceAsStream("/META-INF/MANIFEST.MF"));
        return mf.getMainAttributes().getValue("Implementation-Version");
    } catch (Exception e) {
        return "";
    }
}

From source file:org.hecl.jarhack.JarHack.java

/**
 * The <code>substHecl</code> method takes the filenames of two
 * .jar's - one as input, the second as output, in addition to the
 * name of the application.  Where it counts, the old name (Hecl,
 * usually) is overridden with the new name, and the new .jar file
 * is written to the specified outfile.  Via the iconname argument
 * it is also possible to specify a new icon file to use.
 *
 * @param infile a <code>FileInputStream</code> value
 * @param outfile a <code>String</code> value
 * @param newname a <code>String</code> value
 * @param iconname a <code>String</code> value
 * @exception IOException if an error occurs
 *//*w  ww . j a  v  a 2s. com*/
public static void substHecl(InputStream infile, String outfile, String newname, String iconname,
        String scriptfile) throws IOException {

    JarInputStream jif = new JarInputStream(infile);
    Manifest mf = jif.getManifest();
    Attributes attrs = mf.getMainAttributes();

    Set keys = attrs.keySet();
    Iterator it = keys.iterator();
    while (it.hasNext()) {
        Object key = it.next();
        Object value = attrs.get(key);
        String keyname = key.toString();

        /* These are the three cases that interest us in
         * particular, where we need to make changes. */
        if (keyname.equals("MIDlet-Name")) {
            attrs.putValue(keyname, newname);
        } else if (keyname.equals("MIDlet-1")) {
            String valuestr = value.toString();
            /* FIXME - the stringsplit method is used for older
             * versions of GCJ.  Once newer versions are common,
             * it can go away.  Or not - it works just fine. */
            String properties[] = stringsplit(valuestr, ", ");
            attrs.putValue(keyname, newname + ", " + properties[1] + ", " + properties[2]);
        } else if (keyname.equals("MicroEdition-Configuration")) {
            cldcversion = value.toString();
        } else if (keyname.equals("MicroEdition-Profile")) {
            midpversion = value.toString();
        } else if (keyname.equals("MIDlet-Jar-URL")) {
            attrs.put(key, newname + ".jar");
        }
    }

    JarOutputStream jof = new JarOutputStream(new FileOutputStream(outfile), mf);

    byte[] buf = new byte[4096];

    /* Go through the various entries. */
    JarEntry entry;
    int read;
    while ((entry = jif.getNextJarEntry()) != null) {

        /* Don't copy the manifest file. */
        if ("META-INF/MANIFEST.MF".equals(entry.getName()))
            continue;

        /* Insert our own icon */
        if (iconname != null && "Hecl.png".equals(entry.getName())) {
            jof.putNextEntry(new JarEntry("Hecl.png"));
            FileInputStream inf = new FileInputStream(iconname);
            while ((read = inf.read(buf)) != -1) {
                jof.write(buf, 0, read);
            }
            inf.close();
        }
        /* Insert our own copy of the script file. */
        else if ("script.hcl".equals(entry.getName())) {
            jof.putNextEntry(new JarEntry("script.hcl"));
            FileInputStream inf = new FileInputStream(scriptfile);
            while ((read = inf.read(buf)) != -1) {
                jof.write(buf, 0, read);
            }
            inf.close();
        } else {
            /* Otherwise, just copy the entry. */
            jof.putNextEntry(entry);
            while ((read = jif.read(buf)) != -1) {
                jof.write(buf, 0, read);
            }
        }

        jof.closeEntry();
    }

    jof.flush();
    jof.close();
    jif.close();
}

From source file:org.elasticsearch.bootstrap.JarHell.java

/** inspect manifest for sure incompatibilities */
static void checkManifest(Manifest manifest, Path jar) {
    // give a nice error if jar requires a newer java version
    String targetVersion = manifest.getMainAttributes().getValue("X-Compile-Target-JDK");
    if (targetVersion != null) {
        checkVersionFormat(targetVersion);
        checkJavaVersion(jar.toString(), targetVersion);
    }/*from   w  w w.  ja va  2  s. c  o  m*/

    // give a nice error if jar is compiled against different es version
    String systemESVersion = Version.CURRENT.toString();
    String targetESVersion = manifest.getMainAttributes().getValue("X-Compile-Elasticsearch-Version");
    if (targetESVersion != null && targetESVersion.equals(systemESVersion) == false) {
        throw new IllegalStateException(
                jar + " requires Elasticsearch " + targetESVersion + ", your system: " + systemESVersion);
    }
}