Search in sources :

Example 6 with Metadata

use of org.osgi.service.blueprint.reflect.Metadata in project aries by apache.

the class AbstractParserProxy method parseCDRForServices.

/**
	   * Extract Service metadata from a ComponentDefinitionRegistry. When doing SCA modelling, we
	   * need to suppress anonymous services. We don't want to do that when we're modelling for 
	   * provisioning dependencies. 
	   * @param cdr                       ComponentDefinitionRegistry
	   * @param suppressAnonymousServices Unnamed services will not be returned if this is true
	   * @return List<WrappedServiceMetadata>
	   */
private List<ExportedService> parseCDRForServices(ComponentDefinitionRegistry cdr, boolean suppressAnonymousServices) {
    _logger.debug(LOG_ENTRY, "parseCDRForServices", new Object[] { cdr, suppressAnonymousServices });
    List<ExportedService> result = new ArrayList<ExportedService>();
    for (ComponentMetadata compMetadata : findAllComponents(cdr)) {
        if (compMetadata instanceof ServiceMetadata) {
            ServiceMetadata serviceMetadata = (ServiceMetadata) compMetadata;
            String serviceName;
            int ranking;
            Collection<String> interfaces = new ArrayList<String>();
            Map<String, Object> serviceProps = new HashMap<String, Object>();
            ranking = serviceMetadata.getRanking();
            for (Object i : serviceMetadata.getInterfaces()) {
                interfaces.add((String) i);
            }
            // get the service properties
            List<MapEntry> props = serviceMetadata.getServiceProperties();
            for (MapEntry entry : props) {
                String key = ((ValueMetadata) entry.getKey()).getStringValue();
                Metadata value = entry.getValue();
                if (value instanceof CollectionMetadata) {
                    processMultiValueProperty(serviceProps, key, value);
                } else {
                    serviceProps.put(key, ((ValueMetadata) entry.getValue()).getStringValue());
                }
            }
            // serviceName: use the service id unless that's not set, 
            // in which case we use the bean id. 
            serviceName = serviceMetadata.getId();
            // If the Service references a Bean, export the bean id as a service property
            // as per 121.6.5 p669 of the blueprint 1.0 specification
            Target t = serviceMetadata.getServiceComponent();
            String targetId = null;
            if (t instanceof RefMetadata) {
                targetId = ((RefMetadata) t).getComponentId();
            } else if (t instanceof BeanMetadata) {
                targetId = ((BeanMetadata) t).getId();
            }
            // or auto-generated for an anonymous service. This must ALWAYS be set. 
            if (targetId != null && !targetId.startsWith(".")) {
                // Don't set this for anonymous inner components
                serviceProps.put("osgi.service.blueprint.compname", targetId);
                if (serviceName == null || serviceName.equals("") || serviceName.startsWith(".")) {
                    serviceName = targetId;
                }
            }
            if (serviceName != null && serviceName.startsWith("."))
                serviceName = null;
            // If suppressAnonymous services, do not expose services that have no name
            if (!suppressAnonymousServices || (serviceName != null)) {
                ExportedService wsm = _modellingManager.getExportedService(serviceName, ranking, interfaces, serviceProps);
                result.add(wsm);
            }
        }
    }
    _logger.debug(LOG_EXIT, "parseAllServiceElements", new Object[] { result });
    return result;
}
Also used : CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) MapEntry(org.osgi.service.blueprint.reflect.MapEntry) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) HashMap(java.util.HashMap) ExportedService(org.apache.aries.application.modelling.ExportedService) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) ArrayList(java.util.ArrayList) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) WrappedServiceMetadata(org.apache.aries.application.modelling.WrappedServiceMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) ServiceReferenceMetadata(org.osgi.service.blueprint.reflect.ServiceReferenceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) ReferenceListMetadata(org.osgi.service.blueprint.reflect.ReferenceListMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) Target(org.osgi.service.blueprint.reflect.Target) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) WrappedServiceMetadata(org.apache.aries.application.modelling.WrappedServiceMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata)

Example 7 with Metadata

use of org.osgi.service.blueprint.reflect.Metadata in project aries by apache.

the class AbstractParserProxy method processMultiValueProperty.

