From 2453ef75bb9db857450dfd8f177b86a6ddb2e33f Mon Sep 17 00:00:00 2001 From: mikr Date: Tue, 31 Aug 2021 17:30:10 +0200 Subject: [PATCH 1/5] JAVA-5393 Update Java 9 Process API --- .../java9/process/ProcessAPIEnhancements.java | 101 ++++++++++++++ .../ProcessAPIEnhancementsUnitTest.java | 132 ------------------ 2 files changed, 101 insertions(+), 132 deletions(-) create mode 100644 core-java-modules/core-java-os/src/main/java/com/baeldung/java9/process/ProcessAPIEnhancements.java delete mode 100644 core-java-modules/core-java-os/src/test/java/com/baeldung/java9/process/ProcessAPIEnhancementsUnitTest.java diff --git a/core-java-modules/core-java-os/src/main/java/com/baeldung/java9/process/ProcessAPIEnhancements.java b/core-java-modules/core-java-os/src/main/java/com/baeldung/java9/process/ProcessAPIEnhancements.java new file mode 100644 index 0000000000..cb3c183062 --- /dev/null +++ b/core-java-modules/core-java-os/src/main/java/com/baeldung/java9/process/ProcessAPIEnhancements.java @@ -0,0 +1,101 @@ +package com.baeldung.java9.process; + +import java.io.IOException; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.stream.Stream; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ProcessAPIEnhancements { + + static Logger log = LoggerFactory.getLogger(ProcessAPIEnhancements.class); + + public static void main(String[] args) throws IOException, InterruptedException, ExecutionException { + infoOfCurrentProcess(); + infoOfLiveProcesses(); + infoOfSpawnProcess(); + infoOfExitCallback(); + infoOfChildProcess(); + } + + private static void infoOfCurrentProcess() { + ProcessHandle processHandle = ProcessHandle.current(); + ProcessHandle.Info processInfo = processHandle.info(); + + log.info("PID: " + processHandle.pid()); + log.info("Arguments: " + processInfo.arguments()); + log.info("Command: " + processInfo.command()); + log.info("Instant: " + processInfo.startInstant()); + log.info("Total CPU duration: " + processInfo.totalCpuDuration()); + log.info("User: " + processInfo.user()); + } + + private static void infoOfSpawnProcess() throws IOException { + + String javaCmd = ProcessUtils.getJavaCmd().getAbsolutePath(); + ProcessBuilder processBuilder = new ProcessBuilder(javaCmd, "-version"); + Process process = processBuilder.inheritIO().start(); + ProcessHandle processHandle = process.toHandle(); + ProcessHandle.Info processInfo = processHandle.info(); + + log.info("PID: " + processHandle.pid()); + log.info("Arguments: " + processInfo.arguments()); + log.info("Command: " + processInfo.command()); + log.info("Instant: " + processInfo.startInstant()); + log.info("Total CPU duration: " + processInfo.totalCpuDuration()); + log.info("User: " + processInfo.user()); + } + + private static void infoOfLiveProcesses() { + Stream liveProcesses = ProcessHandle.allProcesses(); + liveProcesses.filter(ProcessHandle::isAlive) + .forEach(ph -> { + log.info("PID: " + ph.pid()); + log.info("Instance: " + ph.info().startInstant()); + log.info("User: " + ph.info().user()); + }); + } + + private static void infoOfChildProcess() throws IOException { + int childProcessCount = 5; + for (int i = 0; i < childProcessCount; i++) { + String javaCmd = ProcessUtils.getJavaCmd() + .getAbsolutePath(); + ProcessBuilder processBuilder + = new ProcessBuilder(javaCmd, "-version"); + processBuilder.inheritIO().start(); + } + + Stream children = ProcessHandle.current() + .children(); + children.filter(ProcessHandle::isAlive) + .forEach(ph -> log.info("PID: {}, Cmd: {}", ph.pid(), ph.info() + .command())); + Stream descendants = ProcessHandle.current() + .descendants(); + descendants.filter(ProcessHandle::isAlive) + .forEach(ph -> log.info("PID: {}, Cmd: {}", ph.pid(), ph.info() + .command())); + } + + private static void infoOfExitCallback() throws IOException, InterruptedException, ExecutionException { + String javaCmd = ProcessUtils.getJavaCmd() + .getAbsolutePath(); + ProcessBuilder processBuilder + = new ProcessBuilder(javaCmd, "-version"); + Process process = processBuilder.inheritIO() + .start(); + ProcessHandle processHandle = process.toHandle(); + + log.info("PID: {} has started", processHandle.pid()); + CompletableFuture onProcessExit = processHandle.onExit(); + onProcessExit.get(); + log.info("Alive: " + processHandle.isAlive()); + onProcessExit.thenAccept(ph -> { + log.info("PID: {} has stopped", ph.pid()); + }); + } + +} diff --git a/core-java-modules/core-java-os/src/test/java/com/baeldung/java9/process/ProcessAPIEnhancementsUnitTest.java b/core-java-modules/core-java-os/src/test/java/com/baeldung/java9/process/ProcessAPIEnhancementsUnitTest.java deleted file mode 100644 index c81f060150..0000000000 --- a/core-java-modules/core-java-os/src/test/java/com/baeldung/java9/process/ProcessAPIEnhancementsUnitTest.java +++ /dev/null @@ -1,132 +0,0 @@ -package com.baeldung.java9.process; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.io.IOException; -import java.util.concurrent.CompletableFuture; -import java.util.stream.Stream; - -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Created by sanaulla on 2/23/2017. - */ - -public class ProcessAPIEnhancementsUnitTest { - - Logger log = LoggerFactory.getLogger(ProcessAPIEnhancementsUnitTest.class); - - // @Test - // OS / Java version dependent - public void givenCurrentProcess_whenInvokeGetInfo_thenSuccess() throws IOException { - ProcessHandle processHandle = ProcessHandle.current(); - ProcessHandle.Info processInfo = processHandle.info(); - assertNotNull(processHandle.pid()); - assertEquals(true, processInfo.arguments() - .isPresent()); - assertEquals(true, processInfo.command() - .isPresent()); - assertTrue(processInfo.command() - .get() - .contains("java")); - - assertEquals(true, processInfo.startInstant() - .isPresent()); - assertEquals(true, processInfo.totalCpuDuration() - .isPresent()); - assertEquals(true, processInfo.user() - .isPresent()); - } - - // @Test - // OS / Java version dependent - public void givenSpawnProcess_whenInvokeGetInfo_thenSuccess() throws IOException { - - String javaCmd = ProcessUtils.getJavaCmd() - .getAbsolutePath(); - ProcessBuilder processBuilder = new ProcessBuilder(javaCmd, "-version"); - Process process = processBuilder.inheritIO() - .start(); - ProcessHandle processHandle = process.toHandle(); - ProcessHandle.Info processInfo = processHandle.info(); - assertNotNull(processHandle.pid()); - assertEquals(true, processInfo.arguments() - .isPresent()); - assertEquals(true, processInfo.command() - .isPresent()); - assertTrue(processInfo.command() - .get() - .contains("java")); - assertEquals(true, processInfo.startInstant() - .isPresent()); - assertEquals(false, processInfo.totalCpuDuration() - .isPresent()); - assertEquals(true, processInfo.user() - .isPresent()); - } - - // @Test - // OS / Java version dependent - public void givenLiveProcesses_whenInvokeGetInfo_thenSuccess() { - Stream liveProcesses = ProcessHandle.allProcesses(); - liveProcesses.filter(ProcessHandle::isAlive) - .forEach(ph -> { - assertNotNull(ph.pid()); - assertEquals(true, ph.info() - .startInstant() - .isPresent()); - assertEquals(true, ph.info() - .user() - .isPresent()); - }); - } - - // @Test - // OS / Java version dependent - public void givenProcess_whenGetChildProcess_thenSuccess() throws IOException { - int childProcessCount = 5; - for (int i = 0; i < childProcessCount; i++) { - String javaCmd = ProcessUtils.getJavaCmd() - .getAbsolutePath(); - ProcessBuilder processBuilder - = new ProcessBuilder(javaCmd, "-version"); - processBuilder.inheritIO().start(); - } - - Stream children = ProcessHandle.current() - .children(); - children.filter(ProcessHandle::isAlive) - .forEach(ph -> log.info("PID: {}, Cmd: {}", ph.pid(), ph.info() - .command())); - Stream descendants = ProcessHandle.current() - .descendants(); - descendants.filter(ProcessHandle::isAlive) - .forEach(ph -> log.info("PID: {}, Cmd: {}", ph.pid(), ph.info() - .command())); - } - - // @Test - // OS / Java version dependent - public void givenProcess_whenAddExitCallback_thenSuccess() throws Exception { - String javaCmd = ProcessUtils.getJavaCmd() - .getAbsolutePath(); - ProcessBuilder processBuilder - = new ProcessBuilder(javaCmd, "-version"); - Process process = processBuilder.inheritIO() - .start(); - ProcessHandle processHandle = process.toHandle(); - - log.info("PID: {} has started", processHandle.pid()); - CompletableFuture onProcessExit = processHandle.onExit(); - onProcessExit.get(); - assertEquals(false, processHandle.isAlive()); - onProcessExit.thenAccept(ph -> { - log.info("PID: {} has stopped", ph.pid()); - }); - } - -} From d4d3ac77f8e47bb44fbf6917ac4a4a16514f9f17 Mon Sep 17 00:00:00 2001 From: anuragkumawat Date: Fri, 3 Sep 2021 02:23:16 +0530 Subject: [PATCH 2/5] Removed the large Geo Location File - GeoLite2-City.mmdb from code and it's references. --- .../src/test/java/com/baeldung/geoip/GeoIpIntegrationTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-web-modules/spring-mvc-xml/src/test/java/com/baeldung/geoip/GeoIpIntegrationTest.java b/spring-web-modules/spring-mvc-xml/src/test/java/com/baeldung/geoip/GeoIpIntegrationTest.java index 0aa23842b1..206e9b6e52 100644 --- a/spring-web-modules/spring-mvc-xml/src/test/java/com/baeldung/geoip/GeoIpIntegrationTest.java +++ b/spring-web-modules/spring-mvc-xml/src/test/java/com/baeldung/geoip/GeoIpIntegrationTest.java @@ -16,7 +16,7 @@ public class GeoIpIntegrationTest { public void givenIP_whenFetchingCity_thenReturnsCityData() throws IOException, GeoIp2Exception { ClassLoader classLoader = getClass().getClassLoader(); - File database = new File(classLoader.getResource("GeoLite2-City.mmdb").getFile()); + File database = new File("your-path-to-db-file"); DatabaseReader dbReader = new DatabaseReader.Builder(database).build(); InetAddress ipAddress = InetAddress.getByName("google.com"); From ec6f5d90ccb8d698bde493a209e1f28b9bd9a8f2 Mon Sep 17 00:00:00 2001 From: Mainak Majumder Date: Mon, 6 Sep 2021 00:08:08 +0200 Subject: [PATCH 3/5] source code for bealdung article "Find IP address of client" (#10995) * Application source code for the Baeldung article "HTTP PUT vs POST method in REST API" * update indention in pom file, update code in Address class * update indention * rename application * update pom * source code for article "Connection timeout vs read timeout" * Source code for Baeldung article BAEL-4896 * update code * source code for Baeldung article bael-4896 * add main method to client and server * remove comment * include command-line argument option for client, remove try-catch block * update greetings message section --- .../clientaddress/ApplicationClient.java | 39 ++++++++++++++ .../clientaddress/ApplicationServer.java | 51 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 core-java-modules/core-java-networking-3/src/main/java/com/baeldung/clientaddress/ApplicationClient.java create mode 100644 core-java-modules/core-java-networking-3/src/main/java/com/baeldung/clientaddress/ApplicationServer.java diff --git a/core-java-modules/core-java-networking-3/src/main/java/com/baeldung/clientaddress/ApplicationClient.java b/core-java-modules/core-java-networking-3/src/main/java/com/baeldung/clientaddress/ApplicationClient.java new file mode 100644 index 0000000000..498046904d --- /dev/null +++ b/core-java-modules/core-java-networking-3/src/main/java/com/baeldung/clientaddress/ApplicationClient.java @@ -0,0 +1,39 @@ +package com.baeldung.clientaddress; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.Socket; + +public class ApplicationClient { + + private Socket clientSocket; + private PrintWriter out; + private BufferedReader in; + + public void connect(String ip, int port) throws IOException { + clientSocket = new Socket(ip, port); + out = new PrintWriter(clientSocket.getOutputStream(), true); + in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + } + + public void sendGreetings(String msg) throws IOException { + out.println(msg); + String reply = in.readLine(); + System.out.println("Reply received from the server :: " + reply); + } + + public void disconnect() throws IOException { + in.close(); + out.close(); + clientSocket.close(); + } + + public static void main(String[] args) throws IOException { + ApplicationClient client = new ApplicationClient(); + client.connect(args[0], Integer.parseInt(args[1])); // IP address and port number of the server + client.sendGreetings(args[2]); // greetings message + client.disconnect(); + } +} diff --git a/core-java-modules/core-java-networking-3/src/main/java/com/baeldung/clientaddress/ApplicationServer.java b/core-java-modules/core-java-networking-3/src/main/java/com/baeldung/clientaddress/ApplicationServer.java new file mode 100644 index 0000000000..ded4482b9a --- /dev/null +++ b/core-java-modules/core-java-networking-3/src/main/java/com/baeldung/clientaddress/ApplicationServer.java @@ -0,0 +1,51 @@ +package com.baeldung.clientaddress; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; + +public class ApplicationServer { + + private ServerSocket serverSocket; + private Socket connectedSocket; + private PrintWriter out; + private BufferedReader in; + + public void startServer(int port) throws IOException { + serverSocket = new ServerSocket(port); + connectedSocket = serverSocket.accept(); + + InetSocketAddress socketAddress = (InetSocketAddress) connectedSocket.getRemoteSocketAddress(); + String clientIpAddress = socketAddress.getAddress() + .getHostAddress(); + System.out.println("IP address of the connected client :: " + clientIpAddress); + + out = new PrintWriter(connectedSocket.getOutputStream(), true); + in = new BufferedReader(new InputStreamReader(connectedSocket.getInputStream())); + String msg = in.readLine(); + System.out.println("Message received from the client :: " + msg); + out.println("Hello Client !!"); + + closeIO(); + stopServer(); + } + + private void closeIO() throws IOException { + in.close(); + out.close(); + } + + private void stopServer() throws IOException { + connectedSocket.close(); + serverSocket.close(); + } + + public static void main(String[] args) throws IOException { + ApplicationServer server = new ApplicationServer(); + server.startServer(5000); + } +} From 8f737fc3c277f2321687cadc5e1eb3a321e76f57 Mon Sep 17 00:00:00 2001 From: anuragkumawat Date: Mon, 6 Sep 2021 19:58:33 +0530 Subject: [PATCH 4/5] JAVA-5947 - Changed the Integration Test to Manual Test and also added the comment to download the location file and run the test manually in code --- .../{GeoIpIntegrationTest.java => GeoIpManualTest.java} | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) rename spring-web-modules/spring-mvc-xml/src/test/java/com/baeldung/geoip/{GeoIpIntegrationTest.java => GeoIpManualTest.java} (68%) diff --git a/spring-web-modules/spring-mvc-xml/src/test/java/com/baeldung/geoip/GeoIpIntegrationTest.java b/spring-web-modules/spring-mvc-xml/src/test/java/com/baeldung/geoip/GeoIpManualTest.java similarity index 68% rename from spring-web-modules/spring-mvc-xml/src/test/java/com/baeldung/geoip/GeoIpIntegrationTest.java rename to spring-web-modules/spring-mvc-xml/src/test/java/com/baeldung/geoip/GeoIpManualTest.java index 206e9b6e52..5bfb78017c 100644 --- a/spring-web-modules/spring-mvc-xml/src/test/java/com/baeldung/geoip/GeoIpIntegrationTest.java +++ b/spring-web-modules/spring-mvc-xml/src/test/java/com/baeldung/geoip/GeoIpManualTest.java @@ -1,4 +1,4 @@ -package com.baeldung.geoip; +package test.java.com.baeldung.geoip; import java.io.File; import java.io.IOException; @@ -10,12 +10,17 @@ import com.maxmind.geoip2.DatabaseReader; import com.maxmind.geoip2.exception.GeoIp2Exception; import com.maxmind.geoip2.model.CityResponse; -public class GeoIpIntegrationTest { +public class GeoIpManualTest { @Test public void givenIP_whenFetchingCity_thenReturnsCityData() throws IOException, GeoIp2Exception { ClassLoader classLoader = getClass().getClassLoader(); + /** + * Download the db file as shown in the article https://www.baeldung.com/geolocation-by-ip-with-maxmind, + * then replace the "your-path-to-db-file" string in the test with the file path before running the test + * HINT : Copy the downloaded file at spring-web-modules/spring-mvc-xml/src/test/resources/GeoLite2-City.mmdb + * **/ File database = new File("your-path-to-db-file"); DatabaseReader dbReader = new DatabaseReader.Builder(database).build(); From 3ead19dc38d324eb82320f1902176cd25ddb99de Mon Sep 17 00:00:00 2001 From: anuragkumawat Date: Mon, 6 Sep 2021 20:03:03 +0530 Subject: [PATCH 5/5] JAVA-5947 - Corrected the package name in code --- .../src/test/java/com/baeldung/geoip/GeoIpManualTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-web-modules/spring-mvc-xml/src/test/java/com/baeldung/geoip/GeoIpManualTest.java b/spring-web-modules/spring-mvc-xml/src/test/java/com/baeldung/geoip/GeoIpManualTest.java index 5bfb78017c..99a76099b2 100644 --- a/spring-web-modules/spring-mvc-xml/src/test/java/com/baeldung/geoip/GeoIpManualTest.java +++ b/spring-web-modules/spring-mvc-xml/src/test/java/com/baeldung/geoip/GeoIpManualTest.java @@ -1,4 +1,4 @@ -package test.java.com.baeldung.geoip; +package com.baeldung.geoip; import java.io.File; import java.io.IOException;