Skip to content

Commit

Permalink
Merge pull request #6 from svt/spring-boot-3
Browse files Browse the repository at this point in the history
Use Freeportfinder instead of SocketUtils that has been removed from …
  • Loading branch information
Lunkers committed Feb 8, 2023
2 parents 8588162 + c6e7883 commit a294b52
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConfigurableApplicationContext> {
Expand All @@ -32,10 +32,10 @@ class RandomPortPropertySource(name: String) : PropertySource<Any>(name) {
val ports = ConcurrentHashMap<String, Int>()

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 {
Expand Down

0 comments on commit a294b52

Please sign in to comment.