in cassandra-launcher/src/main/scala/org/apache/pekko/persistence/cassandra/testkit/CassandraLauncher.scala [201:301]
def start(cassandraDirectory: File, configResource: String, clean: Boolean, port: Int): Unit =
start(cassandraDirectory, configResource, clean, port, Nil)
/**
* Start Cassandra
*
* @param cassandraDirectory the data directory to use
* @param configResource yaml configuration loaded from classpath,
* default configuration for testing is defined in [[CassandraLauncher#DefaultTestConfigResource]]
* @param clean if `true` all files in the data directory will be deleted
* before starting Cassandra
* @param port the `native_transport_port` to use, if 0 a random
* free port is used, which can be retrieved (before starting)
* with [[CassandraLauncher.randomPort]].
* @param classpath Any additional jars/directories to add to the classpath. Use
* [[CassandraLauncher#classpathForResources]] to assist in calculating this.
* @throws org.apache.pekko.persistence.cassandra.testkit.CassandraLauncher.CleanFailedException if `clean`
* is `true` and removal of the directory fails
*/
def start(
cassandraDirectory: File,
configResource: String,
clean: Boolean,
port: Int,
classpath: immutable.Seq[String]): Unit =
start(cassandraDirectory, configResource, clean, port, classpath, None)
/**
* Start Cassandra
*
* @param cassandraDirectory the data directory to use
* @param configResource yaml configuration loaded from classpath,
* default configuration for testing is defined in [[CassandraLauncher#DefaultTestConfigResource]]
* @param clean if `true` all files in the data directory will be deleted
* before starting Cassandra
* @param port the `native_transport_port` to use, if 0 a random
* free port is used, which can be retrieved (before starting)
* with [[CassandraLauncher.randomPort]].
* @param classpath Any additional jars/directories to add to the classpath. Use
* [[CassandraLauncher#classpathForResources]] to assist in calculating this.
* @param host the host to bind the embeded Cassandra to. If None, then 127.0.0.1 is used.
* @throws org.apache.pekko.persistence.cassandra.testkit.CassandraLauncher.CleanFailedException if `clean`
* is `true` and removal of the directory fails
*/
def start(
cassandraDirectory: File,
configResource: String,
clean: Boolean,
port: Int,
classpath: immutable.Seq[String],
host: Option[String]): Unit = this.synchronized {
if (cassandraDaemon.isEmpty) {
prepareCassandraDirectory(cassandraDirectory, clean)
val realHost = host.getOrElse(DEFAULT_HOST)
// NOTE: read comments bellow to get the full picture
if (port != 0) {
// if user explicitly passes a port, we should use it and override `selectedPorts`
// selectedPorts may have been already set if user has previously called `randomPort`
// in such a case, randomPort will be fixed to a 'wrong' old number
selectedPorts.set(selectFreePorts(realHost, port))
} else {
// if a random port is requested, we only override `selectedPorts` if not yet calculated (eg: default (0,0)).
// If user has previously called `randomPort`, we will already have a value and we should keep using it.
selectedPorts.compareAndSet(initialPortsValue, selectFreePorts(realHost, port))
}
val (realPort, storagePort) = selectedPorts.get()
println(
s"Starting Cassandra on port client port: $realPort storage port $storagePort host $realHost java version ${System
.getProperty("java.runtime.version")}")
// http://wiki.apache.org/cassandra/StorageConfiguration
val conf = readResource(configResource)
val amendedConf = conf
.replace("$PORT", realPort.toString)
.replace("$STORAGE_PORT", storagePort.toString)
.replace("$DIR", cassandraDirectory.getAbsolutePath)
.replace("$HOST", realHost)
val configFile = new File(cassandraDirectory, configResource)
writeToFile(configFile, amendedConf)
// Extract the cassandra bundle to the directory
val cassandraBundleFile =
new File(cassandraDirectory, "cassandra-bundle.jar")
if (!cassandraBundleFile.exists()) {
val is =
this.getClass.getClassLoader.getResourceAsStream("pekko/persistence/cassandra/launcher/cassandra-bundle.jar")
try {
Files.copy(is, cassandraBundleFile.toPath)
} finally {
if (is != null) is.close()
}
}
startForked(configFile, cassandraBundleFile, classpath, realHost, realPort)
}
}