Search in sources :

Example 1 with InjectionException

use of javax.enterprise.inject.InjectionException in project camel by apache.

the class CamelContextOsgiProducer method produce.

@Override
public T produce(CreationalContext<T> ctx) {
    T context = super.produce(ctx);
    // Register the context in the OSGi registry
    BundleContext bundle = BundleContextUtils.getBundleContext(getClass());
    context.getManagementStrategy().addEventNotifier(new OsgiCamelContextPublisher(bundle));
    if (!(context instanceof DefaultCamelContext)) {
        // Fail fast for the time being to avoid side effects by some methods get declared on the CamelContext interface
        throw new InjectionException("Camel CDI requires Camel context [" + context.getName() + "] to be a subtype of DefaultCamelContext");
    }
    DefaultCamelContext adapted = context.adapt(DefaultCamelContext.class);
    adapted.setRegistry(OsgiCamelContextHelper.wrapRegistry(context, context.getRegistry(), bundle));
    CamelContextNameStrategy strategy = context.getNameStrategy();
    OsgiCamelContextHelper.osgiUpdate(adapted, bundle);
    // FIXME: the above call should not override explicit strategies provided by the end user or should decorate them instead of overriding them completely
    if (!(strategy instanceof DefaultCamelContextNameStrategy)) {
        context.setNameStrategy(strategy);
    }
    return context;
}
Also used : InjectionException(javax.enterprise.inject.InjectionException) DefaultCamelContextNameStrategy(org.apache.camel.impl.DefaultCamelContextNameStrategy) OsgiCamelContextPublisher(org.apache.camel.core.osgi.OsgiCamelContextPublisher) CamelContextNameStrategy(org.apache.camel.spi.CamelContextNameStrategy) DefaultCamelContextNameStrategy(org.apache.camel.impl.DefaultCamelContextNameStrategy) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) BundleContext(org.osgi.framework.BundleContext)

Example 2 with InjectionException

use of javax.enterprise.inject.InjectionException in project camel by apache.

the class CamelContextProducer method produce.

@Override
public T produce(CreationalContext<T> ctx) {
    T context = super.produce(ctx);
    // Do not override the name if it's been already set (in the bean constructor for example)
    if (context.getNameStrategy() instanceof DefaultCamelContextNameStrategy) {
        context.setNameStrategy(nameStrategy(annotated));
    }
    // Add bean registry and Camel injector
    if (context instanceof DefaultCamelContext) {
        DefaultCamelContext adapted = context.adapt(DefaultCamelContext.class);
        adapted.setRegistry(new CdiCamelRegistry(manager));
        adapted.setInjector(new CdiCamelInjector(context.getInjector(), manager));
    } else {
        // Fail fast for the time being to avoid side effects by the time these two methods get declared on the CamelContext interface
        throw new InjectionException("Camel CDI requires Camel context [" + context.getName() + "] to be a subtype of DefaultCamelContext");
    }
    // Add event notifier if at least one observer is present
    Set<Annotation> qualifiers = annotated.getAnnotations().stream().filter(isAnnotationType(Named.class).negate().and(q -> manager.isQualifier(q.annotationType()))).collect(toSet());
    qualifiers.add(ANY);
    if (qualifiers.size() == 1) {
        qualifiers.add(DEFAULT);
    }
    qualifiers.retainAll(extension.getObserverEvents());
    if (!qualifiers.isEmpty()) {
        context.getManagementStrategy().addEventNotifier(new CdiEventNotifier(manager, qualifiers));
    }
    return context;
}
Also used : InjectionException(javax.enterprise.inject.InjectionException) DefaultCamelContextNameStrategy(org.apache.camel.impl.DefaultCamelContextNameStrategy) DEFAULT(org.apache.camel.cdi.DefaultLiteral.DEFAULT) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Annotation(java.lang.annotation.Annotation)

Example 3 with InjectionException

use of javax.enterprise.inject.InjectionException in project camel by apache.

the class CdiCamelFactory method producerTemplateFromUri.

private static ProducerTemplate producerTemplateFromUri(InjectionPoint ip, @Any Instance<CamelContext> instance, CdiCamelExtension extension, Uri uri) {
    try {
        CamelContext context = uri.context().isEmpty() ? selectContext(ip, instance, extension) : selectContext(uri.context(), instance);
        ProducerTemplate producerTemplate = context.createProducerTemplate();
        Endpoint endpoint = context.getEndpoint(uri.value(), Endpoint.class);
        producerTemplate.setDefaultEndpoint(endpoint);
        return producerTemplate;
    } catch (Exception cause) {
        throw new InjectionException("Error injecting producer template annotated with " + uri + " into " + ip, cause);
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) InjectionException(javax.enterprise.inject.InjectionException) FluentProducerTemplate(org.apache.camel.FluentProducerTemplate) ProducerTemplate(org.apache.camel.ProducerTemplate) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) UnsatisfiedResolutionException(javax.enterprise.inject.UnsatisfiedResolutionException) InjectionException(javax.enterprise.inject.InjectionException)

Example 4 with InjectionException

use of javax.enterprise.inject.InjectionException in project camel by apache.

the class CdiCamelFactory method fluentProducerTemplateFromUri.

private static FluentProducerTemplate fluentProducerTemplateFromUri(InjectionPoint ip, @Any Instance<CamelContext> instance, CdiCamelExtension extension, Uri uri) {
    try {
        CamelContext context = uri.context().isEmpty() ? selectContext(ip, instance, extension) : selectContext(uri.context(), instance);
        FluentProducerTemplate producerTemplate = context.createFluentProducerTemplate();
        Endpoint endpoint = context.getEndpoint(uri.value(), Endpoint.class);
        producerTemplate.setDefaultEndpoint(endpoint);
        return producerTemplate;
    } catch (Exception cause) {
        throw new InjectionException("Error injecting fluent producer template annotated with " + uri + " into " + ip, cause);
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) InjectionException(javax.enterprise.inject.InjectionException) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) FluentProducerTemplate(org.apache.camel.FluentProducerTemplate) UnsatisfiedResolutionException(javax.enterprise.inject.UnsatisfiedResolutionException) InjectionException(javax.enterprise.inject.InjectionException)

Example 5 with InjectionException

use of javax.enterprise.inject.InjectionException in project camel by apache.

the class SyntheticBean method destroy.

@Override
public void destroy(T instance, CreationalContext<T> creationalContext) {
    try {
        target.preDestroy(instance);
        target.dispose(instance);
    } catch (Exception cause) {
        throw new InjectionException("Error while destroying " + this, cause);
    } finally {
        creationalContext.release();
    }
}
Also used : InjectionException(javax.enterprise.inject.InjectionException) CreationException(javax.enterprise.inject.CreationException) InjectionException(javax.enterprise.inject.InjectionException)

Aggregations

InjectionException (javax.enterprise.inject.InjectionException)12 UnsatisfiedResolutionException (javax.enterprise.inject.UnsatisfiedResolutionException)3 ArrayList (java.util.ArrayList)2 Bean (javax.enterprise.inject.spi.Bean)2 CamelContext (org.apache.camel.CamelContext)2 Endpoint (org.apache.camel.Endpoint)2 FluentProducerTemplate (org.apache.camel.FluentProducerTemplate)2 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)2 DefaultCamelContextNameStrategy (org.apache.camel.impl.DefaultCamelContextNameStrategy)2 BeanManagerImpl (org.apache.webbeans.container.BeanManagerImpl)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Annotation (java.lang.annotation.Annotation)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Level (java.util.logging.Level)1 CreationException (javax.enterprise.inject.CreationException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 MediaType (javax.ws.rs.core.MediaType)1