Search in sources :

Example 1 with OCD

use of org.apache.felix.metatype.OCD in project felix by apache.

the class StoreResourceTask method process.

public void process(String name, InputStream stream) throws ResourceProcessorException {
    m_log.log(LogService.LOG_DEBUG, "processing " + name);
    // initial validation
    assertInDeploymentSession("Can not process resource without a Deployment Session");
    Map<String, List<AutoConfResource>> toBeInstalled;
    synchronized (m_lock) {
        toBeInstalled = new HashMap<String, List<AutoConfResource>>(m_toBeInstalled);
    }
    MetaData data = parseAutoConfResource(stream);
    // process resources
    Filter filter = getFilter(data);
    // add to session data
    if (!toBeInstalled.containsKey(name)) {
        toBeInstalled.put(name, new ArrayList<AutoConfResource>());
    }
    List<Designate> designates = data.getDesignates();
    if (designates == null || designates.isEmpty()) {
        // if there are no designates, there's nothing to process
        m_log.log(LogService.LOG_INFO, "No designates found in the resource, so there's nothing to process.");
        return;
    }
    Map<String, OCD> localOcds = data.getObjectClassDefinitions();
    if (localOcds == null) {
        localOcds = Collections.emptyMap();
    }
    for (Designate designate : designates) {
        // check object
        DesignateObject objectDef = designate.getObject();
        if (objectDef == null) {
            throw new ResourceProcessorException(CODE_OTHER_ERROR, "Designate Object child missing or invalid");
        }
        // check attributes
        if (objectDef.getAttributes() == null || objectDef.getAttributes().isEmpty()) {
            throw new ResourceProcessorException(CODE_OTHER_ERROR, "Object Attributes child missing or invalid");
        }
        // check ocdRef
        String ocdRef = objectDef.getOcdRef();
        if (ocdRef == null || "".equals(ocdRef)) {
            throw new ResourceProcessorException(CODE_OTHER_ERROR, "Object ocdRef attribute missing or invalid");
        }
        // determine OCD
        ObjectClassDefinition ocd = null;
        OCD localOcd = localOcds.get(ocdRef);
        // ask meta type service for matching OCD if no local OCD has been defined
        ocd = (localOcd != null) ? new ObjectClassDefinitionImpl(localOcd) : getMetaTypeOCD(data, designate);
        if (ocd == null) {
            throw new ResourceProcessorException(CODE_OTHER_ERROR, "No Object Class Definition found with id=" + ocdRef);
        }
        // determine configuration data based on the values and their type definition
        Dictionary dict = MetaTypeUtil.getProperties(designate, ocd);
        if (dict == null) {
            // designate does not match it's definition, but was marked optional, ignore it
            continue;
        }
        AutoConfResource resource = new AutoConfResource(name, designate.getPid(), designate.getFactoryPid(), designate.getBundleLocation(), designate.isMerge(), dict, filter);
        toBeInstalled.get(name).add(resource);
    }
    synchronized (m_lock) {
        m_toBeInstalled.putAll(toBeInstalled);
    }
    m_log.log(LogService.LOG_DEBUG, "processing " + name + " done");
}
Also used : Dictionary(java.util.Dictionary) ResourceProcessorException(org.osgi.service.deploymentadmin.spi.ResourceProcessorException) Designate(org.apache.felix.metatype.Designate) OCD(org.apache.felix.metatype.OCD) Filter(org.osgi.framework.Filter) MetaData(org.apache.felix.metatype.MetaData) DesignateObject(org.apache.felix.metatype.DesignateObject) ArrayList(java.util.ArrayList) List(java.util.List) ObjectClassDefinition(org.osgi.service.metatype.ObjectClassDefinition)

Example 2 with OCD

use of org.apache.felix.metatype.OCD in project felix by apache.

the class MetaTypeInformationImpl method addMetaData.

