From bc6cb656d3b0edaf1ca661a716da3130cec65305 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 5 Apr 2013 23:41:50 +0200 Subject: [PATCH] Fix some quality flaws --- .../org/sonar/runner/api/CommandExecutor.java | 21 ++++++++++--------- .../org/sonar/runner/api/RunnerVersion.java | 4 +++- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/CommandExecutor.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/CommandExecutor.java index 4c87cfe..2f07c3c 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/CommandExecutor.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/CommandExecutor.java @@ -52,9 +52,7 @@ class CommandExecutor { StreamGobbler errorGobbler = null; try { ProcessBuilder builder = new ProcessBuilder(command.toStrings()); - if (command.directory() != null) { - builder.directory(command.directory()); - } + builder.directory(command.directory()); builder.environment().putAll(command.envVariables()); process = builder.start(); @@ -63,13 +61,8 @@ class CommandExecutor { outputGobbler.start(); errorGobbler.start(); - final Process finalProcess = process; executorService = Executors.newSingleThreadExecutor(); - Future ft = executorService.submit(new Callable() { - public Integer call() throws Exception { - return finalProcess.waitFor(); - } - }); + Future ft = executeProcess(executorService, process); int exitCode = ft.get(timeoutMilliseconds, TimeUnit.MILLISECONDS); waitUntilFinish(outputGobbler); waitUntilFinish(errorGobbler); @@ -91,13 +84,21 @@ class CommandExecutor { waitUntilFinish(outputGobbler); waitUntilFinish(errorGobbler); closeStreams(process); - if (executorService != null) { executorService.shutdown(); } } } + private Future executeProcess(ExecutorService executorService, Process process) { + final Process finalProcess = process; + return executorService.submit(new Callable() { + public Integer call() throws InterruptedException { + return finalProcess.waitFor(); + } + }); + } + private void verifyGobbler(Command command, StreamGobbler gobbler, String type) { if (gobbler.getException() != null) { throw new CommandException("Error inside " + type + " stream", command, gobbler.getException()); diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/RunnerVersion.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/RunnerVersion.java index 76ed836..b93a39f 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/RunnerVersion.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/RunnerVersion.java @@ -19,6 +19,8 @@ */ package org.sonar.runner.api; +import org.apache.commons.io.IOUtils; + import java.util.Scanner; /** @@ -40,7 +42,7 @@ public enum RunnerVersion { try { this.version = scanner.next(); } finally { - scanner.close(); + IOUtils.closeQuietly(scanner); } } } -- 2.39.5