All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.enodeframework.messaging.impl.DefaultTwoMessageHandlerProvider.kt Maven / Gradle / Ivy

There is a newer version: 1.1.10
Show newest version
package org.enodeframework.messaging.impl

import org.enodeframework.eventing.IDomainEvent
import org.enodeframework.infrastructure.impl.AbstractHandlerProvider
import org.enodeframework.infrastructure.impl.ManyType
import org.enodeframework.messaging.IMessage
import org.enodeframework.messaging.IMessageHandlerProxy2
import org.enodeframework.messaging.ITwoMessageHandlerProvider
import org.enodeframework.messaging.MessageHandlerData
import java.lang.reflect.Method
import java.util.*
import kotlin.coroutines.Continuation

/**
 * @author [email protected]
 */
class DefaultTwoMessageHandlerProvider : AbstractHandlerProvider>>(), ITwoMessageHandlerProvider {
    override fun getKey(method: Method): ManyType {
        return ManyType(listOf(*method.parameterTypes))
    }

    override fun getHandlerProxyImplementationType(): Class {
        return MessageHandlerProxy2::class.java
    }

    override fun isHandlerSourceMatchKey(handlerSource: List>, key: ManyType): Boolean {
        for (type in key.types) {
            if (!handlerSource.stream().anyMatch { x: Class<*> -> x == type }) {
                return false
            }
        }
        return true
    }

    override fun isHandleMethodMatch(method: Method): Boolean {
        if (isSuspendMethod(method)) {
            return methodMatchSuspend(method)
        }
        return methodMatch(method)
    }

    private fun methodMatch(method: Method): Boolean {
        if (method.parameterTypes.size != 2) {
            return false
        }
        if (IMessage::class.java == method.parameterTypes[0]) {
            return false
        }
        if (IMessage::class.java == method.parameterTypes[1]) {
            return false
        }
        if (!IDomainEvent::class.java.isAssignableFrom(method.parameterTypes[0])) {
            return false
        }
        if (!IDomainEvent::class.java.isAssignableFrom(method.parameterTypes[1])) {
            return false
        }
        return isMethodAnnotationSubscribe(method)
    }

    private fun methodMatchSuspend(method: Method): Boolean {
        if (method.parameterTypes.size != 3) {
            return false
        }
        if (IMessage::class.java == method.parameterTypes[0]) {
            return false
        }
        if (IMessage::class.java == method.parameterTypes[1]) {
            return false
        }
        if (!IDomainEvent::class.java.isAssignableFrom(method.parameterTypes[0])) {
            return false
        }
        if (!IDomainEvent::class.java.isAssignableFrom(method.parameterTypes[1])) {
            return false
        }
        if (Continuation::class.java != method.parameterTypes[2]) {
            return false
        }
        return isMethodAnnotationSubscribe(method)
    }

    override fun getHandlers(messageTypes: List>): List> {
        return getHandlersInternal(messageTypes)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy