Search in sources :

Example 6 with ComponentDefinitionException

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

the class ReferenceRecipe method internalCreate.

@Override
protected Object internalCreate() throws ComponentDefinitionException {
    try {
        if (explicitDependencies != null) {
            for (Recipe recipe : explicitDependencies) {
                recipe.create();
            }
        }
        // Create the proxy
        Set<Class<?>> interfaces = new HashSet<Class<?>>();
        Class<?> clz = getInterfaceClass();
        if (clz != null)
            interfaces.add(clz);
        if (metadata instanceof ExtendedReferenceMetadata) {
            interfaces.addAll(loadAllClasses(((ExtendedReferenceMetadata) metadata).getExtraInterfaces()));
        }
        proxy = createProxy(new ServiceDispatcher(), interfaces);
        // Add partially created proxy to the context
        ServiceProxyWrapper wrapper = new ServiceProxyWrapper();
        addPartialObject(wrapper);
        // Handle initial references
        createListeners();
        updateListeners();
        // Return a ServiceProxy that can injection of references or proxies can be done correctly
        return wrapper;
    } catch (ComponentDefinitionException e) {
        throw e;
    } catch (Throwable t) {
        throw new ComponentDefinitionException(t);
    }
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) CollectionRecipe(org.apache.aries.blueprint.di.CollectionRecipe) ValueRecipe(org.apache.aries.blueprint.di.ValueRecipe) Recipe(org.apache.aries.blueprint.di.Recipe) ExtendedReferenceMetadata(org.apache.aries.blueprint.ExtendedReferenceMetadata) HashSet(java.util.HashSet)

Example 7 with ComponentDefinitionException

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

the class ExtNamespaceHandler method decorateAdditionalInterfaces.

private ComponentMetadata decorateAdditionalInterfaces(Node node, ComponentMetadata component, ParserContext context) {
    if (!(component instanceof MutableReferenceMetadata)) {
        throw new ComponentDefinitionException("Expected an instanceof MutableReferenceMetadata");
    }
    MutableReferenceMetadata mrm = (MutableReferenceMetadata) component;
    List<String> list = new ArrayList<String>();
    Node nd = node.getFirstChild();
    while (nd != null) {
        if (nd instanceof Element && nodeNameEquals(nd, INTERFACE_VALUE)) {
            list.add(((Element) nd).getTextContent());
        }
        nd = nd.getNextSibling();
    }
    mrm.setExtraInterfaces(list);
    return component;
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) MutableReferenceMetadata(org.apache.aries.blueprint.mutable.MutableReferenceMetadata)

Example 8 with ComponentDefinitionException

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

the class PlaceholdersUtils method validatePlaceholder.

public static void validatePlaceholder(MutableBeanMetadata metadata, ComponentDefinitionRegistry registry) {
    String prefix = getPlaceholderProperty(metadata, "placeholderPrefix");
    String suffix = getPlaceholderProperty(metadata, "placeholderSuffix");
    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())) {
                String otherPrefix = getPlaceholderProperty(bean, "placeholderPrefix");
                String otherSuffix = getPlaceholderProperty(bean, "placeholderSuffix");
                if (prefix.equals(otherPrefix) && suffix.equals(otherSuffix)) {
                    throw new ComponentDefinitionException("Multiple placeholders with the same prefix and suffix are not allowed");
                }
            }
        }
    }
}
Also used : ExtendedBeanMetadata(org.apache.aries.blueprint.ExtendedBeanMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) AbstractPropertyPlaceholder(org.apache.aries.blueprint.ext.AbstractPropertyPlaceholder) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata)

Example 9 with ComponentDefinitionException

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

the class MapRecipe method internalCreate.

protected Object internalCreate() throws ComponentDefinitionException {
    Class<?> mapType = getMap(typeClass);
    if (!ReflectionUtils.hasDefaultConstructor(mapType)) {
        throw new ComponentDefinitionException("Type does not have a default constructor " + mapType.getName());
    }
    Object o;
    try {
        o = mapType.newInstance();
    } catch (Exception e) {
        throw new ComponentDefinitionException("Error while creating set instance: " + mapType.getName());
    }
    Map instance;
    if (o instanceof Map) {
        instance = (Map) o;
    } else if (o instanceof Dictionary) {
        instance = new DummyDictionaryAsMap((Dictionary) o);
    } else {
        throw new ComponentDefinitionException("Specified map type does not implement the Map interface: " + mapType.getName());
    }
    ReifiedType defaultConvertKeyType = getType(keyType);
    ReifiedType defaultConvertValueType = getType(valueType);
    // add map entries
    try {
        for (Recipe[] entry : entries) {
            ReifiedType convertKeyType = workOutConversionType(entry[0], defaultConvertKeyType);
            Object key = convert(entry[0].create(), convertKeyType);
            // Each entry may have its own types
            ReifiedType convertValueType = workOutConversionType(entry[1], defaultConvertValueType);
            Object value = entry[1] != null ? convert(entry[1].create(), convertValueType) : null;
            instance.put(key, value);
        }
    } catch (Exception e) {
        throw new ComponentDefinitionException(e);
    }
    return instance;
}
Also used : Dictionary(java.util.Dictionary) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ReifiedType(org.osgi.service.blueprint.container.ReifiedType) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) AbstractMap(java.util.AbstractMap) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException)

