Search in sources :

Example 6 with Session

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;
}
Also used : SessionFactory(org.apache.karaf.shell.api.console.SessionFactory) PrintStream(java.io.PrintStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) TimeoutException(java.util.concurrent.TimeoutException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) RerunTestException(org.ops4j.pax.exam.RerunTestException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Subject(javax.security.auth.Subject) ExecutionException(java.util.concurrent.ExecutionException) Session(org.apache.karaf.shell.api.console.Session) TimeoutException(java.util.concurrent.TimeoutException)

Example 7 with Session

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);
}
Also used : Session(org.apache.karaf.shell.api.console.Session) Test(org.junit.Test)

Example 8 with Session

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);
}
Also used : Session(org.apache.karaf.shell.api.console.Session) Test(org.junit.Test)

Example 9 with Session

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");
}
Also used : Session(org.apache.karaf.shell.api.console.Session) Test(org.junit.Test)

Example 10 with Session

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);
}
Also used : Closure(org.apache.felix.gogo.runtime.Closure) Token(org.apache.felix.gogo.runtime.Token) Session(org.apache.karaf.shell.api.console.Session) CommandSession(org.apache.felix.service.command.CommandSession)

Aggregations

Session (org.apache.karaf.shell.api.console.Session)18 PrintStream (java.io.PrintStream)7 Test (org.junit.Test)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Subject (javax.security.auth.Subject)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 List (java.util.List)3 CommandSession (org.apache.felix.service.command.CommandSession)3 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)2 Closure (org.apache.felix.gogo.runtime.Closure)2 ThreadIOImpl (org.apache.felix.gogo.runtime.threadio.ThreadIOImpl)2 Command (org.apache.karaf.shell.api.console.Command)2 Completer (org.apache.karaf.shell.api.console.Completer)2 SessionFactory (org.apache.karaf.shell.api.console.SessionFactory)2 ActionCommand (org.apache.karaf.shell.impl.action.command.ActionCommand)2 ManagerImpl (org.apache.karaf.shell.impl.action.command.ManagerImpl)2 HeadlessSessionImpl (org.apache.karaf.shell.impl.console.HeadlessSessionImpl)2 JLineTerminal (org.apache.karaf.shell.impl.console.JLineTerminal)2 SessionFactoryImpl (org.apache.karaf.shell.impl.console.SessionFactoryImpl)2