Ejemplo n.º 1
0
 /** Uninstalls a feature and checks that feature is properly uninstalled. */
 public void unInstallAndCheckFeature(String feature) throws Exception {
   System.err.println(executeCommand("features:uninstall " + feature));
   FeaturesService featuresService = ServiceLocator.getOsgiService(FeaturesService.class);
   System.err.println(executeCommand("osgi:list -t 0"));
   Assert.assertFalse(
       "Expected " + feature + " feature to be installed.",
       featuresService.isInstalled(featuresService.getFeature(feature)));
 }
Ejemplo n.º 2
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;
  }