Search in sources :

Example 1 with ManagerImpl

use of org.apache.karaf.shell.impl.action.command.ManagerImpl in project karaf by apache.

the class CommandExtension method updateState.

@SuppressWarnings("unchecked")
private synchronized void updateState(AggregateServiceTracker.State state) {
    boolean wasSatisfied = manager != null;
    boolean isSatisfied = state != null && state.isSatisfied();
    String action;
    if (wasSatisfied && isSatisfied) {
        action = "Updating";
    } else if (wasSatisfied) {
        action = "Unregistering";
    } else if (isSatisfied) {
        action = "Registering";
    } else {
        return;
    }
    LOGGER.info("{} commands for bundle {}/{}", action, bundle.getSymbolicName(), bundle.getVersion());
    if (wasSatisfied) {
        for (Class clazz : classes) {
            manager.unregister(clazz);
        }
        manager = null;
    }
    if (isSatisfied) {
        Registry reg = new RegistryImpl(registry);
        manager = new ManagerImpl(reg, registry);
        reg.register(bundle.getBundleContext());
        reg.register(manager);
        for (Map.Entry<Class, Object> entry : state.getSingleServices().entrySet()) {
            reg.register(entry.getValue());
        }
        for (final Map.Entry<Class, List> entry : state.getMultiServices().entrySet()) {
            reg.register((Callable) entry::getValue, entry.getKey());
        }
        for (Class clazz : classes) {
            manager.register(clazz);
        }
    }
}
Also used : ManagerImpl(org.apache.karaf.shell.impl.action.command.ManagerImpl) ArrayList(java.util.ArrayList) List(java.util.List) Registry(org.apache.karaf.shell.api.console.Registry) Map(java.util.Map)

Example 2 with ManagerImpl

use of org.apache.karaf.shell.impl.action.command.ManagerImpl 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);
    commandProcessorRegistration = context.registerService(CommandProcessor.class, sessionFactory.getCommandProcessor(), null);
    actionExtender = new CommandExtender(sessionFactory);
    actionExtender.start(context);
    commandTracker = new CommandTracker(sessionFactory, context);
    commandTracker.open();
    converterTracker = new ConverterTracker(sessionFactory, context);
    converterTracker.open();
    listenerTracker = new ListenerTracker(sessionFactory, context);
    listenerTracker.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) CommandProcessor(org.apache.felix.service.command.CommandProcessor)

Example 3 with ManagerImpl

use of org.apache.karaf.shell.impl.action.command.ManagerImpl in project karaf by apache.

the class Main method discoverCommands.

protected void discoverCommands(Session session, ClassLoader cl, String resource) throws IOException, ClassNotFoundException {
    Manager manager = new ManagerImpl(session.getRegistry(), session.getFactory().getRegistry(), true);
    Enumeration<URL> urls = cl.getResources(resource);
    while (urls.hasMoreElements()) {
        URL url = urls.nextElement();
        BufferedReader r = new BufferedReader(new InputStreamReader(url.openStream()));
        String line = r.readLine();
        while (line != null) {
            line = line.trim();
            if (line.length() > 0 && line.charAt(0) != '#') {
                final Class<?> actionClass = cl.loadClass(line);
                manager.register(actionClass);
            }
            line = r.readLine();
        }
        r.close();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ManagerImpl(org.apache.karaf.shell.impl.action.command.ManagerImpl) BufferedReader(java.io.BufferedReader) Manager(org.apache.karaf.shell.api.action.lifecycle.Manager) URL(java.net.URL)

Example 4 with ManagerImpl

use of org.apache.karaf.shell.impl.action.command.ManagerImpl 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 ManagerImpl

use of org.apache.karaf.shell.impl.action.command.ManagerImpl 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)

Aggregations

ManagerImpl (org.apache.karaf.shell.impl.action.command.ManagerImpl)6 ThreadIOImpl (org.apache.felix.gogo.runtime.threadio.ThreadIOImpl)3 SessionFactoryImpl (org.apache.karaf.shell.impl.console.SessionFactoryImpl)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PrintStream (java.io.PrintStream)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 Test (org.junit.Test)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)1 CommandProcessor (org.apache.felix.service.command.CommandProcessor)1 Manager (org.apache.karaf.shell.api.action.lifecycle.Manager)1 CommandLoggingFilter (org.apache.karaf.shell.api.console.CommandLoggingFilter)1