/
SingleResultExecutionWriter.kt
63 lines (58 loc) · 2.1 KB
/
SingleResultExecutionWriter.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
package ru.tinkoff.top.camunda.delegator.delegates.executors.interceptors.output.single
import mu.KLogging
import org.camunda.bpm.engine.delegate.DelegateExecution
import org.springframework.core.annotation.AnnotatedElementUtils
import ru.tinkoff.top.camunda.delegator.delegates.DelegateInformation
import ru.tinkoff.top.camunda.delegator.delegates.executors.interceptors.DelegateInterceptor
import java.lang.reflect.Method
/**
* Interceptor processing delegate result and write variable to context.
* To activate this interceptor mark delegate method with annotation @SingleResultVariable
* @see SingleResultVariable
*
* @author p.pletnev
*/
class SingleResultExecutionWriter : DelegateInterceptor() {
companion object : KLogging()
override fun execute(
execution: DelegateExecution,
delegateInfo: DelegateInformation,
delegateArguments: Array<Any?>?
): Any? {
try {
val result = nextExecutor.execute(execution, delegateInfo, delegateArguments)
processReturnValue(
delegateInfo.metaInformation.executionDelegateMethod,
execution,
result
)
return result
} catch (ex: RuntimeException) {
processReturnValue(
delegateInfo.metaInformation.executionDelegateMethod,
execution,
null
)
throw ex
}
}
private fun processReturnValue(
delegateMethod: Method,
execution: DelegateExecution,
result: Any?
) {
val resultVariable = AnnotatedElementUtils.findMergedAnnotation(
delegateMethod,
SingleResultVariable::class.java
) ?: return
if (delegateMethod.returnType == Void.TYPE) {
logger.warn { "Unsupported ${delegateMethod.returnType} return type for processor SingleResultProcessor" }
return
}
if (resultVariable.isLocal) {
execution.setVariableLocal(resultVariable.name, result)
} else {
execution.setVariable(resultVariable.name, result)
}
}
}