Search in sources :

Example 66 with Attributes

use of java.util.jar.Attributes in project bnd by bndtools.

the class AnnotationHeadersTest method testMultipleManifestHeaders.

/**
	 * Say I want to define a capability namespace for web applications, e.g.
	 * Provide-Capability: webapp; webapp=Petstore. Web application components
	 * necessarily require an HTTP implementation so they should
	 * Require-Capability: osgi.implementation;
	 * filter:="(osgi.implementation=osgi.http)". I want to define an annotation
	 * that I can put onto a component that implies both the above provide and
	 * require. I tried the following:
	 * 
	 * <pre>
	 * &#64;ProvideCapability(ns = "webapp")
	 * &#64;RequireCapability(ns = "osgi.implementation", filter = "(osgi.implementation=osgi.http)")
	 * &#64;interface WebApplication {
	 * 	String name();
	 * }
	 * 
	 * &#64;WebApplication(name = "Petstore")
	 * &#64;Component
	 * public class PetstoreAppComponent {
	 * 	// ..
	 * }
	 * </pre>
	 * 
	 * However this only generated the Provide, it did not generate the Require.
	 * If I switch the order of annotations so that @RequireCapability is first,
	 * then it only generates the Require.
	 */
public void testMultipleManifestHeaders() throws Exception {
    try (Builder b = new Builder()) {
        b.addClasspath(IO.getFile("bin"));
        b.setPrivatePackage("test.annotationheaders.multiple");
        b.build();
        assertTrue(b.check());
        b.getJar().getManifest().write(System.out);
        Attributes mainAttributes = b.getJar().getManifest().getMainAttributes();
        Parameters req = new Parameters(mainAttributes.getValue(Constants.REQUIRE_CAPABILITY));
        Parameters cap = new Parameters(mainAttributes.getValue(Constants.PROVIDE_CAPABILITY));
        assertTrue(cap.get("provide") != null);
        assertTrue(req.get("require") != null);
    }
}
Also used : Parameters(aQute.bnd.header.Parameters) Builder(aQute.bnd.osgi.Builder) Attributes(java.util.jar.Attributes)

Example 67 with Attributes

use of java.util.jar.Attributes in project bnd by bndtools.

the class Analyzer method doNamesection.

/**
	 * Parse the namesection as instructions and then match them against the
	 * current set of resources For example:
	 * 
	 * <pre>
	 *  -namesection: *;baz=true,
	 * abc/def/bar/X.class=3
	 * </pre>
	 * 
	 * The raw value of {@link Constants#NAMESECTION} is used but the values of
	 * the attributes are replaced where @ is set to the resource name. This
	 * allows macro to operate on the resource
	 */
private void doNamesection(Jar dot, Manifest manifest) {
    Parameters namesection = parseHeader(getProperties().getProperty(NAMESECTION));
    Instructions instructions = new Instructions(namesection);
    Set<String> resources = new HashSet<String>(dot.getResources().keySet());
    //
    for (Map.Entry<Instruction, Attrs> instr : instructions.entrySet()) {
        boolean matched = false;
        for (Iterator<String> i = resources.iterator(); i.hasNext(); ) {
            String path = i.next();
            if (instr.getKey().matches(path)) {
                // Instruction matches the resource
                matched = true;
                if (!instr.getKey().isNegated()) {
                    // Positive match, add the attributes
                    Attributes attrs = manifest.getAttributes(path);
                    if (attrs == null) {
                        attrs = new Attributes();
                        manifest.getEntries().put(path, attrs);
                    }
                    for (Map.Entry<String, String> property : instr.getValue().entrySet()) {
                        setProperty("@", path);
                        try {
                            String processed = getReplacer().process(property.getValue());
                            attrs.putValue(property.getKey(), processed);
                        } finally {
                            unsetProperty("@");
                        }
                    }
                }
                i.remove();
            }
        }
        if (!matched && resources.size() > 0)
            warning("The instruction %s in %s did not match any resources", instr.getKey(), NAMESECTION);
    }
}
Also used : Parameters(aQute.bnd.header.Parameters) Attrs(aQute.bnd.header.Attrs) Attributes(java.util.jar.Attributes) MultiMap(aQute.lib.collections.MultiMap) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 68 with Attributes

use of java.util.jar.Attributes in project bnd by bndtools.

the class Analyzer method mergeManifest.

/**
	 * Merge the existing manifest with the instructions but do not override
	 * existing properties.
	 * 
	 * @param manifest The manifest to merge with
	 * @throws IOException
	 */
public void mergeManifest(Manifest manifest) throws IOException {
    if (manifest != null) {
        Attributes attributes = manifest.getMainAttributes();
        for (Iterator<Object> i = attributes.keySet().iterator(); i.hasNext(); ) {
            Name name = (Name) i.next();
            String key = name.toString();
            // Dont want instructions
            if (key.startsWith("-"))
                continue;
            if (getProperty(key) == null)
                setProperty(key, attributes.getValue(name));
        }
    }
}
Also used : Attributes(java.util.jar.Attributes) Name(java.util.jar.Attributes.Name)

Example 69 with Attributes

use of java.util.jar.Attributes in project processdash by dtuma.

the class ProcessAssetPackager method openJarOutputStream.

private static JarOutputStream openJarOutputStream() throws IOException {
    Manifest mf = new Manifest();
    Attributes attrs = mf.getMainAttributes();
    attrs.putValue("Manifest-Version", "1.0");
    attrs.putValue("Dash-Pkg-ID", packageId);
    attrs.putValue("Dash-Pkg-Version", packageVersion);
    attrs.putValue("Dash-Pkg-Name", packageName);
    return new JarOutputStream(new FileOutputStream(destFile), mf);
}
Also used : FileOutputStream(java.io.FileOutputStream) Attributes(java.util.jar.Attributes) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest)

Example 70 with Attributes

use of java.util.jar.Attributes in project android_frameworks_base by crdroidandroid.

the class StrictJarManifestReader method readEntries.

public void readEntries(Map<String, Attributes> entries, Map<String, StrictJarManifest.Chunk> chunks) throws IOException {
    int mark = pos;
    while (readHeader()) {
        if (!Attributes.Name.NAME.equals(name)) {
            throw new IOException("Entry is not named");
        }
        String entryNameValue = value;
        Attributes entry = entries.get(entryNameValue);
        if (entry == null) {
            entry = new Attributes(12);
        }
        while (readHeader()) {
            entry.put(name, value);
        }
        if (chunks != null) {
            if (chunks.get(entryNameValue) != null) {
                // updating the signature; for now a defensive error is thrown
                throw new IOException("A jar verifier does not support more than one entry with the same name");
            }
            chunks.put(entryNameValue, new StrictJarManifest.Chunk(mark, pos));
            mark = pos;
        }
        entries.put(entryNameValue, entry);
    }
}
Also used : Attributes(java.util.jar.Attributes) IOException(java.io.IOException)

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