// ---------- setters to fill the values -----------------------------------
protected void addMetaData(MetaData md) {
    if (md.getDesignates() != null) {
        // meta type provide to register by PID
        DefaultMetaTypeProvider dmtp = new DefaultMetaTypeProvider(this.bundle, md);
        Iterator designates = md.getDesignates().iterator();
        while (designates.hasNext()) {
            Designate designate = (Designate) designates.next();
            // get the OCD reference, ignore the designate if none
            DesignateObject object = designate.getObject();
            String ocdRef = (object == null) ? null : object.getOcdRef();
            if (ocdRef == null) {
                continue;
            }
            // get ocd for the reference, ignore designate if none
            final Map map = md.getObjectClassDefinitions();
            OCD ocd = (OCD) (map == null ? null : map.get(ocdRef));
            if (ocd == null) {
                continue;
            }
            // gather pids and factory pids and register provider
            if (designate.getFactoryPid() != null) {
                this.factoryPids.add(designate.getFactoryPid());
                this.addMetaTypeProvider(designate.getFactoryPid(), dmtp);
            } else {
                this.pids.add(designate.getPid());
                this.addMetaTypeProvider(designate.getPid(), dmtp);
            }
        }
    }
}
Also used : OCD(org.apache.felix.metatype.OCD) DesignateObject(org.apache.felix.metatype.DesignateObject) Iterator(java.util.Iterator) Designate(org.apache.felix.metatype.Designate) DefaultMetaTypeProvider(org.apache.felix.metatype.DefaultMetaTypeProvider) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with OCD

use of org.apache.felix.metatype.OCD in project fabric8 by jboss-fuse.

the class ProfileMetadata method addMetaData.

protected void addMetaData(MetaTypeSummaryDTO summary, MetaData metadata, Properties resources) {
    Map<String, Object> map = metadata.getDesignates();
    Map<String, Object> objects = metadata.getObjectClassDefinitions();
    Set<Map.Entry<String, Object>> entries = map.entrySet();
    for (Map.Entry<String, Object> entry : entries) {
        String pid = entry.getKey();
        Object value = objects.get(pid);
        if (value instanceof OCD) {
            OCD ocd = (OCD) value;
            MetaTypeObjectSummaryDTO object = summary.getOrCreateMetaTypeSummaryDTO(pid);
            object.setId(pid);
            object.setName(localize(resources, ocd.getName()));
            object.setDescription(localize(resources, ocd.getDescription()));
        }
    }
}
Also used : OCD(org.apache.felix.metatype.OCD) JarEntry(java.util.jar.JarEntry) MetaTypeObjectSummaryDTO(io.fabric8.api.jmx.MetaTypeObjectSummaryDTO) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with OCD

use of org.apache.felix.metatype.OCD in project fabric8 by jboss-fuse.

the class ProfileMetadata method getPidMetaTypeObject.

@Override
public MetaTypeObjectDTO getPidMetaTypeObject(String versionId, String profileId, final String pid) throws Exception {
    final AtomicReference<MetaTypeObjectDTO> answer = new AtomicReference<>(null);
    MetadataHandler handler = new MetadataHandler() {

        @Override
        public void invoke(MetaData metadata, Properties resources) {
            Map<String, Object> map = metadata.getDesignates();
            Map<String, Object> objects = metadata.getObjectClassDefinitions();
            Set<Map.Entry<String, Object>> entries = map.entrySet();
            for (Map.Entry<String, Object> entry : entries) {
                String aPid = entry.getKey();
                Object value = objects.get(aPid);
                if (Objects.equal(pid, aPid) && value instanceof OCD) {
                    OCD ocd = (OCD) value;
                    answer.set(createMetaTypeObjectDTO(resources, ocd));
                }
            }
        }
    };
    findMetadataForProfile(versionId, profileId, handler);
    return answer.get();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) Properties(java.util.Properties) OCD(org.apache.felix.metatype.OCD) JarEntry(java.util.jar.JarEntry) MetaData(org.apache.felix.metatype.MetaData) Map(java.util.Map) HashMap(java.util.HashMap) MetaTypeObjectDTO(io.fabric8.api.jmx.MetaTypeObjectDTO)

Example 5 with OCD

use of org.apache.felix.metatype.OCD 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();
                        // 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 {
                        // '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

OCD (org.apache.felix.metatype.OCD)5 Map (java.util.Map)4 HashMap (java.util.HashMap)3 JarEntry (java.util.jar.JarEntry)3 MetaData (org.apache.felix.metatype.MetaData)3 Designate (org.apache.felix.metatype.Designate)2 DesignateObject (org.apache.felix.metatype.DesignateObject)2 MetaTypeObjectDTO (io.fabric8.api.jmx.MetaTypeObjectDTO)1 MetaTypeObjectSummaryDTO (io.fabric8.api.jmx.MetaTypeObjectSummaryDTO)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Dictionary (java.util.Dictionary)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 Properties (java.util.Properties)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 JarFile (java.util.jar.JarFile)1 JarInputStream (java.util.jar.JarInputStream)1 DefaultMetaTypeProvider (org.apache.felix.metatype.DefaultMetaTypeProvider)1