Search in sources :

Example 66 with ComponentDefinitionException

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

the class Parser method handleCustomScope.

/**
 * Tests if a scope attribute value is a custom scope, and if so invokes
 * the appropriate namespace handler, passing the blueprint scope node.
 * <p>
 * Currently this tests for custom scope by looking for the presence of
 * a ':' char within the scope attribute value. This is valid as long as
 * the blueprint schema continues to restrict that custom scopes should
 * require that characters presence.
 * <p>
 *
 * @param scope Value of scope attribute
 * @param bean DOM element for bean associated to this scope
 * @return Metadata as processed by NS Handler.
 * @throws ComponentDefinitionException if an undeclared prefix is used,
 *           if a namespace handler is unavailable for a resolved prefix,
 *           or if the resolved prefix results as the blueprint namespace.
 */
private ComponentMetadata handleCustomScope(Node scope, Element bean, ComponentMetadata metadata) {
    URI scopeNS = getNamespaceForAttributeValue(scope);
    if (scopeNS != null && !BLUEPRINT_NAMESPACE.equals(scopeNS.toString())) {
        NamespaceHandler nsHandler = getNamespaceHandler(scopeNS);
        ParserContextImpl context = new ParserContextImpl(this, registry, metadata, scope);
        metadata = nsHandler.decorate(scope, metadata, context);
    } else if (scopeNS != null) {
        throw new ComponentDefinitionException("Custom scopes cannot use the blueprint namespace " + scope);
    }
    return metadata;
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) NamespaceHandler(org.apache.aries.blueprint.NamespaceHandler) URI(java.net.URI)

Example 67 with ComponentDefinitionException

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

the class CmNamespaceHandler method parseManagedServiceFactory.

