use of org.apache.felix.metatype.MetaDataReader in project bndtools by bndtools.
the class CapabilityBasedTemplate method getMetadata.
@Override
public ObjectClassDefinition getMetadata(IProgressMonitor monitor) throws Exception {
String resourceId = ResourceUtils.getIdentityCapability(capability.getResource()).osgi_identity();
final CompositeOCD compositeOcd = new CompositeOCD(name, description, null);
if (metaTypePath != null) {
try (JarFile bundleJarFile = new JarFile(fetchBundle())) {
JarEntry metaTypeEntry = bundleJarFile.getJarEntry(metaTypePath);
try (InputStream entryInput = bundleJarFile.getInputStream(metaTypeEntry)) {
MetaData metaData = new MetaDataReader().parse(entryInput);
@SuppressWarnings("rawtypes") Map ocdMap = metaData.getObjectClassDefinitions();
if (ocdMap != null) {
if (ocdMap.size() == 1) {
@SuppressWarnings("unchecked") Entry<String, OCD> entry = (Entry<String, OCD>) ocdMap.entrySet().iterator().next();
// There is exactly one OCD, but if the capability specified the 'ocd' property then it must match.
if (ocdRef == null || ocdRef.equals(entry.getKey())) {
compositeOcd.addDelegate(new FelixOCDAdapter(entry.getValue()));
} else {
log(IStatus.WARNING, String.format("MetaType entry '%s' from resource '%s' did not contain an Object Class Definition with id '%s'", metaTypePath, resourceId, ocdRef), null);
}
} else {
// There are multiple OCDs in the MetaType record, so the capability must have specified the 'ocd' property.
if (ocdRef != null) {
OCD felixOcd = (OCD) ocdMap.get(ocdRef);
if (felixOcd != null) {
compositeOcd.addDelegate(new FelixOCDAdapter(felixOcd));
} else {
log(IStatus.WARNING, String.format("MetaType entry '%s' from resource '%s' did not contain an Object Class Definition with id '%s'", metaTypePath, resourceId, ocdRef), null);
}
} else {
log(IStatus.WARNING, String.format("MetaType entry '%s' from resource '%s' contains multiple Object Class Definitions, and no 'ocd' property was specified.", metaTypePath, resourceId), null);
}
}
}
}
}
}
// Add attribute definitions for any parameter names found in the templates and not already
// loaded from the Metatype XML.
ObjectClassDefinitionImpl ocdImpl = new ObjectClassDefinitionImpl(name, description, null);
ResourceMap inputs = getInputSources();
Map<String, String> params = engine.getTemplateParameters(inputs, monitor);
for (Entry<String, String> entry : params.entrySet()) {
AttributeDefinitionImpl ad = new AttributeDefinitionImpl(entry.getKey(), entry.getKey(), 0, AttributeDefinition.STRING);
if (entry.getValue() != null)
ad.setDefaultValue(new String[] { entry.getValue() });
ocdImpl.addAttribute(ad, true);
}
compositeOcd.addDelegate(ocdImpl);
return compositeOcd;
}
Aggregations