Guest User

Untitled

a guest
Mar 8th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. package yourpackage
  2.  
  3. import net.corda.node.internal.StartedNode
  4. import net.corda.testing.node.MockNetwork
  5. import org.h2.tools.Server
  6.  
  7. class H2Server(nodes: MockNetwork.BasketOfNodes, private val port : Int = 9092) {
  8. companion object {
  9. private val H2_NAME_RE = "^jdbc:h2:(mem:[^;:]+).*$".toRegex()
  10. }
  11.  
  12. private val server: Server = org.h2.tools.Server.createTcpServer("-tcpPort", port.toString(), "-tcpAllowOthers").start()
  13.  
  14. init {
  15. nodes.notaryNode.writeJDBCEndpoint()
  16. nodes.partyNodes.forEach { it.writeJDBCEndpoint() }
  17. }
  18.  
  19. /**
  20. * Block the current thread so that we can connect and examine the database
  21. */
  22. fun block() {
  23. val lock = java.lang.Object()
  24. synchronized(lock) {
  25. lock.wait()
  26. }
  27. }
  28.  
  29. fun stop() {
  30. server.stop()
  31. }
  32.  
  33. private fun StartedNode<MockNetwork.MockNode>.writeJDBCEndpoint() {
  34. val url = this.database.dataSource.dataSourceProperties.getProperty("url")
  35. val databaseName = H2_NAME_RE.matchEntire(url)?.groupValues?.get(1) ?: throw RuntimeException("could not extract db name from $url")
  36. println("Database for ${this.info.legalIdentities.first().name.organisation} database: jdbc:h2:tcp://localhost:$port/$databaseName")
  37. }
  38. }
Add Comment
Please, Sign In to add comment