Search in sources :

Example 1 with MetaData

use of org.apache.felix.metatype.MetaData 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;
}
Also used : CompositeOCD(org.bndtools.templating.util.CompositeOCD) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) ObjectClassDefinitionImpl(org.bndtools.templating.util.ObjectClassDefinitionImpl) ResourceMap(org.bndtools.templating.ResourceMap) OCD(org.apache.felix.metatype.OCD) CompositeOCD(org.bndtools.templating.util.CompositeOCD) JarEntry(java.util.jar.JarEntry) Entry(java.util.Map.Entry) MetaData(org.apache.felix.metatype.MetaData) AttributeDefinitionImpl(org.bndtools.templating.util.AttributeDefinitionImpl) Map(java.util.Map) ResourceMap(org.bndtools.templating.ResourceMap) MetaDataReader(org.apache.felix.metatype.MetaDataReader)

Aggregations

InputStream (java.io.InputStream)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1 JarInputStream (java.util.jar.JarInputStream)1 MetaData (org.apache.felix.metatype.MetaData)1 MetaDataReader (org.apache.felix.metatype.MetaDataReader)1 OCD (org.apache.felix.metatype.OCD)1 ResourceMap (org.bndtools.templating.ResourceMap)1 AttributeDefinitionImpl (org.bndtools.templating.util.AttributeDefinitionImpl)1 CompositeOCD (org.bndtools.templating.util.CompositeOCD)1 ObjectClassDefinitionImpl (org.bndtools.templating.util.ObjectClassDefinitionImpl)1