Example usage for java.util.jar JarFile JarFile

List of usage examples for java.util.jar JarFile JarFile

Introduction

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

Prototype

public JarFile(File file, boolean verify) throws IOException 

Source Link

Document

Creates a new JarFile to read from the specified File object.

Usage

From source file:mobac.mapsources.loader.MapPackManager.java

/**
 * Verifies the class file signatures of the specified map pack
 * /*from  w w  w . j  a va  2 s.  c  o m*/
 * @param mapPackFile
 * @throws IOException
 * @throws CertificateException
 */
public void testMapPack(File mapPackFile) throws IOException, CertificateException {
    String fileName = mapPackFile.getName();
    JarFile jf = new JarFile(mapPackFile, true);
    try {
        Enumeration<JarEntry> it = jf.entries();
        while (it.hasMoreElements()) {
            JarEntry entry = it.nextElement();
            // We verify only class files
            if (!entry.getName().endsWith(".class"))
                continue; // directory or other entry
            // Get the input stream (triggers) the signature verification for the specific class
            Utilities.readFully(jf.getInputStream(entry));
            if (entry.getCodeSigners() == null)
                throw new CertificateException("Unsigned class file found: " + entry.getName());
            CodeSigner signer = entry.getCodeSigners()[0];
            List<? extends Certificate> cp = signer.getSignerCertPath().getCertificates();
            if (cp.size() > 1)
                throw new CertificateException("Signature certificate not accepted: "
                        + "certificate path contains more than one certificate");
            // Compare the used certificate with the mapPack certificate
            if (!mapPackCert.equals(cp.get(0)))
                throw new CertificateException(
                        "Signature certificate not accepted: " + "not the MapPack signer certificate");
        }
        Manifest mf = jf.getManifest();
        Attributes a = mf.getMainAttributes();
        String mpv = a.getValue("MapPackVersion");
        if (mpv == null)
            throw new IOException("MapPackVersion info missing!");
        int mapPackVersion = Integer.parseInt(mpv);
        if (requiredMapPackVersion != mapPackVersion)
            throw new IOException("This pack \"" + fileName + "\" is not compatible with this MOBAC version.");
        ZipEntry entry = jf.getEntry("META-INF/services/mobac.program.interfaces.MapSource");
        if (entry == null)
            throw new IOException("MapSources services list is missing in file " + fileName);
    } finally {
        jf.close();
    }

}

From source file:ffx.FFXClassLoader.java

private void loadExtensions() {
    if (extensionsLoaded) {
        return;/* ww w  .j ava  2  s  .  c o m*/
    }
    extensionsLoaded = true;

    String extensionJarsAndDlls[] = FFX_FILES.toArray(new String[FFX_FILES.size()]);

    // Compute DLLs prefix and suffix
    String dllSuffix;
    String dllSuffix2 = null;
    String dllPrefix;

    String osName = System.getProperty("os.name");
    if (osName.startsWith("Windows")) {
        dllSuffix = ".dll";
        dllPrefix = "";
    } else if (osName.startsWith("Mac OS X")) {
        dllSuffix = ".jnilib";
        dllSuffix2 = ".dylib";
        dllPrefix = "lib";
    } else {
        dllSuffix = ".so";
        dllPrefix = "lib";
    }

    // Find extension Jars and DLLs
    ArrayList<JarFile> extensionJarList = new ArrayList<>();
    for (String extensionJarOrDll : extensionJarsAndDlls) {
        try {
            URL extensionJarOrDllUrl = getResource(extensionJarOrDll);
            if (extensionJarOrDllUrl != null) {
                int lastSlashIndex = extensionJarOrDll.lastIndexOf('/');
                if (extensionJarOrDll.endsWith(".jar")) {
                    int start = lastSlashIndex + 1;
                    int end = extensionJarOrDll.indexOf(".jar");
                    String name = extensionJarOrDll.substring(start, end);
                    // Copy jar to a tmp file
                    String extensionJar = copyInputStreamToTmpFile(extensionJarOrDllUrl.openStream(), name,
                            ".jar");
                    // Add extracted file to the extension jars list
                    extensionJarList.add(new JarFile(extensionJar, false));
                    if (name.equals("gluegen-rt")) {
                        gluegen = extensionJar.substring(0, extensionJar.length() - 4);
                    } else if (name.equals("jogl-all")) {
                        jogl = extensionJar.substring(0, extensionJar.length() - 4);
                    } else if (name.equals("jocl")) {
                        jocl = extensionJar.substring(0, extensionJar.length() - 4);
                    }
                } else if (extensionJarOrDll.endsWith(dllSuffix)) {
                    int start = lastSlashIndex + 1 + dllPrefix.length();
                    int end = extensionJarOrDll.indexOf(dllSuffix);
                    String name = extensionJarOrDll.substring(start, end);
                    // Copy DLL to a tmp file
                    String extensionDll = copyInputStreamToTmpFile(extensionJarOrDllUrl.openStream(), name,
                            dllSuffix);
                    // Add extracted file to extension DLLs map
                    extensionDlls.put(name, extensionDll);
                } else if (dllSuffix2 != null && extensionJarOrDll.endsWith(dllSuffix2)) {
                    int start = lastSlashIndex + 1 + dllPrefix.length();
                    int end = extensionJarOrDll.indexOf(dllSuffix2);
                    String name = extensionJarOrDll.substring(start, end);
                    // Copy DLL to a tmp file
                    String extensionDll = copyInputStreamToTmpFile(extensionJarOrDllUrl.openStream(), name,
                            dllSuffix2);
                    // Add extracted file to extension DLLs map
                    extensionDlls.put(name, extensionDll);
                }
            }
        } catch (IOException ex) {
            System.out.println(extensionJarOrDll);
            throw new RuntimeException(" Couldn't extract extension jar.\n", ex);
        }
    }

    // Create extensionJars array
    if (extensionJarList.size() > 0) {
        extensionJars = (JarFile[]) extensionJarList.toArray(new JarFile[extensionJarList.size()]);
    }
}