/** * Generates a unique signature for {@link Bean}. */ static String createBeanId(Bean<?> bean) { return Stream.of(bean.getName(), bean.getScope().getName(), createAnnotationCollectionId(bean.getQualifiers()), createTypeCollectionId(bean.getTypes())) .filter(Objects::nonNull) .collect(joining(",")); }
@Override public String getId() { return new StringJoiner("%") .add("CAMEL-CDI") .add(getClass().getSimpleName()) .add(type.getName()) .add(createBeanId(this)) .toString(); } }
/** * Generates a unique signature for a {@link Type}. */ private static String createTypeId(Type type) { if (type instanceof Class<?>) { return Class.class.cast(type).getName(); } if (type instanceof ParameterizedType) { return createTypeId(((ParameterizedType) type).getRawType()) + Stream.of(((ParameterizedType) type).getActualTypeArguments()) .map(CdiSpiHelper::createTypeId) .collect(joining(",", "<", ">")); } if (type instanceof TypeVariable<?>) { return TypeVariable.class.cast(type).getName(); } if (type instanceof GenericArrayType) { return createTypeId(GenericArrayType.class.cast(type).getGenericComponentType()); } throw new UnsupportedOperationException("Unable to create type id for type [" + type + "]"); }
static Class<?> getRawType(Type type) { if (type instanceof Class<?>) { return Class.class.cast(type); } else if (type instanceof ParameterizedType) { return getRawType(ParameterizedType.class.cast(type).getRawType()); } else if (type instanceof TypeVariable<?>) { return getBound(TypeVariable.class.cast(type).getBounds()); } else if (type instanceof WildcardType) { return getBound(WildcardType.class.cast(type).getUpperBounds()); } else if (type instanceof GenericArrayType) { Class<?> rawType = getRawType(GenericArrayType.class.cast(type).getGenericComponentType()); if (rawType != null) { return Array.newInstance(rawType, 0).getClass(); } } throw new UnsupportedOperationException("Unable to retrieve raw type for [" + type + "]"); }
.filter(hasType(CamelContextAware.class).or(hasType(Component.class)) .or(hasType(RouteContainer.class).or(hasType(RoutesBuilder.class)))) .map(Bean::getQualifiers) .flatMap(Set::stream) .map(Annotated::getAnnotations) .flatMap(Set::stream) .anyMatch(isAnnotationType(Consume.class).and(a -> ((Consume) a).context().isEmpty()) .or(isAnnotationType(BeanInject.class).and(a -> ((BeanInject) a).context().isEmpty())) .or(isAnnotationType(EndpointInject.class).and(a -> ((EndpointInject) a).context().isEmpty())) .or(isAnnotationType(Produce.class).and(a -> ((Produce) a).context().isEmpty())) .or(isAnnotationType(PropertyInject.class).and(a -> ((PropertyInject) a).context().isEmpty()))) .map(Bean::getInjectionPoints) .flatMap(Set::stream) .filter(ip -> getRawType(ip.getType()).getName().startsWith("org.apache.camel")) .map(InjectionPoint::getQualifiers) .flatMap(Set::stream) .anyMatch(isAnnotationType(Uri.class).or(isAnnotationType(Mock.class)).or(isEqual(DEFAULT)));
@Override public T produce(CreationalContext<T> ctx) { T context = createCamelContextWithTCCL(() -> super.produce(ctx), annotated); .filter(isAnnotationType(Named.class).negate() .and(q -> manager.isQualifier(q.annotationType()))) .collect(toSet());
private static Class<?> getBound(Type[] bounds) { if (bounds.length == 0) { return Object.class; } else { return getRawType(bounds[0]); } }
@SafeVarargs static boolean hasAnnotation(AnnotatedType<?> type, Class<? extends Annotation>... annotations) { return Stream.of(annotations) .anyMatch(annotation -> hasAnnotation(type, annotation)); }
/** * Wraps creation of a {@link CamelContext} with the current thread context ClassLoader * set with the ClassLoader associated to the {@link Annotated} java class. */ static <T extends CamelContext> T createCamelContextWithTCCL(Supplier<T> supplier, Annotated annotated) { ClassLoader oldTccl = Thread.currentThread().getContextClassLoader(); try { ClassLoader classLoader = getClassLoader(annotated); Thread.currentThread().setContextClassLoader(classLoader); return supplier.get(); } finally { Thread.currentThread().setContextClassLoader(oldTccl); } }
private void camelFactoryProducers(@Observes ProcessAnnotatedType<CdiCamelFactory> 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()))); }
environment.camelContextInjectionTarget( new SyntheticInjectionTarget<>(() -> { DefaultCamelContext context = createCamelContextWithTCCL(DefaultCamelContext::new, annotated); factory.setContext(context); factory.setBeanManager(manager);
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 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 static CamelContextNameStrategy nameStrategy(Annotated annotated) { if (annotated.isAnnotationPresent(ContextName.class)) { return new ExplicitCamelContextNameStrategy(annotated.getAnnotation(ContextName.class).value()); } else if (annotated.isAnnotationPresent(Named.class)) { // TODO: support stereotype with empty @Named annotation String name = annotated.getAnnotation(Named.class).value(); if (name.isEmpty()) { if (annotated instanceof AnnotatedField) { name = ((AnnotatedField) annotated).getJavaMember().getName(); } else if (annotated instanceof AnnotatedMethod) { name = ((AnnotatedMethod) annotated).getJavaMember().getName(); if (name.startsWith("get")) { name = decapitalize(name.substring(3)); } } else { name = decapitalize(getRawType(annotated.getBaseType()).getSimpleName()); } } return new ExplicitCamelContextNameStrategy(name); } else { // Use a specific naming strategy for Camel CDI as the default one increments the suffix for each CDI proxy created return new CdiCamelContextNameStrategy(); } } }