org.apache.camel.cdi.CdiCamelExtension Maven / Gradle / Ivy
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.cdi;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BooleanSupplier;
import java.util.function.Function;
import java.util.stream.Stream;
import javax.enterprise.context.Dependent;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Default;
import javax.enterprise.inject.InjectionException;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.AfterDeploymentValidation;
import javax.enterprise.inject.spi.Annotated;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.inject.spi.ProcessBean;
import javax.enterprise.inject.spi.ProcessInjectionTarget;
import javax.enterprise.inject.spi.ProcessObserverMethod;
import javax.enterprise.inject.spi.ProcessProducer;
import javax.enterprise.inject.spi.ProcessProducerField;
import javax.enterprise.inject.spi.ProcessProducerMethod;
import javax.enterprise.inject.spi.configurator.BeanConfigurator;
import javax.inject.Named;
import org.apache.camel.BeanInject;
import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
import org.apache.camel.Component;
import org.apache.camel.Consume;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.Converter;
import org.apache.camel.Endpoint;
import org.apache.camel.EndpointInject;
import org.apache.camel.FluentProducerTemplate;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.PropertyInject;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.ServiceStatus;
import org.apache.camel.TypeConverter;
import org.apache.camel.model.Model;
import org.apache.camel.model.RouteContainer;
import org.apache.camel.model.RouteTemplateContainer;
import org.apache.camel.spi.CamelEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static java.lang.ClassLoader.getSystemClassLoader;
import static java.util.Collections.newSetFromMap;
import static java.util.function.Predicate.isEqual;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toSet;
import static java.util.stream.Stream.concat;
import static org.apache.camel.cdi.ApplicationScopedLiteral.APPLICATION_SCOPED;
import static org.apache.camel.cdi.BeanManagerHelper.getReference;
import static org.apache.camel.cdi.BeanManagerHelper.getReferencesByType;
import static org.apache.camel.cdi.CdiCamelFactory.getQualifierByType;
import static org.apache.camel.cdi.CdiCamelFactory.selectContext;
import static org.apache.camel.cdi.CdiEventEndpoint.eventEndpointUri;
import static org.apache.camel.cdi.CdiSpiHelper.getQualifiers;
import static org.apache.camel.cdi.CdiSpiHelper.getRawType;
import static org.apache.camel.cdi.CdiSpiHelper.hasAnnotation;
import static org.apache.camel.cdi.CdiSpiHelper.hasType;
import static org.apache.camel.cdi.CdiSpiHelper.isAnnotationType;
import static org.apache.camel.cdi.Excluded.EXCLUDED;
import static org.apache.camel.cdi.Startup.Literal.STARTUP;
public class CdiCamelExtension implements Extension {
private final Logger logger = LoggerFactory.getLogger(CdiCamelExtension.class);
private final CdiCamelEnvironment environment = new CdiCamelEnvironment();
private final Set> converters = newSetFromMap(new ConcurrentHashMap<>());
private final Set> camelBeans = newSetFromMap(new ConcurrentHashMap<>());
private final Set> eagerBeans = newSetFromMap(new ConcurrentHashMap<>());
private final Map> cdiEventEndpoints = new ConcurrentHashMap<>();
private final Set> cdiBeans = newSetFromMap(new ConcurrentHashMap<>());
private final Set contextQualifiers = newSetFromMap(new ConcurrentHashMap<>());
private final Map> producerBeans = new ConcurrentHashMap<>();
private final Map> producerQualifiers = new ConcurrentHashMap<>();
private final Set eventQualifiers = newSetFromMap(new ConcurrentHashMap<>());
private final Map, ImportResource> resources = new ConcurrentHashMap<>();
private final CdiCamelConfigurationEvent configuration = new CdiCamelConfigurationEvent();
CdiEventEndpoint> getEventEndpoint(String uri) {
return cdiEventEndpoints.get(uri);
}
Set getObserverEvents() {
return eventQualifiers;
}
Set getContextQualifiers() {
return contextQualifiers;
}
private void processAnnotatedType(@Observes ProcessAnnotatedType> pat) {
if (pat.getAnnotatedType().isAnnotationPresent(Vetoed.class)) {
pat.veto();
}
if (hasAnnotation(pat.getAnnotatedType(), Converter.class)) {
converters.add(pat.getAnnotatedType().getJavaClass());
}
if (hasAnnotation(pat.getAnnotatedType(), BeanInject.class, Consume.class, EndpointInject.class, Produce.class,
PropertyInject.class)) {
camelBeans.add(pat.getAnnotatedType());
}
if (hasAnnotation(pat.getAnnotatedType(), Consume.class)) {
eagerBeans.add(pat.getAnnotatedType());
}
if (hasAnnotation(pat.getAnnotatedType(), ImportResource.class)) {
resources.put(pat.getAnnotatedType(), pat.getAnnotatedType().getAnnotation(ImportResource.class));
}
}
private void camelContextBeans(@Observes ProcessInjectionTarget pit, BeanManager manager) {
pit.setInjectionTarget(
environment.camelContextInjectionTarget(pit.getInjectionTarget(), pit.getAnnotatedType(), manager, this));
}
private void camelContextProducers(@Observes ProcessProducer, T> pp, BeanManager manager) {
pp.setProducer(environment.camelContextProducer(pp.getProducer(), pp.getAnnotatedMember(), manager, this));
}
private void camelBeansPostProcessor(@Observes ProcessInjectionTarget pit, BeanManager manager) {
if (camelBeans.contains(pit.getAnnotatedType())) {
pit.setInjectionTarget(new CamelBeanInjectionTarget<>(pit.getInjectionTarget(), manager));
}
}
private void camelContextAware(@Observes ProcessInjectionTarget pit, BeanManager manager) {
pit.setInjectionTarget(new CamelBeanInjectionTarget<>(pit.getInjectionTarget(), manager));
}
private void endpointBeans(@Observes ProcessProducerMethod ppm) {
producerBeans.put(ppm.getAnnotatedProducerMethod().getJavaMember(), ppm.getBean());
}
private void consumerTemplateBeans(@Observes ProcessProducerMethod ppm) {
producerBeans.put(ppm.getAnnotatedProducerMethod().getJavaMember(), ppm.getBean());
}
private void producerTemplateBeans(@Observes ProcessProducerMethod ppm) {
producerBeans.put(ppm.getAnnotatedProducerMethod().getJavaMember(), ppm.getBean());
}
private void fluentProducerTemplateBeans(@Observes ProcessProducerMethod ppm) {
producerBeans.put(ppm.getAnnotatedProducerMethod().getJavaMember(), ppm.getBean());
}
private void camelFactoryProducers(@Observes ProcessAnnotatedType pat, BeanManager manager) {
pat.setAnnotatedType(
new AnnotatedTypeDelegate<>(
pat.getAnnotatedType(), pat.getAnnotatedType().getMethods().stream()
.filter(am -> am.isAnnotationPresent(Produces.class))
.filter(am -> am.getTypeClosure().stream().noneMatch(isEqual(TypeConverter.class)))
.peek(am -> producerQualifiers.put(am.getJavaMember(), getQualifiers(am, manager)))
.map(am -> new AnnotatedMethodDelegate<>(
am, am.getAnnotations().stream()
.filter(annotation -> !manager.isQualifier(annotation.annotationType()))
.collect(collectingAndThen(toSet(), annotations -> {
annotations.add(EXCLUDED);
return annotations;
}))))
.collect(toSet())));
}
private void camelEventNotifiers(@Observes ProcessObserverMethod pom) {
// Only activate Camel event notifiers for explicit Camel event observers, that is, an observer method for a super type won't activate notifiers.
Type type = pom.getObserverMethod().getObservedType();
// Camel events are raw types
if (type instanceof Class && CamelEvent.class.isAssignableFrom(Class.class.cast(type))) {
Set qualifiers = pom.getObserverMethod().getObservedQualifiers();
if (qualifiers.isEmpty()) {
eventQualifiers.add(Any.Literal.INSTANCE);
} else if (qualifiers.size() == 1 && qualifiers.stream()
.anyMatch(isAnnotationType(Named.class))) {
eventQualifiers.add(Default.Literal.INSTANCE);
} else {
eventQualifiers.addAll(qualifiers);
}
}
}
private void beans(@Observes ProcessProducerField, ?> pb) {
cdiBeans.add(pb.getBean());
}
private void beans(@Observes ProcessProducerMethod, ?> pb) {
cdiBeans.add(pb.getBean());
}
private void beans(@Observes ProcessBean> pb, BeanManager manager) {
cdiBeans.add(pb.getBean());
// Lookup for CDI event endpoint injection points
pb.getBean().getInjectionPoints().stream()
.filter(ip -> CdiEventEndpoint.class.equals(getRawType(ip.getType())))
.forEach(ip -> {
Type type = ip.getType() instanceof ParameterizedType
? ((ParameterizedType) ip.getType()).getActualTypeArguments()[0]
: Object.class;
String uri = eventEndpointUri(type, ip.getQualifiers());
cdiEventEndpoints.put(uri, new CdiEventEndpoint<>(uri, type, ip.getQualifiers(), manager));
});
}
private void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager manager) {
// The set of extra Camel CDI beans
Set> extraBeans = new HashSet<>();
// Camel contexts from the imported Camel XML
concat(cdiBeans.stream(), extraBeans.stream())
.filter(hasType(CamelContext.class))
.map(Bean::getQualifiers)
.forEach(contextQualifiers::addAll);
Set> allBeans = concat(cdiBeans.stream(), extraBeans.stream())
.collect(toSet());
Set> contexts = allBeans.stream()
.filter(hasType(CamelContext.class))
.collect(toSet());
if (contexts.isEmpty() && shouldDeployDefaultCamelContext(allBeans)) {
// Add @Default Camel context bean if any
extraBeans.add(camelContextBean(
manager, null, Any.Literal.INSTANCE, Default.Literal.INSTANCE, APPLICATION_SCOPED));
} else if (contexts.size() == 1) {
// Add the @Default qualifier if there is only one Camel context bean
Bean> context = contexts.iterator().next();
if (!context.getQualifiers().contains(Default.Literal.INSTANCE)) {
// Only decorate if that's a programmatic bean
if (context instanceof SyntheticBean) {
((SyntheticBean>) context).addQualifier(Default.Literal.INSTANCE);
}
}
}
// Finally add the beans to the deployment
extraBeans.forEach(abd::addBean);
// Update the CDI Camel factory beans
Set endpointQualifiers = cdiEventEndpoints.values().stream()
.map(CdiEventEndpoint::getQualifiers)
.flatMap(Set::stream)
.collect(toSet());
Set templateQualifiers = contextQualifiers.stream()
.filter(isAnnotationType(Default.class).or(isAnnotationType(Named.class)).negate())
.collect(toSet());
// TODO: would be more correct to add a bean for each Camel context bean
producerBeans.entrySet().stream()
.map(producer -> new BeanDelegate<>(
producer.getValue(),
producerQualifiers.get(producer.getKey()),
CdiEventEndpoint.class.equals(producer.getKey().getReturnType())
? endpointQualifiers
: templateQualifiers))
.forEach(abd::addBean);
// optional mock support if camel-mock is there
try {
var loader = Thread.currentThread().getContextClassLoader();
if (loader == null) {
loader = getClass().getClassLoader();
if (loader == null) {
loader = getSystemClassLoader();
}
}
final Class extends Endpoint> endpointType = loader
.loadClass("org.apache.camel.component.mock.MockEndpoint")
.asSubclass(Endpoint.class);
addCamelMockBeans(abd, endpointType, templateQualifiers);
} catch (final ClassNotFoundException | NoClassDefFoundError e) {
// not needed
}
// Add CDI event endpoint observer methods
cdiEventEndpoints.values().stream()
.map(ForwardingObserverMethod::new)
.forEach(abd::addObserverMethod);
}
private void addCamelMockBeans(
final AfterBeanDiscovery afterBeanDiscovery, final Class type,
final Set templateQualifiers) {
addBean(afterBeanDiscovery, type)
.id(getClass().getName() + "#mockEndpoint")
.qualifiers(Default.Literal.INSTANCE, Uri.Literal.of(""), Any.Literal.INSTANCE)
.addQualifiers(templateQualifiers)
.produceWith(instance -> newEndpoint(type, instance, ip -> getQualifierByType(ip, Uri.class)
.map(Uri::value)
.orElseGet(() -> "mock:" + ip.getMember().getName())));
}
private BeanConfigurator addBean(
final AfterBeanDiscovery afterBeanDiscovery, final Class type) {
return afterBeanDiscovery
. addBean()
.scope(Dependent.class)
.beanClass(type)
.types(type, Object.class);
}
private T newEndpoint(
final Class type,
final Instance