private ComponentMetadata parseManagedServiceFactory(ParserContext context, Element element) {
    String id = getId(context, element);
    MutableBeanMetadata factoryMetadata = context.createMetadata(MutableBeanMetadata.class);
    generateIdIfNeeded(context, factoryMetadata);
    factoryMetadata.addProperty("id", createValue(context, factoryMetadata.getId()));
    factoryMetadata.setScope(BeanMetadata.SCOPE_SINGLETON);
    factoryMetadata.setRuntimeClass(CmManagedServiceFactory.class);
    factoryMetadata.setInitMethod("init");
    factoryMetadata.setDestroyMethod("destroy");
    factoryMetadata.addArgument(createRef(context, "blueprintContainer"), null, 0);
    factoryMetadata.addProperty("factoryPid", createValue(context, element.getAttribute(FACTORY_PID_ATTRIBUTE)));
    String autoExport = element.hasAttribute(AUTO_EXPORT_ATTRIBUTE) ? element.getAttribute(AUTO_EXPORT_ATTRIBUTE) : AUTO_EXPORT_DEFAULT;
    if (AUTO_EXPORT_DISABLED.equals(autoExport)) {
        autoExport = Integer.toString(ServiceMetadata.AUTO_EXPORT_DISABLED);
    } else if (AUTO_EXPORT_INTERFACES.equals(autoExport)) {
        autoExport = Integer.toString(ServiceMetadata.AUTO_EXPORT_INTERFACES);
    } else if (AUTO_EXPORT_CLASS_HIERARCHY.equals(autoExport)) {
        autoExport = Integer.toString(ServiceMetadata.AUTO_EXPORT_CLASS_HIERARCHY);
    } else if (AUTO_EXPORT_ALL.equals(autoExport)) {
        autoExport = Integer.toString(ServiceMetadata.AUTO_EXPORT_ALL_CLASSES);
    } else {
        throw new ComponentDefinitionException("Illegal value (" + autoExport + ") for " + AUTO_EXPORT_ATTRIBUTE + " attribute");
    }
    factoryMetadata.addProperty("autoExport", createValue(context, autoExport));
    String ranking = element.hasAttribute(RANKING_ATTRIBUTE) ? element.getAttribute(RANKING_ATTRIBUTE) : RANKING_DEFAULT;
    factoryMetadata.addProperty("ranking", createValue(context, ranking));
    List<String> interfaces = null;
    if (element.hasAttribute(INTERFACE_ATTRIBUTE)) {
        interfaces = Collections.singletonList(element.getAttribute(INTERFACE_ATTRIBUTE));
        factoryMetadata.addProperty("interfaces", createList(context, interfaces));
    }
    // Parse elements
    List<RegistrationListener> listeners = new ArrayList<RegistrationListener>();
    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, INTERFACES_ELEMENT)) {
                    if (interfaces != null) {
                        throw new ComponentDefinitionException("Only one of " + INTERFACE_ATTRIBUTE + " attribute or " + INTERFACES_ELEMENT + " element must be used");
                    }
                    interfaces = parseInterfaceNames(e);
                    factoryMetadata.addProperty("interfaces", createList(context, interfaces));
                } else if (nodeNameEquals(e, SERVICE_PROPERTIES_ELEMENT)) {
                    MapMetadata map = context.parseElement(MapMetadata.class, factoryMetadata, e);
                    factoryMetadata.addProperty("serviceProperties", map);
                    NodeList enl = e.getChildNodes();
                    for (int j = 0; j < enl.getLength(); j++) {
                        Node enode = enl.item(j);
                        if (enode instanceof Element) {
                            if (isCmNamespace(enode.getNamespaceURI()) && nodeNameEquals(enode, CM_PROPERTIES_ELEMENT)) {
                                decorateCmProperties(context, (Element) enode, factoryMetadata);
                            }
                        }
                    }
                } else if (nodeNameEquals(e, REGISTRATION_LISTENER_ELEMENT)) {
                    listeners.add(context.parseElement(RegistrationListener.class, factoryMetadata, e));
                }
            } else if (isCmNamespace(e.getNamespaceURI())) {
                if (nodeNameEquals(e, MANAGED_COMPONENT_ELEMENT)) {
                    MutableBeanMetadata managedComponent = context.parseElement(MutableBeanMetadata.class, null, e);
                    generateIdIfNeeded(context, managedComponent);
                    managedComponent.setScope(BeanMetadata.SCOPE_PROTOTYPE);
                    // destroy-method on managed-component has different signature than on regular beans
                    // so we'll handle it differently
                    String destroyMethod = managedComponent.getDestroyMethod();
                    if (destroyMethod != null) {
                        factoryMetadata.addProperty("componentDestroyMethod", createValue(context, destroyMethod));
                        managedComponent.setDestroyMethod(null);
                    }
                    context.getComponentDefinitionRegistry().registerComponentDefinition(managedComponent);
                    factoryMetadata.addProperty("managedComponentName", createIdRef(context, managedComponent.getId()));
                }
            }
        }
    }
    MutableCollectionMetadata listenerCollection = context.createMetadata(MutableCollectionMetadata.class);
    listenerCollection.setCollectionClass(List.class);
    for (RegistrationListener listener : listeners) {
        MutableBeanMetadata bean = context.createMetadata(MutableBeanMetadata.class);
        bean.setRuntimeClass(ServiceListener.class);
        bean.addProperty("listener", listener.getListenerComponent());
        bean.addProperty("registerMethod", createValue(context, listener.getRegistrationMethod()));
        bean.addProperty("unregisterMethod", createValue(context, listener.getUnregistrationMethod()));
        listenerCollection.addValue(bean);
    }
    factoryMetadata.addProperty("listeners", listenerCollection);
    context.getComponentDefinitionRegistry().registerComponentDefinition(factoryMetadata);
    MutableBeanMetadata mapMetadata = context.createMetadata(MutableBeanMetadata.class);
    mapMetadata.setScope(BeanMetadata.SCOPE_SINGLETON);
    mapMetadata.setId(id);
    mapMetadata.setFactoryComponent(createRef(context, factoryMetadata.getId()));
    mapMetadata.setFactoryMethod("getServiceMap");
    return mapMetadata;
}
Also used : RegistrationListener(org.osgi.service.blueprint.reflect.RegistrationListener) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) MutableCollectionMetadata(org.apache.aries.blueprint.mutable.MutableCollectionMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) MutableMapMetadata(org.apache.aries.blueprint.mutable.MutableMapMetadata)

