@@ -17,20 +17,32 @@ import java.nio.file.Files
1717import java.nio.file.Paths
1818import kotlin.concurrent.thread
1919
20+ /* *
21+ * Listener that prepares the environment so that CassandraUnit will work when it is initialized.
22+ * For instance, by finding free ports for the server to listen on.
23+ *
24+ * @author Abhijit Sarkar
25+ * @since 1.0.0
26+ */
2027@Order(Ordered .LOWEST_PRECEDENCE )
2128class CassandraUnitApplicationListener : ApplicationListener <ApplicationPreparedEvent > {
2229 private val log = LoggerFactory .getLogger(CassandraUnitApplicationListener ::class .java)
2330 private val prefix = " cassandra-unit"
2431
2532 override fun onApplicationEvent (event : ApplicationPreparedEvent ) {
2633 val env = event.applicationContext.environment
34+ // If the config is not found it means the AutoConfigureCassandraUnit hasn't been initialized.
2735 val config = env.getProperty(" $prefix .config" , String ::class .java) ? : return
36+ // Already running; get the ports from EmbeddedCassandraServerHelper.
2837 val cassandra = if (CassandraUnit .isRunning()) {
2938 mapOf (
3039 " $prefix .native-transport-port" to EmbeddedCassandraServerHelper .getNativeTransportPort(),
3140 " $prefix .rpc-port" to EmbeddedCassandraServerHelper .getRpcPort()
3241 )
3342 } else {
43+ // If using default config with known ports, parse the config for the ports.
44+ // If not using default config, assign a free port to whichever one is zero, and create a new
45+ // config file.
3446 val outFile = config.takeIf { it != EmbeddedCassandraServerHelper .DEFAULT_CASSANDRA_YML_FILE }
3547 ?.let { this .createTempFile(it) }
3648 discoverPorts(config, outFile).also {
@@ -64,13 +76,19 @@ class CassandraUnitApplicationListener : ApplicationListener<ApplicationPrepared
6476 var portName: String? = null
6577 Yaml ().parse(reader)
6678 .forEach {
79+ // If not a ScalarEvent, pass-through.
6780 if (it !is ScalarEvent ) emitter?.emit(it)
6881 else {
82+ // Try to get the port name and value from the event.
6983 val (name, value) = getPortNameAndValue(it, portName)
84+ // Existing port value is zero; new value assigned.
7085 if (value != null ) {
7186 cassandra[" $prefix .$portName " ] = value
7287 emitter?.emit(it.withValue(value))
73- } else emitter?.emit(it)
88+ }
89+ // No need to modify the event; either it isn't a port, or the existing
90+ // port value isn't zero.
91+ else emitter?.emit(it)
7492 portName = name
7593 }
7694 }
@@ -80,9 +98,12 @@ class CassandraUnitApplicationListener : ApplicationListener<ApplicationPrepared
8098 }
8199
82100 private fun getPortNameAndValue (event : ScalarEvent , portName : String? ): Pair <String ?, Int ?> {
101+ // Found a port key.
83102 if (portName == null && event.value.endsWith(" _port" )) {
84103 return event.value.replace(' _' , ' -' ) to null
85- } else if (portName != null ) {
104+ }
105+ // Found the port value corresponding to the key found previously.
106+ else if (portName != null ) {
86107 val port = if (event.value.matches(" 0+" .toRegex())) SocketUtils .findAvailableTcpPort()
87108 else event.value.toInt()
88109 return null to port
0 commit comments