From 33670d62b16c58e02f393f050ad1b40093351925 Mon Sep 17 00:00:00 2001 From: Shams Asari Date: Mon, 31 Dec 2018 13:15:29 +0000 Subject: [PATCH] Closing the app class loader on node shutdown (#4479) --- .../net/corda/node/cordapp/CordappLoader.kt | 2 +- .../net/corda/node/internal/AbstractNode.kt | 4 ++-- .../cordapp/JarScanningCordappLoader.kt | 22 +++++++++---------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/node/src/main/kotlin/net/corda/node/cordapp/CordappLoader.kt b/node/src/main/kotlin/net/corda/node/cordapp/CordappLoader.kt index 8a8feffc2d..2ecf6dc9d8 100644 --- a/node/src/main/kotlin/net/corda/node/cordapp/CordappLoader.kt +++ b/node/src/main/kotlin/net/corda/node/cordapp/CordappLoader.kt @@ -8,7 +8,7 @@ import net.corda.core.schemas.MappedSchema /** * Handles loading [Cordapp]s. */ -interface CordappLoader { +interface CordappLoader : AutoCloseable { /** * Returns all [Cordapp]s found. diff --git a/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt b/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt index c2c5c49207..0dea38f8ef 100644 --- a/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt +++ b/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt @@ -145,7 +145,7 @@ abstract class AbstractNode(val configuration: NodeConfiguration, } } - val cordappLoader: CordappLoader = makeCordappLoader(configuration, versionInfo) + val cordappLoader: CordappLoader = makeCordappLoader(configuration, versionInfo).closeOnStop() val schemaService = NodeSchemaService(cordappLoader.cordappSchemas).tokenize() val identityService = PersistentIdentityService(cacheFactory).tokenize() val database: CordaPersistence = createCordaPersistence( @@ -544,7 +544,7 @@ abstract class AbstractNode(val configuration: NodeConfiguration, generatedCordapps += VirtualCordapp.generateSimpleNotaryCordapp(versionInfo) } val blacklistedKeys = if (configuration.devMode) emptyList() - else configuration.cordappSignerKeyFingerprintBlacklist.mapNotNull { + else configuration.cordappSignerKeyFingerprintBlacklist.map { try { SecureHash.parse(it) } catch (e: IllegalArgumentException) { diff --git a/node/src/main/kotlin/net/corda/node/internal/cordapp/JarScanningCordappLoader.kt b/node/src/main/kotlin/net/corda/node/internal/cordapp/JarScanningCordappLoader.kt index 4a319df1eb..d27134880b 100644 --- a/node/src/main/kotlin/net/corda/node/internal/cordapp/JarScanningCordappLoader.kt +++ b/node/src/main/kotlin/net/corda/node/internal/cordapp/JarScanningCordappLoader.kt @@ -43,13 +43,6 @@ class JarScanningCordappLoader private constructor(private val cordappJarPaths: private val versionInfo: VersionInfo = VersionInfo.UNKNOWN, extraCordapps: List, private val signerKeyFingerprintBlacklist: List = emptyList()) : CordappLoaderTemplate() { - - override val cordapps: List by lazy { - loadCordapps() + extraCordapps - } - - override val appClassLoader: ClassLoader = URLClassLoader(cordappJarPaths.stream().map { it.url }.toTypedArray(), javaClass.classLoader) - init { if (cordappJarPaths.isEmpty()) { logger.info("No CorDapp paths provided") @@ -58,6 +51,12 @@ class JarScanningCordappLoader private constructor(private val cordappJarPaths: } } + override val cordapps: List by lazy { loadCordapps() + extraCordapps } + + override val appClassLoader: URLClassLoader = URLClassLoader(cordappJarPaths.stream().map { it.url }.toTypedArray(), javaClass.classLoader) + + override fun close() = appClassLoader.close() + companion object { private val logger = contextLogger() @@ -260,7 +259,10 @@ class JarScanningCordappLoader private constructor(private val cordappJarPaths: } private fun findPlugins(cordappJarPath: RestrictedURL): List { - return ServiceLoader.load(SerializationWhitelist::class.java, URLClassLoader(arrayOf(cordappJarPath.url), appClassLoader)).toList().filter { + val whitelists = URLClassLoader(arrayOf(cordappJarPath.url), appClassLoader).use { + ServiceLoader.load(SerializationWhitelist::class.java, it).toList() + } + return whitelists.filter { it.javaClass.location == cordappJarPath.url && it.javaClass.name.startsWith(cordappJarPath.qualifiedNamePrefix) } + DefaultWhitelist // Always add the DefaultWhitelist to the whitelist for an app. } @@ -378,8 +380,4 @@ abstract class CordappLoaderTemplate : CordappLoader { override val cordappSchemas: Set by lazy { cordapps.flatMap { it.customSchemas }.toSet() } - - override val appClassLoader: ClassLoader by lazy { - URLClassLoader(cordapps.stream().map { it.jarPath }.toTypedArray(), javaClass.classLoader) - } }