Search in sources :

Example 61 with ComponentDefinitionException

use of org.osgi.service.blueprint.container.ComponentDefinitionException in project aries by apache.

the class Parser method parseRegistrationListener.

public RegistrationListener parseRegistrationListener(Element element, ComponentMetadata enclosingComponent) {
    RegistrationListenerImpl listener = new RegistrationListenerImpl();
    Metadata listenerComponent = null;
    // Parse attributes
    if (element.hasAttribute(REF_ATTRIBUTE)) {
        listenerComponent = new RefMetadataImpl(element.getAttribute(REF_ATTRIBUTE));
    }
    String registrationMethod = null;
    if (element.hasAttribute(REGISTRATION_METHOD_ATTRIBUTE)) {
        registrationMethod = element.getAttribute(REGISTRATION_METHOD_ATTRIBUTE);
        listener.setRegistrationMethod(registrationMethod);
    }
    String unregistrationMethod = null;
    if (element.hasAttribute(UNREGISTRATION_METHOD_ATTRIBUTE)) {
        unregistrationMethod = element.getAttribute(UNREGISTRATION_METHOD_ATTRIBUTE);
        listener.setUnregistrationMethod(unregistrationMethod);
    }
    if (registrationMethod == null && unregistrationMethod == null) {
        throw new ComponentDefinitionException("One of " + REGISTRATION_METHOD_ATTRIBUTE + " or " + UNREGISTRATION_METHOD_ATTRIBUTE + " must be set");
    }
    // Parse elements
    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 (isBlueprintNamespace(e.getNamespaceURI())) {
                if (nodeNameEquals(e, REF_ELEMENT)) {
                    if (listenerComponent != null) {
                        throw new ComponentDefinitionException("Only one of " + REF_ATTRIBUTE + " attribute, " + REF_ELEMENT + ", " + BEAN_ELEMENT + ", " + REFERENCE_ELEMENT + ", " + SERVICE_ELEMENT + " or custom element can be set");
                    }
                    String component = e.getAttribute(COMPONENT_ID_ATTRIBUTE);
                    if (component == null || component.length() == 0) {
                        throw new ComponentDefinitionException("Element " + REF_ELEMENT + " must have a valid " + COMPONENT_ID_ATTRIBUTE + " attribute");
                    }
                    listenerComponent = new RefMetadataImpl(component);
                } else if (nodeNameEquals(e, BEAN_ELEMENT)) {
                    if (listenerComponent != null) {
                        throw new ComponentDefinitionException("Only one of " + REF_ATTRIBUTE + " attribute, " + REF_ELEMENT + ", " + BEAN_ELEMENT + ", " + REFERENCE_ELEMENT + ", " + SERVICE_ELEMENT + " or custom element can be set");
                    }
                    listenerComponent = parseBeanMetadata(e, false);
                } else if (nodeNameEquals(e, REFERENCE_ELEMENT)) {
                    if (listenerComponent != null) {
                        throw new ComponentDefinitionException("Only one of " + REF_ATTRIBUTE + " attribute, " + REF_ELEMENT + ", " + BEAN_ELEMENT + ", " + REFERENCE_ELEMENT + ", " + SERVICE_ELEMENT + " or custom element can be set");
                    }
                    listenerComponent = parseReference(e, false);
                } else if (nodeNameEquals(e, SERVICE_ELEMENT)) {
                    if (listenerComponent != null) {
                        throw new ComponentDefinitionException("Only one of " + REF_ATTRIBUTE + " attribute, " + REF_ELEMENT + ", " + BEAN_ELEMENT + ", " + REFERENCE_ELEMENT + ", " + SERVICE_ELEMENT + " or custom element can be set");
                    }
                    listenerComponent = parseService(e, false);
                }
            } else {
                if (listenerComponent != null) {
                    throw new ComponentDefinitionException("Only one of " + REF_ATTRIBUTE + " attribute, " + REF_ELEMENT + ", " + BEAN_ELEMENT + ", " + REFERENCE_ELEMENT + ", " + SERVICE_ELEMENT + " or custom element can be set");
                }
                listenerComponent = parseCustomElement(e, enclosingComponent);
            }
        }
    }
    if (listenerComponent == null) {
        throw new ComponentDefinitionException("One of " + REF_ATTRIBUTE + " attribute, " + REF_ELEMENT + ", " + BEAN_ELEMENT + ", " + REFERENCE_ELEMENT + ", " + SERVICE_ELEMENT + " or custom element must be set");
    }
    listener.setListenerComponent((Target) listenerComponent);
    return listener;
}
Also used : 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) 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) RegistrationListenerImpl(org.apache.aries.blueprint.reflect.RegistrationListenerImpl) RefMetadataImpl(org.apache.aries.blueprint.reflect.RefMetadataImpl) IdRefMetadataImpl(org.apache.aries.blueprint.reflect.IdRefMetadataImpl)

