Skip to content

Commit 3f4a354

Browse files
committed
#37 improvements to Introspection.kt logic
1 parent daf8cfc commit 3f4a354

File tree

1 file changed

+55
-39
lines changed

1 file changed

+55
-39
lines changed

corda-common/src/main/kotlin/tech/b180/cordaptor/corda/Introspection.kt

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ class CordappInfoBuilder(
2525

2626
companion object {
2727
private val logger = loggerFor<CordappInfoBuilder>()
28-
private val packageNamesToFilterOut = listOf("kotlin", "java", "[", "org.apache", "org.xerial", "org.brotli",
29-
"org.osgi", "org.jetbrains")
3028
}
3129

3230
fun build(): List<CordappInfo> {
@@ -39,62 +37,80 @@ class CordappInfoBuilder(
3937
shortName = settings.urlPath,
4038
jarHash = cordapp.jarHash,
4139
jarURL = cordapp.jarPath,
42-
flows = cordapp.allFlows
43-
.filter { flowClass ->
44-
if (!FlowLogic::class.java.isAssignableFrom(flowClass)) {
45-
throw AssertionError("Class ${flowClass.canonicalName} " +
46-
"was identified as a flow by Corda, but cannot be cast to FlowLogic<*>")
47-
}
48-
49-
val canStartByService = flowClass.getAnnotation(StartableByService::class.java) != null
50-
val canStartByRPC = flowClass.getAnnotation(StartableByRPC::class.java) != null
51-
52-
if (isEmbedded) {
40+
flows = when(isEmbedded) {
41+
true -> {
42+
cordapp.serviceFlows
43+
.filter {
44+
flowClass ->
45+
if (!FlowLogic::class.java.isAssignableFrom(flowClass)) {
46+
throw AssertionError(
47+
"Class ${flowClass.canonicalName} " +
48+
"was identified as a flow by Corda, but cannot be cast to FlowLogic<*>"
49+
)
50+
}
51+
val canStartByService = flowClass.getAnnotation(StartableByService::class.java) != null
52+
val canStartByRPC = flowClass.getAnnotation(StartableByRPC::class.java) != null
5353
// ignore the flow as it is not available to Corda service
5454
// and provide further information in the log to help with troubleshooting
5555
if (!canStartByService) {
5656
if (canStartByRPC) {
57-
logger.warn("Flow class ${flowClass.canonicalName} can be started by RPC, " +
58-
"but is not available to Cordaptor running as a service within the node. " +
59-
"Annotate the flow class with @StartableByService to make it available")
57+
logger.warn(
58+
"Flow class ${flowClass.canonicalName} can be started by RPC, " +
59+
"but is not available to Cordaptor running as a service within the node. " +
60+
"Annotate the flow class with @StartableByService to make it available"
61+
)
6062
}
61-
62-
logger.debug("Ignoring flow class {} as it is not available to Corda services",
63-
flowClass.canonicalName)
63+
logger.debug(
64+
"Ignoring flow class {} as it is not available to Corda services",
65+
flowClass.canonicalName
66+
)
6467
}
65-
6668
canStartByService
67-
} else {
69+
}
70+
}
71+
false -> {
72+
cordapp.rpcFlows
73+
.filter {
74+
flowClass ->
75+
if (!FlowLogic::class.java.isAssignableFrom(flowClass)) {
76+
throw AssertionError(
77+
"Class ${flowClass.canonicalName} " +
78+
"was identified as a flow by Corda, but cannot be cast to FlowLogic<*>"
79+
)
80+
}
81+
val canStartByService = flowClass.getAnnotation(StartableByService::class.java) != null
82+
val canStartByRPC = flowClass.getAnnotation(StartableByRPC::class.java) != null
6883
// ignore the flow as it is not available to Corda RPC
6984
// and provide further information in the log to help with troubleshooting
7085
if (!canStartByRPC) {
7186
if (canStartByService) {
72-
logger.warn("Flow class ${flowClass.canonicalName} can be started by a Corda service, " +
73-
"but is not available to Cordaptor running as a standalone gateway. " +
74-
"Annotate the flow class with @StartableByRPC to make it available")
87+
logger.warn(
88+
"Flow class ${flowClass.canonicalName} can be started by a Corda service, " +
89+
"but is not available to Cordaptor running as a standalone gateway. " +
90+
"Annotate the flow class with @StartableByRPC to make it available"
91+
)
7592
}
76-
77-
logger.debug("Ignoring flow class {} as it is not available to Corda RPC clients",
78-
flowClass.canonicalName)
93+
logger.debug(
94+
"Ignoring flow class {} as it is not available to Corda RPC clients",
95+
flowClass.canonicalName
96+
)
7997
}
80-
8198
canStartByRPC
8299
}
83-
}
84-
.map { flowClass ->
85-
@Suppress("UNCHECKED_CAST")
86-
val flowKClass = flowClass.kotlin as KClass<out FlowLogic<Any>>
87-
val flowResultClass = determineFlowResultClass(flowKClass)
88-
89-
logger.debug("Registering flow class {} returning instances of {}",
90-
flowClass.canonicalName, flowResultClass.qualifiedName)
100+
}
101+
}.map { flowClass ->
102+
@Suppress("UNCHECKED_CAST")
103+
val flowKClass = flowClass.kotlin as KClass<out FlowLogic<Any>>
104+
val flowResultClass = determineFlowResultClass(flowKClass)
91105

92-
CordappFlowInfo(flowClass = flowKClass, flowResultClass = flowResultClass)
93-
},
106+
logger.debug("Registering flow class {} returning instances of {}",
107+
flowClass.canonicalName, flowResultClass.qualifiedName)
94108

109+
CordappFlowInfo(flowClass = flowKClass, flowResultClass = flowResultClass)
110+
},
95111
contractStates = cordapp.cordappClasses
96112
// filtering out SDK classes that could be picked up by the introspecting scanner
97-
.filterNot { pkg -> packageNamesToFilterOut.any{pkg.startsWith(it)} }
113+
.filter { pkg -> pkg.contains("state") }
98114
.map { Class.forName(it) }
99115
.filter { clazz ->
100116
ContractState::class.java.isAssignableFrom(clazz).also {

0 commit comments

Comments
 (0)