Search in sources :

Example 16 with Command

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

the class ShellHelpProvider method getHelp.

public String getHelp(Session session, String path) {
    if (path.indexOf('|') > 0) {
        if (path.startsWith("shell|")) {
            path = path.substring("shell|".length());
        } else {
            return null;
        }
    }
    // Retrieve matching commands
    Set<Command> commands = getCommands(session, path);
    // Compute the scopes and matching bundles
    Set<Bundle> bundles = new HashSet<>();
    Set<String> scopes = new HashSet<>();
    for (Command command : commands) {
        if (command instanceof ActionCommand) {
            Class<? extends Action> action = ((ActionCommand) command).getActionClass();
            bundles.add(FrameworkUtil.getBundle(action));
        }
        scopes.add(command.getScope());
    }
    // use that one instead
    if (scopes.size() == 1 && bundles.size() == 1 && path.equals(scopes.iterator().next())) {
        Bundle bundle = bundles.iterator().next();
        URL resource = bundle.getResource("OSGI-INF/shell-" + path + ".info");
        if (resource != null) {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.openStream()))) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                PrintStream ps = new PrintStream(baos);
                int maxSize = 80;
                Terminal terminal = session.getTerminal();
                if (terminal != null) {
                    maxSize = terminal.getWidth();
                }
                WikiVisitor visitor = new AnsiPrintingWikiVisitor(ps, maxSize);
                WikiParser parser = new WikiParser(visitor);
                parser.parse(reader);
                return baos.toString();
            } catch (IOException e) {
            // ignore
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        printShellHelp(session, new PrintStream(baos), path);
        return baos.toString();
    }
    return null;
}
Also used : PrintStream(java.io.PrintStream) InputStreamReader(java.io.InputStreamReader) Bundle(org.osgi.framework.Bundle) AnsiPrintingWikiVisitor(org.apache.karaf.shell.impl.console.commands.help.wikidoc.AnsiPrintingWikiVisitor) WikiVisitor(org.apache.karaf.shell.impl.console.commands.help.wikidoc.WikiVisitor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) WikiParser(org.apache.karaf.shell.impl.console.commands.help.wikidoc.WikiParser) Terminal(org.apache.karaf.shell.api.console.Terminal) URL(java.net.URL) AnsiPrintingWikiVisitor(org.apache.karaf.shell.impl.console.commands.help.wikidoc.AnsiPrintingWikiVisitor) ActionCommand(org.apache.karaf.shell.impl.action.command.ActionCommand) Command(org.apache.karaf.shell.api.console.Command) ActionCommand(org.apache.karaf.shell.impl.action.command.ActionCommand) BufferedReader(java.io.BufferedReader) HashSet(java.util.HashSet)

Example 17 with Command

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

the class CommandWrapper 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, (org.apache.karaf.shell.api.console.Function) (s, a) -> closure.execute(commandSession, a));
        }
    }
    return command.execute(session, arguments);
}
Also used : Session(org.apache.karaf.shell.api.console.Session) List(java.util.List) Closure(org.apache.felix.gogo.runtime.Closure) Function(org.apache.felix.service.command.Function) CommandSession(org.apache.felix.service.command.CommandSession) Command(org.apache.karaf.shell.api.console.Command) Closure(org.apache.felix.gogo.runtime.Closure) Session(org.apache.karaf.shell.api.console.Session) CommandSession(org.apache.felix.service.command.CommandSession)

Example 18 with Command

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

the class CommandsCompleter method checkData.

@SuppressWarnings("unchecked")
protected Map<String, Completer>[] checkData() {
    // Copy the set to avoid concurrent modification exceptions
    // TODO: fix that in gogo instead
    Collection<Command> commands;
    boolean update;
    synchronized (this) {
        commands = factory.getRegistry().getCommands();
        update = !commands.equals(this.commands);
    }
    if (update) {
        // get command aliases
        Map<String, Completer> global = new HashMap<>();
        Map<String, Completer> local = new HashMap<>();
        // add argument completers for each command
        for (Command command : commands) {
            String key = command.getScope() + ":" + command.getName();
            Completer cg = command.getCompleter(false);
            Completer cl = command.getCompleter(true);
            if (cg == null) {
                if (Session.SCOPE_GLOBAL.equals(command.getScope())) {
                    cg = new FixedSimpleCommandCompleter(Collections.singletonList(command.getName()));
                } else {
                    cg = new FixedSimpleCommandCompleter(Arrays.asList(key, command.getName()));
                }
            }
            if (cl == null) {
                cl = new FixedSimpleCommandCompleter(Collections.singletonList(command.getName()));
            }
            global.put(key, cg);
            local.put(key, cl);
        }
        synchronized (this) {
            this.commands.clear();
            this.globalCompleters.clear();
            this.localCompleters.clear();
            this.commands.addAll(commands);
            this.globalCompleters.putAll(global);
            this.localCompleters.putAll(local);
        }
    }
    synchronized (this) {
        return new Map[] { new HashMap<>(this.globalCompleters), new HashMap<>(this.localCompleters) };
    }
}
Also used : Command(org.apache.karaf.shell.api.console.Command) HashMap(java.util.HashMap) Completer(org.apache.karaf.shell.api.console.Completer) StringsCompleter(org.apache.karaf.shell.support.completers.StringsCompleter) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 19 with Command

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

the class RegistryImpl method register.

@Override
public void register(Object service) {
    synchronized (services) {
        services.put(service, service);
        if (service instanceof Command) {
            Command cmd = (Command) service;
            String key = cmd.getScope() + ":" + cmd.getName();
            commands.computeIfAbsent(key, k -> new ArrayList<>()).add(cmd);
        }
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) Command(org.apache.karaf.shell.api.console.Command) Registry(org.apache.karaf.shell.api.console.Registry) ArrayList(java.util.ArrayList) Command(org.apache.karaf.shell.api.console.Command) ArrayList(java.util.ArrayList)

Example 20 with Command

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

the class RegistryImpl method unregister.

@Override
public void unregister(Object service) {
    synchronized (services) {
        services.remove(service);
        if (service instanceof Command) {
            Command cmd = (Command) service;
            String key = cmd.getScope() + ":" + cmd.getName();
            List<Command> cmds = commands.get(key);
            if (cmds != null) {
                cmds.remove(cmd);
                if (cmds.isEmpty()) {
                    commands.remove(key);
                }
            }
        }
    }
}
Also used : Command(org.apache.karaf.shell.api.console.Command)

Aggregations

Command (org.apache.karaf.shell.api.console.Command)20 HashSet (java.util.HashSet)5 List (java.util.List)4 CommandLine (org.apache.karaf.shell.api.console.CommandLine)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Parser (org.apache.karaf.shell.api.console.Parser)3 StringsCompleter (org.apache.karaf.shell.support.completers.StringsCompleter)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PrintStream (java.io.PrintStream)2 LinkedHashMap (java.util.LinkedHashMap)2 TreeMap (java.util.TreeMap)2 Callable (java.util.concurrent.Callable)2 Completer (org.apache.karaf.shell.api.console.Completer)2 Registry (org.apache.karaf.shell.api.console.Registry)2 Session (org.apache.karaf.shell.api.console.Session)2 ActionCommand (org.apache.karaf.shell.impl.action.command.ActionCommand)2 ExitCommand (org.apache.karaf.shell.impl.console.commands.ExitCommand)2 SubShellCommand (org.apache.karaf.shell.impl.console.commands.SubShellCommand)2