mirror of
https://github.com/corda/corda.git
synced 2025-01-30 08:04:16 +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
|
||||
|
||||
import net.corda.core.contracts.Contract
|
||||
import net.corda.core.cordapp.Cordapp
|
||||
import net.corda.core.internal.PLATFORM_VERSION
|
||||
import net.corda.core.internal.VisibleForTesting
|
||||
import net.corda.core.internal.warnOnce
|
||||
import net.corda.core.utilities.loggerFor
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
@ -14,7 +14,6 @@ object CordappResolver {
|
||||
|
||||
private val logger = loggerFor<CordappResolver>()
|
||||
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+
|
||||
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.
|
||||
*/
|
||||
@Synchronized
|
||||
fun register(cordapp: Cordapp, classLoader: ClassLoader) {
|
||||
fun register(cordapp: Cordapp) {
|
||||
val contractClasses = cordapp.contractClassNames.toSet()
|
||||
val existingClasses = cordappClasses.keys
|
||||
val classesToRegister = cordapp.cordappClasses.toSet()
|
||||
val notAlreadyRegisteredClasses = classesToRegister - existingClasses
|
||||
@ -41,8 +41,8 @@ object CordappResolver {
|
||||
|
||||
for ((className, registeredCordapps) in alreadyRegistered) {
|
||||
if (registeredCordapps.any { it.jarHash == cordapp.jarHash }) continue
|
||||
if (duplicateRegistrationFilter.shouldNotify(className, classLoader)) {
|
||||
logger.warn("More than one CorDapp registered for $className.")
|
||||
if (className in contractClasses) {
|
||||
logger.warnOnce("More than one CorDapp registered for contract $className.")
|
||||
}
|
||||
cordappClasses[className] = registeredCordapps + cordapp
|
||||
}
|
||||
@ -86,23 +86,4 @@ object CordappResolver {
|
||||
internal fun 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()
|
||||
}
|
||||
|
||||
private val classLoader = ClassLoader.getSystemClassLoader()
|
||||
|
||||
@Test
|
||||
fun `the correct cordapp resolver is used after calling withCordappInfo`() {
|
||||
val defaultTargetVersion = 222
|
||||
@ -24,7 +22,7 @@ class CordappResolverTest {
|
||||
contractClassNames = listOf(javaClass.name),
|
||||
minimumPlatformVersion = 3,
|
||||
targetPlatformVersion = defaultTargetVersion
|
||||
), classLoader)
|
||||
))
|
||||
assertEquals(defaultTargetVersion, CordappResolver.currentTargetVersion)
|
||||
|
||||
val expectedTargetVersion = 555
|
||||
@ -41,12 +39,12 @@ class CordappResolverTest {
|
||||
contractClassNames = listOf(javaClass.name),
|
||||
minimumPlatformVersion = 3,
|
||||
targetPlatformVersion = 222
|
||||
), classLoader)
|
||||
))
|
||||
CordappResolver.register(CordappImpl.TEST_INSTANCE.copy(
|
||||
contractClassNames = listOf(javaClass.name),
|
||||
minimumPlatformVersion = 2,
|
||||
targetPlatformVersion = 456
|
||||
), classLoader)
|
||||
))
|
||||
assertThat(CordappResolver.currentCordapp).isNotNull()
|
||||
}
|
||||
|
||||
@ -57,13 +55,13 @@ class CordappResolverTest {
|
||||
minimumPlatformVersion = 3,
|
||||
targetPlatformVersion = 222,
|
||||
jarHash = SecureHash.randomSHA256()
|
||||
), classLoader)
|
||||
))
|
||||
CordappResolver.register(CordappImpl.TEST_INSTANCE.copy(
|
||||
contractClassNames = listOf(javaClass.name),
|
||||
minimumPlatformVersion = 2,
|
||||
targetPlatformVersion = 456,
|
||||
jarHash = SecureHash.randomSHA256()
|
||||
), classLoader)
|
||||
))
|
||||
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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user