Search in sources :

Example 31 with ComponentDefinitionException

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

the class BlueprintRepository method validate.

public void validate() {
    for (Recipe recipe : getAllRecipes()) {
        // Check that references are satisfied
        String ref = null;
        if (recipe instanceof RefRecipe) {
            ref = ((RefRecipe) recipe).getIdRef();
        } else if (recipe instanceof IdRefRecipe) {
            ref = ((IdRefRecipe) recipe).getIdRef();
        }
        if (ref != null && getRecipe(ref) == null) {
            throw new ComponentDefinitionException("Unresolved ref/idref to component: " + ref);
        }
        // Check service
        if (recipe instanceof ServiceRecipe) {
            Recipe r = ((ServiceRecipe) recipe).getServiceRecipe();
            if (r instanceof RefRecipe) {
                r = getRecipe(((RefRecipe) r).getIdRef());
            }
            if (r instanceof ServiceRecipe) {
                throw new ComponentDefinitionException("The target for a <service> element must not be <service> element");
            }
            if (r instanceof ReferenceListRecipe) {
                throw new ComponentDefinitionException("The target for a <service> element must not be <reference-list> element");
            }
            CollectionRecipe listeners = ((ServiceRecipe) recipe).getListenersRecipe();
            for (Recipe lr : listeners.getDependencies()) {
                // The listener recipe is a bean recipe with the listener being set in a property
                for (Recipe l : lr.getDependencies()) {
                    if (l instanceof RefRecipe) {
                        l = getRecipe(((RefRecipe) l).getIdRef());
                    }
                    if (l instanceof ServiceRecipe) {
                        throw new ComponentDefinitionException("The target for a <registration-listener> element must not be <service> element");
                    }
                    if (l instanceof ReferenceListRecipe) {
                        throw new ComponentDefinitionException("The target for a <registration-listener> element must not be <reference-list> element");
                    }
                }
            }
        }
        // Check references
        if (recipe instanceof AbstractServiceReferenceRecipe) {
            CollectionRecipe listeners = ((AbstractServiceReferenceRecipe) recipe).getListenersRecipe();
            for (Recipe lr : listeners.getDependencies()) {
                // The listener recipe is a bean recipe with the listener being set in a property
                for (Recipe l : lr.getDependencies()) {
                    if (l instanceof RefRecipe) {
                        l = getRecipe(((RefRecipe) l).getIdRef());
                    }
                    if (l instanceof ServiceRecipe) {
                        throw new ComponentDefinitionException("The target for a <reference-listener> element must not be <service> element");
                    }
                    if (l instanceof ReferenceListRecipe) {
                        throw new ComponentDefinitionException("The target for a <reference-listener> element must not be <reference-list> element");
                    }
                }
            }
        }
    }
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) IdRefRecipe(org.apache.aries.blueprint.di.IdRefRecipe) Recipe(org.apache.aries.blueprint.di.Recipe) CollectionRecipe(org.apache.aries.blueprint.di.CollectionRecipe) RefRecipe(org.apache.aries.blueprint.di.RefRecipe) IdRefRecipe(org.apache.aries.blueprint.di.IdRefRecipe) IdRefRecipe(org.apache.aries.blueprint.di.IdRefRecipe) RefRecipe(org.apache.aries.blueprint.di.RefRecipe) CollectionRecipe(org.apache.aries.blueprint.di.CollectionRecipe)

Example 32 with ComponentDefinitionException

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

the class ExtNamespaceHandler method mergeList.

