diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-07-10 17:34:00 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-07-10 17:47:03 +0200 |
commit | 6d3ab7ef3aa6672e6af3925f670fdd660225fe5f (patch) | |
tree | 4c1c2d5028163b1c577dceb42d6a7540da2c3ac0 /sonar-plugin-api | |
parent | e0e6819529fc8543bcb927dce1ddf78e89c03cff (diff) | |
download | sonarqube-6d3ab7ef3aa6672e6af3925f670fdd660225fe5f.tar.gz sonarqube-6d3ab7ef3aa6672e6af3925f670fdd660225fe5f.zip |
Minor improvements on org.sonar.api.utils.command
* shorten the method CommandExecutor#execute()
* add javadoc
Diffstat (limited to 'sonar-plugin-api')
3 files changed, 16 insertions, 13 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java index 461c82f11f3..04614bd2bc3 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java @@ -80,6 +80,9 @@ public final class Command { } /** + * Adds or overrides an environment variable. The initial values are a copy of the environment + * of the current process. + * * @since 3.2 */ public Command setEnvironmentVariable(String name, String value) { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java index 05de3914947..0384b5251cb 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java @@ -76,23 +76,17 @@ public final class CommandExecutor { errorGobbler.start(); final Process finalProcess = process; - Callable<Integer> call = new Callable<Integer>() { + executorService = Executors.newSingleThreadExecutor(); + Future<Integer> ft = executorService.submit(new Callable<Integer>() { public Integer call() throws Exception { return finalProcess.waitFor(); } - }; - - executorService = Executors.newSingleThreadExecutor(); - Future<Integer> ft = executorService.submit(call); + }); int exitCode = ft.get(timeoutMilliseconds, TimeUnit.MILLISECONDS); waitUntilFinish(outputGobbler); waitUntilFinish(errorGobbler); - if (outputGobbler.getException() != null) { - throw new CommandException(command, "Error inside stdOut parser", outputGobbler.getException()); - } - if (errorGobbler.getException() != null) { - throw new CommandException(command, "Error inside stdErr parser", errorGobbler.getException()); - } + verifyGobbler(command, outputGobbler, "stdOut"); + verifyGobbler(command, errorGobbler, "stdErr"); return exitCode; } catch (TimeoutException te) { @@ -116,6 +110,12 @@ public final class CommandExecutor { } } + private void verifyGobbler(Command command, StreamGobbler gobbler, String type) { + if (gobbler.getException() != null) { + throw new CommandException(command, "Error inside " + type + " stream", gobbler.getException()); + } + } + /** * Execute command and display error and output streams in log. * Method {@link #execute(Command, StreamConsumer, StreamConsumer, long)} is preferable, diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandExecutorTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandExecutorTest.java index b06194bbbd8..391573f8057 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandExecutorTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandExecutorTest.java @@ -82,7 +82,7 @@ public class CommandExecutorTest { public void stdOut_consumer_can_throw_exception() throws Exception { Command command = Command.create(getScript("output")).setDirectory(workDir); thrown.expect(CommandException.class); - thrown.expectMessage("Error inside stdOut parser"); + thrown.expectMessage("Error inside stdOut stream"); CommandExecutor.create().execute(command, BAD_CONSUMER, NOP_CONSUMER, 1000L); } @@ -90,7 +90,7 @@ public class CommandExecutorTest { public void stdErr_consumer_can_throw_exception() throws Exception { Command command = Command.create(getScript("output")).setDirectory(workDir); thrown.expect(CommandException.class); - thrown.expectMessage("Error inside stdErr parser"); + thrown.expectMessage("Error inside stdErr stream"); CommandExecutor.create().execute(command, NOP_CONSUMER, BAD_CONSUMER, 1000L); } |