Search in sources :

Example 11 with Manifest

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

the class JarContentHasher method getContentHashes.

public ImmutableMap<Path, HashCodeAndFileType> getContentHashes() throws IOException {
    Manifest manifest = filesystem.getJarManifest(jarRelativePath);
    if (manifest == null) {
        throw new UnsupportedOperationException("Cache does not know how to return hash codes for archive members except " + "when the archive contains a META-INF/MANIFEST.MF with " + HashingDeterministicJarWriter.DIGEST_ATTRIBUTE_NAME + " attributes for each file.");
    }
    ImmutableMap.Builder<Path, HashCodeAndFileType> builder = ImmutableMap.builder();
    for (Map.Entry<String, Attributes> nameAttributesEntry : manifest.getEntries().entrySet()) {
        Path memberPath = Paths.get(nameAttributesEntry.getKey());
        Attributes attributes = nameAttributesEntry.getValue();
        String hashStringValue = attributes.getValue(HashingDeterministicJarWriter.DIGEST_ATTRIBUTE_NAME);
        if (hashStringValue == null) {
            continue;
        }
        HashCode memberHash = HashCode.fromString(hashStringValue);
        HashCodeAndFileType memberHashCodeAndFileType = HashCodeAndFileType.ofFile(memberHash);
        builder.put(memberPath, memberHashCodeAndFileType);
    }
    return builder.build();
}
Also used : Path(java.nio.file.Path) HashCode(com.google.common.hash.HashCode) Attributes(java.util.jar.Attributes) Manifest(java.util.jar.Manifest) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 12 with Manifest

use of java.util.jar.Manifest in project mongo-java-driver by mongodb.

the class ClientMetadataHelper method getDriverVersion.

private static String getDriverVersion() {
    String driverVersion = "unknown";
    try {
        CodeSource codeSource = InternalStreamConnectionInitializer.class.getProtectionDomain().getCodeSource();
        if (codeSource != null && codeSource.getLocation() != null) {
            String path = codeSource.getLocation().getPath();
            URL jarUrl = path.endsWith(".jar") ? new URL("jar:file:" + path + "!/") : null;
            if (jarUrl != null) {
                JarURLConnection jarURLConnection = (JarURLConnection) jarUrl.openConnection();
                Manifest manifest = jarURLConnection.getManifest();
                String version = (String) manifest.getMainAttributes().get(new Attributes.Name("Build-Version"));
                if (version != null) {
                    driverVersion = version;
                }
            }
        }
    } catch (SecurityException e) {
    // do nothing
    } catch (IOException e) {
    // do nothing
    }
    return driverVersion;
}
Also used : JarURLConnection(java.net.JarURLConnection) BsonString(org.bson.BsonString) IOException(java.io.IOException) CodeSource(java.security.CodeSource) Manifest(java.util.jar.Manifest) URL(java.net.URL)

Example 13 with Manifest

use of java.util.jar.Manifest in project languagetool by languagetool-org.

the class JLanguageTool method getBuildDate.

/**
   * Returns the build date or {@code null} if not run from JAR.
   */