Example 68 with ComponentDefinitionException

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

the class CmNamespaceHandler method decorateManagedProperties.

private ComponentMetadata decorateManagedProperties(ParserContext context, Element element, ComponentMetadata component) {
    if (!(component instanceof MutableBeanMetadata)) {
        throw new ComponentDefinitionException("Element " + MANAGED_PROPERTIES_ELEMENT + " must be used inside a <bp:bean> element");
    }
    generateIdIfNeeded(context, ((MutableBeanMetadata) component));
    MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class);
    metadata.setProcessor(true);
    metadata.setId(getId(context, element));
    metadata.setRuntimeClass(CmManagedProperties.class);
    String persistentId = element.getAttribute(PERSISTENT_ID_ATTRIBUTE);
    // ManagedService if the persistentId is not an empty string.
    if (persistentId.length() > 0) {
        metadata.setInitMethod("init");
        metadata.setDestroyMethod("destroy");
    }
    metadata.addProperty("blueprintContainer", createRef(context, "blueprintContainer"));
    metadata.addProperty("configAdmin", createConfigurationAdminRef(context));
    metadata.addProperty("managedObjectManager", createRef(context, MANAGED_OBJECT_MANAGER_NAME));
    metadata.addProperty("persistentId", createValue(context, persistentId));
    String updateStrategy = element.getAttribute(UPDATE_STRATEGY_ATTRIBUTE);
    if (updateStrategy != null) {
        metadata.addProperty("updateStrategy", createValue(context, updateStrategy));
    }
    if (element.hasAttribute(UPDATE_METHOD_ATTRIBUTE)) {
        metadata.addProperty("updateMethod", createValue(context, element.getAttribute(UPDATE_METHOD_ATTRIBUTE)));
    } else if ("component-managed".equals(updateStrategy)) {
        throw new ComponentDefinitionException(UPDATE_METHOD_ATTRIBUTE + " attribute must be set when " + UPDATE_STRATEGY_ATTRIBUTE + " is set to 'component-managed'");
    }
    metadata.addProperty("beanName", createIdRef(context, component.getId()));
    context.getComponentDefinitionRegistry().registerComponentDefinition(metadata);
    return component;
}
Also used : MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException)

Example 69 with ComponentDefinitionException

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

the class CmNamespaceHandler method parseInterfaceNames.

public List<String> parseInterfaceNames(Element element) {
    List<String> interfaceNames = 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 (nodeNameEquals(e, VALUE_ELEMENT)) {
                String v = getTextValue(e).trim();
                if (interfaceNames.contains(v)) {
                    throw new ComponentDefinitionException("The element " + INTERFACES_ELEMENT + " should not contain the same interface twice");
                }
                interfaceNames.add(getTextValue(e));
            } else {
                throw new ComponentDefinitionException("Unsupported element " + e.getNodeName() + " inside an " + INTERFACES_ELEMENT + " element");
            }
        }
    }
    return interfaceNames;
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element)

Example 70 with ComponentDefinitionException

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

the class CmNamespaceHandler method decorate.

public ComponentMetadata decorate(Node node, ComponentMetadata component, ParserContext context) {
    LOGGER.debug("Decorating node {{}}{}", node.getNamespaceURI(), node.getLocalName());
    ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry();
    registerManagedObjectManager(context, registry);
    if (node instanceof Element) {
        if (nodeNameEquals(node, MANAGED_PROPERTIES_ELEMENT)) {
            return decorateManagedProperties(context, (Element) node, component);
        } else if (nodeNameEquals(node, CM_PROPERTIES_ELEMENT)) {
            return decorateCmProperties(context, (Element) node, component);
        } else {
            throw new ComponentDefinitionException("Unsupported element: " + node.getNodeName());
        }
    } else {
        throw new ComponentDefinitionException("Illegal use of blueprint cm namespace");
    }
}
Also used : ComponentDefinitionRegistry(org.apache.aries.blueprint.ComponentDefinitionRegistry) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) Element(org.w3c.dom.Element)

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