From 5233c50098625c28202f4b80bd284b81c9c1ea05 Mon Sep 17 00:00:00 2001 From: Andrzej Cichocki Date: Fri, 23 Jun 2017 10:17:49 +0100 Subject: [PATCH] More specific message when a corda service fails to instantiate. (#901) --- .../net/corda/node/internal/AbstractNode.kt | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) 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 8c1a680dea..c0d7053e62 100644 --- a/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt +++ b/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt @@ -65,6 +65,7 @@ import org.jetbrains.exposed.sql.Database import org.slf4j.Logger import rx.Observable import java.io.IOException +import java.lang.reflect.InvocationTargetException import java.lang.reflect.Modifier.* import java.net.JarURLConnection import java.net.URI @@ -273,6 +274,8 @@ abstract class AbstractNode(open val configuration: NodeConfiguration, return this } + private class ServiceInstantiationException(cause: Throwable?) : Exception(cause) + private fun installCordaServices(scanResult: ScanResult) { fun getServiceType(clazz: Class<*>): ServiceType? { return try { @@ -300,6 +303,8 @@ abstract class AbstractNode(open val configuration: NodeConfiguration, } catch (e: NoSuchMethodException) { log.error("${it.name}, as a Corda service, must have a constructor with a single parameter " + "of type ${PluginServiceHub::class.java.name}") + } catch (e: ServiceInstantiationException) { + log.error("Corda service ${it.name} failed to instantiate", e.cause) } catch (e: Exception) { log.error("Unable to install Corda service ${it.name}", e) } @@ -310,13 +315,17 @@ abstract class AbstractNode(open val configuration: NodeConfiguration, * Use this method to install your Corda services in your tests. This is automatically done by the node when it * starts up for all classes it finds which are annotated with [CordaService]. */ - fun installCordaService(clazz: Class): T { - clazz.requireAnnotation() - val ctor = clazz.getDeclaredConstructor(PluginServiceHub::class.java).apply { isAccessible = true } - val service = ctor.newInstance(services) - cordappServices.putInstance(clazz, service) + fun installCordaService(serviceClass: Class): T { + serviceClass.requireAnnotation() + val constructor = serviceClass.getDeclaredConstructor(PluginServiceHub::class.java).apply { isAccessible = true } + val service = try { + constructor.newInstance(services) + } catch (e: InvocationTargetException) { + throw ServiceInstantiationException(e.cause) + } + cordappServices.putInstance(serviceClass, service) smm.tokenizableServices += service - log.info("Installed ${clazz.name} Corda service") + log.info("Installed ${serviceClass.name} Corda service") return service }