Search in sources :

Example 1 with Session

use of org.apache.karaf.shell.api.console.Session in project karaf by apache.

the class ManageRealmCommandTest method doVerifyIndex.

/**
     * Verify that command selects the correct realm, given some index.
     *
     * @param cmd the command to use.
     * @param index the index to verify.
     * @param realms the array of realms.
     * @throws Exception in case of failure.
     */
private void doVerifyIndex(ManageRealmCommand cmd, int index, Config[] realms) throws Exception {
    // prepare mocks
    Session session = createMock(Session.class);
    BundleContext bundleContext = createMock(BundleContext.class);
    Bundle bundle = createMock(Bundle.class);
    // prepare command
    cmd.index = index;
    cmd.setRealms(Arrays.asList(realms));
    cmd.setSession(session);
    for (Config realm : realms) realm.setBundleContext(bundleContext);
    Object[] mocks = { session, bundleContext, bundle };
    expect(session.get(ManageRealmCommand.JAAS_REALM)).andReturn(null).anyTimes();
    expect(session.get(ManageRealmCommand.JAAS_ENTRY)).andReturn(null).anyTimes();
    expect(session.get(ManageRealmCommand.JAAS_CMDS)).andReturn(null).anyTimes();
    expect(bundleContext.getBundle()).andReturn(bundle).anyTimes();
    expect(bundle.getBundleId()).andReturn(4711L).anyTimes();
    // verify that the correct realm is returned -- cmd.index is 1-based
    session.put(ManageRealmCommand.JAAS_REALM, realms[index - 1]);
    session.put(eq(ManageRealmCommand.JAAS_ENTRY), anyObject());
    session.put(eq(ManageRealmCommand.JAAS_CMDS), anyObject());
    // start the test
    replay(mocks);
    cmd.execute();
    verify(mocks);
}
Also used : Bundle(org.osgi.framework.Bundle) Config(org.apache.karaf.jaas.config.impl.Config) EasyMock.anyObject(org.easymock.EasyMock.anyObject) Session(org.apache.karaf.shell.api.console.Session) BundleContext(org.osgi.framework.BundleContext)

Example 2 with Session

use of org.apache.karaf.shell.api.console.Session in project karaf by apache.

the class LocalConsoleManager method start.

public void start() throws Exception {
    final Terminal terminal = TerminalBuilder.builder().nativeSignals(true).signalHandler(Terminal.SignalHandler.SIG_IGN).build();
    final Subject subject = createLocalKarafSubject();
    this.session = JaasHelper.doAs(subject, (PrivilegedAction<Session>) () -> {
        String encoding = getEncoding();
        session = sessionFactory.create(terminal.input(), new PrintStream(terminal.output()), new PrintStream(terminal.output()), new JLineTerminal(terminal), encoding, LocalConsoleManager.this::close);
        registration = bundleContext.registerService(Session.class, session, null);
        String name = "Karaf local console user " + ShellUtil.getCurrentUserName();
        boolean delayconsole = Boolean.parseBoolean(System.getProperty(KARAF_DELAY_CONSOLE));
        if (delayconsole) {
            DelayedStarted watcher = new DelayedStarted(session, name, bundleContext, System.in);
            new Thread(watcher).start();
        } else {
            new Thread(session, name).start();
        }
        return session;
    });
// TODO: register the local session so that ssh can add the agent
//        registration = bundleContext.register(CommandSession.class, console.getSession(), null);
}
Also used : JLineTerminal(org.apache.karaf.shell.impl.console.JLineTerminal) PrintStream(java.io.PrintStream) PrivilegedAction(java.security.PrivilegedAction) JLineTerminal(org.apache.karaf.shell.impl.console.JLineTerminal) Terminal(org.jline.terminal.Terminal) Subject(javax.security.auth.Subject) Session(org.apache.karaf.shell.api.console.Session)

Example 3 with Session

use of org.apache.karaf.shell.api.console.Session in project karaf by apache.

the class ParsingTest method testCommandLineParserMultiLine.

