Changeset 13467 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2018-02-26T19:54:01+01:00 (7 years ago)
Author:
Don-vip
Message:

fix #15992 - make sure PowerShell call is never blocking

Location:
trunk/src/org/openstreetmap/josm/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookWindows.java

    r13466 r13467  
    6666import java.util.Properties;
    6767import java.util.concurrent.ExecutionException;
     68import java.util.concurrent.TimeUnit;
    6869import java.util.regex.Matcher;
    6970import java.util.regex.Pattern;
     
    725726    public static int getPowerShellVersion() {
    726727        try {
    727             return Integer.valueOf(Utils.execOutput(Arrays.asList("powershell", "-Command", "$PSVersionTable.PSVersion.Major")));
     728            return Integer.valueOf(Utils.execOutput(Arrays.asList(
     729                    "powershell", "-Command", "$PSVersionTable.PSVersion.Major"), 2, TimeUnit.SECONDS));
    728730        } catch (NumberFormatException | IOException | ExecutionException | InterruptedException e) {
    729731            Logging.error(e);
     
    750752                        "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;"+
    751753                        "[System.Net.WebRequest]::Create('"+uri+"').GetResponse()"
    752                         ));
     754                        ), 5, TimeUnit.SECONDS);
    753755            } catch (ExecutionException | InterruptedException e) {
    754756                Logging.error(e);
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r13450 r13467  
    827827     * Runs an external command and returns the standard output.
    828828     *
    829      * The program is expected to execute fast.
     829     * The program is expected to execute fast, as this call waits 10 seconds at most.
    830830     *
    831831     * @param command the command with arguments
     
    836836     */
    837837    public static String execOutput(List<String> command) throws IOException, ExecutionException, InterruptedException {
     838        return execOutput(command, 10, TimeUnit.SECONDS);
     839    }
     840
     841    /**
     842     * Runs an external command and returns the standard output. Waits at most the specified time.
     843     *
     844     * @param command the command with arguments
     845     * @param timeout the maximum time to wait
     846     * @param unit the time unit of the {@code timeout} argument. Must not be null
     847     * @return the output
     848     * @throws IOException when there was an error, e.g. command does not exist
     849     * @throws ExecutionException when the return code is != 0. The output is can be retrieved in the exception message
     850     * @throws InterruptedException if the current thread is {@linkplain Thread#interrupt() interrupted} by another thread while waiting
     851     * @since 13467
     852     */
     853    public static String execOutput(List<String> command, long timeout, TimeUnit unit)
     854            throws IOException, ExecutionException, InterruptedException {
    838855        if (Logging.isDebugEnabled()) {
    839856            Logging.debug(join(" ", command));
     
    852869            }
    853870            String msg = all != null ? all.toString() : null;
    854             if (p.waitFor() != 0) {
     871            if (!p.waitFor(timeout, unit)) {
     872                throw new ExecutionException(msg, null);
     873            }
     874            if (p.exitValue() != 0) {
    855875                throw new ExecutionException(msg, null);
    856876            }
Note: See TracChangeset for help on using the changeset viewer.