Skip to content

Instantly share code, notes, and snippets.

@iocanel
Created March 5, 2012 16:12
Show Gist options
  • Save iocanel/1979051 to your computer and use it in GitHub Desktop.
Save iocanel/1979051 to your computer and use it in GitHub Desktop.
Creating a method for invoking Karaf shell from inside a pax-exam karaf unit test
@Inject
CommandProcessor commandProcessor;
protected String executeCommands(final String ...commands) {
String response;
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
final PrintStream printStream = new PrintStream(byteArrayOutputStream);
final CommandSession commandSession = commandProcessor.createSession(System.in, printStream, System.err);
FutureTask<string> commandFuture = new FutureTask<string>(
new Callable<string>() {
public String call() {
try {
for(String command:commands) {
System.err.println(command);
commandSession.execute(command);
}
} catch (Exception e) {
e.printStackTrace(System.err);
}
return byteArrayOutputStream.toString();
}
});
try {
executor.submit(commandFuture);
response = commandFuture.get(COMMAND_TIMEOUT, TimeUnit.MILLISECONDS);
} catch (Exception e) {
e.printStackTrace(System.err);
response = "SHELL COMMAND TIMED OUT: ";
}
return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment