Search in sources :

Example 96 with Attributes

use of java.util.jar.Attributes in project vertx-docgen by vert-x3.

the class BaseProcessor method resolveCoordinate.

/**
 * Resolve the coordinate of the type element, this method returns either:
 * <ul>
 * <li>a {@link io.vertx.docgen.Coordinate} object, the coordinate object can have null fields</li>
 * <li>{@code null} : the current element is being compiled, which likely means create a local link</li>
 * </ul>
 *
 * @param typeElt the type element to resolve
 * @return the resolved coordinate object or null if the element is locally compiled
 */
private Coordinate resolveCoordinate(TypeElement typeElt) {
    try {
        Symbol.ClassSymbol cs = (Symbol.ClassSymbol) typeElt;
        if (cs.sourcefile != null && getURL(cs.sourcefile) != null) {
            // .java source we can link locally
            return null;
        }
        if (cs.classfile != null) {
            JavaFileObject cf = cs.classfile;
            URL classURL = getURL(cf);
            if (classURL != null && classURL.getFile().endsWith(".class")) {
                URL manifestURL = new URL(classURL.toString().substring(0, classURL.toString().length() - (typeElt.getQualifiedName().toString().length() + 6)) + "META-INF/MANIFEST.MF");
                InputStream manifestIs = manifestURL.openStream();
                if (manifestIs != null) {
                    Manifest manifest = new Manifest(manifestIs);
                    Attributes attributes = manifest.getMainAttributes();
                    String groupId = attributes.getValue(new Attributes.Name("Maven-Group-Id"));
                    String artifactId = attributes.getValue(new Attributes.Name("Maven-Artifact-Id"));
                    String version = attributes.getValue(new Attributes.Name("Maven-Version"));
                    return new Coordinate(groupId, artifactId, version);
                }
            }
        }
    } catch (Exception ignore) {
    // 
    }
    return new Coordinate(null, null, null);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) Symbol(com.sun.tools.javac.code.Symbol) Attributes(java.util.jar.Attributes) Manifest(java.util.jar.Manifest) URL(java.net.URL)

Example 97 with Attributes

use of java.util.jar.Attributes in project tigervnc by TigerVNC.

the class VncViewer method getTimestamp.

private static void getTimestamp() {
    if (version == null || build == null) {
        try {
            Manifest manifest = new Manifest(timestamp);
            Attributes attributes = manifest.getMainAttributes();
            version = attributes.getValue("Version");
            build = attributes.getValue("Build");
            buildDate = attributes.getValue("Package-Date");
            buildTime = attributes.getValue("Package-Time");
        } catch (java.lang.Exception e) {
        }
    }
}
Also used : Attributes(java.util.jar.Attributes) Manifest(java.util.jar.Manifest)

Example 98 with Attributes

use of java.util.jar.Attributes in project J2ME-Loader by nikita36078.

the class AndroidProducer method processJar.

public static void processJar(File jarInputFile, File jarOutputFile, boolean isMidlet) throws IOException {
    JarInputStream jis = null;
    JarOutputStream jos = null;
    HashMap<String, byte[]> resources = new HashMap<>();
    try {
        jis = new JarInputStream(new FileInputStream(jarInputFile));
        Manifest manifest = jis.getManifest();
        if (manifest == null) {
            jos = new JarOutputStream(new FileOutputStream(jarOutputFile));
        } else {
            Attributes attributes = manifest.getMainAttributes();
            if (!attributes.containsKey(Attributes.Name.MANIFEST_VERSION)) {
                attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
            }
            jos = new JarOutputStream(new FileOutputStream(jarOutputFile), manifest);
        }
        byte[] buffer = new byte[1024];
        JarEntry jarEntry;
        while ((jarEntry = jis.getNextJarEntry()) != null) {
            if (!jarEntry.isDirectory()) {
                String name = jarEntry.getName();
                int size = 0;
                int read;
                int length = buffer.length;
                while ((read = jis.read(buffer, size, length)) > 0) {
                    size += read;
                    length = 1024;
                    if (size + length > buffer.length) {
                        byte[] newInputBuffer = new byte[size + length];
                        System.arraycopy(buffer, 0, newInputBuffer, 0, buffer.length);
                        buffer = newInputBuffer;
                    }
                }
                byte[] inBuffer = new byte[size];
                System.arraycopy(buffer, 0, inBuffer, 0, size);
                resources.put(name, inBuffer);
                if (name.endsWith(".class")) {
                    analyze(name.substring(0, name.length() - ".class".length()), new ByteArrayInputStream(inBuffer));
                }
            }
        }
        for (String name : resources.keySet()) {
            byte[] inBuffer = resources.get(name);
            byte[] outBuffer = inBuffer;
            if (name.endsWith(".class")) {
                outBuffer = instrument(name, new ByteArrayInputStream(inBuffer), isMidlet);
            }
            jos.putNextEntry(new JarEntry(name));
            jos.write(outBuffer);
        }
    } finally {
        if (jis != null) {
            jis.close();
        }
        if (jos != null) {
            jos.close();
        }
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) HashMap(java.util.HashMap) Attributes(java.util.jar.Attributes) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream)

