Search in sources :

Example 1 with CommandWithAction

use of org.apache.karaf.shell.commands.CommandWithAction in project karaf by apache.

the class ArgumentCompleter method getCompleterValues.

private Map<Integer, Object> getCompleterValues(CommandWithAction function) {
    final Map<Integer, Object> values = new HashMap<>();
    Action action = null;
    try {
        for (Class<?> type = function.getActionClass(); type != null; type = type.getSuperclass()) {
            for (Method method : type.getDeclaredMethods()) {
                CompleterValues completerMethod = method.getAnnotation(CompleterValues.class);
                if (completerMethod != null) {
                    int index = completerMethod.index();
                    Integer key = index;
                    if (index >= arguments.size() || index < 0) {
                        LOGGER.warn("Index out of range on @CompleterValues on class " + type.getName() + " for index: " + key + " see: " + method);
                    } else if (values.containsKey(key)) {
                        LOGGER.warn("Duplicate @CompleterMethod annotations on class " + type.getName() + " for index: " + key + " see: " + method);
                    } else {
                        try {
                            Object value;
                            if (Modifier.isStatic(method.getModifiers())) {
                                value = method.invoke(null);
                            } else {
                                if (action == null) {
                                    action = function.createNewAction();
                                }
                                value = method.invoke(action);
                            }
                            values.put(key, value);
                        } catch (IllegalAccessException e) {
                            LOGGER.warn("Could not invoke @CompleterMethod on " + function + ". " + e, e);
                        } catch (InvocationTargetException e) {
                            Throwable target = e.getTargetException();
                            if (target == null) {
                                target = e;
                            }
                            LOGGER.warn("Could not invoke @CompleterMethod on " + function + ". " + target, target);
                        }
                    }
                }
            }
        }
    } finally {
        if (action != null) {
            try {
                function.releaseAction(action);
            } catch (Exception e) {
                LOGGER.warn("Failed to release action: " + action + ". " + e, e);
            }
        }
    }
    return values;
}
Also used : CommandWithAction(org.apache.karaf.shell.commands.CommandWithAction) Action(org.apache.felix.gogo.commands.Action) CompleterValues(org.apache.karaf.shell.commands.CompleterValues) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with CommandWithAction

use of org.apache.karaf.shell.commands.CommandWithAction in project karaf by apache.

the class ArgumentCompleter method getCompleterValues.

private Map<Integer, Object> getCompleterValues(CommandWithAction function) {
    final Map<Integer, Object> values = new HashMap<>();
    Action action = null;
    try {
        for (Class<?> type = function.getActionClass(); type != null; type = type.getSuperclass()) {
            for (Method method : type.getDeclaredMethods()) {
                CompleterValues completerMethod = method.getAnnotation(CompleterValues.class);
                if (completerMethod != null) {
                    int index = completerMethod.index();
                    Integer key = index;
                    if (index >= arguments.size() || index < 0) {
                        LOGGER.warn("Index out of range on @CompleterValues on class " + type.getName() + " for index: " + key + " see: " + method);
                    } else if (values.containsKey(key)) {
                        LOGGER.warn("Duplicate @CompleterMethod annotations on class " + type.getName() + " for index: " + key + " see: " + method);
                    } else {
                        try {
                            Object value;
                            if (Modifier.isStatic(method.getModifiers())) {
                                value = method.invoke(null);
                            } else {
                                if (action == null) {
                                    action = function.createNewAction();
                                }
                                value = method.invoke(action);
                            }
                            values.put(key, value);
                        } catch (IllegalAccessException e) {
                            LOGGER.warn("Could not invoke @CompleterMethod on " + function + ". " + e, e);
                        } catch (InvocationTargetException e) {
                            Throwable target = e.getTargetException();
                            if (target == null) {
                                target = e;
                            }
                            LOGGER.warn("Could not invoke @CompleterMethod on " + function + ". " + target, target);
                        }
                    }
                }
            }
        }
    } finally {
        if (action != null) {
            try {
                function.releaseAction(action);
            } catch (Exception e) {
                LOGGER.warn("Failed to release action: " + action + ". " + e, e);
            }
        }
    }
    return values;
}
Also used : CommandWithAction(org.apache.karaf.shell.commands.CommandWithAction) Action(org.apache.felix.gogo.commands.Action) CompleterValues(org.apache.karaf.shell.commands.CompleterValues) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with CommandWithAction

use of org.apache.karaf.shell.commands.CommandWithAction in project karaf by apache.

the class CommandTracker method addingService.