@Nullable
private static String getBuildDate() {
    try {
        URL res = JLanguageTool.class.getResource(JLanguageTool.class.getSimpleName() + ".class");
        if (res == null) {
            // this will happen on Android, see http://stackoverflow.com/questions/15371274/
            return null;
        }
        Object connObj = res.openConnection();
        if (connObj instanceof JarURLConnection) {
            JarURLConnection conn = (JarURLConnection) connObj;
            Manifest manifest = conn.getManifest();
            return manifest.getMainAttributes().getValue("Implementation-Date");
        } else {
            return null;
        }
    } catch (IOException e) {
        throw new RuntimeException("Could not get build date from JAR", e);
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) URL(java.net.URL) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with Manifest

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

the class JarUtils method createJarFile.

public static File createJarFile(final String name, final Suffix s, final String base, final Map<String, String> entries) throws IOException {
    final File tempJar = File.createTempFile(name, "." + s);
    tempJar.deleteOnExit();
    final JarOutputStream jos = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(tempJar)), new Manifest());
    final Set<String> usedSegments = new HashSet<String>();
    for (final Map.Entry<String, String> entry : entries.entrySet()) {
        for (final String path : getPaths(entry.getValue())) {
            if (usedSegments.contains(path)) {
                continue;
            }
            usedSegments.add(path);
            final JarEntry e = new JarEntry(path);
            jos.putNextEntry(e);
            jos.closeEntry();
        }
        final JarEntry e = new JarEntry(entry.getValue());
        jos.putNextEntry(e);
        final InputStream f = new BufferedInputStream(new FileInputStream(base + entry.getKey()));
        final byte[] buf = new byte[1024];
        int read = 1024;
        while ((read = f.read(buf, 0, read)) != -1) {
            jos.write(buf, 0, read);
        }
        jos.closeEntry();
    }
    jos.close();
    return tempJar;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 15 with Manifest

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

the class JarListHandler method handleJsonRequest.

@Override
public String handleJsonRequest(Map<String, String> pathParams, Map<String, String> queryParams, ActorGateway jobManager) throws Exception {
    try {
        StringWriter writer = new StringWriter();
        JsonGenerator gen = JsonFactory.jacksonFactory.createGenerator(writer);
        gen.writeStartObject();
        gen.writeStringField("address", queryParams.get(RuntimeMonitorHandler.WEB_MONITOR_ADDRESS_KEY));
        gen.writeArrayFieldStart("files");
        File[] list = jarDir.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".jar");
            }
        });
        for (File f : list) {
            // separate the uuid and the name parts.
            String id = f.getName();
            int startIndex = id.indexOf("_");
            if (startIndex < 0) {
                continue;
            }
            String name = id.substring(startIndex + 1);
            if (name.length() < 5 || !name.endsWith(".jar")) {
                continue;
            }
            gen.writeStartObject();
            gen.writeStringField("id", id);
            gen.writeStringField("name", name);
            gen.writeNumberField("uploaded", f.lastModified());
            gen.writeArrayFieldStart("entry");
            String[] classes = new String[0];
            try {
                JarFile jar = new JarFile(f);
                Manifest manifest = jar.getManifest();
                String assemblerClass = null;
                if (manifest != null) {
                    assemblerClass = manifest.getMainAttributes().getValue(PackagedProgram.MANIFEST_ATTRIBUTE_ASSEMBLER_CLASS);
                    if (assemblerClass == null) {
                        assemblerClass = manifest.getMainAttributes().getValue(PackagedProgram.MANIFEST_ATTRIBUTE_MAIN_CLASS);
                    }
                }
                if (assemblerClass != null) {
                    classes = assemblerClass.split(",");
                }
            } catch (IOException ignored) {
            // we simply show no entries here
            }
            // show every entry class that can be loaded later on.
            for (String clazz : classes) {
                clazz = clazz.trim();
                PackagedProgram program = null;
                try {
                    program = new PackagedProgram(f, clazz, new String[0]);
                } catch (Exception ignored) {
                // ignore jar files which throw an error upon creating a PackagedProgram
                }
                if (program != null) {
                    gen.writeStartObject();
                    gen.writeStringField("name", clazz);
                    String desc = program.getDescription();
                    gen.writeStringField("description", desc == null ? "No description provided" : desc);
                    gen.writeEndObject();
                }
            }
            gen.writeEndArray();
            gen.writeEndObject();
        }
        gen.writeEndArray();
        gen.writeEndObject();
        gen.close();
        return writer.toString();
    } catch (Exception e) {
        throw new RuntimeException("Failed to fetch jar list: " + e.getMessage(), e);
    }
}
Also used : IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) IOException(java.io.IOException) FilenameFilter(java.io.FilenameFilter) PackagedProgram(org.apache.flink.client.program.PackagedProgram) StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) JarFile(java.util.jar.JarFile) File(java.io.File)

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