Search in sources :

Example 1 with NonNullMetadata

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

the class Parser method parseMapEntry.

private MapEntry parseMapEntry(Element element, ComponentMetadata enclosingComponent, String keyType, String valueType) {
    // Parse attributes
    String key = element.hasAttribute(KEY_ATTRIBUTE) ? element.getAttribute(KEY_ATTRIBUTE) : null;
    String keyRef = element.hasAttribute(KEY_REF_ATTRIBUTE) ? element.getAttribute(KEY_REF_ATTRIBUTE) : null;
    String value = element.hasAttribute(VALUE_ATTRIBUTE) ? element.getAttribute(VALUE_ATTRIBUTE) : null;
    String valueRef = element.hasAttribute(VALUE_REF_ATTRIBUTE) ? element.getAttribute(VALUE_REF_ATTRIBUTE) : null;
    // Parse elements
    NonNullMetadata keyValue = null;
    Metadata valValue = null;
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element) {
            Element e = (Element) node;
            if (nodeNameEquals(e, KEY_ELEMENT)) {
                keyValue = parseMapKeyEntry(e, enclosingComponent, keyType);
            } else {
                valValue = parseValueGroup(e, enclosingComponent, valueType, true);
            }
        }
    }
    // Check key
    if (keyValue != null && (key != null || keyRef != null) || (keyValue == null && key == null && keyRef == null)) {
        throw new ComponentDefinitionException("Only and only one of " + KEY_ATTRIBUTE + " attribute, " + KEY_REF_ATTRIBUTE + " attribute or " + KEY_ELEMENT + " element must be set");
    } else if (keyValue == null && key != null) {
        keyValue = new ValueMetadataImpl(key, keyType);
    } else if (keyValue == null) /*&& keyRef != null*/
    {
        keyValue = new RefMetadataImpl(keyRef);
    }
    // Check value
    if (valValue != null && (value != null || valueRef != null) || (valValue == null && value == null && valueRef == null)) {
        throw new ComponentDefinitionException("Only and only one of " + VALUE_ATTRIBUTE + " attribute, " + VALUE_REF_ATTRIBUTE + " attribute or sub element must be set");
    } else if (valValue == null && value != null) {
        valValue = new ValueMetadataImpl(value, valueType);
    } else if (valValue == null) /*&& valueRef != null*/
    {
        valValue = new RefMetadataImpl(valueRef);
    }
    return new MapEntryImpl(keyValue, valValue);
}
Also used : ValueMetadataImpl(org.apache.aries.blueprint.reflect.ValueMetadataImpl) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) MapEntryImpl(org.apache.aries.blueprint.reflect.MapEntryImpl) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) 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) NonNullMetadata(org.osgi.service.blueprint.reflect.NonNullMetadata) RefMetadataImpl(org.apache.aries.blueprint.reflect.RefMetadataImpl) IdRefMetadataImpl(org.apache.aries.blueprint.reflect.IdRefMetadataImpl)

Example 2 with NonNullMetadata

use of org.osgi.service.blueprint.reflect.NonNullMetadata in project geronimo-xbean by apache.

the class XBeanNamespaceHandler method parseCustomMapElement.