@Override
public Object addingService(final ServiceReference reference) {
    Object service = context.getService(reference);
    if (service instanceof CommandWithAction) {
        final CommandWithAction oldCommand = (CommandWithAction) service;
        final org.apache.karaf.shell.api.console.Command command = new org.apache.karaf.shell.api.console.Command() {

            @Override
            public String getScope() {
                return reference.getProperty(CommandProcessor.COMMAND_SCOPE).toString();
            }

            @Override
            public String getName() {
                return reference.getProperty(CommandProcessor.COMMAND_FUNCTION).toString();
            }

            @Override
            public String getDescription() {
                final Command cmd = oldCommand.getActionClass().getAnnotation(Command.class);
                if (cmd != null) {
                    return cmd.description();
                }
                try {
                    Method method = oldCommand.getActionClass().getMethod("description");
                    method.setAccessible(true);
                    return (String) method.invoke(oldCommand.createNewAction());
                } catch (Throwable ignore) {
                }
                return getName();
            }

            @Override
            public Completer getCompleter(final boolean scoped) {
                final ArgumentCompleter completer = new ArgumentCompleter(oldCommand, getScope(), getName(), scoped);
                return completer::complete;
            }

            @Override
            public Parser getParser() {
                return null;
            }

            @Override
            public Object execute(Session session, List<Object> arguments) throws Exception {
                // TODO: remove not really nice cast
                CommandSession commandSession = (CommandSession) session.get(".commandSession");
                return oldCommand.execute(commandSession, arguments);
            }
        };
        sessionFactory.getRegistry().register(command);
        return command;
    } else if (service instanceof org.apache.felix.gogo.commands.CommandWithAction) {
        final org.apache.felix.gogo.commands.CommandWithAction oldCommand = (org.apache.felix.gogo.commands.CommandWithAction) service;
        final org.apache.karaf.shell.api.console.Command command = new org.apache.karaf.shell.api.console.Command() {

            @Override
            public String getScope() {
                return reference.getProperty(CommandProcessor.COMMAND_SCOPE).toString();
            }

            @Override
            public String getName() {
                return reference.getProperty(CommandProcessor.COMMAND_FUNCTION).toString();
            }

            @Override
            public String getDescription() {
                final org.apache.felix.gogo.commands.Command cmd = oldCommand.getActionClass().getAnnotation(org.apache.felix.gogo.commands.Command.class);
                if (cmd != null) {
                    return cmd.description();
                }
                try {
                    Method method = oldCommand.getActionClass().getMethod("description");
                    method.setAccessible(true);
                    return (String) method.invoke(oldCommand.createNewAction());
                } catch (Throwable ignore) {
                }
                return getName();
            }

            @Override
            public Completer getCompleter(final boolean scoped) {
                final OldArgumentCompleter completer = new OldArgumentCompleter(oldCommand, getScope(), getName(), scoped);
                return completer::complete;
            }

            @Override
            public Parser getParser() {
                return null;
            }

            @Override
            public Object execute(Session session, List<Object> arguments) throws Exception {
                // TODO: remove not really nice cast
                CommandSession commandSession = (CommandSession) session.get(".commandSession");
                return oldCommand.execute(commandSession, arguments);
            }
        };
        sessionFactory.getRegistry().register(command);
        return command;
    } else {
        return null;
    }
}
Also used : List(java.util.List) Completer(org.apache.karaf.shell.api.console.Completer) Method(java.lang.reflect.Method) CommandWithAction(org.apache.karaf.shell.commands.CommandWithAction) Parser(org.apache.karaf.shell.api.console.Parser) CommandSession(org.apache.felix.service.command.CommandSession) Command(org.apache.karaf.shell.commands.Command) Session(org.apache.karaf.shell.api.console.Session) CommandSession(org.apache.felix.service.command.CommandSession)

Example 4 with CommandWithAction

use of org.apache.karaf.shell.commands.CommandWithAction 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
    Set<String> names;
    boolean update;
    synchronized (this) {
        names = new HashSet<>((Set<String>) session.get(COMMANDS));
        update = !names.equals(commands);
    }
    if (update) {
        // get command aliases
        Set<String> commands = new HashSet<>();
        Map<String, Completer> global = new HashMap<>();
        Map<String, Completer> local = new HashMap<>();
        // add argument completers for each command
        for (String command : names) {
            String rawCommand = stripScope(command);
            Function function = (Function) session.get(command);
            function = unProxy(function);
            if (function instanceof CommandWithAction) {
                try {
                    global.put(command, new ArgumentCompleter(session, (CommandWithAction) function, command));
                    local.put(command, new ArgumentCompleter(session, (CommandWithAction) function, rawCommand));
                } catch (Throwable t) {
                    LOGGER.debug("Unable to create completers for command '" + command + "'", t);
                }
            } else if (function instanceof org.apache.felix.gogo.commands.CommandWithAction) {
                try {
                    global.put(command, new OldArgumentCompleter(session, (org.apache.felix.gogo.commands.CommandWithAction) function, command));
                    local.put(command, new OldArgumentCompleter(session, (org.apache.felix.gogo.commands.CommandWithAction) function, rawCommand));
                } catch (Throwable t) {
                    LOGGER.debug("Unable to create completers for command '" + command + "'", t);
                }
            }
            commands.add(command);
        }
        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 : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Completer(org.apache.karaf.shell.console.Completer) CommandWithAction(org.apache.karaf.shell.commands.CommandWithAction) Function(org.apache.felix.service.command.Function) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

CommandWithAction (org.apache.karaf.shell.commands.CommandWithAction)4 Method (java.lang.reflect.Method)3 HashMap (java.util.HashMap)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Action (org.apache.felix.gogo.commands.Action)2 CompleterValues (org.apache.karaf.shell.commands.CompleterValues)2 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeMap (java.util.TreeMap)1 CommandSession (org.apache.felix.service.command.CommandSession)1 Function (org.apache.felix.service.command.Function)1 Completer (org.apache.karaf.shell.api.console.Completer)1 Parser (org.apache.karaf.shell.api.console.Parser)1 Session (org.apache.karaf.shell.api.console.Session)1 Command (org.apache.karaf.shell.commands.Command)1 Completer (org.apache.karaf.shell.console.Completer)1