Search in sources :

Example 76 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 77 with ComponentDefinitionException

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

the class BlueprintRepository method destroyInstance.

public void destroyInstance(String name) {
    ExecutionContext oldContext = ExecutionContext.Holder.setContext(this);
    try {
        Map<Recipe, Future<Object>> toDestroy = new LinkedHashMap<Recipe, Future<Object>>();
        doGetInstancesToDestroy(toDestroy, name);
        for (Map.Entry<Recipe, Future<Object>> entry : toDestroy.entrySet()) {
            try {
                Recipe recipe = entry.getKey();
                Future<Object> future = entry.getValue();
                Object instance = future.get();
                if (instance != null) {
                    recipe.destroy(instance);
                }
            } catch (Exception e) {
                throw new ComponentDefinitionException("Error destroying instance", e);
            }
        }
    } finally {
        ExecutionContext.Holder.setContext(oldContext);
    }
}
Also used : ExecutionContext(org.apache.aries.blueprint.di.ExecutionContext) 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) Future(java.util.concurrent.Future) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CircularDependencyException(org.apache.aries.blueprint.di.CircularDependencyException) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) NoSuchComponentException(org.osgi.service.blueprint.container.NoSuchComponentException) ExecutionException(java.util.concurrent.ExecutionException) LinkedHashMap(java.util.LinkedHashMap)

Example 78 with ComponentDefinitionException

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

the class ArrayRecipe method internalCreate.

protected Object internalCreate() throws ComponentDefinitionException {
    ReifiedType type;
    if (this.type instanceof Class) {
        type = new ReifiedType((Class) this.type);
    } else if (this.type instanceof String) {
        type = loadType((String) this.type);
    } else {
        type = new ReifiedType(Object.class);
    }
    // create array instance
    Object array;
    try {
        array = Array.newInstance(type.getRawClass(), list.size());
    } catch (Exception e) {
        throw new ComponentDefinitionException("Error while creating array instance: " + type);
    }
    int index = 0;
    for (Recipe recipe : list) {
        Object value;
        if (recipe != null) {
            try {
                value = convert(recipe.create(), type);
            } catch (Exception e) {
                throw new ComponentDefinitionException("Unable to convert value " + recipe + " to type " + type, e);
            }
        } else {
            value = null;
        }
        Array.set(array, index, value);
        index++;
    }
    return array;
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ReifiedType(org.osgi.service.blueprint.container.ReifiedType) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException)

Example 79 with ComponentDefinitionException

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

the class CollectionRecipe method internalCreate.

protected Object internalCreate() throws ComponentDefinitionException {
    Class type = getCollection(collectionTypeClass);
    if (!ReflectionUtils.hasDefaultConstructor(type)) {
        throw new ComponentDefinitionException("Type does not have a default constructor " + type.getName());
    }
    // create collection instance
    Object o;
    try {
        o = type.newInstance();
    } catch (Exception e) {
        throw new ComponentDefinitionException("Error while creating collection instance: " + type.getName());
    }
    if (!(o instanceof Collection)) {
        throw new ComponentDefinitionException("Specified collection type does not implement the Collection interface: " + type.getName());
    }
    Collection instance = (Collection) o;
    ReifiedType defaultConversionType = loadType(defaultValueType);
    Type conversionType = null;
    for (Recipe recipe : list) {
        Object value;
        if (recipe != null) {
            try {
                conversionType = defaultConversionType.getRawClass();
                if (recipe instanceof ValueRecipe) {
                    conversionType = ((ValueRecipe) recipe).getValueType();
                }
                value = convert(recipe.create(), conversionType);
            } catch (Exception e) {
                throw new ComponentDefinitionException("Unable to convert value " + recipe + " to type " + conversionType, e);
            }
        } else {
            value = null;
        }
        instance.add(value);
    }
    return instance;
}
Also used : ReifiedType(org.osgi.service.blueprint.container.ReifiedType) Type(java.lang.reflect.Type) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ReifiedType(org.osgi.service.blueprint.container.ReifiedType) Collection(java.util.Collection) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException)

Example 80 with ComponentDefinitionException

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

the class ValueRecipe method internalCreate.

@Override
protected Object internalCreate() throws ComponentDefinitionException {
    try {
        Type type = getValueType();
        Object v = null;
        if (value instanceof ExtendedValueMetadata) {
            v = ((ExtendedValueMetadata) value).getValue();
        }
        if (v == null) {
            v = value.getStringValue();
        }
        return convert(v, type);
    } catch (Exception e) {
        throw new ComponentDefinitionException(e);
    }
}
Also used : Type(java.lang.reflect.Type) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ExtendedValueMetadata(org.apache.aries.blueprint.ExtendedValueMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException)

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