private void mergeList(ExtendedBeanMetadata bean1, MutableBeanMetadata bean2, String name) {
    Metadata m1 = getProperty(bean1, name);
    Metadata m2 = getProperty(bean2, name);
    if (m1 == null && m2 != null) {
        ((MutableBeanMetadata) bean1).addProperty(name, m2);
    } else if (m1 != null && m2 != null) {
        if (!(m1 instanceof MutableCollectionMetadata) || !(m2 instanceof MutableCollectionMetadata)) {
            throw new ComponentDefinitionException("Unable to merge " + name + " list properties");
        }
        MutableCollectionMetadata c1 = (MutableCollectionMetadata) m1;
        MutableCollectionMetadata c2 = (MutableCollectionMetadata) m2;
        for (Metadata v : c2.getValues()) {
            c1.addValue(v);
        }
    }
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ExtendedBeanMetadata(org.apache.aries.blueprint.ExtendedBeanMetadata) ExtendedReferenceListMetadata(org.apache.aries.blueprint.ExtendedReferenceListMetadata)

Example 33 with ComponentDefinitionException

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

the class ExtNamespaceHandler method mergeMap.

private void mergeMap(ExtendedBeanMetadata bean1, MutableBeanMetadata bean2, String name) {
    Metadata m1 = getProperty(bean1, name);
    Metadata m2 = getProperty(bean2, name);
    if (m1 == null && m2 != null) {
        ((MutableBeanMetadata) bean1).addProperty(name, m2);
    } else if (m1 != null && m2 != null) {
        if (!(m1 instanceof MutableMapMetadata) || !(m2 instanceof MutableMapMetadata)) {
            throw new ComponentDefinitionException("Unable to merge " + name + " list properties");
        }
        MutableMapMetadata c1 = (MutableMapMetadata) m1;
        MutableMapMetadata c2 = (MutableMapMetadata) m2;
        for (MapEntry e : c2.getEntries()) {
            c1.addEntry(e);
        }
    }
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ExtendedBeanMetadata(org.apache.aries.blueprint.ExtendedBeanMetadata) ExtendedReferenceListMetadata(org.apache.aries.blueprint.ExtendedReferenceListMetadata)

Example 34 with ComponentDefinitionException

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

the class ExtNamespaceHandler method parsePropertyPlaceholder.

private Metadata 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(PropertyPlaceholder.class);
    metadata.setInitMethod("init");
    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));
    metadata.addProperty("blueprintContainer", createRef(context, "blueprintContainer"));
    String defaultsRef = element.hasAttribute(DEFAULTS_REF_ATTRIBUTE) ? element.getAttribute(DEFAULTS_REF_ATTRIBUTE) : null;
    if (defaultsRef != null) {
        metadata.addProperty("defaultProperties", createRef(context, defaultsRef));
    }
    String ignoreMissingLocations = element.hasAttribute(IGNORE_MISSING_LOCATIONS_ATTRIBUTE) ? element.getAttribute(IGNORE_MISSING_LOCATIONS_ATTRIBUTE) : null;
    if (ignoreMissingLocations != null) {
        metadata.addProperty("ignoreMissingLocations", createValue(context, ignoreMissingLocations));
    }
    String systemProperties = element.hasAttribute(SYSTEM_PROPERTIES_ATTRIBUTE) ? element.getAttribute(SYSTEM_PROPERTIES_ATTRIBUTE) : null;
    if (systemProperties != null) {
        metadata.addProperty("systemProperties", createValue(context, systemProperties));
    }
    String evaluator = element.hasAttribute(EVALUATOR_ATTRIBUTE) ? element.getAttribute(EVALUATOR_ATTRIBUTE) : null;
    if (evaluator != null) {
        throw new IllegalStateException("Evaluators are not supported outside OSGi");
    }
    // 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 (BLUEPRINT_EXT_NAMESPACE_V1_0.equals(e.getNamespaceURI()) || BLUEPRINT_EXT_NAMESPACE_V1_1.equals(e.getNamespaceURI()) || BLUEPRINT_EXT_NAMESPACE_V1_2.equals(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 (nodeNameEquals(e, LOCATION_ELEMENT)) {
                    locations.add(getTextValue(e));
                }
            }
        }
    }
    if (!locations.isEmpty()) {
        metadata.addProperty("locations", createList(context, locations));
    }
    boolean result = validatePlaceholder(metadata, context.getComponentDefinitionRegistry());
    return result ? metadata : null;
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ExtendedBeanMetadata(org.apache.aries.blueprint.ExtendedBeanMetadata) ExtendedReferenceListMetadata(org.apache.aries.blueprint.ExtendedReferenceListMetadata)

Example 35 with ComponentDefinitionException

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

the class ExtNamespaceHandler method validatePlaceholder.

private boolean validatePlaceholder(MutableBeanMetadata metadata, ComponentDefinitionRegistry registry) {
    for (String id : registry.getComponentDefinitionNames()) {
        ComponentMetadata component = registry.getComponentDefinition(id);
        if (component instanceof ExtendedBeanMetadata) {
            ExtendedBeanMetadata bean = (ExtendedBeanMetadata) component;
            if (bean.getRuntimeClass() != null && AbstractPropertyPlaceholder.class.isAssignableFrom(bean.getRuntimeClass())) {
                if (arePropertiesEquals(bean, metadata, "placeholderPrefix") && arePropertiesEquals(bean, metadata, "placeholderSuffix")) {
                    if (!arePropertiesEquals(bean, metadata, "systemProperties") || !arePropertiesEquals(bean, metadata, "ignoreMissingLocations")) {
                        throw new ComponentDefinitionException("Multiple incompatible placeholders found");
                    }
                    // Merge both placeholders
                    mergeList(bean, metadata, "locations");
                    mergeMap(bean, metadata, "defaultProperties");
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : ExtendedBeanMetadata(org.apache.aries.blueprint.ExtendedBeanMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) AbstractPropertyPlaceholder(org.apache.aries.blueprint.ext.AbstractPropertyPlaceholder)

Aggregations

ComponentDefinitionException (org.osgi.service.blueprint.container.ComponentDefinitionException)57 Node (org.w3c.dom.Node)23 Element (org.w3c.dom.Element)17 NodeList (org.w3c.dom.NodeList)15 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)14 MutableBeanMetadata (org.apache.aries.blueprint.mutable.MutableBeanMetadata)12 ArrayList (java.util.ArrayList)11 BeanMetadata (org.osgi.service.blueprint.reflect.BeanMetadata)8 JAXBException (javax.xml.bind.JAXBException)7 MutablePassThroughMetadata (org.apache.aries.blueprint.mutable.MutablePassThroughMetadata)7 IdRefMetadataImpl (org.apache.aries.blueprint.reflect.IdRefMetadataImpl)7 RefMetadataImpl (org.apache.aries.blueprint.reflect.RefMetadataImpl)7 ExpressionNode (org.apache.camel.model.ExpressionNode)7 MapMetadata (org.osgi.service.blueprint.reflect.MapMetadata)7 RefMetadata (org.osgi.service.blueprint.reflect.RefMetadata)7 ExtendedBeanMetadata (org.apache.aries.blueprint.ExtendedBeanMetadata)6 CollectionMetadata (org.osgi.service.blueprint.reflect.CollectionMetadata)6 IdRefMetadata (org.osgi.service.blueprint.reflect.IdRefMetadata)6 Metadata (org.osgi.service.blueprint.reflect.Metadata)6 ReferenceMetadata (org.osgi.service.blueprint.reflect.ReferenceMetadata)6