protected Metadata parseCustomMapElement(MutableBeanMetadata beanMetadata, Element element, String name, ParserContext parserContext) {
    MutableMapMetadata map = parserContext.createMetadata(MutableMapMetadata.class);
    Element parent = (Element) element.getParentNode();
    String entryName = mappingMetaData.getMapEntryName(getLocalName(parent), name);
    String keyName = mappingMetaData.getMapKeyName(getLocalName(parent), name);
    String dups = mappingMetaData.getMapDupsMode(getLocalName(parent), name);
    boolean flat = mappingMetaData.isFlatMap(getLocalName(parent), name);
    String defaultKey = mappingMetaData.getMapDefaultKey(getLocalName(parent), name);
    if (entryName == null)
        entryName = "property";
    if (keyName == null)
        keyName = "key";
    if (dups == null)
        dups = "replace";
    // TODO : support further customizations
    // String valueName = "value";
    // boolean keyIsAttr = true;
    // boolean valueIsAttr = false;
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element) {
            Element childElement = (Element) node;
            String localName = childElement.getLocalName();
            String uri = childElement.getNamespaceURI();
            if (localName == null || localName.equals("xmlns") || localName.startsWith("xmlns:")) {
                continue;
            }
            // attributes from namespace-specific attributes
            if (!flat && !isEmpty(uri) && localName.equals(entryName)) {
                String key = childElement.getAttributeNS(uri, keyName);
                if (key == null || key.length() == 0) {
                    key = defaultKey;
                }
                if (key == null) {
                    throw new RuntimeException("No key defined for map " + entryName);
                }
                NonNullMetadata keyValue = (NonNullMetadata) getValue(key, mappingMetaData.getPropertyEditor(localName, key), parserContext);
                Element valueElement = getFirstChildElement(childElement);
                Metadata value;
                if (valueElement != null) {
                    value = parserContext.parseElement(Metadata.class, beanMetadata, valueElement);
                // String valueElUri = valueElement.getNamespaceURI();
                // String valueElLocalName = valueElement.getLocalName();
                // if (valueElUri == null ||
                // valueElUri.equals(BLUEPRINT_NAMESPACE)) {
                // if ("bean".equals(valueElLocalName)) {
                // value = parserContext.parseElement(BeanMetadata.class, beanMetadata, valueElement);
                // } else {
                // value = parserContext.parseElement(BeanProperty.class, beanMetadata, valueElement).getValue();
                // }
                // } else {
                // value = parserContext.parseElement(ValueMetadata.class, beanMetadata, valueElement);
                // }
                } else {
                    value = getValue(getElementText(childElement), mappingMetaData.getPropertyEditor(localName, key), parserContext);
                }
                addValueToMap(map, keyValue, value, dups, parserContext);
            } else if (flat && !isEmpty(uri)) {
                String key = childElement.getAttributeNS(uri, keyName);
                if (key == null || key.length() == 0) {
                    key = defaultKey;
                }
                if (key == null) {
                    throw new RuntimeException("No key defined for map entry " + entryName);
                }
                NonNullMetadata keyValue = (NonNullMetadata) getValue(key, mappingMetaData.getPropertyEditor(localName, key), parserContext);
                childElement = cloneElement(childElement);
                childElement.removeAttributeNS(uri, keyName);
                Metadata bdh = parse(childElement, parserContext);
                addValueToMap(map, keyValue, bdh, dups, parserContext);
            }
        }
    }
    return map;
}
Also used : 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) Metadata(org.osgi.service.blueprint.reflect.Metadata) NonNullMetadata(org.osgi.service.blueprint.reflect.NonNullMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata) ServiceReferenceMetadata(org.osgi.service.blueprint.reflect.ServiceReferenceMetadata) ReferenceMetadata(org.osgi.service.blueprint.reflect.ReferenceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) NullMetadata(org.osgi.service.blueprint.reflect.NullMetadata) MutableCollectionMetadata(org.apache.aries.blueprint.mutable.MutableCollectionMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) MutableValueMetadata(org.apache.aries.blueprint.mutable.MutableValueMetadata) MutableMapMetadata(org.apache.aries.blueprint.mutable.MutableMapMetadata) NonNullMetadata(org.osgi.service.blueprint.reflect.NonNullMetadata) MutableMapMetadata(org.apache.aries.blueprint.mutable.MutableMapMetadata)

Example 3 with NonNullMetadata

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

the class SpringOsgiNamespaceHandler method parseService.