Example 99 with Attributes

use of java.util.jar.Attributes in project ignite by apache.

the class GridUriDeploymentJarVerifier method getSignedFiles.

/**
 * Gets all signed files from the manifest.
 * <p>
 * It scans all manifest entries and their attributes. If there is an attribute
 * name which ends with "-DIGEST" we are assuming that manifest entry name is a
 * signed file name.
 *
 * @param manifest JAR file manifest.
 * @return Either empty set if none found or set of signed file names.
 */
private static Set<String> getSignedFiles(Manifest manifest) {
    Set<String> fileNames = new HashSet<>();
    Map<String, Attributes> entries = manifest.getEntries();
    if (entries != null && entries.size() > 0) {
        for (Map.Entry<String, Attributes> entry : entries.entrySet()) {
            Attributes attrs = entry.getValue();
            for (Map.Entry<Object, Object> attrEntry : attrs.entrySet()) {
                if (attrEntry.getKey().toString().toUpperCase().endsWith("-DIGEST")) {
                    fileNames.add(entry.getKey());
                    break;
                }
            }
        }
    }
    return fileNames;
}
Also used : Attributes(java.util.jar.Attributes) Map(java.util.Map) HashSet(java.util.HashSet)

Example 100 with Attributes

use of java.util.jar.Attributes in project Lucee by lucee.

the class BundleBuilderFactory method extendManifest.

private void extendManifest(Manifest mf) {
    Attributes attrs = mf.getMainAttributes();
    attrs.putValue("Bundle-ManifestVersion", "" + MANIFEST_VERSION);
    if (!StringUtil.isEmpty(name))
        attrs.putValue("Bundle-Name", name);
    attrs.putValue("Bundle-SymbolicName", symbolicName);
    if (!StringUtil.isEmpty(description))
        attrs.putValue("Bundle-Description", description);
    if (!StringUtil.isEmpty(bundleActivationPolicy))
        attrs.putValue("Bundle-ActivationPolicy", bundleActivationPolicy);
    if (version != null)
        attrs.putValue("Bundle-Version", version.toString());
    if (!StringUtil.isEmpty(activator)) {
        if (!activator.equalsIgnoreCase("none")) {
            attrs.putValue("Bundle-Activator", activator);
            addImportPackage("org.osgi.framework");
        } else {
            // attrs.remove("Bundle-Activator");
            attrs.putValue("Bundle-Activator", "");
        }
    }
    // Export-Package
    String str = ignoreExistingManifest ? null : attrs.getValue("Export-Package");
    // no existing Export-Package
    Set<String> set;
    if (Util.isEmpty(str, true)) {
        set = existingPackages;
    } else {
        set = new HashSet<String>();
        addPackages(set, str);
    }
    if (!ArrayUtil.isEmpty(exportPackage) && !isAsterix(exportPackage)) {
        Iterator<String> it = exportPackage.iterator();
        while (it.hasNext()) {
            set.add(it.next());
        }
    }
    exportPackage = ListUtil.toList(set);
    addList(attrs, "Export-Package", exportPackage);
    // Require-Bundle
    str = attrs.getValue("Require-Bundle");
    if (Util.isEmpty(str, true))
        addList(attrs, "Require-Bundle", requireBundle);
    // Require-Bundle
    str = attrs.getValue("Require-Bundle-Fragment");
    if (Util.isEmpty(str, true))
        addList(attrs, "Require-Bundle-Fragment", requireBundleFragment);
    // str = attrs.getValue("Fragment-Host");
    // if(Util.isEmpty(str,true))
    attrs.remove("Fragment-Host");
    addList(attrs, "Fragment-Host", fragmentHost);
    str = attrs.getValue("Import-Package");
    if (Util.isEmpty(str, true))
        addList(attrs, "Import-Package", importPackage);
    str = attrs.getValue("DynamicImport-Package");
    if (Util.isEmpty(str, true))
        addList(attrs, "DynamicImport-Package", dynImportPackage);
    str = attrs.getValue("Bundle-ClassPath");
    if (Util.isEmpty(str, true))
        addList(attrs, "Bundle-ClassPath", classPath);
}
Also used : Attributes(java.util.jar.Attributes)

Aggregations

Attributes (java.util.jar.Attributes)629 Manifest (java.util.jar.Manifest)356 IOException (java.io.IOException)152 File (java.io.File)143 JarFile (java.util.jar.JarFile)106 URL (java.net.URL)92 Map (java.util.Map)78 InputStream (java.io.InputStream)62 HashMap (java.util.HashMap)62 Jar (aQute.bnd.osgi.Jar)49 Builder (aQute.bnd.osgi.Builder)43 ArrayList (java.util.ArrayList)43 ByteArrayOutputStream (java.io.ByteArrayOutputStream)42 Test (org.junit.Test)41 ZipEntry (java.util.zip.ZipEntry)39 FileOutputStream (java.io.FileOutputStream)37 JarOutputStream (java.util.jar.JarOutputStream)36 ByteArrayInputStream (java.io.ByteArrayInputStream)33 FileInputStream (java.io.FileInputStream)33 JarEntry (java.util.jar.JarEntry)32