@Test
public void testCommandLineParserMultiLine() {
    SessionFactoryImpl sessionFactory = new SessionFactoryImpl(new ThreadIOImpl());
    ManagerImpl manager = new ManagerImpl(sessionFactory, sessionFactory);
    sessionFactory.getRegistry().register(new ActionCommand(manager, FooCommand.class));
    sessionFactory.getRegistry().register(new ActionCommand(manager, AnotherCommand.class));
    sessionFactory.getRegistry().register(new CustomParser());
    Session session = new HeadlessSessionImpl(sessionFactory, sessionFactory.getCommandProcessor(), new ByteArrayInputStream(new byte[0]), new PrintStream(new ByteArrayOutputStream()), new PrintStream(new ByteArrayOutputStream()));
    String parsed = CommandLineParser.parse(session, "echo a\necho b");
    assertEquals("echo a\necho b", parsed);
}
Also used : PrintStream(java.io.PrintStream) HeadlessSessionImpl(org.apache.karaf.shell.impl.console.HeadlessSessionImpl) ThreadIOImpl(org.apache.felix.gogo.runtime.threadio.ThreadIOImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ManagerImpl(org.apache.karaf.shell.impl.action.command.ManagerImpl) ActionCommand(org.apache.karaf.shell.impl.action.command.ActionCommand) SessionFactoryImpl(org.apache.karaf.shell.impl.console.SessionFactoryImpl) Session(org.apache.karaf.shell.api.console.Session) Test(org.junit.Test)

Example 4 with Session

use of org.apache.karaf.shell.api.console.Session in project karaf by apache.

the class ParsingTest method testCommandLineParser.

@Test
public void testCommandLineParser() {
    SessionFactoryImpl sessionFactory = new SessionFactoryImpl(new ThreadIOImpl());
    ManagerImpl manager = new ManagerImpl(sessionFactory, sessionFactory);
    sessionFactory.getRegistry().register(new ActionCommand(manager, FooCommand.class));
    sessionFactory.getRegistry().register(new ActionCommand(manager, AnotherCommand.class));
    sessionFactory.getRegistry().register(new CustomParser());
    Session session = new HeadlessSessionImpl(sessionFactory, sessionFactory.getCommandProcessor(), new ByteArrayInputStream(new byte[0]), new PrintStream(new ByteArrayOutputStream()), new PrintStream(new ByteArrayOutputStream()));
    String parsed = CommandLineParser.parse(session, " foo bar (a + b); another   command with spaces ");
    assertEquals("foo bar (a + b) ; another \"command with spaces\"", parsed);
}
Also used : PrintStream(java.io.PrintStream) HeadlessSessionImpl(org.apache.karaf.shell.impl.console.HeadlessSessionImpl) ThreadIOImpl(org.apache.felix.gogo.runtime.threadio.ThreadIOImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ManagerImpl(org.apache.karaf.shell.impl.action.command.ManagerImpl) ActionCommand(org.apache.karaf.shell.impl.action.command.ActionCommand) SessionFactoryImpl(org.apache.karaf.shell.impl.console.SessionFactoryImpl) Session(org.apache.karaf.shell.api.console.Session) Test(org.junit.Test)

Example 5 with Session

use of org.apache.karaf.shell.api.console.Session in project karaf by apache.

the class Main method run.

private void run(final SessionFactory sessionFactory, String command, final InputStream in, final PrintStream out, final PrintStream err, ClassLoader cl) throws Exception {
    try (org.jline.terminal.Terminal jlineTerminal = TerminalBuilder.terminal()) {
        final Terminal terminal = new JLineTerminal(jlineTerminal);
        try (Session session = createSession(sessionFactory, command.length() > 0 ? null : in, out, err, terminal)) {
            session.put("USER", user);
            session.put("APPLICATION", application);
            discoverCommands(session, cl, getDiscoveryResource());
            if (command.length() > 0) {
                // Shell is directly executing a sub/command, we don't setup a console
                // in this case, this avoids us reading from stdin un-necessarily.
                session.put(NameScoping.MULTI_SCOPE_MODE_KEY, Boolean.toString(isMultiScopeMode()));
                session.put(Session.PRINT_STACK_TRACES, "execution");
                try {
                    session.execute(command);
                } catch (Throwable t) {
                    ShellUtil.logException(session, t);
                }
            } else {
                // We are going into full blown interactive shell mode.
                session.run();
            }
        }
    }
}
Also used : JLineTerminal(org.apache.karaf.shell.impl.console.JLineTerminal) JLineTerminal(org.apache.karaf.shell.impl.console.JLineTerminal) Terminal(org.apache.karaf.shell.api.console.Terminal) Session(org.apache.karaf.shell.api.console.Session)

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