Search in sources :

Example 36 with Manifest

use of java.util.jar.Manifest in project intellij-community by JetBrains.

the class ManifestFileUtil method createManifestFileConfiguration.

@NotNull
public static ManifestFileConfiguration createManifestFileConfiguration(@NotNull VirtualFile manifestFile) {
    final String path = manifestFile.getPath();
    Manifest manifest = readManifest(manifestFile);
    String mainClass = manifest.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
    final String classpathText = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
    final List<String> classpath = new ArrayList<>();
    if (classpathText != null) {
        classpath.addAll(StringUtil.split(classpathText, " "));
    }
    return new ManifestFileConfiguration(path, classpath, mainClass, manifestFile.isWritable());
}
Also used : ArrayList(java.util.ArrayList) ManifestFileConfiguration(com.intellij.packaging.ui.ManifestFileConfiguration) Manifest(java.util.jar.Manifest) NotNull(org.jetbrains.annotations.NotNull)

Example 37 with Manifest

use of java.util.jar.Manifest in project intellij-community by JetBrains.

the class ManifestFileUtil method updateManifest.

public static void updateManifest(@NotNull final VirtualFile file, @Nullable final String mainClass, @Nullable final List<String> classpath, final boolean replaceValues) {
    final Manifest manifest = readManifest(file);
    final Attributes mainAttributes = manifest.getMainAttributes();
    if (mainClass != null) {
        mainAttributes.put(Attributes.Name.MAIN_CLASS, mainClass);
    } else if (replaceValues) {
        mainAttributes.remove(Attributes.Name.MAIN_CLASS);
    }
    if (classpath != null && !classpath.isEmpty()) {
        List<String> updatedClasspath;
        if (replaceValues) {
            updatedClasspath = classpath;
        } else {
            updatedClasspath = new ArrayList<>();
            final String oldClasspath = (String) mainAttributes.get(Attributes.Name.CLASS_PATH);
            if (!StringUtil.isEmpty(oldClasspath)) {
                updatedClasspath.addAll(StringUtil.split(oldClasspath, " "));
            }
            for (String path : classpath) {
                if (!updatedClasspath.contains(path)) {
                    updatedClasspath.add(path);
                }
            }
        }
        mainAttributes.put(Attributes.Name.CLASS_PATH, StringUtil.join(updatedClasspath, " "));
    } else if (replaceValues) {
        mainAttributes.remove(Attributes.Name.CLASS_PATH);
    }
    ManifestBuilder.setVersionAttribute(mainAttributes);
    ApplicationManager.getApplication().runWriteAction(() -> {
        try {
            final OutputStream outputStream = file.getOutputStream(ManifestFileUtil.class);
            try {
                manifest.write(outputStream);
            } finally {
                outputStream.close();
            }
        } catch (IOException e) {
            LOG.info(e);
        }
    });
}
Also used : OutputStream(java.io.OutputStream) Attributes(java.util.jar.Attributes) IOException(java.io.IOException) Manifest(java.util.jar.Manifest)

Example 38 with Manifest

use of java.util.jar.Manifest in project Activiti by Activiti.

the class BarDeploymentListener method canHandle.

public boolean canHandle(File artifact) {
    JarFile jar = null;
    try {
        if (!artifact.getName().endsWith(".bar")) {
            return false;
        }
        jar = new JarFile(artifact);
        // Only handle non OSGi bundles
        Manifest m = jar.getManifest();
        if (m != null && m.getMainAttributes().getValue(new Attributes.Name(BUNDLE_SYMBOLICNAME)) != null && m.getMainAttributes().getValue(new Attributes.Name(BUNDLE_VERSION)) != null) {
            return false;
        }
        return true;
    } catch (Exception e) {
        return false;
    } finally {
        if (jar != null) {
            try {
                jar.close();
            } catch (IOException e) {
                LOGGER.error("Unable to close jar", e);
            }
        }
    }
}
Also used : Attributes(java.util.jar.Attributes) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) IOException(java.io.IOException)

Example 39 with Manifest

use of java.util.jar.Manifest in project XobotOS by xamarin.

the class PackageParser method collectCertificates.

