-
Notifications
You must be signed in to change notification settings - Fork 1
/
DefaultFactory.kt
188 lines (172 loc) · 9.35 KB
/
DefaultFactory.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package io.mahdibohloul.kpringmediator.infrastructure
import io.mahdibohloul.kpringmediator.core.*
import kotlin.reflect.KClass
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.context.ApplicationContext
import org.springframework.core.GenericTypeResolver
/**
* @author Mahdi Bohloul
*/
class DefaultFactory(
private val applicationContext: ApplicationContext
) : Factory {
private val registeredRequestHandlers: MutableMap<KClass<out Request<*>>, RequestHandlerProvider<*>> = HashMap()
private val registeredNotificationHandlers: MutableMap<KClass<out Notification>, MutableSet<NotificationHandlerProvider<*>>> =
HashMap()
private val registeredNotificationExceptionHandlers:
MutableMap<
KClass<out Notification>,
MutableMap<KClass<out Exception>,
MutableSet<NotificationExceptionHandlerProvider<*>>>> =
HashMap()
private val registeredCommandHandler: MutableMap<KClass<out Command>, CommandHandlerProvider<*>> = HashMap()
private var initialized: Boolean = false
internal var handleNotificationExceptions: Boolean = false
override fun <TRequest : Request<TResponse>, TResponse> getRequestHandler(
requestClass: KClass<out TRequest>
):
RequestHandler<TRequest, TResponse> {
if (!initialized) {
initializeHandlers()
}
registeredRequestHandlers[requestClass]?.let {
return it.handler as RequestHandler<TRequest, TResponse>
}
?: throw NoRequestHandlerException("No RequestHandler is registered to handle request of type ${requestClass.simpleName}")
}
override fun <TNotification : Notification> getNotificationHandlers(notificationClass: KClass<out TNotification>): Set<NotificationHandler<TNotification>> {
if (!initialized) {
initializeHandlers()
}
val handlers = mutableSetOf<NotificationHandler<TNotification>>()
registeredNotificationHandlers[notificationClass]?.let {
it.forEach { provider ->
val handler = provider.handler as NotificationHandler<TNotification>
handlers.add(handler)
}
}
?: throw NoNotificationHandlersException("No NotificationHandlers are registered to receive notification of type ${notificationClass.simpleName}")
return handlers
}
override fun <TCommand : Command> getCommandHandler(commandClass: KClass<out TCommand>): CommandHandler<TCommand> {
if (!initialized) {
initializeHandlers()
}
registeredCommandHandler[commandClass]?.let { provider ->
return provider.handler as CommandHandler<TCommand>
}
?: throw NoCommandHandlerException("No CommandHandler is registered to handle request of type ${commandClass.simpleName}")
}
override fun <TNotification : Notification, TNotificationException : Exception> getNotificationExceptionHandlers(
notificationClass: KClass<out TNotification>,
exceptionClass: KClass<out TNotificationException>
): Set<NotificationExceptionHandler<TNotification, TNotificationException>> {
if (!handleNotificationExceptions) {
logger.warn("Notification exception handling is disabled")
return emptySet()
}
if (!initialized) {
initializeHandlers()
}
val handlers = mutableSetOf<NotificationExceptionHandler<TNotification, TNotificationException>>()
registeredNotificationExceptionHandlers[notificationClass]?.get(exceptionClass)?.let {
it.forEach { provider ->
val handler = provider.handler as NotificationExceptionHandler<TNotification, TNotificationException>
handlers.add(handler)
}
}
?: logger.warn("No NotificationExceptionHandlers are registered to receive notification exception of type ${notificationClass.simpleName}")
return handlers
}
private fun initializeHandlers() {
synchronized(this) {
if (!initialized) {
applicationContext.getBeanNamesForType(RequestHandler::class.java)
.forEach { registerRequestHandler(it) }
applicationContext.getBeanNamesForType(NotificationHandler::class.java)
.forEach { registerNotificationHandler(it) }
applicationContext.getBeanNamesForType(CommandHandler::class.java)
.forEach { registerCommandHandler(it) }
if (handleNotificationExceptions)
applicationContext.getBeanNamesForType(NotificationExceptionHandler::class.java)
.forEach { registerNotificationExceptionHandler(it) }
initialized = true
}
}
}
private fun registerRequestHandler(requestHandlerName: String) {
logger.debug("Registering RequestHandler with name $requestHandlerName")
val handler: RequestHandler<*, *> = applicationContext.getBean(requestHandlerName) as RequestHandler<*, *>
val generics = GenericTypeResolver.resolveTypeArguments(handler::class.java, RequestHandler::class.java)
generics?.let {
val requestType = (it[0] as Class<out Request<*>>).kotlin
if (registeredRequestHandlers.contains(requestType)) {
throw DuplicateRequestHandlerRegistrationException(
"${requestType.simpleName} already has a registered handler. Each request must have a single request handler"
)
}
val requestProvider = RequestHandlerProvider(applicationContext, handler::class)
registeredRequestHandlers[requestType] = requestProvider
logger.info("Registered RequestHandler ${handler::class.simpleName} to handle request ${requestType.simpleName}")
}
}
private fun registerNotificationHandler(notificationHandlerName: String) {
logger.debug("Registering NotificationHandler with name $notificationHandlerName")
val notificationHandler: NotificationHandler<*> =
applicationContext.getBean(notificationHandlerName) as NotificationHandler<*>
val generics =
GenericTypeResolver.resolveTypeArguments(notificationHandler::class.java, NotificationHandler::class.java)
generics?.let {
val notificationType = (it[0] as Class<out Notification>).kotlin
val eventProvider = NotificationHandlerProvider(applicationContext, notificationHandler::class)
registeredNotificationHandlers[notificationType]?.add(eventProvider) ?: kotlin.run {
registeredNotificationHandlers[notificationType] = mutableSetOf(eventProvider)
}
logger.info("Registered NotificationHandler ${notificationHandler::class.simpleName} to receive Notification ${notificationType.simpleName}")
}
}
private fun registerNotificationExceptionHandler(notificationExceptionHandlerName: String) {
logger.debug("Registering NotificationExceptionHandler with name $notificationExceptionHandlerName")
val notificationExceptionHandler: NotificationExceptionHandler<*, *> =
applicationContext.getBean(notificationExceptionHandlerName) as NotificationExceptionHandler<*, *>
val generics =
GenericTypeResolver.resolveTypeArguments(
notificationExceptionHandler::class.java,
NotificationExceptionHandler::class.java
)
generics?.let {
val notificationType = (it[0] as Class<out Notification>).kotlin
val exceptionType = (it[1] as Class<out Exception>).kotlin
val eventProvider =
NotificationExceptionHandlerProvider(applicationContext, notificationExceptionHandler::class)
registeredNotificationExceptionHandlers[notificationType]?.let { exceptionTypes ->
exceptionTypes[exceptionType]?.add(eventProvider) ?: kotlin.run {
exceptionTypes[exceptionType] = mutableSetOf(eventProvider)
}
} ?: kotlin.run {
registeredNotificationExceptionHandlers[notificationType] =
mutableMapOf(exceptionType to mutableSetOf(eventProvider))
}
}
}
private fun registerCommandHandler(commandHandlerName: String) {
logger.debug("Registering CommandHandler with name $commandHandlerName")
val handler: CommandHandler<*> = applicationContext.getBean(commandHandlerName) as CommandHandler<*>
val generics = GenericTypeResolver.resolveTypeArguments(handler::class.java, CommandHandler::class.java)
generics?.let {
val requestType = (it[0] as Class<out Command>).kotlin
if (registeredCommandHandler.contains(requestType)) {
throw DuplicateCommandHandlerRegistrationException(
"${requestType.simpleName} already has a registered handler. Each request must have a single request handler"
)
}
val requestProvider = CommandHandlerProvider(applicationContext, handler::class)
registeredCommandHandler[requestType] = requestProvider
logger.info("Registered CommandHandler ${handler::class.simpleName} to handle request ${requestType.simpleName}")
}
}
companion object {
val logger: Logger = LoggerFactory.getLogger(DefaultFactory::class.java)
}
}