private void processMultiValueProperty(Map<String, Object> serviceProps, String key, Metadata value) {
    List<Metadata> values = ((CollectionMetadata) value).getValues();
    Class<?> collectionClass = ((CollectionMetadata) value).getCollectionClass();
    Object collectionValue;
    if (Collection.class.isAssignableFrom(collectionClass)) {
        Collection<String> theseValues = getCollectionFromClass(collectionClass);
        for (Metadata m : values) {
            theseValues.add(((ValueMetadata) m).getStringValue());
        }
        collectionValue = theseValues;
    } else {
        String[] theseValues = new String[values.size()];
        for (int i = 0; i < values.size(); i++) {
            Metadata m = values.get(i);
            theseValues[i] = ((ValueMetadata) m).getStringValue();
        }
        collectionValue = theseValues;
    }
    serviceProps.put(key, collectionValue);
}
Also used : CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) WrappedServiceMetadata(org.apache.aries.application.modelling.WrappedServiceMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) ServiceReferenceMetadata(org.osgi.service.blueprint.reflect.ServiceReferenceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) ReferenceListMetadata(org.osgi.service.blueprint.reflect.ReferenceListMetadata)

Example 8 with Metadata

use of org.osgi.service.blueprint.reflect.Metadata in project aries by apache.

the class SpringOsgiNamespaceHandler method parseInlinedTarget.

private Target parseInlinedTarget(ParserContext context, ComponentMetadata metadata, Element element) {
    Target listenerComponent;
    if (BLUEPRINT_NAMESPACE.equals(element.getNamespaceURI()) && BEAN_ELEMENT.equals(element.getLocalName())) {
        listenerComponent = context.parseElement(BeanMetadata.class, metadata, element);
    } else {
        NamespaceHandler handler = context.getNamespaceHandler(URI.create(element.getNamespaceURI()));
        if (handler == null) {
            throw new IllegalStateException("No NamespaceHandler found for " + element.getNamespaceURI());
        }
        Metadata md = handler.parse(element, context);
        if (!(md instanceof Target)) {
            throw new IllegalStateException("NamespaceHandler did not return a Target instance but " + md);
        }
        listenerComponent = (Target) md;
    }
    return listenerComponent;
}
Also used : Target(org.osgi.service.blueprint.reflect.Target) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) NonNullMetadata(org.osgi.service.blueprint.reflect.NonNullMetadata) MutableReferenceMetadata(org.apache.aries.blueprint.mutable.MutableReferenceMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata) ReferenceMetadata(org.osgi.service.blueprint.reflect.ReferenceMetadata) MutableServiceMetadata(org.apache.aries.blueprint.mutable.MutableServiceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) MutableValueMetadata(org.apache.aries.blueprint.mutable.MutableValueMetadata) NamespaceHandler(org.apache.aries.blueprint.NamespaceHandler)

Example 9 with Metadata

use of org.osgi.service.blueprint.reflect.Metadata in project aries by apache.

the class Parser method parseCollection.

private CollectionMetadata parseCollection(Class collectionType, Element element, ComponentMetadata enclosingComponent) {
    // Parse attributes
    String valueType = element.hasAttribute(VALUE_TYPE_ATTRIBUTE) ? element.getAttribute(VALUE_TYPE_ATTRIBUTE) : null;
    // Parse elements
    List<Metadata> list = new ArrayList<Metadata>();
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element) {
            Metadata val = parseValueGroup((Element) node, enclosingComponent, null, true);
            list.add(val);
        }
    }
    return new CollectionMetadataImpl(collectionType, valueType, list);
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) NonNullMetadata(org.osgi.service.blueprint.reflect.NonNullMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ServiceReferenceMetadata(org.osgi.service.blueprint.reflect.ServiceReferenceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) NullMetadata(org.osgi.service.blueprint.reflect.NullMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) PropsMetadata(org.osgi.service.blueprint.reflect.PropsMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) IdRefMetadata(org.osgi.service.blueprint.reflect.IdRefMetadata) ReferenceMetadata(org.osgi.service.blueprint.reflect.ReferenceMetadata) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) ReferenceListMetadata(org.osgi.service.blueprint.reflect.ReferenceListMetadata) CollectionMetadataImpl(org.apache.aries.blueprint.reflect.CollectionMetadataImpl)

Example 10 with Metadata

use of org.osgi.service.blueprint.reflect.Metadata in project aries by apache.

the class Parser method loadComponents.

