예제 #1
0
  /**
   * Executes a shell command and returns output as a String. Commands have a default timeout of 10
   * seconds.
   *
   * @param command The command to execute.
   * @param timeout The amount of time in millis to wait for the command to execute.
   * @param silent Specifies if the command should be displayed in the screen.
   * @return
   */
  protected String executeCommand(final String command, final Long timeout, final Boolean silent) {
    String response;
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    final PrintStream printStream = new PrintStream(byteArrayOutputStream);
    final CommandProcessor commandProcessor = getOsgiService(CommandProcessor.class);
    final CommandSession commandSession =
        commandProcessor.createSession(System.in, printStream, System.err);
    FutureTask<String> commandFuture =
        new FutureTask<String>(
            new Callable<String>() {
              public String call() {
                try {
                  if (!silent) {
                    System.err.println(command);
                  }
                  commandSession.execute(command);
                } catch (Exception e) {
                  e.printStackTrace(System.err);
                }
                printStream.flush();
                return byteArrayOutputStream.toString();
              }
            });

    try {
      executor.submit(commandFuture);
      response = commandFuture.get(timeout, TimeUnit.MILLISECONDS);
    } catch (Exception e) {
      e.printStackTrace(System.err);
      response = "SHELL COMMAND TIMED OUT: ";
    }

    return response;
  }
예제 #2
0
  private CommandProcessor getCommandProcessor(CommandSession session) {
    CommandProcessor processor = mock(CommandProcessor.class);

    when(processor.createSession(
            isNull(InputStream.class), isA(PrintStream.class), isA(PrintStream.class)))
        .thenReturn(session);
    return processor;
  }
예제 #3
0
  /**
   * Executes a shell command and returns output as a String. Commands have a default timeout of 10
   * seconds.
   *
   * @param timeout The amount of time in millis to wait for the command to execute.
   * @param silent Specifies if the command should be displayed in the screen.
   * @param commands The command to execute.
   */
  protected String executeCommands(
      final long timeout, final boolean silent, final String... commands) {
    String response = null;
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    final PrintStream printStream = new PrintStream(byteArrayOutputStream);
    final CommandProcessor commandProcessor = ServiceLocator.getOsgiService(CommandProcessor.class);
    final CommandSession commandSession =
        commandProcessor.createSession(System.in, printStream, printStream);
    commandSession.put("APPLICATION", System.getProperty("karaf.name", "root"));
    commandSession.put("USER", "karaf");
    FutureTask<String> commandFuture =
        new FutureTask<String>(
            new Callable<String>() {
              public String call() throws Exception {
                for (String command : commands) {
                  boolean keepRunning = true;

                  if (!silent) {
                    System.out.println(command);
                    System.out.flush();
                  }

                  while (!Thread.currentThread().isInterrupted() && keepRunning) {
                    try {
                      commandSession.execute(command);
                      keepRunning = false;
                    } catch (Exception e) {
                      if (retryException(e)) {
                        keepRunning = true;
                        sleep(1000);
                      } else {
                        throw new CommandExecutionException(e);
                      }
                    }
                  }
                }
                printStream.flush();
                return byteArrayOutputStream.toString();
              }
            });

    try {
      executor.submit(commandFuture);
      response = commandFuture.get(timeout, TimeUnit.MILLISECONDS);
    } catch (ExecutionException e) {
      throw CommandExecutionException.launderThrowable(e.getCause());
    } catch (Exception e) {
      throw CommandExecutionException.launderThrowable(e);
    }

    return response;
  }