Search in sources :

Example 1 with CreationException

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

the class SyntheticBean method create.

@Override
public T create(CreationalContext<T> creationalContext) {
    try {
        T instance = target.produce(creationalContext);
        target.inject(instance, creationalContext);
        target.postConstruct(instance);
        return instance;
    } catch (Exception cause) {
        throw new CreationException("Error while instantiating " + this, cause);
    }
}
Also used : CreationException(javax.enterprise.inject.CreationException) CreationException(javax.enterprise.inject.CreationException) InjectionException(javax.enterprise.inject.InjectionException)

Example 2 with CreationException

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

the class XmlCdiBeanFactory method camelContextBean.

private SyntheticBean<?> camelContextBean(CamelContextFactoryBean factory, URL url) {
    Set<Annotation> annotations = new HashSet<>();
    annotations.add(ANY);
    if (hasId(factory)) {
        addAll(annotations, ContextName.Literal.of(factory.getId()), NamedLiteral.of(factory.getId()));
    } else {
        annotations.add(DEFAULT);
        factory.setImplicitId(true);
        factory.setId(new CdiCamelContextNameStrategy().getNextName());
    }
    annotations.add(APPLICATION_SCOPED);
    SyntheticAnnotated annotated = new SyntheticAnnotated(DefaultCamelContext.class, manager.createAnnotatedType(DefaultCamelContext.class).getTypeClosure(), annotations);
    return new SyntheticBean<>(manager, annotated, DefaultCamelContext.class, environment.camelContextInjectionTarget(new SyntheticInjectionTarget<>(() -> {
        DefaultCamelContext context = new DefaultCamelContext();
        factory.setContext(context);
        factory.setBeanManager(manager);
        return context;
    }, context -> {
        try {
            factory.afterPropertiesSet();
        } catch (Exception cause) {
            throw new CreationException(cause);
        }
    }), annotated, manager, extension), bean -> "imported Camel context with " + (factory.isImplicitId() ? "implicit " : "") + "id [" + factory.getId() + "] " + "from resource [" + url + "] " + "with qualifiers " + bean.getQualifiers());
}
Also used : CreationException(javax.enterprise.inject.CreationException) Annotation(java.lang.annotation.Annotation) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) IOException(java.io.IOException) CreationException(javax.enterprise.inject.CreationException) JAXBException(javax.xml.bind.JAXBException) HashSet(java.util.HashSet)

Example 3 with CreationException

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

the class XmlServiceExporterBean method create.

@Override
public T create(CreationalContext<T> creationalContext) {
    try {
        CamelContext context = isNotEmpty(exporter.getCamelContextId()) ? getReferenceByName(manager, exporter.getCamelContextId(), CamelContext.class).get() : getReference(manager, CamelContext.class, this.context);
        Bean<?> bean = manager.resolve(manager.getBeans(exporter.getServiceRef()));
        if (bean == null) {
            throw new UnsatisfiedResolutionException("No bean with name [" + exporter.getServiceRef() + "] is deployed!");
        }
        @SuppressWarnings("unchecked") T service = (T) manager.getReference(bean, type, manager.createCreationalContext(bean));
        Endpoint endpoint = getMandatoryEndpoint(context, exporter.getUri());
        try {
            // need to start endpoint before we create consumer
            startService(endpoint);
            Consumer consumer = endpoint.createConsumer(new BeanProcessor(service, context));
            // add and start consumer
            context.addService(consumer, true, true);
        } catch (Exception cause) {
            throw new FailedToCreateConsumerException(endpoint, cause);
        }
        return service;
    } catch (Exception cause) {
        throw new CreationException("Error while creating instance for " + this, cause);
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) FailedToCreateConsumerException(org.apache.camel.FailedToCreateConsumerException) CamelContextHelper.getMandatoryEndpoint(org.apache.camel.util.CamelContextHelper.getMandatoryEndpoint) Endpoint(org.apache.camel.Endpoint) Consumer(org.apache.camel.Consumer) BeanProcessor(org.apache.camel.component.bean.BeanProcessor) UnsatisfiedResolutionException(javax.enterprise.inject.UnsatisfiedResolutionException) CreationException(javax.enterprise.inject.CreationException) FailedToCreateConsumerException(org.apache.camel.FailedToCreateConsumerException) UnsatisfiedResolutionException(javax.enterprise.inject.UnsatisfiedResolutionException) CreationException(javax.enterprise.inject.CreationException)

Example 4 with CreationException

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

the class XmlProxyFactoryBean method create.

@Override
public T create(CreationalContext<T> creationalContext) {
    try {
        CamelContext context = isNotEmpty(proxy.getCamelContextId()) ? getReferenceByName(manager, proxy.getCamelContextId(), CamelContext.class).get() : getReference(manager, CamelContext.class, this.context);
        Endpoint endpoint;
        if (isNotEmpty(proxy.getServiceRef())) {
            endpoint = context.getRegistry().lookupByNameAndType(proxy.getServiceRef(), Endpoint.class);
        } else {
            if (isNotEmpty(proxy.getServiceUrl())) {
                endpoint = context.getEndpoint(proxy.getServiceUrl());
            } else {
                throw new IllegalStateException("serviceUrl or serviceRef must not be empty!");
            }
        }
        if (endpoint == null) {
            throw new UnsatisfiedResolutionException("Could not resolve endpoint: " + (isNotEmpty(proxy.getServiceRef()) ? proxy.getServiceRef() : proxy.getServiceUrl()));
        }
        // binding is enabled by default
        boolean bind = proxy.getBinding() != null ? proxy.getBinding() : true;
        try {
            // Start the endpoint before we create the producer
            startService(endpoint);
            Producer producer = endpoint.createProducer();
            // Add and start the producer
            context.addService(producer, true, true);
            return createProxy(endpoint, bind, producer, (Class<T>) proxy.getServiceInterface());
        } catch (Exception cause) {
            throw new FailedToCreateProducerException(endpoint, cause);
        }
    } catch (Exception cause) {
        throw new CreationException("Error while creating instance for " + this, cause);
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException) Endpoint(org.apache.camel.Endpoint) Producer(org.apache.camel.Producer) UnsatisfiedResolutionException(javax.enterprise.inject.UnsatisfiedResolutionException) CreationException(javax.enterprise.inject.CreationException) UnsatisfiedResolutionException(javax.enterprise.inject.UnsatisfiedResolutionException) CreationException(javax.enterprise.inject.CreationException) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException)

Aggregations

CreationException (javax.enterprise.inject.CreationException)4 UnsatisfiedResolutionException (javax.enterprise.inject.UnsatisfiedResolutionException)2 CamelContext (org.apache.camel.CamelContext)2 Endpoint (org.apache.camel.Endpoint)2 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1 HashSet (java.util.HashSet)1 InjectionException (javax.enterprise.inject.InjectionException)1 JAXBException (javax.xml.bind.JAXBException)1 Consumer (org.apache.camel.Consumer)1 FailedToCreateConsumerException (org.apache.camel.FailedToCreateConsumerException)1 FailedToCreateProducerException (org.apache.camel.FailedToCreateProducerException)1 Producer (org.apache.camel.Producer)1 BeanProcessor (org.apache.camel.component.bean.BeanProcessor)1 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)1 CamelContextHelper.getMandatoryEndpoint (org.apache.camel.util.CamelContextHelper.getMandatoryEndpoint)1