Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS-981] Update project version and return backward compatibility #5

Open
wants to merge 3 commits into
base: review
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 48 additions & 33 deletions src/main/kotlin/com/exactpro/th2/conn/amqp/BoxMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,37 +70,7 @@ fun main(args: Array<String>) {
.build()
)

if (configuration.parameters != null) {
var reason: String? = null
if (configuration.sessionAlias == null) {
reason = "In old connection parameters version {sessionAlias} or {parameters} couldn't be null"
} else {
if (configuration.sessions != null) {
reason = "Configuration couldn't contain old and new connection parameters version. it must be {sessions} or {parameters}"
} else {
configuration.sessions = ArrayList()
configuration.sessions?.plusAssign(
ConnParameters(
sessionAlias = configuration.sessionAlias,
initialContextFactory = configuration.parameters.initialContextFactory,
factorylookup = configuration.parameters.factorylookup,
sendQueue = configuration.parameters.sendQueue,
receiveQueue = configuration.parameters.receiveQueue
)
)
}
}
if (reason != null) {
eventRouter.safeSend(
Event.start().endTimestamp()
.status(Event.Status.FAILED)
.type("Error")
.name(reason),
rootEvent.id
)
throw java.lang.IllegalArgumentException(reason)
}
}
configureConnectionParameters(configuration, eventRouter, rootEvent)

val rawRouter: MessageRouter<RawMessageBatch> = factory.messageRouterRawBatch
val aliasToService = HashMap<String, ConnService>()
Expand Down Expand Up @@ -130,14 +100,14 @@ fun main(args: Array<String>) {
aliasToService[connParameters.sessionAlias] = service
service.start()
}
}?: throw java.lang.IllegalArgumentException("Connection parameters can't be blank. Use {sessions} or {parameters + sessioAlias}")
} ?: throw java.lang.IllegalArgumentException("Connection parameters can't be blank. Use {sessions} or {parameters + sessioAlias}")
Sergey-Sitnikov-Exactpro marked this conversation as resolved.
Show resolved Hide resolved
rawRouter.subscribeAll { _, rawBatch ->
rawBatch.messagesList.forEach { msg ->
msg.runCatching {
val alias = msg.metadata.id.connectionId.sessionAlias
aliasToService.getOrElse(
alias,
{throw IllegalArgumentException("Can't find service by alias {$alias}")}
{ throw IllegalArgumentException("Can't find service by alias {$alias}") }
Sergey-Sitnikov-Exactpro marked this conversation as resolved.
Show resolved Hide resolved
).send(this)
}.onFailure {
eventRouter.safeSend(
Expand Down Expand Up @@ -179,6 +149,51 @@ fun main(args: Array<String>) {
}
}

private fun configureConnectionParameters(
configuration: Configuration,
eventRouter: MessageRouter<EventBatch>,
rootEvent: Event
) {
when {
configuration.parameters != null && configuration.sessionAlias == null -> {
sendError(
eventRouter,
"The connection {parameters} configuration requires {sessionAlias}. Please specify the option or use the {sessions} configuration instead",
rootEvent
)
}
configuration.sessions != null && configuration.parameters != null ->
sendError(
eventRouter,
"Configuration can't contain both connection version. It must be {sessions} or {parameters}",
rootEvent
)

configuration.parameters != null && configuration.sessions == null && configuration.sessionAlias != null -> {
(configuration.sessions as ArrayList<ConnParameters>).plusAssign(
ConnParameters(
sessionAlias = configuration.sessionAlias,
initialContextFactory = configuration.parameters.initialContextFactory,
factorylookup = configuration.parameters.factorylookup,
sendQueue = configuration.parameters.sendQueue,
receiveQueue = configuration.parameters.receiveQueue
)
)
}
}
}

private fun sendError(eventRouter: MessageRouter<EventBatch>, reason: String?, rootEvent: Event) {
eventRouter.safeSend(
Event.start().endTimestamp()
.status(Event.Status.FAILED)
.type("Error")
.name(reason),
rootEvent.id
)
throw java.lang.IllegalArgumentException(reason)
Sergey-Sitnikov-Exactpro marked this conversation as resolved.
Show resolved Hide resolved
}

private fun MessageRouter<EventBatch>.safeSend(event: Event, parentId: String?) {
runCatching { send(EventBatch.newBuilder().addEvents(event.toProtoEvent(parentId)).build()) }
.onFailure { LOGGER.error(it) { "Cannot send event with id ${event.id}" } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import javax.jms.Message
import javax.jms.MessageListener
import javax.naming.InitialContext

typealias Config = Map<String, String?>
typealias Config = Map<String, String>

@NotThreadSafe
class AmqpClient(config: Config, val errorReporter: (Throwable) -> Unit) : IClient {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ class Configuration(
val rootEventName: String = "ConnAmqp",
var sessions: MutableList<ConnParameters>?,
val sessionAlias: String?,
val parameters: ConnParameters?
val parameters: SingleConnectionConnParameters?
)

class ConnParameters(
val sessionAlias: String?,
val sessionAlias: String,
val initialContextFactory: String,
val factorylookup: String,
val sendQueue: String,
val receiveQueue: String
)
class SingleConnectionConnParameters(
val initialContextFactory: String,
val factorylookup: String,
val sendQueue: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ConnServiceImpl(
client.stop()
}
companion object {
private fun ConnParameters.toMap(): Map<String, String?> = mutableMapOf(
private fun ConnParameters.toMap(): Map<String, String> = mutableMapOf(
Context.INITIAL_CONTEXT_FACTORY to initialContextFactory,
"connectionfactory.factorylookup" to factorylookup,
"queue.sendQueue" to sendQueue,
Expand Down