Example 62 with ComponentDefinitionException

use of org.osgi.service.blueprint.container.ComponentDefinitionException in project aries by apache.

the class BlueprintContainerImpl method processTypeConverters.

private void processTypeConverters() throws Exception {
    List<String> typeConverters = new ArrayList<String>();
    for (Target target : componentDefinitionRegistry.getTypeConverters()) {
        if (target instanceof ComponentMetadata) {
            typeConverters.add(((ComponentMetadata) target).getId());
        } else if (target instanceof RefMetadata) {
            typeConverters.add(((RefMetadata) target).getComponentId());
        } else {
            throw new ComponentDefinitionException("Unexpected metadata for type converter: " + target);
        }
    }
    Map<String, Object> objects = repository.createAll(typeConverters, Arrays.<Class<?>>asList(Converter.class));
    for (String name : typeConverters) {
        Object obj = objects.get(name);
        if (obj instanceof Converter) {
            converter.registerConverter((Converter) obj);
        } else {
            throw new ComponentDefinitionException("Type converter " + obj + " does not implement the " + Converter.class.getName() + " interface");
        }
    }
}
Also used : Target(org.osgi.service.blueprint.reflect.Target) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ArrayList(java.util.ArrayList) Converter(org.osgi.service.blueprint.container.Converter) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata)

Example 63 with ComponentDefinitionException

use of org.osgi.service.blueprint.container.ComponentDefinitionException in project aries by apache.

the class Parser method getScope.

/**
 * Takes an Attribute Node for the scope, and returns the value.<br>
 *
 * @param attrNode The DOM Node with the attribute value.
 * @return The scope as a stringified value. It should be either the value <code>prototype</code>,
 * <code>singleton</code>, or a namespace qualified value, e.g. {http://foo}bar
 * @throws ComponentDefinitionException if the namespace prefix in the attribute value cannot be resolved.
 */
private String getScope(Node attrNode) throws ComponentDefinitionException {
    String scope = null;
    if (attrNode != null && (attrNode instanceof Attr)) {
        Attr attr = (Attr) attrNode;
        String attrValue = attr.getValue();
        if (attrValue != null && attrValue.indexOf(":") != -1) {
            String[] parts = attrValue.split(":");
            String prefix = parts[0];
            String localName = parts[1];
            String namespaceURI = attr.getOwnerElement().lookupNamespaceURI(prefix);
            if (namespaceURI != null) {
                scope = new QName(namespaceURI, localName).toString();
            } else {
                throw new ComponentDefinitionException("Unable to determine namespace binding for prefix, " + prefix);
            }
        } else {
            scope = attrValue;
        }
    }
    return scope;
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) QName(javax.xml.namespace.QName) Attr(org.w3c.dom.Attr)

Example 64 with ComponentDefinitionException

use of org.osgi.service.blueprint.container.ComponentDefinitionException in project aries by apache.

the class Parser method parseProperty.

private MapEntry parseProperty(Element element) {
    // Parse attributes
    if (!element.hasAttribute(KEY_ATTRIBUTE)) {
        throw new ComponentDefinitionException(KEY_ATTRIBUTE + " attribute is required");
    }
    String value;
    if (element.hasAttribute(VALUE_ATTRIBUTE)) {
        value = element.getAttribute(VALUE_ATTRIBUTE);
    } else {
        value = getTextValue(element);
    }
    String key = element.getAttribute(KEY_ATTRIBUTE);
    return new MapEntryImpl(new ValueMetadataImpl(key), new ValueMetadataImpl(value));
}
Also used : ValueMetadataImpl(org.apache.aries.blueprint.reflect.ValueMetadataImpl) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) MapEntryImpl(org.apache.aries.blueprint.reflect.MapEntryImpl)

Example 65 with ComponentDefinitionException

use of org.osgi.service.blueprint.container.ComponentDefinitionException in project aries by apache.

the class Parser method parseBlueprintElement.

private void parseBlueprintElement(Element element) {
    if (nodeNameEquals(element, DESCRIPTION_ELEMENT)) {
    // Ignore description
    } else if (nodeNameEquals(element, TYPE_CONVERTERS_ELEMENT)) {
        parseTypeConverters(element);
    } else if (nodeNameEquals(element, BEAN_ELEMENT)) {
        ComponentMetadata component = parseBeanMetadata(element, true);
        registry.registerComponentDefinition(component);
    } else if (nodeNameEquals(element, SERVICE_ELEMENT)) {
        ComponentMetadata service = parseService(element, true);
        registry.registerComponentDefinition(service);
    } else if (nodeNameEquals(element, REFERENCE_ELEMENT)) {
        ComponentMetadata reference = parseReference(element, true);
        registry.registerComponentDefinition(reference);
    } else if (nodeNameEquals(element, REFERENCE_LIST_ELEMENT)) {
        ComponentMetadata references = parseRefList(element, true);
        registry.registerComponentDefinition(references);
    } else {
        throw new ComponentDefinitionException("Unknown element " + element.getNodeName() + " in namespace " + BLUEPRINT_NAMESPACE);
    }
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata)

Aggregations

ComponentDefinitionException (org.osgi.service.blueprint.container.ComponentDefinitionException)80 Node (org.w3c.dom.Node)26 Element (org.w3c.dom.Element)21 MutableBeanMetadata (org.apache.aries.blueprint.mutable.MutableBeanMetadata)18 NodeList (org.w3c.dom.NodeList)18 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)15 ArrayList (java.util.ArrayList)9 MutableReferenceMetadata (org.apache.aries.blueprint.mutable.MutableReferenceMetadata)8 RefMetadata (org.osgi.service.blueprint.reflect.RefMetadata)8 JAXBException (javax.xml.bind.JAXBException)7 MutablePassThroughMetadata (org.apache.aries.blueprint.mutable.MutablePassThroughMetadata)7 MutableServiceReferenceMetadata (org.apache.aries.blueprint.mutable.MutableServiceReferenceMetadata)7 IdRefMetadataImpl (org.apache.aries.blueprint.reflect.IdRefMetadataImpl)7 RefMetadataImpl (org.apache.aries.blueprint.reflect.RefMetadataImpl)7 ExpressionNode (org.apache.camel.model.ExpressionNode)7 BeanMetadata (org.osgi.service.blueprint.reflect.BeanMetadata)7 MapMetadata (org.osgi.service.blueprint.reflect.MapMetadata)7 Recipe (org.apache.aries.blueprint.di.Recipe)6 ComponentDefinitionRegistry (org.apache.aries.blueprint.ComponentDefinitionRegistry)5 MutableCollectionMetadata (org.apache.aries.blueprint.mutable.MutableCollectionMetadata)5