Search in sources :

Example 41 with Metadata

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

the class Parser method parseBeanProperty.

public BeanProperty parseBeanProperty(ComponentMetadata enclosingComponent, Element element) {
    String name = element.hasAttribute(NAME_ATTRIBUTE) ? element.getAttribute(NAME_ATTRIBUTE) : null;
    Metadata value = parseArgumentOrPropertyValue(element, enclosingComponent);
    return new BeanPropertyImpl(name, value);
}
Also used : BeanPropertyImpl(org.apache.aries.blueprint.reflect.BeanPropertyImpl) 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)

Example 42 with Metadata

use of org.osgi.service.blueprint.reflect.Metadata 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 43 with Metadata

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

the class CmNamespaceHandler method parsePropertyPlaceholder.

private ComponentMetadata parsePropertyPlaceholder(ParserContext context, Element element) {
    MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class);
    metadata.setProcessor(true);
    metadata.setId(getId(context, element));
    metadata.setScope(BeanMetadata.SCOPE_SINGLETON);
    metadata.setRuntimeClass(CmPropertyPlaceholder.class);
    metadata.setInitMethod("init");
    metadata.setDestroyMethod("destroy");
    metadata.addProperty("blueprintContainer", createRef(context, "blueprintContainer"));
    metadata.addProperty("configAdmin", createConfigurationAdminRef(context));
    metadata.addProperty("persistentId", createValue(context, element.getAttribute(PERSISTENT_ID_ATTRIBUTE)));
    String prefix = element.hasAttribute(PLACEHOLDER_PREFIX_ATTRIBUTE) ? element.getAttribute(PLACEHOLDER_PREFIX_ATTRIBUTE) : "${";
    metadata.addProperty("placeholderPrefix", createValue(context, prefix));
    String suffix = element.hasAttribute(PLACEHOLDER_SUFFIX_ATTRIBUTE) ? element.getAttribute(PLACEHOLDER_SUFFIX_ATTRIBUTE) : "}";
    metadata.addProperty("placeholderSuffix", createValue(context, suffix));
    String nullValue = element.hasAttribute(PLACEHOLDER_NULL_VALUE_ATTRIBUTE) ? element.getAttribute(PLACEHOLDER_NULL_VALUE_ATTRIBUTE) : null;
    if (nullValue != null) {
        metadata.addProperty("nullValue", createValue(context, nullValue));
    }
    String defaultsRef = element.hasAttribute(DEFAULTS_REF_ATTRIBUTE) ? element.getAttribute(DEFAULTS_REF_ATTRIBUTE) : null;
    if (defaultsRef != null) {
        metadata.addProperty("defaultProperties", createRef(context, defaultsRef));
    }
    String ignoreMissingLocations = extractIgnoreMissingLocations(element);
    if (ignoreMissingLocations != null) {
        metadata.addProperty("ignoreMissingLocations", createValue(context, ignoreMissingLocations));
    }
    String systemProperties = extractSystemPropertiesAttribute(element);
    if (systemProperties == null) {
        systemProperties = SYSTEM_PROPERTIES_NEVER;
    }
    metadata.addProperty("systemProperties", createValue(context, systemProperties));
    String updateStrategy = element.getAttribute(UPDATE_STRATEGY_ATTRIBUTE);
    if (updateStrategy != null) {
        metadata.addProperty("updateStrategy", createValue(context, updateStrategy));
    }
    metadata.addProperty("managedObjectManager", createRef(context, MANAGED_OBJECT_MANAGER_NAME));
    // Parse elements
    List<String> locations = new ArrayList<String>();
    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 (isCmNamespace(e.getNamespaceURI())) {
                if (nodeNameEquals(e, DEFAULT_PROPERTIES_ELEMENT)) {
                    if (defaultsRef != null) {
                        throw new ComponentDefinitionException("Only one of " + DEFAULTS_REF_ATTRIBUTE + " attribute or " + DEFAULT_PROPERTIES_ELEMENT + " element is allowed");
                    }
                    Metadata props = parseDefaultProperties(context, metadata, e);
                    metadata.addProperty("defaultProperties", props);
                }
            } else if (isExtNamespace(e.getNamespaceURI())) {
                if (nodeNameEquals(e, LOCATION_ELEMENT)) {
                    locations.add(getTextValue(e));
                }
            }
        }
    }
    if (!locations.isEmpty()) {
        metadata.addProperty("locations", createList(context, locations));
    }
    PlaceholdersUtils.validatePlaceholder(metadata, context.getComponentDefinitionRegistry());
    return metadata;
}
Also used : MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) 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) Metadata(org.osgi.service.blueprint.reflect.Metadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) MutableReferenceMetadata(org.apache.aries.blueprint.mutable.MutableReferenceMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata) IdRefMetadata(org.osgi.service.blueprint.reflect.IdRefMetadata) MutableIdRefMetadata(org.apache.aries.blueprint.mutable.MutableIdRefMetadata) MutableComponentMetadata(org.apache.aries.blueprint.mutable.MutableComponentMetadata) ReferenceMetadata(org.osgi.service.blueprint.reflect.ReferenceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) 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)

