Skip to content

Commit

Permalink
Delegate single instance resolve to QuarkusTypeResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
osoykan committed Oct 28, 2024
1 parent 1880fc6 commit f170f10
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
@file:Suppress("UNCHECKED_CAST")

package com.trendyol.kediatr.quarkus

import com.trendyol.kediatr.*
import io.quarkus.runtime.Startup
import jakarta.enterprise.context.ApplicationScoped
import jakarta.enterprise.inject.spi.BeanManager

class KediatRBeanProvider(
private val beanManager: BeanManager,
private val quarkusTypeScanner: QuarkusTypeScanner
private val resolver: QuarkusTypeResolver
) : DependencyProvider {
override fun <T> getSingleInstanceOf(clazz: Class<T>): T {
val beans = beanManager.getBeans(clazz)
val bean = beans.firstOrNull() ?: error("No bean found for class $clazz")
val ctx = beanManager.createCreationalContext(bean)
return beanManager.getReference(bean, clazz, ctx) as T
}
override fun <T> getSingleInstanceOf(clazz: Class<T>): T = resolver.resolveOrThrow(clazz)

override fun <T> getSubTypesOf(clazz: Class<T>): Collection<Class<T>> = quarkusTypeScanner.getSubTypesOf(clazz)
override fun <T> getSubTypesOf(clazz: Class<T>): Collection<Class<T>> = resolver.resolveTypesOrEmpty(clazz)
}

@ApplicationScoped
class QuarkusMediatorBuilder {
@ApplicationScoped
fun kediatRBeanProvider(
beanManager: BeanManager,
quarkusTypeScanner: QuarkusTypeScanner
): KediatRBeanProvider =
KediatRBeanProvider(beanManager, quarkusTypeScanner)
fun kediatRBeanProvider(resolver: QuarkusTypeResolver): KediatRBeanProvider = KediatRBeanProvider(resolver)

@ApplicationScoped
@Startup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ import jakarta.enterprise.inject.spi.*
import java.lang.reflect.*

@ApplicationScoped
class QuarkusTypeScanner(
class QuarkusTypeResolver(
private val beanManager: BeanManager
) {
fun <T> getSubTypesOf(
clazz: Class<T>
): Collection<Class<T>> = beanManager.getBeans(Any::class.java)
fun <T> resolveOrThrow(clazz: Class<T>): T {
val beans = beanManager.getBeans(clazz)
val bean = beans.firstOrNull() ?: error("No bean found for class $clazz")
val ctx = beanManager.createCreationalContext(bean)
return beanManager.getReference(bean, clazz, ctx) as T
}

fun <T> resolveTypesOrEmpty(clazz: Class<T>): Collection<Class<T>> = beanManager.getBeans(Any::class.java)
.asSequence()
.filterNot(::quarkusThings)
.flatMap { it.types }
Expand Down

0 comments on commit f170f10

Please sign in to comment.