Search in sources :

Example 6 with Command

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

the class ShellHelpProvider method getCommands.

private Set<Command> getCommands(Session session, String path) {
    // TODO: this is not really clean
    List<Command> commands = session.getRegistry().getCommands();
    String subshell = (String) session.get(Session.SUBSHELL);
    String completionMode = (String) session.get(Session.COMPLETION_MODE);
    Set<Command> matchingCommands = new HashSet<>();
    for (Command command : commands) {
        String name = command.getScope() + ":" + command.getName();
        if (command != null && !name.startsWith(path)) {
            continue;
        }
        if (completionMode != null && completionMode.equalsIgnoreCase(Session.COMPLETION_MODE_SUBSHELL)) {
            // filter the help only for "global" commands
            if (subshell == null || subshell.trim().isEmpty()) {
                if (!name.startsWith(Session.SCOPE_GLOBAL)) {
                    continue;
                }
            }
        }
        if (completionMode != null && (completionMode.equalsIgnoreCase(Session.COMPLETION_MODE_SUBSHELL) || completionMode.equalsIgnoreCase(Session.COMPLETION_MODE_FIRST))) {
            // filter the help only for commands local to the subshell
            if (!name.startsWith(subshell)) {
                continue;
            }
        }
        matchingCommands.add(command);
    }
    return matchingCommands;
}
Also used : ActionCommand(org.apache.karaf.shell.impl.action.command.ActionCommand) Command(org.apache.karaf.shell.api.console.Command) HashSet(java.util.HashSet)

Example 7 with Command

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

the class ConsoleSessionImpl method resolveCommand.

@Override
public String resolveCommand(String name) {
    // TODO: optimize
    if (!name.contains(":")) {
        String[] scopes = ((String) get(Session.SCOPE)).split(":");
        List<Command> commands = registry.getCommands();
        for (String scope : scopes) {
            for (Command command : commands) {
                if ((Session.SCOPE_GLOBAL.equals(scope) || command.getScope().equals(scope)) && command.getName().equals(name)) {
                    return command.getScope() + ":" + name;
                }
            }
        }
    }
    return name;
}
Also used : Command(org.apache.karaf.shell.api.console.Command)

Example 8 with Command

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

the class HeadlessSessionImpl method resolveCommand.

@Override
public String resolveCommand(String name) {
    // TODO: optimize
    if (!name.contains(":")) {
        String[] scopes = ((String) get(Session.SCOPE)).split(":");
        List<Command> commands = registry.getCommands();
        for (String scope : scopes) {
            for (Command command : commands) {
                if ((Session.SCOPE_GLOBAL.equals(scope) || command.getScope().equals(scope)) && command.getName().equals(name)) {
                    return command.getScope() + ":" + name;
                }
            }
        }
    }
    return name;
}
Also used : Command(org.apache.karaf.shell.api.console.Command)

Example 9 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)

Example 10 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)

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