Search in sources :

Example 1 with ThreadIOImpl

use of org.apache.felix.gogo.runtime.threadio.ThreadIOImpl in project karaf by apache.

the class Activator method start.

@Override
public void start(final BundleContext context) throws Exception {
    threadIO = new ThreadIOImpl();
    threadIO.start();
    sessionFactory = new SecuredSessionFactoryImpl(context, threadIO);
    sessionFactory.getCommandProcessor().addConverter(new Converters(context));
    sessionFactory.getCommandProcessor().addConstant(".context", context.getBundle(0).getBundleContext());
    final CopyOnWriteArraySet<CommandLoggingFilter> listeners = new CopyOnWriteArraySet<>();
    filterTracker = new ServiceTracker<>(context, CommandLoggingFilter.class, new ServiceTrackerCustomizer<CommandLoggingFilter, CommandLoggingFilter>() {

        @Override
        public CommandLoggingFilter addingService(ServiceReference<CommandLoggingFilter> reference) {
            CommandLoggingFilter service = context.getService(reference);
            listeners.add(service);
            return service;
        }

        @Override
        public void modifiedService(ServiceReference<CommandLoggingFilter> reference, CommandLoggingFilter service) {
        }

        @Override
        public void removedService(ServiceReference<CommandLoggingFilter> reference, CommandLoggingFilter service) {
            listeners.remove(service);
            context.ungetService(reference);
        }
    });
    filterTracker.open();
    LoggingCommandSessionListener loggingCommandSessionListener = new LoggingCommandSessionListener();
    loggingCommandSessionListener.setFilters(listeners);
    sessionFactory.getCommandProcessor().addListener(loggingCommandSessionListener);
    try {
        EventAdminListener listener = new EventAdminListener(context);
        sessionFactory.getCommandProcessor().addListener(listener);
        eventAdminListener = listener;
    } catch (NoClassDefFoundError error) {
    // Ignore the listener if EventAdmin package isn't present
    }
    sessionFactory.register(new ManagerImpl(sessionFactory, sessionFactory));
    sessionFactoryRegistration = context.registerService(SessionFactory.class, sessionFactory, null);
    actionExtender = new CommandExtender(sessionFactory);
    actionExtender.start(context);
    commandTracker = new CommandTracker(sessionFactory, context);
    commandTracker.open();
    if (Boolean.parseBoolean(context.getProperty(START_CONSOLE))) {
        localConsoleManager = new LocalConsoleManager(context, sessionFactory);
        localConsoleManager.start();
    } else {
        LOGGER.info("Not starting local console. To activate set " + START_CONSOLE + "=true");
    }
}
Also used : SessionFactory(org.apache.karaf.shell.api.console.SessionFactory) SecuredSessionFactoryImpl(org.apache.karaf.shell.impl.console.osgi.secured.SecuredSessionFactoryImpl) ThreadIOImpl(org.apache.felix.gogo.runtime.threadio.ThreadIOImpl) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) ServiceReference(org.osgi.framework.ServiceReference) CommandExtender(org.apache.karaf.shell.impl.action.osgi.CommandExtender) ManagerImpl(org.apache.karaf.shell.impl.action.command.ManagerImpl) CommandLoggingFilter(org.apache.karaf.shell.api.console.CommandLoggingFilter)

Example 2 with ThreadIOImpl

use of org.apache.felix.gogo.runtime.threadio.ThreadIOImpl 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 3 with ThreadIOImpl

use of org.apache.felix.gogo.runtime.threadio.ThreadIOImpl 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 4 with ThreadIOImpl

use of org.apache.felix.gogo.runtime.threadio.ThreadIOImpl in project karaf by apache.

the class ShellTableTest method testNonUtf8.

private void testNonUtf8(String encoding) throws Exception {
    ShellTable table = new ShellTable();
    table.column("col1");
    table.column("col2").maxSize(-1).wrap();
    table.addRow().addContent("my first column value", "my second column value is quite long");
    table.size(50);
    ThreadIO tio = new ThreadIOImpl();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos, false, encoding);
    table.print(ps, true);
    tio.setStreams(new FileInputStream(FileDescriptor.in), ps, ps);
    table.print(System.out);
    assertEquals("col1                  | col2\n" + "----------------------+---------------------------\n" + "my first column value | my second column value is\n" + "                      | quite long\n" + "col1                  | col2\n" + "----------------------+---------------------------\n" + "my first column value | my second column value is\n" + "                      | quite long\n", baos.toString());
}
Also used : ThreadIO(org.apache.felix.service.threadio.ThreadIO) PrintStream(java.io.PrintStream) ThreadIOImpl(org.apache.felix.gogo.runtime.threadio.ThreadIOImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileInputStream(java.io.FileInputStream)

Example 5 with ThreadIOImpl

use of org.apache.felix.gogo.runtime.threadio.ThreadIOImpl in project karaf by apache.

the class Main method run.

/**
     * Use this method when the shell is being executed as a top level shell.
     *
     * @param args the arguments.
     * @throws Exception in case of a failure.
     */
public void run(String[] args) throws Exception {
    InputStream in = System.in;
    PrintStream out = System.out;
    PrintStream err = System.err;
    ThreadIOImpl threadio = new ThreadIOImpl();
    threadio.start();
    run(threadio, args, in, out, err);
// TODO: do we need to stop the threadio that was started?
// threadio.stop();
}
Also used : PrintStream(java.io.PrintStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ThreadIOImpl(org.apache.felix.gogo.runtime.threadio.ThreadIOImpl)

Aggregations

ThreadIOImpl (org.apache.felix.gogo.runtime.threadio.ThreadIOImpl)5 PrintStream (java.io.PrintStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ManagerImpl (org.apache.karaf.shell.impl.action.command.ManagerImpl)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 Session (org.apache.karaf.shell.api.console.Session)2 ActionCommand (org.apache.karaf.shell.impl.action.command.ActionCommand)2 HeadlessSessionImpl (org.apache.karaf.shell.impl.console.HeadlessSessionImpl)2 SessionFactoryImpl (org.apache.karaf.shell.impl.console.SessionFactoryImpl)2 Test (org.junit.Test)2 InputStream (java.io.InputStream)1 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)1 ThreadIO (org.apache.felix.service.threadio.ThreadIO)1 CommandLoggingFilter (org.apache.karaf.shell.api.console.CommandLoggingFilter)1 SessionFactory (org.apache.karaf.shell.api.console.SessionFactory)1 CommandExtender (org.apache.karaf.shell.impl.action.osgi.CommandExtender)1 SecuredSessionFactoryImpl (org.apache.karaf.shell.impl.console.osgi.secured.SecuredSessionFactoryImpl)1 ServiceReference (org.osgi.framework.ServiceReference)1 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)1