public boolean collectCertificates(Package pkg, int flags) {
    pkg.mSignatures = null;
    WeakReference<byte[]> readBufferRef;
    byte[] readBuffer = null;
    synchronized (mSync) {
        readBufferRef = mReadBuffer;
        if (readBufferRef != null) {
            mReadBuffer = null;
            readBuffer = readBufferRef.get();
        }
        if (readBuffer == null) {
            readBuffer = new byte[8192];
            readBufferRef = new WeakReference<byte[]>(readBuffer);
        }
    }
    try {
        JarFile jarFile = new JarFile(mArchiveSourcePath);
        Certificate[] certs = null;
        if ((flags & PARSE_IS_SYSTEM) != 0) {
            // If this package comes from the system image, then we
            // can trust it...  we'll just use the AndroidManifest.xml
            // to retrieve its signatures, not validating all of the
            // files.
            JarEntry jarEntry = jarFile.getJarEntry(ANDROID_MANIFEST_FILENAME);
            certs = loadCertificates(jarFile, jarEntry, readBuffer);
            if (certs == null) {
                Slog.e(TAG, "Package " + pkg.packageName + " has no certificates at entry " + jarEntry.getName() + "; ignoring!");
                jarFile.close();
                mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
                return false;
            }
            if (DEBUG_JAR) {
                Slog.i(TAG, "File " + mArchiveSourcePath + ": entry=" + jarEntry + " certs=" + (certs != null ? certs.length : 0));
                if (certs != null) {
                    final int N = certs.length;
                    for (int i = 0; i < N; i++) {
                        Slog.i(TAG, "  Public key: " + certs[i].getPublicKey().getEncoded() + " " + certs[i].getPublicKey());
                    }
                }
            }
        } else {
            Enumeration<JarEntry> entries = jarFile.entries();
            final Manifest manifest = jarFile.getManifest();
            while (entries.hasMoreElements()) {
                final JarEntry je = entries.nextElement();
                if (je.isDirectory())
                    continue;
                final String name = je.getName();
                if (name.startsWith("META-INF/"))
                    continue;
                if (ANDROID_MANIFEST_FILENAME.equals(name)) {
                    final Attributes attributes = manifest.getAttributes(name);
                    pkg.manifestDigest = ManifestDigest.fromAttributes(attributes);
                }
                final Certificate[] localCerts = loadCertificates(jarFile, je, readBuffer);
                if (DEBUG_JAR) {
                    Slog.i(TAG, "File " + mArchiveSourcePath + " entry " + je.getName() + ": certs=" + certs + " (" + (certs != null ? certs.length : 0) + ")");
                }
                if (localCerts == null) {
                    Slog.e(TAG, "Package " + pkg.packageName + " has no certificates at entry " + je.getName() + "; ignoring!");
                    jarFile.close();
                    mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
                    return false;
                } else if (certs == null) {
                    certs = localCerts;
                } else {
                    // Ensure all certificates match.
                    for (int i = 0; i < certs.length; i++) {
                        boolean found = false;
                        for (int j = 0; j < localCerts.length; j++) {
                            if (certs[i] != null && certs[i].equals(localCerts[j])) {
                                found = true;
                                break;
                            }
                        }
                        if (!found || certs.length != localCerts.length) {
                            Slog.e(TAG, "Package " + pkg.packageName + " has mismatched certificates at entry " + je.getName() + "; ignoring!");
                            jarFile.close();
                            mParseError = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
                            return false;
                        }
                    }
                }
            }
        }
        jarFile.close();
        synchronized (mSync) {
            mReadBuffer = readBufferRef;
        }
        if (certs != null && certs.length > 0) {
            final int N = certs.length;
            pkg.mSignatures = new Signature[certs.length];
            for (int i = 0; i < N; i++) {
                pkg.mSignatures[i] = new Signature(certs[i].getEncoded());
            }
        } else {
            Slog.e(TAG, "Package " + pkg.packageName + " has no certificates; ignoring!");
            mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
            return false;
        }
    } catch (CertificateEncodingException e) {
        Slog.w(TAG, "Exception reading " + mArchiveSourcePath, e);
        mParseError = PackageManager.INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING;
        return false;
    } catch (IOException e) {
        Slog.w(TAG, "Exception reading " + mArchiveSourcePath, e);
        mParseError = PackageManager.INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING;
        return false;
    } catch (RuntimeException e) {
        Slog.w(TAG, "Exception reading " + mArchiveSourcePath, e);
        mParseError = PackageManager.INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION;
        return false;
    }
    return true;
}
Also used : Attributes(java.util.jar.Attributes) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) Manifest(java.util.jar.Manifest) Certificate(java.security.cert.Certificate)

Example 40 with Manifest

use of java.util.jar.Manifest in project jdk8u_jdk by JetBrains.

the class JarUtils method createJar.

/**
     * Create jar file with specified files. If a specified file does not exist,
     * a new jar entry will be created with the file name itself as the content.
     */
public static void createJar(String dest, Path filesLocation, String... fileNames) throws IOException {
    try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(dest), new Manifest())) {
        for (String fileName : fileNames) {
            System.out.println(String.format("Adding %s to %s", fileName, dest));
            // add an archive entry, and write a file
            jos.putNextEntry(new JarEntry(fileName));
            File file;
            if (filesLocation != null) {
                file = filesLocation.resolve(fileName).toFile();
            } else {
                file = new File(fileName);
            }
            try (FileInputStream fis = new FileInputStream(file)) {
                Utils.transferBetweenStreams(fis, jos);
            } catch (FileNotFoundException e) {
                jos.write(fileName.getBytes());
            }
        }
    }
    System.out.println();
}
Also used : FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

Manifest (java.util.jar.Manifest)1226 Attributes (java.util.jar.Attributes)392 File (java.io.File)391 IOException (java.io.IOException)336 JarFile (java.util.jar.JarFile)231 InputStream (java.io.InputStream)184 URL (java.net.URL)177 JarOutputStream (java.util.jar.JarOutputStream)145 FileOutputStream (java.io.FileOutputStream)131 Test (org.junit.Test)129 FileInputStream (java.io.FileInputStream)119 Jar (aQute.bnd.osgi.Jar)105 JarInputStream (java.util.jar.JarInputStream)104 Builder (aQute.bnd.osgi.Builder)99 ZipEntry (java.util.zip.ZipEntry)99 ByteArrayOutputStream (java.io.ByteArrayOutputStream)96 JarEntry (java.util.jar.JarEntry)96 ByteArrayInputStream (java.io.ByteArrayInputStream)93 ArrayList (java.util.ArrayList)83 Map (java.util.Map)83