Example 44 with Metadata

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

the class NSHandlerOne method parse.

// process elements
public Metadata parse(Element element, ParserContext context) {
    Metadata retval = null;
    if (element.getLocalName().equals(ELT_NAME)) {
        final String id = element.getAttributeNS(NSURI, ATTRIB_ONE);
        final String value = element.getAttributeNS(NSURI, ATTRIB_TWO);
        PassThroughMetadata ptm = new PassThroughMetadata() {

            public String getId() {
                return id;
            }

            // not used currently
            public List<String> getDependsOn() {
                return null;
            }

            // also not used currently
            public int getActivation() {
                return 0;
            }

            public Object getObject() {
                return value;
            }
        };
        retval = ptm;
    }
    return retval;
}
Also used : Metadata(org.osgi.service.blueprint.reflect.Metadata) PassThroughMetadata(org.apache.aries.blueprint.PassThroughMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) PassThroughMetadata(org.apache.aries.blueprint.PassThroughMetadata)

Example 45 with Metadata

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

the class NSHandlerSix method parse.

// process elements
public Metadata parse(Element element, ParserContext context) {
    Metadata retval = null;
    if (element.getLocalName().equals(ELT_NAME)) {
        final String id = element.getAttributeNS(NSURI, ATTRIB_ID);
        MutableBeanMetadata bm = context.createMetadata(MutableBeanMetadata.class);
        bm.setId(id);
        bm.setScope("PROTOTYPE");
        bm.setClassName(TestBean.class.getName());
        retval = bm;
    }
    return retval;
}
Also used : MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) PassThroughMetadata(org.apache.aries.blueprint.PassThroughMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata)

Aggregations

Metadata (org.osgi.service.blueprint.reflect.Metadata)46 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)35 BeanMetadata (org.osgi.service.blueprint.reflect.BeanMetadata)32 ValueMetadata (org.osgi.service.blueprint.reflect.ValueMetadata)27 RefMetadata (org.osgi.service.blueprint.reflect.RefMetadata)26 MutableBeanMetadata (org.apache.aries.blueprint.mutable.MutableBeanMetadata)24 CollectionMetadata (org.osgi.service.blueprint.reflect.CollectionMetadata)23 Element (org.w3c.dom.Element)20 ReferenceMetadata (org.osgi.service.blueprint.reflect.ReferenceMetadata)17 ServiceMetadata (org.osgi.service.blueprint.reflect.ServiceMetadata)17 MapMetadata (org.osgi.service.blueprint.reflect.MapMetadata)16 ServiceReferenceMetadata (org.osgi.service.blueprint.reflect.ServiceReferenceMetadata)16 MutableRefMetadata (org.apache.aries.blueprint.mutable.MutableRefMetadata)15 NonNullMetadata (org.osgi.service.blueprint.reflect.NonNullMetadata)15 NullMetadata (org.osgi.service.blueprint.reflect.NullMetadata)15 IdRefMetadata (org.osgi.service.blueprint.reflect.IdRefMetadata)12 ReferenceListMetadata (org.osgi.service.blueprint.reflect.ReferenceListMetadata)12 Node (org.w3c.dom.Node)12 NodeList (org.w3c.dom.NodeList)12 MutableMapMetadata (org.apache.aries.blueprint.mutable.MutableMapMetadata)11