diff --git a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/WakeOnLanUtility.java b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/WakeOnLanUtility.java index 3592f01a027..913e8230665 100644 --- a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/WakeOnLanUtility.java +++ b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/WakeOnLanUtility.java @@ -18,9 +18,11 @@ import java.net.DatagramSocket; import java.net.InetAddress; import java.net.InterfaceAddress; import java.net.NetworkInterface; +import java.time.Duration; import java.util.Enumeration; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Stream; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; @@ -73,8 +75,8 @@ public class WakeOnLanUtility { return null; } - String cmd = String.format(COMMAND, hostName); - String response = ExecUtil.executeCommandLineAndWaitResponse(cmd, CMD_TIMEOUT_MS); + String[] cmds = Stream.of(COMMAND.split(" ")).map(arg -> String.format(arg, hostName)).toArray(String[]::new); + String response = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofMillis(CMD_TIMEOUT_MS), cmds); Matcher matcher = MAC_REGEX.matcher(response); String macAddress = null; @@ -90,7 +92,8 @@ public class WakeOnLanUtility { if (macAddress != null) { LOGGER.debug("MAC address of host {} is {}", hostName, macAddress); } else { - LOGGER.debug("Problem executing command {} to retrieve MAC address for {}: {}", cmd, hostName, response); + LOGGER.debug("Problem executing command {} to retrieve MAC address for {}: {}", + String.format(COMMAND, hostName), hostName, response); } return macAddress; } diff --git a/bundles/org.openhab.binding.network/src/main/java/org/openhab/binding/network/internal/utils/NetworkUtils.java b/bundles/org.openhab.binding.network/src/main/java/org/openhab/binding/network/internal/utils/NetworkUtils.java index 0f7d350a50d..072e2059644 100644 --- a/bundles/org.openhab.binding.network/src/main/java/org/openhab/binding/network/internal/utils/NetworkUtils.java +++ b/bundles/org.openhab.binding.network/src/main/java/org/openhab/binding/network/internal/utils/NetworkUtils.java @@ -15,8 +15,25 @@ package org.openhab.binding.network.internal.utils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; -import java.net.*; -import java.util.*; +import java.net.ConnectException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.Inet4Address; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.NetworkInterface; +import java.net.NoRouteToHostException; +import java.net.PortUnreachableException; +import java.net.Socket; +import java.net.SocketAddress; +import java.net.SocketException; +import java.net.SocketTimeoutException; +import java.time.Duration; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.Optional; +import java.util.Set; import java.util.stream.Collectors; import org.apache.commons.lang.StringUtils; @@ -185,7 +202,7 @@ public class NetworkUtils { * Return true if the external arp ping utility (arping) is available and executable on the given path. */ public ArpPingUtilEnum determineNativeARPpingMethod(String arpToolPath) { - String result = ExecUtil.executeCommandLineAndWaitResponse(arpToolPath + " --help", 100); + String result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofMillis(100), arpToolPath, "--help"); if (StringUtils.isBlank(result)) { return ArpPingUtilEnum.UNKNOWN_TOOL; } else if (result.contains("Thomas Habets")) { diff --git a/bundles/org.openhab.binding.samsungtv/src/main/java/org/openhab/binding/samsungtv/internal/WakeOnLanUtility.java b/bundles/org.openhab.binding.samsungtv/src/main/java/org/openhab/binding/samsungtv/internal/WakeOnLanUtility.java index 2b4c97f3061..2a7e563f6dc 100644 --- a/bundles/org.openhab.binding.samsungtv/src/main/java/org/openhab/binding/samsungtv/internal/WakeOnLanUtility.java +++ b/bundles/org.openhab.binding.samsungtv/src/main/java/org/openhab/binding/samsungtv/internal/WakeOnLanUtility.java @@ -18,9 +18,11 @@ import java.net.DatagramSocket; import java.net.InetAddress; import java.net.InterfaceAddress; import java.net.NetworkInterface; +import java.time.Duration; import java.util.Enumeration; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Stream; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; @@ -73,8 +75,8 @@ public class WakeOnLanUtility { return null; } - String cmd = String.format(COMMAND, hostName); - String response = ExecUtil.executeCommandLineAndWaitResponse(cmd, CMD_TIMEOUT_MS); + String[] cmds = Stream.of(COMMAND.split(" ")).map(arg -> String.format(arg, hostName)).toArray(String[]::new); + String response = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofMillis(CMD_TIMEOUT_MS), cmds); Matcher matcher = MAC_REGEX.matcher(response); String macAddress = null; @@ -90,7 +92,8 @@ public class WakeOnLanUtility { if (macAddress != null) { LOGGER.debug("MAC address of host {} is {}", hostName, macAddress); } else { - LOGGER.debug("Problem executing command {} to retrieve MAC address for {}: {}", cmd, hostName, response); + LOGGER.debug("Problem executing command {} to retrieve MAC address for {}: {}", + String.format(COMMAND, hostName), hostName, response); } return macAddress; } diff --git a/bundles/org.openhab.transform.exec/src/main/java/org/openhab/transform/exec/internal/ExecTransformationService.java b/bundles/org.openhab.transform.exec/src/main/java/org/openhab/transform/exec/internal/ExecTransformationService.java index 85e283447cb..3d74b563d7f 100644 --- a/bundles/org.openhab.transform.exec/src/main/java/org/openhab/transform/exec/internal/ExecTransformationService.java +++ b/bundles/org.openhab.transform.exec/src/main/java/org/openhab/transform/exec/internal/ExecTransformationService.java @@ -12,6 +12,8 @@ */ package org.openhab.transform.exec.internal; +import java.time.Duration; + import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.io.net.exec.ExecUtil; @@ -64,7 +66,8 @@ public class ExecTransformationService implements TransformationService { long startTime = System.currentTimeMillis(); String formattedCommandLine = String.format(commandLine, source); - String result = ExecUtil.executeCommandLineAndWaitResponse(formattedCommandLine, 5000); + String result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofSeconds(5), + formattedCommandLine.split(" ")); logger.trace("command line execution elapsed {} ms", System.currentTimeMillis() - startTime); return result;