Example 10 with ComponentDefinitionException

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

the class Parser method parseBeanMetadata.

private ComponentMetadata parseBeanMetadata(Element element, boolean topElement) {
    BeanMetadataImpl metadata = new BeanMetadataImpl();
    if (topElement) {
        metadata.setId(getId(element));
        if (element.hasAttribute(SCOPE_ATTRIBUTE)) {
            metadata.setScope(getScope(element.getAttributeNode(SCOPE_ATTRIBUTE)));
            if (!metadata.getScope().equals(BeanMetadata.SCOPE_SINGLETON)) {
                if (element.hasAttribute(ACTIVATION_ATTRIBUTE)) {
                    if (element.getAttribute(ACTIVATION_ATTRIBUTE).equals(ACTIVATION_EAGER)) {
                        throw new ComponentDefinitionException("A <bean> with a prototype or custom scope can not have an eager activation");
                    }
                }
                metadata.setActivation(ComponentMetadata.ACTIVATION_LAZY);
            } else {
                metadata.setActivation(parseActivation(element));
            }
        } else {
            metadata.setActivation(parseActivation(element));
        }
    } else {
        metadata.setActivation(ComponentMetadata.ACTIVATION_LAZY);
    }
    if (element.hasAttribute(CLASS_ATTRIBUTE)) {
        metadata.setClassName(element.getAttribute(CLASS_ATTRIBUTE));
    }
    if (element.hasAttribute(DEPENDS_ON_ATTRIBUTE)) {
        metadata.setDependsOn(parseList(element.getAttribute(DEPENDS_ON_ATTRIBUTE)));
    }
    if (element.hasAttribute(INIT_METHOD_ATTRIBUTE)) {
        metadata.setInitMethod(element.getAttribute(INIT_METHOD_ATTRIBUTE));
    }
    if (element.hasAttribute(DESTROY_METHOD_ATTRIBUTE)) {
        metadata.setDestroyMethod(element.getAttribute(DESTROY_METHOD_ATTRIBUTE));
    }
    if (element.hasAttribute(FACTORY_REF_ATTRIBUTE)) {
        metadata.setFactoryComponent(new RefMetadataImpl(element.getAttribute(FACTORY_REF_ATTRIBUTE)));
    }
    if (element.hasAttribute(FACTORY_METHOD_ATTRIBUTE)) {
        String factoryMethod = element.getAttribute(FACTORY_METHOD_ATTRIBUTE);
        metadata.setFactoryMethod(factoryMethod);
    }
    // Do some validation
    if (metadata.getClassName() == null && metadata.getFactoryComponent() == null) {
        throw new ComponentDefinitionException("Bean class or factory-ref must be specified");
    }
    if (metadata.getFactoryComponent() != null && metadata.getFactoryMethod() == null) {
        throw new ComponentDefinitionException("factory-method is required when factory-component is set");
    }
    if (MetadataUtil.isPrototypeScope(metadata) && metadata.getDestroyMethod() != null) {
        throw new ComponentDefinitionException("destroy-method must not be set for a <bean> with a prototype scope");
    }
    // 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(node.getNamespaceURI())) {
                if (nodeNameEquals(node, ARGUMENT_ELEMENT)) {
                    metadata.addArgument(parseBeanArgument(metadata, e));
                } else if (nodeNameEquals(node, PROPERTY_ELEMENT)) {
                    metadata.addProperty(parseBeanProperty(metadata, e));
                }
            }
        }
    }
    MetadataUtil.validateBeanArguments(metadata.getArguments());
    ComponentMetadata m = metadata;
    // Parse custom scopes
    m = handleCustomScope(element.getAttributeNode(SCOPE_ATTRIBUTE), element, m);
    // Parse custom attributes
    m = handleCustomAttributes(element.getAttributes(), m);
    // Parse custom elements;
    m = handleCustomElements(element, m);
    return m;
}
Also used : BeanMetadataImpl(org.apache.aries.blueprint.reflect.BeanMetadataImpl) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) RefMetadataImpl(org.apache.aries.blueprint.reflect.RefMetadataImpl) IdRefMetadataImpl(org.apache.aries.blueprint.reflect.IdRefMetadataImpl) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata)

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