use of org.apache.camel.quarkus.core.CamelRecorder in project camel-quarkus by apache.
the class CamelProcessor method typeConverterRegistry.
/*
* Discover {@link TypeConverterLoader}.
*/
@SuppressWarnings("unchecked")
@Record(ExecutionTime.STATIC_INIT)
@BuildStep
CamelTypeConverterRegistryBuildItem typeConverterRegistry(CamelRecorder recorder, ApplicationArchivesBuildItem applicationArchives, List<CamelTypeConverterLoaderBuildItem> additionalLoaders, CombinedIndexBuildItem combinedIndex, BuildProducer<ReflectiveClassBuildItem> reflectiveClasses) {
IndexView index = combinedIndex.getIndex();
RuntimeValue<TypeConverterRegistry> typeConverterRegistry = recorder.createTypeConverterRegistry();
//
// This should be simplified by searching for classes implementing TypeConverterLoader but that
// would lead to have org.apache.camel.impl.converter.AnnotationTypeConverterLoader taken into
// account even if it should not.
//
final ClassLoader TCCL = Thread.currentThread().getContextClassLoader();
for (ApplicationArchive archive : applicationArchives.getAllApplicationArchives()) {
for (Path root : archive.getRootDirs()) {
Path path = root.resolve(BaseTypeConverterRegistry.META_INF_SERVICES_TYPE_CONVERTER_LOADER);
if (!Files.isRegularFile(path)) {
continue;
}
try {
Files.readAllLines(path, StandardCharsets.UTF_8).stream().map(String::trim).filter(l -> !l.isEmpty()).filter(l -> !l.startsWith("#")).map(l -> (Class<? extends TypeConverterLoader>) CamelSupport.loadClass(l, TCCL)).forEach(loader -> recorder.addTypeConverterLoader(typeConverterRegistry, loader));
} catch (IOException e) {
throw new RuntimeException("Error discovering TypeConverterLoader", e);
}
}
}
Set<String> internalConverters = new HashSet<>();
// ignore all @converters from org.apache.camel:camel-* dependencies
for (ApplicationArchive archive : applicationArchives.getAllApplicationArchives()) {
ArtifactKey artifactKey = archive.getKey();
if (artifactKey != null && "org.apache.camel".equals(artifactKey.getGroupId()) && artifactKey.getArtifactId().startsWith("camel-")) {
internalConverters.addAll(archive.getIndex().getAnnotations(DotName.createSimple(Converter.class.getName())).stream().filter(a -> a.target().kind() == AnnotationTarget.Kind.CLASS).map(a -> a.target().asClass().name().toString()).collect(Collectors.toSet()));
}
}
Set<Class> convertersClasses = index.getAnnotations(DotName.createSimple(Converter.class.getName())).stream().filter(a -> a.target().kind() == AnnotationTarget.Kind.CLASS && (a.value("generateBulkLoader") == null || !a.value("generateBulkLoader").asBoolean()) && (a.value("generateLoader") == null || !a.value("generateLoader").asBoolean())).map(a -> a.target().asClass().name().toString()).filter(s -> !internalConverters.contains(s)).map(s -> CamelSupport.loadClass(s, TCCL)).collect(Collectors.toSet());
recorder.loadAnnotatedConverters(typeConverterRegistry, convertersClasses);
//
for (CamelTypeConverterLoaderBuildItem item : additionalLoaders) {
recorder.addTypeConverterLoader(typeConverterRegistry, item.getValue());
}
return new CamelTypeConverterRegistryBuildItem(typeConverterRegistry);
}
use of org.apache.camel.quarkus.core.CamelRecorder in project camel-quarkus by apache.
the class CamelRegistryProcessor method bindBeansToRegistry.
@Record(ExecutionTime.STATIC_INIT)
@BuildStep
public void bindBeansToRegistry(CamelRecorder recorder, CamelConfig camelConfig, ApplicationArchivesBuildItem applicationArchives, ContainerBeansBuildItem containerBeans, CamelRegistryBuildItem registry, // CamelContextBuildItem placeholder ensures this build step runs after the CamelContext is created
CamelContextBuildItem camelContextBuildItem, List<CamelBeanBuildItem> registryItems, List<CamelServiceFilterBuildItem> serviceFilters, List<CamelServicePatternBuildItem> servicePatterns) {
final ClassLoader TCCL = Thread.currentThread().getContextClassLoader();
final PathFilter pathFilter = servicePatterns.stream().filter(patterns -> patterns.getDestination() == CamelServiceDestination.REGISTRY).collect(PathFilter.Builder::new, (builder, patterns) -> builder.patterns(patterns.isInclude(), patterns.getPatterns()), PathFilter.Builder::combine).include(camelConfig.service.registry.includePatterns).exclude(camelConfig.service.registry.excludePatterns).build();
CamelSupport.services(applicationArchives, pathFilter).filter(si -> !containerBeans.getBeans().contains(si)).filter(si -> {
//
// by default all the service found in META-INF/service/org/apache/camel are
// bound to the registry but some of the services are then replaced or set
// to the camel context directly by extension so it does not make sense to
// instantiate them in this phase.
//
boolean blacklisted = serviceFilters.stream().anyMatch(filter -> filter.getPredicate().test(si));
if (blacklisted) {
LOGGER.debug("Ignore service: {}", si);
}
return !blacklisted;
}).forEach(si -> {
LOGGER.debug("Binding bean with name: {}, type {}", si.name, si.type);
recorder.bind(registry.getRegistry(), si.name, CamelSupport.loadClass(si.type, TCCL));
});
registryItems.stream().filter(item -> !containerBeans.getBeans().contains(item)).forEach(item -> {
LOGGER.debug("Binding bean with name: {}, type {}", item.getName(), item.getType());
if (item.getValue().isPresent()) {
recorder.bind(registry.getRegistry(), item.getName(), CamelSupport.loadClass(item.getType(), TCCL), item.getValue().get());
} else {
// the instance of the service will be instantiated by the recorder, this avoid
// creating a recorder for trivial services.
recorder.bind(registry.getRegistry(), item.getName(), CamelSupport.loadClass(item.getType(), TCCL));
}
});
}
use of org.apache.camel.quarkus.core.CamelRecorder in project camel-quarkus by apache.
the class InjectionPointsProcessor method produceBeans.
void produceBeans(CamelRecorder recorder, List<CapabilityBuildItem> capabilities, BuildProducer<SyntheticBeanBuildItem> syntheticBeans, BuildProducer<NativeImageProxyDefinitionBuildItem> proxyDefinitions, AtomicReference<Boolean> beanCapabilityAvailable, AnnotationInstance annot, final DotName fieldType, String annotationTarget, DotName declaringClass) {
try {
Class<?> clazz = Class.forName(fieldType.toString(), false, Thread.currentThread().getContextClassLoader());
if (ProducerTemplate.class.isAssignableFrom(clazz)) {
syntheticBeans.produce(SyntheticBeanBuildItem.configure(fieldType).setRuntimeInit().scope(Singleton.class).supplier(recorder.createProducerTemplate(annot.value().asString())).addQualifier(annot).done());
/*
* Note that ProducerTemplate injection points not having @EndpointInject are produced via
* CamelProducers.camelProducerTemplate()
*/
} else if (FluentProducerTemplate.class.isAssignableFrom(clazz)) {
syntheticBeans.produce(SyntheticBeanBuildItem.configure(fieldType).setRuntimeInit().scope(Singleton.class).supplier(recorder.createFluentProducerTemplate(annot.value().asString())).addQualifier(annot).done());
/*
* Note that FluentProducerTemplate injection points not having @EndpointInject are produced via
* CamelProducers.camelFluentProducerTemplate()
*/
} else if (clazz.isInterface()) {
if (beanCapabilityAvailable.get() == null) {
beanCapabilityAvailable.set(capabilities.stream().map(CapabilityBuildItem::getName).anyMatch(feature -> CamelCapabilities.BEAN.equals(feature)));
}
if (!beanCapabilityAvailable.get()) {
throw new IllegalStateException("Add camel-quarkus-bean dependency to be able to use @org.apache.camel.Produce on fields with interface type: " + fieldType.toString() + " " + annotationTarget + " in " + declaringClass.toString());
}
proxyDefinitions.produce(new NativeImageProxyDefinitionBuildItem(fieldType.toString()));
syntheticBeans.produce(SyntheticBeanBuildItem.configure(fieldType).setRuntimeInit().scope(Singleton.class).supplier(recorder.produceProxy(clazz, annot.value().asString())).addQualifier(annot).done());
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
Aggregations