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>
* @ProvideCapability(ns = "webapp")
* @RequireCapability(ns = "osgi.implementation", filter = "(osgi.implementation=osgi.http)")
* @interface WebApplication {
* String name();
* }
*
* @WebApplication(name = "Petstore")
* @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);
}
}
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);
}
}
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));
}
}
}
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);
}
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);
}
}
Aggregations