Search in sources :

Example 1 with Action

use of org.apache.felix.gogo.commands.Action 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 Action

use of org.apache.felix.gogo.commands.Action 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 Action

use of org.apache.felix.gogo.commands.Action in project karaf by apache.

the class BlueprintCommand method createNewAction.

public Action createNewAction() {
    Action action = (Action) blueprintContainer.getComponentInstance(actionId);
    if (action instanceof BlueprintContainerAware) {
        ((BlueprintContainerAware) action).setBlueprintContainer(blueprintContainer);
    }
    if (action instanceof BundleContextAware) {
        BundleContext context = (BundleContext) blueprintContainer.getComponentInstance("blueprintBundleContext");
        ((BundleContextAware) action).setBundleContext(context);
    }
    return action;
}
Also used : Action(org.apache.felix.gogo.commands.Action) BundleContextAware(org.apache.karaf.shell.console.BundleContextAware) BlueprintContainerAware(org.apache.karaf.shell.console.BlueprintContainerAware) BundleContext(org.osgi.framework.BundleContext)

Aggregations

Action (org.apache.felix.gogo.commands.Action)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 CommandWithAction (org.apache.karaf.shell.commands.CommandWithAction)2 CompleterValues (org.apache.karaf.shell.commands.CompleterValues)2 BlueprintContainerAware (org.apache.karaf.shell.console.BlueprintContainerAware)1 BundleContextAware (org.apache.karaf.shell.console.BundleContextAware)1 BundleContext (org.osgi.framework.BundleContext)1