private void loadComponents(Document doc) {
    defaultTimeout = TIMEOUT_DEFAULT;
    defaultAvailability = AVAILABILITY_DEFAULT;
    defaultActivation = ACTIVATION_DEFAULT;
    Element root = doc.getDocumentElement();
    if (!isBlueprintNamespace(root.getNamespaceURI()) || !nodeNameEquals(root, BLUEPRINT_ELEMENT)) {
        throw new ComponentDefinitionException("Root element must be {" + BLUEPRINT_NAMESPACE + "}" + BLUEPRINT_ELEMENT + " element");
    }
    // Parse global attributes
    if (root.hasAttribute(DEFAULT_ACTIVATION_ATTRIBUTE)) {
        defaultActivation = root.getAttribute(DEFAULT_ACTIVATION_ATTRIBUTE);
    }
    if (root.hasAttribute(DEFAULT_TIMEOUT_ATTRIBUTE)) {
        defaultTimeout = root.getAttribute(DEFAULT_TIMEOUT_ATTRIBUTE);
    }
    if (root.hasAttribute(DEFAULT_AVAILABILITY_ATTRIBUTE)) {
        defaultAvailability = root.getAttribute(DEFAULT_AVAILABILITY_ATTRIBUTE);
    }
    // Parse custom attributes
    handleCustomAttributes(root.getAttributes(), null);
    // Parse elements
    // Break into 2 loops to ensure we scan the blueprint elements before
    // This is needed so that when we process the custom element, we know
    // the component definition registry has populated all blueprint components.
    NodeList nl = root.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element) {
            Element element = (Element) node;
            String namespaceUri = element.getNamespaceURI();
            if (isBlueprintNamespace(namespaceUri)) {
                parseBlueprintElement(element);
            }
        }
    }
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element) {
            Element element = (Element) node;
            String namespaceUri = element.getNamespaceURI();
            if (!isBlueprintNamespace(namespaceUri)) {
                Metadata component = parseCustomElement(element, null);
                if (component != null) {
                    if (!(component instanceof ComponentMetadata)) {
                        throw new ComponentDefinitionException("Expected a ComponentMetadata to be returned when parsing element " + element.getNodeName());
                    }
                    registry.registerComponentDefinition((ComponentMetadata) component);
                }
            }
        }
    }
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) NonNullMetadata(org.osgi.service.blueprint.reflect.NonNullMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ServiceReferenceMetadata(org.osgi.service.blueprint.reflect.ServiceReferenceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) NullMetadata(org.osgi.service.blueprint.reflect.NullMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) PropsMetadata(org.osgi.service.blueprint.reflect.PropsMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) IdRefMetadata(org.osgi.service.blueprint.reflect.IdRefMetadata) ReferenceMetadata(org.osgi.service.blueprint.reflect.ReferenceMetadata) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) ReferenceListMetadata(org.osgi.service.blueprint.reflect.ReferenceListMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata)

Aggregations

Metadata (org.osgi.service.blueprint.reflect.Metadata)30 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)26 BeanMetadata (org.osgi.service.blueprint.reflect.BeanMetadata)23 RefMetadata (org.osgi.service.blueprint.reflect.RefMetadata)21 ServiceMetadata (org.osgi.service.blueprint.reflect.ServiceMetadata)17 CollectionMetadata (org.osgi.service.blueprint.reflect.CollectionMetadata)16 MapMetadata (org.osgi.service.blueprint.reflect.MapMetadata)16 ValueMetadata (org.osgi.service.blueprint.reflect.ValueMetadata)16 MutableBeanMetadata (org.apache.aries.blueprint.mutable.MutableBeanMetadata)12 ReferenceListMetadata (org.osgi.service.blueprint.reflect.ReferenceListMetadata)12 ReferenceMetadata (org.osgi.service.blueprint.reflect.ReferenceMetadata)12 ServiceReferenceMetadata (org.osgi.service.blueprint.reflect.ServiceReferenceMetadata)12 Element (org.w3c.dom.Element)12 IdRefMetadata (org.osgi.service.blueprint.reflect.IdRefMetadata)11 NonNullMetadata (org.osgi.service.blueprint.reflect.NonNullMetadata)11 NullMetadata (org.osgi.service.blueprint.reflect.NullMetadata)11 PropsMetadata (org.osgi.service.blueprint.reflect.PropsMetadata)11 MutableRefMetadata (org.apache.aries.blueprint.mutable.MutableRefMetadata)9 PassThroughMetadata (org.apache.aries.blueprint.PassThroughMetadata)8 Node (org.w3c.dom.Node)8