private Metadata parseService(Element element, ParserContext context) {
    MutableServiceMetadata metadata = context.createMetadata(MutableServiceMetadata.class);
    // Parse attributes
    if (element.hasAttribute(ID_ATTRIBUTE)) {
        metadata.setId(element.getAttribute(ID_ATTRIBUTE));
    } else {
        metadata.setId(generateId(context));
    }
    if (nonEmpty(element.getAttribute(REF_ATTRIBUTE)) != null) {
        MutableRefMetadata ref = context.createMetadata(MutableRefMetadata.class);
        ref.setComponentId(element.getAttribute(REF_ATTRIBUTE));
        metadata.setServiceComponent(ref);
    }
    metadata.setRanking(nonEmpty(element.getAttribute(RANKING_ATTRIBUTE)) != null ? Integer.parseInt(element.getAttribute(RANKING_ATTRIBUTE)) : 0);
    String itf = nonEmpty(element.getAttribute(INTERFACE_ATTRIBUTE));
    if (itf != null) {
        metadata.addInterface(itf);
    }
    String[] dependsOn = StringUtils.tokenizeToStringArray(nonEmpty(element.getAttribute(DEPENDS_ON_ATTRIBUTE)), ",; ");
    metadata.setDependsOn(dependsOn != null ? Arrays.asList(dependsOn) : null);
    String autoExp = nonEmpty(element.getAttribute(AUTO_EXPORT_ATTRIBUTE));
    if (AUTO_EXPORT_INTERFACES.equals(autoExp)) {
        metadata.setAutoExport(ServiceMetadata.AUTO_EXPORT_INTERFACES);
    } else if (AUTO_EXPORT_CLASS_HIERARCHY.equals(autoExp)) {
        metadata.setAutoExport(ServiceMetadata.AUTO_EXPORT_CLASS_HIERARCHY);
    } else if (AUTO_EXPORT_ALL_CLASSES.equals(autoExp)) {
        metadata.setAutoExport(ServiceMetadata.AUTO_EXPORT_ALL_CLASSES);
    } else {
        metadata.setAutoExport(ServiceMetadata.AUTO_EXPORT_DISABLED);
    }
    // Parse child elements
    for (Element child : getChildren(element)) {
        if (element.getNamespaceURI().equals(child.getNamespaceURI())) {
            if (INTERFACES_ELEMENT.equals(child.getLocalName())) {
                List<String> itfs = parseInterfaces(child);
                for (String intf : itfs) {
                    metadata.addInterface(intf);
                }
            } else if (REGISTRATION_LISTENER_ELEMENT.equals(child.getLocalName())) {
                String regMethod = nonEmpty(child.getAttribute(REGISTRATION_METHOD_ATTRIBUTE));
                String unregMethod = nonEmpty(child.getAttribute(UNREGISTRATION_METHOD_ATTRIBUTE));
                String refStr = nonEmpty(child.getAttribute(REF_ATTRIBUTE));
                Target listenerComponent = null;
                if (refStr != null) {
                    MutableRefMetadata ref = context.createMetadata(MutableRefMetadata.class);
                    ref.setComponentId(refStr);
                    listenerComponent = ref;
                }
                for (Element cchild : getChildren(child)) {
                    if (listenerComponent != null) {
                        throw new IllegalArgumentException("Only one of @ref attribute or inlined bean definition element is allowed");
                    }
                    listenerComponent = parseInlinedTarget(context, metadata, cchild);
                }
                if (listenerComponent == null) {
                    throw new IllegalArgumentException("Missing @ref attribute or inlined bean definition element");
                }
                metadata.addRegistrationListener(listenerComponent, regMethod, unregMethod);
            } else if (SERVICE_PROPERTIES_ELEMENT.equals(child.getLocalName())) {
                // TODO: @key-type
                for (Element e : getChildren(child)) {
                    if (ENTRY_ELEMENT.equals(e.getLocalName())) {
                        NonNullMetadata key;
                        Metadata val;
                        boolean hasKeyAttribute = e.hasAttribute(KEY_ATTRIBUTE);
                        boolean hasKeyRefAttribute = e.hasAttribute(KEY_REF_ATTRIBUTE);
                        if (hasKeyRefAttribute && !hasKeyAttribute) {
                            MutableRefMetadata r = context.createMetadata(MutableRefMetadata.class);
                            r.setComponentId(e.getAttribute(KEY_REF_ATTRIBUTE));
                            key = r;
                        } else if (hasKeyAttribute && !hasKeyRefAttribute) {
                            MutableValueMetadata v = context.createMetadata(MutableValueMetadata.class);
                            v.setStringValue(e.getAttribute(KEY_ATTRIBUTE));
                            key = v;
                        } else {
                            throw new IllegalStateException("Either key or key-ref must be specified");
                        }
                        // TODO: support key
                        boolean hasValAttribute = e.hasAttribute(VALUE_ATTRIBUTE);
                        boolean hasValRefAttribute = e.hasAttribute(VALUE_REF_ATTRIBUTE);
                        if (hasValRefAttribute && !hasValAttribute) {
                            MutableRefMetadata r = context.createMetadata(MutableRefMetadata.class);
                            r.setComponentId(e.getAttribute(VALUE_REF_ATTRIBUTE));
                            val = r;
                        } else if (hasValAttribute && !hasValRefAttribute) {
                            MutableValueMetadata v = context.createMetadata(MutableValueMetadata.class);
                            v.setStringValue(e.getAttribute(VALUE_ATTRIBUTE));
                            val = v;
                        } else {
                            throw new IllegalStateException("Either val or val-ref must be specified");
                        }
                        // TODO: support children elements ?
                        metadata.addServiceProperty(key, val);
                    }
                }
            }
        } else if (BLUEPRINT_NAMESPACE.equals(child.getNamespaceURI()) && BEAN_ELEMENT.equals(child.getLocalName())) {
            if (metadata.getServiceComponent() != null) {
                throw new IllegalArgumentException("Only one of @ref attribute and bean element is allowed");
            }
            Target bean = context.parseElement(BeanMetadata.class, metadata, child);
            metadata.setServiceComponent(bean);
        } else {
            if (metadata.getServiceComponent() != null) {
                throw new IllegalArgumentException("Only one of @ref attribute or inlined bean definition element is allowed");
            }
            NamespaceHandler handler = context.getNamespaceHandler(URI.create(child.getNamespaceURI()));
            if (handler == null) {
                throw new IllegalStateException("No NamespaceHandler found for " + child.getNamespaceURI());
            }
            Metadata md = handler.parse(child, context);
            if (!(md instanceof Target)) {
                throw new IllegalStateException("NamespaceHandler did not return a Target instance but " + md);
            }
            metadata.setServiceComponent((Target) md);
        }
    }
    return metadata;
}
Also used : Element(org.w3c.dom.Element) 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) MutableValueMetadata(org.apache.aries.blueprint.mutable.MutableValueMetadata) MutableServiceMetadata(org.apache.aries.blueprint.mutable.MutableServiceMetadata) Target(org.osgi.service.blueprint.reflect.Target) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata) NonNullMetadata(org.osgi.service.blueprint.reflect.NonNullMetadata) NamespaceHandler(org.apache.aries.blueprint.NamespaceHandler)

