Adapt to changes in ExecUtil (#8690)

Signed-off-by: Connor Petty <mistercpp2000+gitsignoff@gmail.com>
This commit is contained in:
Connor Petty 2020-10-08 12:08:11 -07:00 committed by GitHub
parent 178e657d80
commit 0496d35f32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 10 deletions

View File

@ -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;
}

View File

@ -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")) {

View File

@ -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;
}

View File

@ -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;