diff --git a/server/src/main/java/server/PortChecker.java b/server/src/main/java/server/PortChecker.java new file mode 100644 index 0000000..01f0b44 --- /dev/null +++ b/server/src/main/java/server/PortChecker.java @@ -0,0 +1,51 @@ +package server; + +import java.io.IOException; +import java.net.ServerSocket; +import java.util.Scanner; + +public class PortChecker { + private static final int defaultPort = 8080; + private static final int lastValidPort = 65535; + + + /** + * Finds a free port to launch the program on. + * made with the help of https://medium.com/@rrlinus5/find-available-port-in-your-system-java-f532ee48c5b3 + * @return the port that will be used to launch the program + * @throws IOException when no available port is found + */ + public int findFreePort() throws IOException { + if(isPortAvailable(defaultPort)){ + return defaultPort; + } + + System.out.println("Please enter the port number you want to use"); + Scanner portScanner = new Scanner(System.in); + while(true){ + int port = portScanner.nextInt(); + + if(port > 0 && port <=lastValidPort){ //range of valid ports + isPortAvailable(port); + return port; + } + else { + System.out.println("Try a different port:"); + } + } + } + + /** + * Checks whether a port is actually available. + * @param port the number of the port that's being checker + * @return whether the port was available + */ + public boolean isPortAvailable(int port){ + try(ServerSocket socket = new ServerSocket(port)){ + return true; + } + catch(IOException i) { + return false; + } + } +} \ No newline at end of file diff --git a/server/src/main/java/server/ServerPortCustomizer.java b/server/src/main/java/server/ServerPortCustomizer.java new file mode 100644 index 0000000..9524cbf --- /dev/null +++ b/server/src/main/java/server/ServerPortCustomizer.java @@ -0,0 +1,27 @@ +package server; + +import org.springframework.boot.web.server.ConfigurableWebServerFactory; +import org.springframework.boot.web.server.WebServerFactoryCustomizer; +import org.springframework.stereotype.Component; + +@Component +public class ServerPortCustomizer + implements WebServerFactoryCustomizer{ + /** + * Changes the port that will be used to launch the program. + * made with the help of: https://www.baeldung.com/spring-boot-change-port + * @param factory + */ + @Override + public void customize(ConfigurableWebServerFactory factory) { + try { + PortChecker portChecker = new PortChecker(); + int port = portChecker.findFreePort(); + factory.setPort(port); + System.out.println("Server starts on port: " + port); + } + catch (Exception e) { + throw new RuntimeException("Failed to find a free port", e); + } + } +} \ No newline at end of file diff --git a/server/src/test/java/server/PortCheckerTest.java b/server/src/test/java/server/PortCheckerTest.java new file mode 100644 index 0000000..2843d99 --- /dev/null +++ b/server/src/test/java/server/PortCheckerTest.java @@ -0,0 +1,61 @@ +package server; + +import org.junit.jupiter.api.Test; +import java.io.IOException; +import java.net.ServerSocket; + +import static org.junit.jupiter.api.Assertions.*; + +class PortCheckerTest { + @Test + void portAvailable() { + PortChecker checker = new PortChecker(); + + int port = 0; //will find a random available port + boolean available = checker.isPortAvailable(port); + + assertTrue(available); + } + @Test + void portNotAvailable(){ + try { + ServerSocket socket = new ServerSocket(0); + int usedPort = socket.getLocalPort(); + + PortChecker checker = new PortChecker(); + boolean notFree = checker.isPortAvailable(usedPort); + assertFalse(notFree); + } + catch (IOException e) { + throw new RuntimeException(e); + } + } + @Test + void invalidPort(){ + PortChecker checker = new PortChecker(); + + assertThrows(IllegalArgumentException.class, ()-> { + checker.isPortAvailable(-1); + } + ); + assertThrows(IllegalArgumentException.class, ()-> { + checker.isPortAvailable(65536); + } + ); + } + @Test + void findFreePort() throws IOException { + PortChecker checker = new PortChecker(); + + int port = checker.findFreePort(); + int defaultPort = 8080; + int lastPort = 8090; + + boolean greaterOrEqual = port >= defaultPort; + boolean lessOrEqual = port <= lastPort; + boolean inRange = greaterOrEqual && lessOrEqual; + boolean isItFree = checker.isPortAvailable(port); + + assertTrue(inRange); + } +} \ No newline at end of file