Example 4 with NonNullMetadata

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

the class Parser method parseMapKeyEntry.

private NonNullMetadata parseMapKeyEntry(Element element, ComponentMetadata enclosingComponent, String keyType) {
    NonNullMetadata keyValue = null;
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element) {
            Element e = (Element) node;
            if (keyValue != null) {
            // TODO: throw an exception
            }
            keyValue = (NonNullMetadata) parseValueGroup(e, enclosingComponent, keyType, false);
            break;
        }
    }
    if (keyValue == null) {
    // TODO: throw an exception
    }
    return keyValue;
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) NonNullMetadata(org.osgi.service.blueprint.reflect.NonNullMetadata)

Aggregations

NonNullMetadata (org.osgi.service.blueprint.reflect.NonNullMetadata)4 Element (org.w3c.dom.Element)4 BeanMetadata (org.osgi.service.blueprint.reflect.BeanMetadata)3 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)3 Metadata (org.osgi.service.blueprint.reflect.Metadata)3 ReferenceMetadata (org.osgi.service.blueprint.reflect.ReferenceMetadata)3 Node (org.w3c.dom.Node)3 NodeList (org.w3c.dom.NodeList)3 MutableRefMetadata (org.apache.aries.blueprint.mutable.MutableRefMetadata)2 MutableValueMetadata (org.apache.aries.blueprint.mutable.MutableValueMetadata)2 CollectionMetadata (org.osgi.service.blueprint.reflect.CollectionMetadata)2 NullMetadata (org.osgi.service.blueprint.reflect.NullMetadata)2 RefMetadata (org.osgi.service.blueprint.reflect.RefMetadata)2 ServiceMetadata (org.osgi.service.blueprint.reflect.ServiceMetadata)2 ServiceReferenceMetadata (org.osgi.service.blueprint.reflect.ServiceReferenceMetadata)2 ValueMetadata (org.osgi.service.blueprint.reflect.ValueMetadata)2 NamespaceHandler (org.apache.aries.blueprint.NamespaceHandler)1 MutableBeanMetadata (org.apache.aries.blueprint.mutable.MutableBeanMetadata)1 MutableCollectionMetadata (org.apache.aries.blueprint.mutable.MutableCollectionMetadata)1 MutableMapMetadata (org.apache.aries.blueprint.mutable.MutableMapMetadata)1