Search in sources :

Example 26 with ComponentDefinitionException

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

the class CamelNamespaceHandler method parseRouteContextNode.

private Metadata parseRouteContextNode(Element element, ParserContext context) {
    LOG.trace("Parsing RouteContext {}", element);
    // now parse the routes with JAXB
    Binder<Node> binder;
    try {
        binder = getJaxbContext().createBinder();
    } catch (JAXBException e) {
        throw new ComponentDefinitionException("Failed to create the JAXB binder : " + e, e);
    }
    Object value = parseUsingJaxb(element, context, binder);
    if (!(value instanceof CamelRouteContextFactoryBean)) {
        throw new ComponentDefinitionException("Expected an instance of " + CamelRouteContextFactoryBean.class);
    }
    CamelRouteContextFactoryBean rcfb = (CamelRouteContextFactoryBean) value;
    String id = rcfb.getId();
    MutablePassThroughMetadata factory = context.createMetadata(MutablePassThroughMetadata.class);
    factory.setId(".camelBlueprint.passThrough." + id);
    factory.setObject(new PassThroughCallable<Object>(rcfb));
    MutableBeanMetadata factory2 = context.createMetadata(MutableBeanMetadata.class);
    factory2.setId(".camelBlueprint.factory." + id);
    factory2.setFactoryComponent(factory);
    factory2.setFactoryMethod("call");
    MutableBeanMetadata ctx = context.createMetadata(MutableBeanMetadata.class);
    ctx.setId(id);
    ctx.setRuntimeClass(List.class);
    ctx.setFactoryComponent(factory2);
    ctx.setFactoryMethod("getRoutes");
    // must be lazy as we want CamelContext to be activated first
    ctx.setActivation(ACTIVATION_LAZY);
    // lets inject the namespaces into any namespace aware POJOs
    injectNamespaces(element, binder);
    LOG.trace("Parsing RouteContext done, returning {}", element, ctx);
    return ctx;
}
Also used : MutablePassThroughMetadata(org.apache.aries.blueprint.mutable.MutablePassThroughMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) CamelRouteContextFactoryBean(org.apache.camel.blueprint.CamelRouteContextFactoryBean) ExpressionNode(org.apache.camel.model.ExpressionNode) Node(org.w3c.dom.Node) JAXBException(javax.xml.bind.JAXBException)

Example 27 with ComponentDefinitionException

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

the class ReferenceListRecipe method internalCreate.

@Override
protected Object internalCreate() throws ComponentDefinitionException {
    try {
        if (explicitDependencies != null) {
            for (Recipe recipe : explicitDependencies) {
                recipe.create();
            }
        }
        ProvidedObject object = new ProvidedObject();
        addPartialObject(object);
        // Handle initial references
        createListeners();
        updateListeners();
        return object;
    } catch (ComponentDefinitionException t) {
        throw t;
    } 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)

Example 28 with ComponentDefinitionException

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

the class ServiceRecipe method createService.

private void createService() {
    try {
        if (service == null) {
            LOGGER.debug("Creating service instance");
            //We can't use the BlueprintRepository because we don't know what interfaces
            //to use yet! We have to be a bit smarter.
            ExecutionContext old = ExecutionContext.Holder.setContext(blueprintContainer.getRepository());
            try {
                Object o = serviceRecipe.create();
                if (o instanceof Convertible) {
                    o = blueprintContainer.getRepository().convert(o, new ReifiedType(Object.class));
                    validateClasses(o);
                } else if (o instanceof UnwrapperedBeanHolder) {
                    UnwrapperedBeanHolder holder = (UnwrapperedBeanHolder) o;
                    if (holder.unwrapperedBean instanceof ServiceFactory) {
                        //If a service factory is used, make sure the proxy classes implement this
                        //interface so that later on, internalGetService will create the real
                        //service from it.
                        LOGGER.debug("{} implements ServiceFactory, creating proxy that also implements this", holder.unwrapperedBean);
                        Collection<Class<?>> cls = getClassesForProxying(holder.unwrapperedBean);
                        cls.add(blueprintContainer.loadClass("org.osgi.framework.ServiceFactory"));
                        o = BeanRecipe.wrap(holder, cls);
                    } else {
                        validateClasses(holder.unwrapperedBean);
                        o = BeanRecipe.wrap(holder, getClassesForProxying(holder.unwrapperedBean));
                    }
                } else if (!(o instanceof ServiceFactory)) {
                    validateClasses(o);
                }
                service = o;
            } catch (Exception e) {
                LOGGER.error("Error retrieving service from " + this, e);
                throw new ComponentDefinitionException(e);
            } finally {
                ExecutionContext.Holder.setContext(old);
            }
            LOGGER.debug("Service created: {}", service);
        }
        // When the service is first requested, we need to create listeners and call them
        if (!initialServiceRegistration && listeners == null) {
            LOGGER.debug("Creating listeners");
            if (listenersRecipe != null) {
                listeners = (List) createRecipe(listenersRecipe);
            } else {
                listeners = Collections.emptyList();
            }
            LOGGER.debug("Listeners created: {}", listeners);
            if (registration.get() != null) {
                LOGGER.debug("Calling listeners for initial service registration");
                for (ServiceListener listener : listeners) {
                    listener.register(service, registrationProperties);
                }
            } else {
                LOGGER.debug("Calling listeners for initial service unregistration");
                for (ServiceListener listener : listeners) {
                    listener.unregister(service, registrationProperties);
                }
            }
        }
    } catch (RuntimeException e) {
        LOGGER.error("Error retrieving service from " + this, e);
        throw e;
    }
}
Also used : ExecutionContext(org.apache.aries.blueprint.di.ExecutionContext) Convertible(org.apache.aries.blueprint.container.AggregateConverter.Convertible) ServiceListener(org.apache.aries.blueprint.utils.ServiceListener) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ServiceFactory(org.osgi.framework.ServiceFactory) ReifiedType(org.osgi.service.blueprint.container.ReifiedType) Collection(java.util.Collection) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) UnwrapperedBeanHolder(org.apache.aries.blueprint.container.BeanRecipe.UnwrapperedBeanHolder)

Example 29 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 30 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)

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