use of org.apache.karaf.shell.api.console.Session in project karaf by apache.
the class KarafTestSupport method executeCommand.
/**
* 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.
* @param principals The principals (e.g. RolePrincipal objects) to run the command under
* @return
*/
protected String executeCommand(final String command, final Long timeout, final Boolean silent, final Principal... principals) {
waitForCommandService(command);
String response;
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
final PrintStream printStream = new PrintStream(byteArrayOutputStream);
final SessionFactory sessionFactory = getOsgiService(SessionFactory.class);
final Session session = sessionFactory.create(System.in, printStream, System.err);
final Callable<String> commandCallable = () -> {
try {
if (!silent) {
System.err.println(command);
}
session.execute(command);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
printStream.flush();
return byteArrayOutputStream.toString();
};
FutureTask<String> commandFuture;
if (principals.length == 0) {
commandFuture = new FutureTask<>(commandCallable);
} else {
// If principals are defined, run the command callable via Subject.doAs()
commandFuture = new FutureTask<>(() -> {
Subject subject = new Subject();
subject.getPrincipals().addAll(Arrays.asList(principals));
return Subject.doAs(subject, (PrivilegedExceptionAction<String>) commandCallable::call);
});
}
try {
executor.submit(commandFuture);
response = commandFuture.get(timeout, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
e.printStackTrace(System.err);
response = "SHELL COMMAND TIMED OUT: ";
} catch (ExecutionException e) {
Throwable cause = e.getCause().getCause();
throw new RuntimeException(cause.getMessage(), cause);
} catch (InterruptedException e) {
throw new RuntimeException(e.getMessage(), e);
}
return response;
}
use of org.apache.karaf.shell.api.console.Session in project ddf by codice.
the class CommandJobTest method testSimpleCommand.
/**
* Tests the simplest command will be executed.
*
* @throws Exception
*/
@Test
public void testSimpleCommand() throws Exception {
// given
String command = "info";
FirstArgumentAnswer captureInput = new FirstArgumentAnswer();
Session session = getSession(captureInput);
when(sessionFactory.create(any(), any(), any())).thenReturn(session);
CommandJob job = getCommandJob();
// when
job.execute(getJobExecutionContext(command));
// then
assertThat(captureInput.getInputArg(), is(command));
verifySessionCalls(command, session, 1, 1);
}
use of org.apache.karaf.shell.api.console.Session in project ddf by codice.
the class CommandJobTest method testEmptyCommand.
/**
* An empty command will be executed
*
* @throws Exception
*/
@Test
public void testEmptyCommand() throws Exception {
// given
String command = "";
FirstArgumentAnswer captureInput = new FirstArgumentAnswer();
Session session = getSession(captureInput);
when(sessionFactory.create(any(), any(), any())).thenReturn(session);
CommandJob job = getCommandJob();
// when
job.execute(getJobExecutionContext(command));
// then
assertThat(captureInput.getInputArg(), is(command));
verifySessionCalls(command, session, 1, 1);
}
use of org.apache.karaf.shell.api.console.Session in project camel by apache.
the class CdiOsgiIT method testExecuteCommands.
@Test
public void testExecuteCommands() throws Exception {
Session session = sessionFactory.create(System.in, System.out, System.err);
session.execute("camel:context-list");
session.execute("camel:route-list");
session.execute("camel:route-info consumer-route");
}
use of org.apache.karaf.shell.api.console.Session in project karaf by apache.
the class SecuredCommand method execute.
@Override
public Object execute(final CommandSession commandSession, List<Object> arguments) throws Exception {
// TODO: remove the hack for .session
Session session = (Session) commandSession.get(".session");
// When need to translate closures to a compatible type for the command
for (int i = 0; i < arguments.size(); i++) {
Object v = arguments.get(i);
if (v instanceof Closure) {
final Closure closure = (Closure) v;
arguments.set(i, new VersatileFunction(closure));
}
if (v instanceof Token) {
arguments.set(i, v.toString());
}
}
return execute(session, arguments);
}
Aggregations