Search in sources :

Example 6 with Manifest

use of java.util.jar.Manifest in project jetty.project by eclipse.

the class WarBundleManifestGenerator method createBundleManifest.

public static Manifest createBundleManifest(Manifest originalManifest, URL url, JarFile jarFile) {
    Manifest res = new Manifest();
    res.getMainAttributes().putAll(createBundleManifest(originalManifest.getMainAttributes(), url.toString(), jarFile));
    return res;
}
Also used : Manifest(java.util.jar.Manifest)

Example 7 with Manifest

use of java.util.jar.Manifest in project jetty.project by eclipse.

the class JarResource method copyTo.

/* ------------------------------------------------------------ */
@Override
public void copyTo(File directory) throws IOException {
    if (!exists())
        return;
    if (LOG.isDebugEnabled())
        LOG.debug("Extract " + this + " to " + directory);
    String urlString = this.getURL().toExternalForm().trim();
    int endOfJarUrl = urlString.indexOf("!/");
    int startOfJarUrl = (endOfJarUrl >= 0 ? 4 : 0);
    if (endOfJarUrl < 0)
        throw new IOException("Not a valid jar url: " + urlString);
    URL jarFileURL = new URL(urlString.substring(startOfJarUrl, endOfJarUrl));
    String subEntryName = (endOfJarUrl + 2 < urlString.length() ? urlString.substring(endOfJarUrl + 2) : null);
    boolean subEntryIsDir = (subEntryName != null && subEntryName.endsWith("/") ? true : false);
    if (LOG.isDebugEnabled())
        LOG.debug("Extracting entry = " + subEntryName + " from jar " + jarFileURL);
    URLConnection c = jarFileURL.openConnection();
    c.setUseCaches(false);
    try (InputStream is = c.getInputStream();
        JarInputStream jin = new JarInputStream(is)) {
        JarEntry entry;
        boolean shouldExtract;
        while ((entry = jin.getNextJarEntry()) != null) {
            String entryName = entry.getName();
            if ((subEntryName != null) && (entryName.startsWith(subEntryName))) {
                // is the subentry really a dir?
                if (!subEntryIsDir && subEntryName.length() + 1 == entryName.length() && entryName.endsWith("/"))
                    subEntryIsDir = true;
                //extract it.
                if (subEntryIsDir) {
                    //if it is a subdirectory we are looking for, then we
                    //are looking to extract its contents into the target
                    //directory. Remove the name of the subdirectory so
                    //that we don't wind up creating it too.
                    entryName = entryName.substring(subEntryName.length());
                    if (!entryName.equals("")) {
                        //the entry is
                        shouldExtract = true;
                    } else
                        shouldExtract = false;
                } else
                    shouldExtract = true;
            } else if ((subEntryName != null) && (!entryName.startsWith(subEntryName))) {
                //there is a particular entry we are looking for, and this one
                //isn't it
                shouldExtract = false;
            } else {
                //we are extracting everything
                shouldExtract = true;
            }
            if (!shouldExtract) {
                if (LOG.isDebugEnabled())
                    LOG.debug("Skipping entry: " + entryName);
                continue;
            }
            String dotCheck = entryName.replace('\\', '/');
            dotCheck = URIUtil.canonicalPath(dotCheck);
            if (dotCheck == null) {
                if (LOG.isDebugEnabled())
                    LOG.debug("Invalid entry: " + entryName);
                continue;
            }
            File file = new File(directory, entryName);
            if (entry.isDirectory()) {
                // Make directory
                if (!file.exists())
                    file.mkdirs();
            } else {
                // make directory (some jars don't list dirs)
                File dir = new File(file.getParent());
                if (!dir.exists())
                    dir.mkdirs();
                // Make file
                try (OutputStream fout = new FileOutputStream(file)) {
                    IO.copy(jin, fout);
                }
                // touch the file.
                if (entry.getTime() >= 0)
                    file.setLastModified(entry.getTime());
            }
        }
        if ((subEntryName == null) || (subEntryName != null && subEntryName.equalsIgnoreCase("META-INF/MANIFEST.MF"))) {
            Manifest manifest = jin.getManifest();
            if (manifest != null) {
                File metaInf = new File(directory, "META-INF");
                metaInf.mkdir();
                File f = new File(metaInf, "MANIFEST.MF");
                try (OutputStream fout = new FileOutputStream(f)) {
                    manifest.write(fout);
                }
            }
        }
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) FilterInputStream(java.io.FilterInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) Manifest(java.util.jar.Manifest) URL(java.net.URL) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 8 with Manifest

use of java.util.jar.Manifest in project tomcat by apache.

the class ExtensionValidator method addSystemResource.

/**
     * Checks to see if the given system JAR file contains a MANIFEST, and adds
     * it to the container's manifest resources.
     *
     * @param jarFile The system JAR whose manifest to add
     * @throws IOException Error reading JAR file
     */
public static void addSystemResource(File jarFile) throws IOException {
    try (InputStream is = new FileInputStream(jarFile)) {
        Manifest manifest = getManifest(is);
        if (manifest != null) {
            ManifestResource mre = new ManifestResource(jarFile.getAbsolutePath(), manifest, ManifestResource.SYSTEM);
            containerManifestResources.add(mre);
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) Manifest(java.util.jar.Manifest) FileInputStream(java.io.FileInputStream)

Example 9 with Manifest

use of java.util.jar.Manifest in project buck by facebook.

the class JarDirectoryStepHelper method createManifest.

private static Manifest createManifest(ProjectFilesystem filesystem, ImmutableSortedSet<Path> entriesToJar, Optional<String> mainClass, Optional<Path> manifestFile, boolean mergeManifests) throws IOException {
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    if (mergeManifests) {
        for (Path entry : entriesToJar) {
            entry = filesystem.getPathForRelativePath(entry);
            Manifest readManifest;
            if (Files.isDirectory(entry)) {
                Path manifestPath = entry.resolve(JarFile.MANIFEST_NAME);
                if (!Files.exists(manifestPath)) {
                    continue;
                }
                try (InputStream inputStream = Files.newInputStream(manifestPath)) {
                    readManifest = new Manifest(inputStream);
                }
            } else {
                // Assume a zip or jar file.
                try (ZipFile zipFile = new ZipFile(entry.toFile())) {
                    ZipEntry manifestEntry = zipFile.getEntry(JarFile.MANIFEST_NAME);
                    if (manifestEntry == null) {
                        continue;
                    }
                    try (InputStream inputStream = zipFile.getInputStream(manifestEntry)) {
                        readManifest = new Manifest(inputStream);
                    }
                }
            }
            merge(manifest, readManifest);
        }
    }
    // so that values from the user overwrite values from merged manifests.
    if (manifestFile.isPresent()) {
        Path path = filesystem.getPathForRelativePath(manifestFile.get());
        try (InputStream stream = Files.newInputStream(path)) {
            Manifest readManifest = new Manifest(stream);
            merge(manifest, readManifest);
        }
    }
    // We may have merged manifests and over-written the user-supplied main class. Add it back.
    if (mainClass.isPresent()) {
        manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, mainClass.get());
    }
    return manifest;
}
Also used : Path(java.nio.file.Path) ZipFile(java.util.zip.ZipFile) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) Manifest(java.util.jar.Manifest)

Example 10 with Manifest

use of java.util.jar.Manifest in project buck by facebook.

the class JarDirectoryStepHelper method createJarFile.

public static int createJarFile(ProjectFilesystem filesystem, Path pathToOutputFile, CustomZipOutputStream outputFile, ImmutableSortedSet<Path> entriesToJar, ImmutableSet<String> alreadyAddedEntriesToOutputFile, Optional<String> mainClass, Optional<Path> manifestFile, boolean mergeManifests, Iterable<Pattern> blacklist, JavacEventSink eventSink, PrintStream stdErr) throws IOException {
    Set<String> alreadyAddedEntries = Sets.newHashSet(alreadyAddedEntriesToOutputFile);
    // Write the manifest first.
    JarEntry metaInf = new JarEntry("META-INF/");
    // We want deterministic JARs, so avoid mtimes. -1 is timzeone independent, 0 is not.
    metaInf.setTime(ZipConstants.getFakeTime());
    outputFile.putNextEntry(metaInf);
    outputFile.closeEntry();
    alreadyAddedEntries.add("META-INF/");
    Manifest manifest = createManifest(filesystem, entriesToJar, mainClass, manifestFile, mergeManifests);
    JarEntry manifestEntry = new JarEntry(JarFile.MANIFEST_NAME);
    // We want deterministic JARs, so avoid mtimes. -1 is timzeone independent, 0 is not.
    manifestEntry.setTime(ZipConstants.getFakeTime());
    outputFile.putNextEntry(manifestEntry);
    manifest.write(outputFile);
    outputFile.closeEntry();
    alreadyAddedEntries.add(JarFile.MANIFEST_NAME);
    Path absoluteOutputPath = filesystem.getPathForRelativePath(pathToOutputFile);
    for (Path entry : entriesToJar) {
        Path file = filesystem.getPathForRelativePath(entry);
        if (Files.isRegularFile(file)) {
            Preconditions.checkArgument(!file.equals(absoluteOutputPath), "Trying to put file %s into itself", file);
            // Assume the file is a ZIP/JAR file.
            copyZipEntriesToJar(file, pathToOutputFile, outputFile, alreadyAddedEntries, eventSink, blacklist);
        } else if (Files.isDirectory(file)) {
            addFilesInDirectoryToJar(filesystem, file, outputFile, alreadyAddedEntries, blacklist, eventSink);
        } else {
            throw new IllegalStateException("Must be a file or directory: " + file);
        }
    }
    if (mainClass.isPresent() && !mainClassPresent(mainClass.get(), alreadyAddedEntries)) {
        stdErr.print(String.format("ERROR: Main class %s does not exist.\n", mainClass.get()));
        return 1;
    }
    return 0;
}
Also used : Path(java.nio.file.Path) JarEntry(java.util.jar.JarEntry) Manifest(java.util.jar.Manifest)

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