Search in sources :

Example 1 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 2 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 3 with Command

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

the class KarafParser method doParse.

private ParsedLine doParse(String line, int cursor, ParseContext parseContext) throws SyntaxError {
    Program program = null;
    List<Statement> statements = null;
    String repaired = line;
    while (program == null) {
        try {
            org.apache.felix.gogo.runtime.Parser parser = new org.apache.felix.gogo.runtime.Parser(repaired);
            program = parser.program();
            statements = parser.statements();
        } catch (EOFError e) {
            // Make sure we don't loop forever
            if (parseContext == ParseContext.COMPLETE && repaired.length() < line.length() + 1024) {
                repaired = repaired + " " + e.repair();
            } else {
                throw e;
            }
        }
    }
    // Find corresponding statement
    Statement statement = null;
    for (int i = statements.size() - 1; i >= 0; i--) {
        Statement s = statements.get(i);
        if (s.start() <= cursor) {
            boolean isOk = true;
            // check if there are only spaces after the previous statement
            if (s.start() + s.length() < cursor) {
                for (int j = s.start() + s.length(); isOk && j < cursor; j++) {
                    isOk = Character.isWhitespace(line.charAt(j));
                }
            }
            statement = s;
            break;
        }
    }
    if (statement != null && statement.tokens() != null && !statement.tokens().isEmpty()) {
        String cmdName = session.resolveCommand(statement.tokens().get(0).toString());
        String[] parts = cmdName.split(":");
        Command cmd = parts.length == 2 ? session.getRegistry().getCommand(parts[0], parts[1]) : null;
        Parser cmdParser = cmd != null ? cmd.getParser() : null;
        if (cmdParser != null) {
            final CommandLine cmdLine = cmdParser.parse(session, statement.toString(), cursor - statement.start());
            return new ParsedLine() {

                @Override
                public String word() {
                    return cmdLine.getCursorArgument();
                }

                @Override
                public int wordCursor() {
                    return cmdLine.getArgumentPosition();
                }

                @Override
                public int wordIndex() {
                    return cmdLine.getCursorArgumentIndex();
                }

                @Override
                public List<String> words() {
                    return Arrays.asList(cmdLine.getArguments());
                }

                @Override
                public String line() {
                    return cmdLine.getBuffer();
                }

                @Override
                public int cursor() {
                    return cmdLine.getBufferPosition();
                }
            };
        }
        if (repaired != line) {
            Token stmt = statement.subSequence(0, line.length() - statement.start());
            List<Token> tokens = new ArrayList<>(statement.tokens());
            Token last = tokens.get(tokens.size() - 1);
            tokens.set(tokens.size() - 1, last.subSequence(0, line.length() - last.start()));
            return new ParsedLineImpl(program, stmt, cursor, tokens);
        }
        return new ParsedLineImpl(program, statement, cursor, statement.tokens());
    } else {
        // TODO:
        return new ParsedLineImpl(program, program, cursor, Collections.singletonList(program));
    }
}
Also used : Program(org.apache.felix.gogo.runtime.Parser.Program) Statement(org.apache.felix.gogo.runtime.Parser.Statement) ArrayList(java.util.ArrayList) Token(org.apache.felix.gogo.runtime.Token) Parser(org.apache.karaf.shell.api.console.Parser) CommandLine(org.apache.karaf.shell.api.console.CommandLine) Command(org.apache.karaf.shell.api.console.Command) ParsedLineImpl(org.apache.felix.gogo.jline.ParsedLineImpl) ParsedLine(org.jline.reader.ParsedLine) EOFError(org.apache.felix.gogo.runtime.EOFError)

Example 4 with Command

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

the class CommandLineParser method parse.

public static String parse(Session session, String command) {
    StringBuilder parsed = new StringBuilder();
    int pos = 0;
    while (pos < command.length()) {
        String rem = command.substring(pos);
        GogoParser cmdNameParser = new GogoParser(rem, rem.length());
        String name = cmdNameParser.value();
        name = session.resolveCommand(name);
        Parser cmdParser = null;
        for (Command cmd : session.getRegistry().getCommands()) {
            if (name.equals(cmd.getScope() + ":" + cmd.getName())) {
                cmdParser = cmd.getParser();
                break;
            }
        }
        if (cmdParser == null) {
            cmdParser = new DefaultParser();
        }
        CommandLine cmdLine = cmdParser.parse(session, rem, rem.length());
        parsed.append(cmdParser.preprocess(session, cmdLine));
        int length = cmdLine.getBuffer().length();
        if (length < rem.length()) {
            char ch = rem.charAt(length);
            if (ch == ';' || ch == '|') {
                parsed.append(" ");
                parsed.append(ch);
                parsed.append(" ");
                length++;
            } else if (ch == '\n') {
                parsed.append(ch);
                length++;
            } else {
                throw new IllegalArgumentException("Unrecognized character: '" + ch + "'");
            }
        }
        pos += length;
    }
    return parsed.toString();
}
Also used : CommandLine(org.apache.karaf.shell.api.console.CommandLine) Command(org.apache.karaf.shell.api.console.Command) GogoParser(org.apache.karaf.shell.support.parsing.GogoParser) DefaultParser(org.apache.karaf.shell.support.parsing.DefaultParser) GogoParser(org.apache.karaf.shell.support.parsing.GogoParser) Parser(org.apache.karaf.shell.api.console.Parser) DefaultParser(org.apache.karaf.shell.support.parsing.DefaultParser)

Example 5 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) {
            boolean globalScope = Session.SCOPE_GLOBAL.equals(scope);
            for (Command command : commands) {
                if ((globalScope || command.getScope().equals(scope)) && command.getName().equals(name)) {
                    return command.getScope() + ":" + name;
                }
            }
        }
    }
    return name;
}
Also used : ActionCommand(org.apache.karaf.shell.impl.action.command.ActionCommand) Command(org.apache.karaf.shell.api.console.Command)

Aggregations

Command (org.apache.karaf.shell.api.console.Command)22 HashSet (java.util.HashSet)5 CommandLine (org.apache.karaf.shell.api.console.CommandLine)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Parser (org.apache.karaf.shell.api.console.Parser)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Session (org.apache.karaf.shell.api.console.Session)3 ActionCommand (org.apache.karaf.shell.impl.action.command.ActionCommand)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 ParsedLineImpl (org.apache.felix.gogo.jline.ParsedLineImpl)2 Program (org.apache.felix.gogo.runtime.Parser.Program)2 Statement (org.apache.felix.gogo.runtime.Parser.Statement)2 Completer (org.apache.karaf.shell.api.console.Completer)2