CORDA-2434 - CordaRPCClient now only has c'tors rather a mix of c'tors and factory methods (#4569) (#4578)

(cherry picked from commit 24a7821dce)
This commit is contained in:
Shams Asari 2019-01-16 19:49:32 +00:00 committed by Katelyn Baker
parent 6bced399d7
commit 3b1929371c
9 changed files with 32 additions and 68 deletions

View File

@ -6568,10 +6568,6 @@ public final class net.corda.client.rpc.CordaRPCClient extends java.lang.Object
public static final net.corda.client.rpc.CordaRPCClient$Companion Companion
##
public static final class net.corda.client.rpc.CordaRPCClient$Companion extends java.lang.Object
@NotNull
public final net.corda.client.rpc.CordaRPCClient createWithSsl(java.util.List<net.corda.core.utilities.NetworkHostAndPort>, net.corda.core.messaging.ClientRpcSslOptions, net.corda.client.rpc.CordaRPCClientConfiguration)
@NotNull
public final net.corda.client.rpc.CordaRPCClient createWithSsl(net.corda.core.utilities.NetworkHostAndPort, net.corda.core.messaging.ClientRpcSslOptions, net.corda.client.rpc.CordaRPCClientConfiguration)
##
public class net.corda.client.rpc.CordaRPCClientConfiguration extends java.lang.Object
public <init>(java.time.Duration)

View File

@ -1,6 +1,5 @@
package net.corda.client.rpc
import net.corda.client.rpc.internal.createCordaRPCClientWithSslAndClassLoader
import net.corda.core.context.*
import net.corda.core.contracts.FungibleAsset
import net.corda.core.crypto.random63BitValue
@ -269,7 +268,7 @@ class CordaRPCClientTest : NodeBasedTest(listOf("net.corda.finance"), notaries =
val address = NetworkHostAndPort.parse(args[0])
val financeClassLoader = URLClassLoader(arrayOf(Paths.get(args[1]).toUri().toURL()))
val rpcUser = CordaRPCClientTest.rpcUser
val client = createCordaRPCClientWithSslAndClassLoader(address, classLoader = financeClassLoader)
val client = CordaRPCClient(address, classLoader = financeClassLoader)
val state = client.use(rpcUser.username, rpcUser.password) {
// financeClassLoader should be allowing the Cash.State to materialise
@Suppress("DEPRECATION")

View File

@ -23,6 +23,7 @@ import net.corda.serialization.internal.amqp.SerializationFactoryCacheKey
import net.corda.serialization.internal.amqp.SerializerFactory
import java.time.Duration
import java.util.ServiceLoader
import java.net.URLClassLoader
/**
* This class is essentially just a wrapper for an RPCConnection<CordaRPCOps> and can be treated identically.
@ -251,11 +252,11 @@ open class CordaRPCClientConfiguration @JvmOverloads constructor(
* a classloader will need to be provided that contains the associated CorDapp jars.
*/
class CordaRPCClient private constructor(
private val hostAndPort: NetworkHostAndPort,
private val hostAndPort: NetworkHostAndPort?,
private val haAddressPool: List<NetworkHostAndPort>,
private val configuration: CordaRPCClientConfiguration = CordaRPCClientConfiguration.DEFAULT,
private val sslConfiguration: ClientRpcSslOptions? = null,
private val classLoader: ClassLoader? = null,
private val haAddressPool: List<NetworkHostAndPort> = emptyList()
private val classLoader: ClassLoader? = null
) {
@JvmOverloads
@ -273,41 +274,24 @@ class CordaRPCClient private constructor(
classLoader: ClassLoader? = null
) : this(hostAndPort = hostAndPort, haAddressPool = emptyList(), sslConfiguration = sslConfiguration, classLoader = classLoader)
/**
* @param haAddressPool A list of [NetworkHostAndPort] representing the addresses of servers in HA mode.
* The client will attempt to connect to a live server by trying each address in the list. If the servers are not in
* HA mode, the client will round-robin from the beginning of the list and try all servers.
* @param configuration An optional configuration used to tweak client behaviour.
*/
@JvmOverloads
constructor(haAddressPool: List<NetworkHostAndPort>, configuration: CordaRPCClientConfiguration = CordaRPCClientConfiguration.DEFAULT) : this(haAddressPool.first(), configuration, null, null, haAddressPool)
companion object {
fun createWithSsl(
constructor(
hostAndPort: NetworkHostAndPort,
sslConfiguration: ClientRpcSslOptions,
configuration: CordaRPCClientConfiguration = CordaRPCClientConfiguration.DEFAULT
): CordaRPCClient {
return CordaRPCClient(hostAndPort, configuration, sslConfiguration)
}
configuration: CordaRPCClientConfiguration,
sslConfiguration: ClientRpcSslOptions?,
classLoader: ClassLoader? = null
) : this(hostAndPort = hostAndPort, haAddressPool = emptyList(), configuration = configuration, sslConfiguration = sslConfiguration, classLoader = classLoader)
fun createWithSsl(
@JvmOverloads
constructor(
haAddressPool: List<NetworkHostAndPort>,
sslConfiguration: ClientRpcSslOptions,
configuration: CordaRPCClientConfiguration = CordaRPCClientConfiguration.DEFAULT
): CordaRPCClient {
return CordaRPCClient(haAddressPool.first(), configuration, sslConfiguration, haAddressPool = haAddressPool)
}
internal fun createWithSslAndClassLoader(
hostAndPort: NetworkHostAndPort,
configuration: CordaRPCClientConfiguration = CordaRPCClientConfiguration.DEFAULT,
sslConfiguration: ClientRpcSslOptions? = null,
classLoader: ClassLoader? = null
): CordaRPCClient {
return CordaRPCClient(hostAndPort, configuration, sslConfiguration, classLoader = classLoader)
}
}
) : this(hostAndPort = null, haAddressPool = haAddressPool, configuration = configuration, sslConfiguration = sslConfiguration, classLoader = classLoader)
// Here to keep the keep ABI compatibility happy
companion object {}
init {
try {
@ -334,7 +318,7 @@ class CordaRPCClient private constructor(
return when {
// Client->RPC broker
haAddressPool.isEmpty() -> RPCClient(
rpcConnectorTcpTransport(hostAndPort, config = sslConfiguration),
rpcConnectorTcpTransport(hostAndPort!!, config = sslConfiguration),
configuration,
if (classLoader != null) AMQP_RPC_CLIENT_CONTEXT.withClassLoader(classLoader) else AMQP_RPC_CLIENT_CONTEXT)
else -> {

View File

@ -1,14 +0,0 @@
package net.corda.client.rpc.internal
import net.corda.client.rpc.CordaRPCClient
import net.corda.client.rpc.CordaRPCClientConfiguration
import net.corda.core.utilities.NetworkHostAndPort
import net.corda.core.messaging.ClientRpcSslOptions
/** Utility which exposes the internal Corda RPC constructor to other internal Corda components */
fun createCordaRPCClientWithSslAndClassLoader(
hostAndPort: NetworkHostAndPort,
configuration: CordaRPCClientConfiguration = CordaRPCClientConfiguration.DEFAULT,
sslConfiguration: ClientRpcSslOptions? = null,
classLoader: ClassLoader? = null
) = CordaRPCClient.createWithSslAndClassLoader(hostAndPort, configuration, sslConfiguration, classLoader)

View File

@ -440,8 +440,6 @@ Wire security
``CordaRPCClient`` has an optional constructor parameter of type ``ClientRpcSslOptions``, defaulted to ``null``, which allows
communication with the node using SSL. Default ``null`` value means no SSL used in the context of RPC.
To use this feature, the ``CordaRPCClient`` object provides a static factory method ``createWithSsl``.
In order for this to work, the client needs to provide a truststore containing a certificate received from the node admin.
(The Node does not expect the RPC client to present a certificate, as the client already authenticates using the mechanism described above.)

View File

@ -47,7 +47,7 @@ class RpcSslTest {
driver(DriverParameters(startNodesInProcess = true, notarySpecs = emptyList())) {
val node = startNode(rpcUsers = listOf(user), customOverrides = brokerSslOptions.useSslRpcOverrides()).getOrThrow()
val client = CordaRPCClient.createWithSsl(node.rpcAddress, sslConfiguration = clientSslOptions)
val client = CordaRPCClient(node.rpcAddress, sslConfiguration = clientSslOptions)
val connection = client.start(user.username, user.password)
connection.proxy.apply {
@ -58,7 +58,7 @@ class RpcSslTest {
connection.close()
Assertions.assertThatThrownBy {
val connection2 = CordaRPCClient.createWithSsl(node.rpcAddress, sslConfiguration = clientSslOptions).start(user.username, "wrong")
val connection2 = CordaRPCClient(node.rpcAddress, sslConfiguration = clientSslOptions).start(user.username, "wrong")
connection2.proxy.apply {
nodeInfo()
failedLogin = true
@ -86,7 +86,7 @@ class RpcSslTest {
driver(DriverParameters(startNodesInProcess = true, notarySpecs = emptyList())) {
val node = startNode(rpcUsers = listOf(user), customOverrides = brokerSslOptions.useSslRpcOverrides()).getOrThrow()
Assertions.assertThatThrownBy {
val connection = CordaRPCClient.createWithSsl(node.rpcAddress, sslConfiguration = clientSslOptions).start(user.username, user.password)
val connection = CordaRPCClient(node.rpcAddress, sslConfiguration = clientSslOptions).start(user.username, user.password)
connection.proxy.apply {
nodeInfo()
successful = true
@ -125,7 +125,7 @@ class RpcSslTest {
driver(DriverParameters(startNodesInProcess = true, notarySpecs = emptyList())) {
val node = startNode(customOverrides = brokerSslOptions.useSslRpcOverrides()).getOrThrow()
val client = CordaRPCClient.createWithSsl(node.rpcAddress, sslConfiguration = clientSslOptions)
val client = CordaRPCClient(node.rpcAddress, sslConfiguration = clientSslOptions)
Assertions.assertThatThrownBy {
client.start(NODE_RPC_USER, NODE_RPC_USER).use { connection ->
@ -133,7 +133,7 @@ class RpcSslTest {
}
}.isInstanceOf(ActiveMQException::class.java)
val clientAdmin = CordaRPCClient.createWithSsl(node.rpcAdminAddress, sslConfiguration = clientSslOptions)
val clientAdmin = CordaRPCClient(node.rpcAdminAddress, sslConfiguration = clientSslOptions)
Assertions.assertThatThrownBy {
clientAdmin.start(NODE_RPC_USER, NODE_RPC_USER).use { connection ->

View File

@ -5,7 +5,8 @@ import com.typesafe.config.Config
import com.typesafe.config.ConfigFactory
import com.typesafe.config.ConfigRenderOptions
import com.typesafe.config.ConfigValueFactory
import net.corda.client.rpc.internal.createCordaRPCClientWithSslAndClassLoader
import net.corda.client.rpc.CordaRPCClient
import net.corda.client.rpc.CordaRPCClientConfiguration
import net.corda.cliutils.CommonCliConstants.BASE_DIR
import net.corda.core.concurrent.CordaFuture
import net.corda.core.concurrent.firstOf
@ -164,7 +165,7 @@ class DriverDSLImpl(
private fun establishRpc(config: NodeConfig, processDeathFuture: CordaFuture<out Process>): CordaFuture<CordaRPCOps> {
val rpcAddress = config.corda.rpcOptions.address
val clientRpcSslOptions = clientSslOptionsCompatibleWith(config.corda.rpcOptions)
val client = createCordaRPCClientWithSslAndClassLoader(rpcAddress, sslConfiguration = clientRpcSslOptions)
val client = CordaRPCClient(rpcAddress, sslConfiguration = clientRpcSslOptions)
val connectionFuture = poll(executorService, "RPC connection") {
try {
config.corda.rpcUsers[0].run { client.start(username, password) }

View File

@ -8,10 +8,10 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator
import net.corda.client.jackson.JacksonSupport
import net.corda.client.jackson.StringToMethodCallParser
import net.corda.client.rpc.CordaRPCClient
import net.corda.client.rpc.CordaRPCClientConfiguration
import net.corda.client.rpc.CordaRPCConnection
import net.corda.client.rpc.PermissionException
import net.corda.client.rpc.internal.createCordaRPCClientWithSslAndClassLoader
import net.corda.core.CordaException
import net.corda.core.concurrent.CordaFuture
import net.corda.core.contracts.UniqueIdentifier
@ -95,7 +95,7 @@ object InteractiveShell {
*/
fun startShell(configuration: ShellConfiguration, classLoader: ClassLoader? = null) {
rpcOps = { username: String, credentials: String ->
val client = createCordaRPCClientWithSslAndClassLoader(hostAndPort = configuration.hostAndPort,
val client = CordaRPCClient(hostAndPort = configuration.hostAndPort,
configuration = CordaRPCClientConfiguration.DEFAULT.copy(
maxReconnectAttempts = 1
),

View File

@ -199,7 +199,7 @@ class NodeWebServer(val config: WebServerConfig) {
private fun connectLocalRpcAsNodeUser(): CordaRPCOps {
log.info("Connecting to node at ${config.rpcAddress} as ${config.runAs}")
val client = CordaRPCClient(config.rpcAddress, classLoader = javaClass.classLoader)
val client = CordaRPCClient(hostAndPort = config.rpcAddress, classLoader = javaClass.classLoader)
val connection = client.start(config.runAs.username, config.runAs.password)
return connection.proxy
}