From c6e78830704285633837fca5ee46a33631be6bf3 Mon Sep 17 00:00:00 2001 From: Finn Hermansson Date: Wed, 8 Feb 2023 10:18:15 +0100 Subject: [PATCH] Use Freeportfinder instead of SocketUtils that has been removed from spring. Signed-off-by: Finn Hermansson --- build.gradle.kts | 1 + .../svt/oss/randomportinitializer/RandomPortInitializer.kt | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 0eaace4..5af8c5d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -38,6 +38,7 @@ kotlin { dependencies { api(kotlin("reflect")) api("org.springframework:spring-context:5.3.20") + implementation("me.alexpanov:free-port-finder:1.1.1") testImplementation("org.assertj:assertj-core:3.22.0") testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.2") diff --git a/src/main/kotlin/se/svt/oss/randomportinitializer/RandomPortInitializer.kt b/src/main/kotlin/se/svt/oss/randomportinitializer/RandomPortInitializer.kt index 9fffa94..d2565f8 100644 --- a/src/main/kotlin/se/svt/oss/randomportinitializer/RandomPortInitializer.kt +++ b/src/main/kotlin/se/svt/oss/randomportinitializer/RandomPortInitializer.kt @@ -4,11 +4,11 @@ package se.svt.oss.randomportinitializer +import me.alexpanov.net.FreePortFinder import org.springframework.context.ApplicationContextInitializer import org.springframework.context.ConfigurableApplicationContext import org.springframework.core.env.PropertySource import org.springframework.core.env.StandardEnvironment -import org.springframework.util.SocketUtils import java.util.concurrent.ConcurrentHashMap class RandomPortInitializer : ApplicationContextInitializer { @@ -32,10 +32,10 @@ class RandomPortPropertySource(name: String) : PropertySource(name) { val ports = ConcurrentHashMap() override fun getProperty(name: String): Any? { - if (! this.isRandomPortProperty(name)) { + if (!this.isRandomPortProperty(name)) { return null } - return ports.computeIfAbsent(name) { _ -> SocketUtils.findAvailableTcpPort() } + return ports.computeIfAbsent(name) { _ -> FreePortFinder.findFreeLocalPort() } } override fun containsProperty(name: String): Boolean {