aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2014-04-25 00:01:03 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2014-04-25 00:02:10 +0200
commit9889c094f169beff6c9b9104f594b8f9ac20f0c8 (patch)
treecb1f1055623d5514bc3be5884a3c1443cba2b738
parent478383011bba15b19869380384aa569697706681 (diff)
downloadsonarqube-9889c094f169beff6c9b9104f594b8f9ac20f0c8.tar.gz
sonarqube-9889c094f169beff6c9b9104f594b8f9ac20f0c8.zip
SONAR-5234 new org.sonar.api.utils.command.TimeoutException
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandException.java6
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java7
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/command/TimeoutException.java32
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandExecutorTest.java8
4 files changed, 42 insertions, 11 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandException.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandException.java
index 562f0e4488b..211031ee01c 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandException.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandException.java
@@ -19,13 +19,11 @@
*/
package org.sonar.api.utils.command;
-import javax.annotation.Nullable;
-
-public final class CommandException extends RuntimeException {
+public class CommandException extends RuntimeException {
private final Command command;
- public CommandException(Command command, String message, @Nullable Throwable throwable) {
+ public CommandException(Command command, String message, Throwable throwable) {
super(message + " [command: " + command + "]", throwable);
this.command = command;
}
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 d6f5ea64b92..bdb9cd4b3ef 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
@@ -50,7 +50,8 @@ public class CommandExecutor {
}
/**
- * @throws CommandException
+ * @throws org.sonar.api.utils.command.TimeoutException on timeout, since 4.4
+ * @throws CommandException on any other error
* @since 3.0
*/
public int execute(Command command, StreamConsumer stdOut, StreamConsumer stdErr, long timeoutMilliseconds) {
@@ -86,9 +87,9 @@ public class CommandExecutor {
verifyGobbler(command, errorGobbler, "stdErr");
return exitCode;
- } catch (TimeoutException te) {
+ } catch (java.util.concurrent.TimeoutException te) {
process.destroy();
- throw new CommandException(command, "Timeout exceeded: " + timeoutMilliseconds + " ms", te);
+ throw new TimeoutException(command, "Timeout exceeded: " + timeoutMilliseconds + " ms", te);
} catch (CommandException e) {
throw e;
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/TimeoutException.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/TimeoutException.java
new file mode 100644
index 00000000000..74eb4436fe5
--- /dev/null
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/TimeoutException.java
@@ -0,0 +1,32 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.utils.command;
+
+/**
+ * Unchecked version of {@link java.util.concurrent.TimeoutException}
+ *
+ * @since 4.4
+ */
+public class TimeoutException extends CommandException {
+
+ public TimeoutException(Command command, String message, Throwable throwable) {
+ super(command, message, throwable);
+ }
+}
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 b3891832993..3eaca6e0072 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
@@ -110,9 +110,9 @@ public class CommandExecutorTest {
@Test
public void should_use_working_directory_to_store_argument_and_environment_variable() throws Exception {
Command command = Command.create(getScript("echo"))
- .setDirectory(workDir)
- .addArgument("1")
- .setEnvironmentVariable("ENVVAR", "2");
+ .setDirectory(workDir)
+ .addArgument("1")
+ .setEnvironmentVariable("ENVVAR", "2");
int exitCode = CommandExecutor.create().execute(command, 1000L);
assertThat(exitCode).isEqualTo(0);
File logFile = new File(workDir, "echo.log");
@@ -130,7 +130,7 @@ public class CommandExecutorTest {
try {
CommandExecutor.create().execute(Command.create(executable).setDirectory(workDir), 300L);
fail();
- } catch (CommandException e) {
+ } catch (TimeoutException e) {
long duration = System.currentTimeMillis() - start;
// should test >= 300 but it strangly fails during build on windows.
// The timeout is raised after 297ms (??)