*/
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;
}
}
/**
- * @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) {
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;
--- /dev/null
+/*
+ * 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);
+ }
+}
@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");
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 (??)