Search in sources :

Example 6 with MutableRefMetadata

use of org.apache.aries.blueprint.mutable.MutableRefMetadata in project karaf by apache.

the class NamespaceHandler method createRef.

private RefMetadata createRef(ParserContext context, String value) {
    MutableRefMetadata r = context.createMetadata(MutableRefMetadata.class);
    r.setComponentId(value);
    return r;
}
Also used : MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata)

Example 7 with MutableRefMetadata

use of org.apache.aries.blueprint.mutable.MutableRefMetadata in project karaf by apache.

the class NamespaceHandler method createRef.

private RefMetadata createRef(ParserContext context, String id) {
    MutableRefMetadata idref = context.createMetadata(MutableRefMetadata.class);
    idref.setComponentId(id);
    return idref;
}
Also used : MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata)

Example 8 with MutableRefMetadata

use of org.apache.aries.blueprint.mutable.MutableRefMetadata in project aries by apache.

the class BlueprintNamespaceHandler method createRef.

private Metadata createRef(ParserContext parserContext, String id) {
    MutableRefMetadata ref = parserContext.createMetadata(MutableRefMetadata.class);
    ref.setComponentId(id);
    return ref;
}
Also used : MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata)

Example 9 with MutableRefMetadata

use of org.apache.aries.blueprint.mutable.MutableRefMetadata 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 10 with MutableRefMetadata

use of org.apache.aries.blueprint.mutable.MutableRefMetadata in project aries by apache.

the class NSHandlerOne method decorate.

//process attributes
public ComponentMetadata decorate(Node node, ComponentMetadata component, ParserContext context) {
    //and RefMetadata.
    if (component != null && component instanceof MutableBeanMetadata) {
        MutableBeanMetadata mbm = (MutableBeanMetadata) component;
        Attr a = (Attr) node;
        Element bean = a.getOwnerElement();
        String propname = bean.getAttributeNS(NSURI, ATTRIB_ONE);
        //if this were not a test, we might attempt to ensure this ref existed
        String passthruref = bean.getAttributeNS(NSURI, ATTRIB_TWO);
        MutableRefMetadata ref = (MutableRefMetadata) context.createMetadata(RefMetadata.class);
        ref.setComponentId(passthruref);
        mbm.addProperty(propname, ref);
    }
    return component;
}
Also used : MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata) MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata) Element(org.w3c.dom.Element) Attr(org.w3c.dom.Attr)

Aggregations

MutableRefMetadata (org.apache.aries.blueprint.mutable.MutableRefMetadata)11 Element (org.w3c.dom.Element)3 MutableReferenceMetadata (org.apache.aries.blueprint.mutable.MutableReferenceMetadata)2 Target (org.osgi.service.blueprint.reflect.Target)2 NamespaceHandler (org.apache.aries.blueprint.NamespaceHandler)1 MutableBeanMetadata (org.apache.aries.blueprint.mutable.MutableBeanMetadata)1 MutableServiceMetadata (org.apache.aries.blueprint.mutable.MutableServiceMetadata)1 MutableValueMetadata (org.apache.aries.blueprint.mutable.MutableValueMetadata)1 BeanMetadata (org.osgi.service.blueprint.reflect.BeanMetadata)1 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)1 Metadata (org.osgi.service.blueprint.reflect.Metadata)1 NonNullMetadata (org.osgi.service.blueprint.reflect.NonNullMetadata)1 RefMetadata (org.osgi.service.blueprint.reflect.RefMetadata)1 ReferenceMetadata (org.osgi.service.blueprint.reflect.ReferenceMetadata)1 ServiceMetadata (org.osgi.service.blueprint.reflect.ServiceMetadata)1 Attr (org.w3c.dom.Attr)1