From 86b5b7299ad97fe21111c30f319f1b301c36bb52 Mon Sep 17 00:00:00 2001 From: Clinton Alexander Date: Mon, 13 Jun 2016 15:30:26 +0100 Subject: [PATCH] Moved node directory creation into the abstract node. --- .../com/r3corda/node/internal/AbstractNode.kt | 8 +++++ .../kotlin/com/r3corda/node/internal/Node.kt | 35 ++++++++++--------- .../kotlin/com/r3corda/demos/RateFixDemo.kt | 5 --- .../kotlin/com/r3corda/demos/TraderDemo.kt | 8 +---- 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/node/src/main/kotlin/com/r3corda/node/internal/AbstractNode.kt b/node/src/main/kotlin/com/r3corda/node/internal/AbstractNode.kt index 3bb2c963c1..76a41713ae 100644 --- a/node/src/main/kotlin/com/r3corda/node/internal/AbstractNode.kt +++ b/node/src/main/kotlin/com/r3corda/node/internal/AbstractNode.kt @@ -121,6 +121,8 @@ abstract class AbstractNode(val dir: Path, val configuration: NodeConfiguration, require(!started) { "Node has already been started" } log.info("Node starting up ...") + createNodeDir() + val storageServices = initialiseStorageService(dir) storage = storageServices.first checkpointStorage = storageServices.second @@ -316,4 +318,10 @@ abstract class AbstractNode(val dir: Path, val configuration: NodeConfiguration, } return NodeAttachmentService(attachmentsDir, services.monitoringService.metrics) } + + private fun createNodeDir() { + if (!Files.exists(dir)) { + Files.createDirectories(dir) + } + } } diff --git a/node/src/main/kotlin/com/r3corda/node/internal/Node.kt b/node/src/main/kotlin/com/r3corda/node/internal/Node.kt index 7ce76b0e18..264984df83 100644 --- a/node/src/main/kotlin/com/r3corda/node/internal/Node.kt +++ b/node/src/main/kotlin/com/r3corda/node/internal/Node.kt @@ -26,6 +26,7 @@ import org.glassfish.jersey.servlet.ServletContainer import java.io.RandomAccessFile import java.lang.management.ManagementFactory import java.nio.channels.FileLock +import java.nio.file.Files import java.nio.file.Path import java.time.Clock import javax.management.ObjectName @@ -164,22 +165,24 @@ class Node(dir: Path, val p2pAddr: HostAndPort, configuration: NodeConfiguration // file that we'll do our best to delete on exit. But if we don't, it'll be overwritten next time. If it already // exists, we try to take the file lock first before replacing it and if that fails it means we're being started // twice with the same directory: that's a user error and we should bail out. - val pidPath = dir.resolve("process-id") - val file = pidPath.toFile() - if (!file.exists()) { - file.createNewFile() + if(Files.exists(dir)) { + val pidPath = dir.resolve("process-id") + val file = pidPath.toFile() + if (!file.exists()) { + file.createNewFile() + } + file.deleteOnExit() + val f = RandomAccessFile(file, "rw") + val l = f.channel.tryLock() + if (l == null) { + println("It appears there is already a node running with the specified data directory $dir") + println("Shut that other node down and try again. It may have process ID ${file.readText()}") + System.exit(1) + } + nodeFileLock = l + val ourProcessID: String = ManagementFactory.getRuntimeMXBean().name.split("@")[0] + f.setLength(0) + f.write(ourProcessID.toByteArray()) } - file.deleteOnExit() - val f = RandomAccessFile(file, "rw") - val l = f.channel.tryLock() - if (l == null) { - println("It appears there is already a node running with the specified data directory $dir") - println("Shut that other node down and try again. It may have process ID ${file.readText()}") - System.exit(1) - } - nodeFileLock = l - val ourProcessID: String = ManagementFactory.getRuntimeMXBean().name.split("@")[0] - f.setLength(0) - f.write(ourProcessID.toByteArray()) } } diff --git a/src/main/kotlin/com/r3corda/demos/RateFixDemo.kt b/src/main/kotlin/com/r3corda/demos/RateFixDemo.kt index 74d57a34c4..49fd466de0 100644 --- a/src/main/kotlin/com/r3corda/demos/RateFixDemo.kt +++ b/src/main/kotlin/com/r3corda/demos/RateFixDemo.kt @@ -50,12 +50,7 @@ fun main(args: Array) { // Suppress the Artemis MQ noise, and activate the demo logging. BriefLogFormatter.initVerbose("+demo.ratefix", "-org.apache.activemq") - // TODO: Move this into the AbstractNode class. val dir = Paths.get(options.valueOf(dirArg)) - if (!Files.exists(dir)) { - Files.createDirectory(dir) - } - val networkMapAddr = ArtemisMessagingService.makeRecipient(options.valueOf(networkMapAddrArg)) val networkMapIdentity = Files.readAllBytes(Paths.get(options.valueOf(networkMapIdentityArg))).deserialize() val networkMapAddress = NodeInfo(networkMapAddr, networkMapIdentity) diff --git a/src/main/kotlin/com/r3corda/demos/TraderDemo.kt b/src/main/kotlin/com/r3corda/demos/TraderDemo.kt index 63aec87b2f..76cd298687 100644 --- a/src/main/kotlin/com/r3corda/demos/TraderDemo.kt +++ b/src/main/kotlin/com/r3corda/demos/TraderDemo.kt @@ -95,7 +95,7 @@ fun main(args: Array) { // for protocols will change in future. BriefLogFormatter.initVerbose("+demo.buyer", "+demo.seller", "-org.apache.activemq") - val directory = setupDirectory(role) + val directory = Paths.get(DIRNAME, role.name.toLowerCase()) // Override the default config file (which you can find in the file "reference.conf") to give each node a name. val config = run { @@ -141,12 +141,6 @@ fun main(args: Array) { } } -fun setupDirectory(mode: Role): Path { - val directory = Paths.get(DIRNAME, mode.name.toLowerCase()) - Files.createDirectories(directory) - return directory -} - fun parseOptions(args: Array, parser: OptionParser): OptionSet { try { return parser.parse(*args)