mirror of
https://github.com/corda/corda.git
synced 2025-02-07 03:29:19 +00:00
Just check the class against the list of contract class names
This commit is contained in:
parent
4a32a3673f
commit
db276caa77
@ -1,9 +1,9 @@
|
|||||||
package net.corda.core.internal.cordapp
|
package net.corda.core.internal.cordapp
|
||||||
|
|
||||||
import net.corda.core.contracts.Contract
|
|
||||||
import net.corda.core.cordapp.Cordapp
|
import net.corda.core.cordapp.Cordapp
|
||||||
import net.corda.core.internal.PLATFORM_VERSION
|
import net.corda.core.internal.PLATFORM_VERSION
|
||||||
import net.corda.core.internal.VisibleForTesting
|
import net.corda.core.internal.VisibleForTesting
|
||||||
|
import net.corda.core.internal.warnOnce
|
||||||
import net.corda.core.utilities.loggerFor
|
import net.corda.core.utilities.loggerFor
|
||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
|
|
||||||
@ -14,7 +14,6 @@ object CordappResolver {
|
|||||||
|
|
||||||
private val logger = loggerFor<CordappResolver>()
|
private val logger = loggerFor<CordappResolver>()
|
||||||
private val cordappClasses: ConcurrentHashMap<String, Set<Cordapp>> = ConcurrentHashMap()
|
private val cordappClasses: ConcurrentHashMap<String, Set<Cordapp>> = ConcurrentHashMap()
|
||||||
private val duplicateRegistrationFilter = DuplicateRegistrationFilter(setOf("org.jolokia", "org.json"))
|
|
||||||
|
|
||||||
// TODO Use the StackWalker API once we migrate to Java 9+
|
// TODO Use the StackWalker API once we migrate to Java 9+
|
||||||
private var cordappResolver: () -> Cordapp? = {
|
private var cordappResolver: () -> Cordapp? = {
|
||||||
@ -31,7 +30,8 @@ object CordappResolver {
|
|||||||
* This could happen when trying to run different versions of the same CorDapp on the same node.
|
* This could happen when trying to run different versions of the same CorDapp on the same node.
|
||||||
*/
|
*/
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun register(cordapp: Cordapp, classLoader: ClassLoader) {
|
fun register(cordapp: Cordapp) {
|
||||||
|
val contractClasses = cordapp.contractClassNames.toSet()
|
||||||
val existingClasses = cordappClasses.keys
|
val existingClasses = cordappClasses.keys
|
||||||
val classesToRegister = cordapp.cordappClasses.toSet()
|
val classesToRegister = cordapp.cordappClasses.toSet()
|
||||||
val notAlreadyRegisteredClasses = classesToRegister - existingClasses
|
val notAlreadyRegisteredClasses = classesToRegister - existingClasses
|
||||||
@ -41,8 +41,8 @@ object CordappResolver {
|
|||||||
|
|
||||||
for ((className, registeredCordapps) in alreadyRegistered) {
|
for ((className, registeredCordapps) in alreadyRegistered) {
|
||||||
if (registeredCordapps.any { it.jarHash == cordapp.jarHash }) continue
|
if (registeredCordapps.any { it.jarHash == cordapp.jarHash }) continue
|
||||||
if (duplicateRegistrationFilter.shouldNotify(className, classLoader)) {
|
if (className in contractClasses) {
|
||||||
logger.warn("More than one CorDapp registered for $className.")
|
logger.warnOnce("More than one CorDapp registered for contract $className.")
|
||||||
}
|
}
|
||||||
cordappClasses[className] = registeredCordapps + cordapp
|
cordappClasses[className] = registeredCordapps + cordapp
|
||||||
}
|
}
|
||||||
@ -87,22 +87,3 @@ object CordappResolver {
|
|||||||
cordappClasses.clear()
|
cordappClasses.clear()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class DuplicateRegistrationFilter(private val ignoreList: Set<String>) {
|
|
||||||
|
|
||||||
private var alreadySeen: Set<String> = emptySet()
|
|
||||||
|
|
||||||
fun shouldNotify(className: String, classLoader: ClassLoader): Boolean {
|
|
||||||
if (className in alreadySeen) return false
|
|
||||||
alreadySeen += className
|
|
||||||
|
|
||||||
if (className.canBeIgnored) return false
|
|
||||||
println("Checking status of $className with $classLoader")
|
|
||||||
return className.isContractClass(classLoader)
|
|
||||||
}
|
|
||||||
|
|
||||||
private val String.canBeIgnored: Boolean get() = ignoreList.any { startsWith(it) }
|
|
||||||
|
|
||||||
private fun String.isContractClass(classLoader: ClassLoader): Boolean = Contract::class.java.isAssignableFrom(classLoader.loadClass(this))
|
|
||||||
|
|
||||||
}
|
|
||||||
|
@ -14,8 +14,6 @@ class CordappResolverTest {
|
|||||||
CordappResolver.clear()
|
CordappResolver.clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
private val classLoader = ClassLoader.getSystemClassLoader()
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `the correct cordapp resolver is used after calling withCordappInfo`() {
|
fun `the correct cordapp resolver is used after calling withCordappInfo`() {
|
||||||
val defaultTargetVersion = 222
|
val defaultTargetVersion = 222
|
||||||
@ -24,7 +22,7 @@ class CordappResolverTest {
|
|||||||
contractClassNames = listOf(javaClass.name),
|
contractClassNames = listOf(javaClass.name),
|
||||||
minimumPlatformVersion = 3,
|
minimumPlatformVersion = 3,
|
||||||
targetPlatformVersion = defaultTargetVersion
|
targetPlatformVersion = defaultTargetVersion
|
||||||
), classLoader)
|
))
|
||||||
assertEquals(defaultTargetVersion, CordappResolver.currentTargetVersion)
|
assertEquals(defaultTargetVersion, CordappResolver.currentTargetVersion)
|
||||||
|
|
||||||
val expectedTargetVersion = 555
|
val expectedTargetVersion = 555
|
||||||
@ -41,12 +39,12 @@ class CordappResolverTest {
|
|||||||
contractClassNames = listOf(javaClass.name),
|
contractClassNames = listOf(javaClass.name),
|
||||||
minimumPlatformVersion = 3,
|
minimumPlatformVersion = 3,
|
||||||
targetPlatformVersion = 222
|
targetPlatformVersion = 222
|
||||||
), classLoader)
|
))
|
||||||
CordappResolver.register(CordappImpl.TEST_INSTANCE.copy(
|
CordappResolver.register(CordappImpl.TEST_INSTANCE.copy(
|
||||||
contractClassNames = listOf(javaClass.name),
|
contractClassNames = listOf(javaClass.name),
|
||||||
minimumPlatformVersion = 2,
|
minimumPlatformVersion = 2,
|
||||||
targetPlatformVersion = 456
|
targetPlatformVersion = 456
|
||||||
), classLoader)
|
))
|
||||||
assertThat(CordappResolver.currentCordapp).isNotNull()
|
assertThat(CordappResolver.currentCordapp).isNotNull()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,13 +55,13 @@ class CordappResolverTest {
|
|||||||
minimumPlatformVersion = 3,
|
minimumPlatformVersion = 3,
|
||||||
targetPlatformVersion = 222,
|
targetPlatformVersion = 222,
|
||||||
jarHash = SecureHash.randomSHA256()
|
jarHash = SecureHash.randomSHA256()
|
||||||
), classLoader)
|
))
|
||||||
CordappResolver.register(CordappImpl.TEST_INSTANCE.copy(
|
CordappResolver.register(CordappImpl.TEST_INSTANCE.copy(
|
||||||
contractClassNames = listOf(javaClass.name),
|
contractClassNames = listOf(javaClass.name),
|
||||||
minimumPlatformVersion = 2,
|
minimumPlatformVersion = 2,
|
||||||
targetPlatformVersion = 456,
|
targetPlatformVersion = 456,
|
||||||
jarHash = SecureHash.randomSHA256()
|
jarHash = SecureHash.randomSHA256()
|
||||||
), classLoader)
|
))
|
||||||
assertThat(CordappResolver.currentCordapp).isNull()
|
assertThat(CordappResolver.currentCordapp).isNull()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,49 +0,0 @@
|
|||||||
package net.corda.core.internal.cordapp
|
|
||||||
|
|
||||||
import net.corda.core.contracts.Contract
|
|
||||||
import net.corda.core.transactions.LedgerTransaction
|
|
||||||
import org.junit.Assert.assertFalse
|
|
||||||
import org.junit.Assert.assertTrue
|
|
||||||
import org.junit.Test
|
|
||||||
|
|
||||||
class DuplicationRegistrationFilterTest {
|
|
||||||
|
|
||||||
private val classLoader = ClassLoader.getSystemClassLoader()
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `ignores anything in the ignore list`() {
|
|
||||||
val filter = DuplicateRegistrationFilter(setOf("foo.bar", "foo.baz", "net.corda"))
|
|
||||||
|
|
||||||
assertFalse(filter.shouldNotify("foo.bar.A", classLoader))
|
|
||||||
assertFalse(filter.shouldNotify("foo.baz.xyzzy.B", classLoader))
|
|
||||||
assertFalse(filter.shouldNotify(ActuallyAContract::class.java.name, classLoader))
|
|
||||||
}
|
|
||||||
|
|
||||||
class NotAContract
|
|
||||||
|
|
||||||
class ActuallyAContract : Contract {
|
|
||||||
override fun verify(tx: LedgerTransaction) = Unit
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `ignores anything that isn't a contract`() {
|
|
||||||
val filter = DuplicateRegistrationFilter(setOf("foo.bar", "foo.baz"))
|
|
||||||
|
|
||||||
assertFalse(filter.shouldNotify(NotAContract::class.java.name, classLoader))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `notifies anything that is a contract`() {
|
|
||||||
val filter = DuplicateRegistrationFilter(setOf("foo.bar", "foo.baz"))
|
|
||||||
|
|
||||||
assertTrue(filter.shouldNotify(ActuallyAContract::class.java.name, classLoader))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `ignores anything it's seen before`() {
|
|
||||||
val filter = DuplicateRegistrationFilter(setOf("foo.bar", "foo.baz"))
|
|
||||||
|
|
||||||
assertTrue(filter.shouldNotify(ActuallyAContract::class.java.name, classLoader))
|
|
||||||
assertFalse(filter.shouldNotify(ActuallyAContract::class.java.name, classLoader))
|
|
||||||
}
|
|
||||||
}
|
|
@ -126,7 +126,7 @@ class JarScanningCordappLoader private constructor(private val cordappJarPaths:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cordapps.forEach { CordappResolver.register(it, appClassLoader) }
|
cordapps.forEach { CordappResolver.register(it) }
|
||||||
return cordapps
|
return cordapps
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user