Package org.apache.karaf.shell.console.completer

Examples of org.apache.karaf.shell.console.completer.StringsCompleter


        return "--service";
    }

    @Override
    public int complete(String buffer, int cursor, List<String> candidates) {
        StringsCompleter delegate = new StringsCompleter();
        try {
            Kubernetes kubernetes = kubernetesService.getKubernetes();
            if (kubernetes != null) {
                ServiceListSchema list = kubernetes.getServices();
                if (list != null) {
                    List<ServiceSchema> items = list.getItems();
                    if (items != null) {
                        for (ServiceSchema item : items) {
                            String id = item.getId();
                            delegate.getStrings().add(id);
                        }
                    }
                }
            }
        } catch (Exception ex) {
            LOG.warn("Caught: " + ex, ex);
        }
        return delegate.complete(buffer, cursor, candidates);
    }
View Full Code Here


        this.function = function;
        this.scope = scope;
        this.name = name;
        // Command name completer
        String[] names = scoped ? new String[] { name } : new String[] { name, scope + ":" + name };
        commandCompleter = new StringsCompleter(names);
        // Build options completer
        for (Class<?> type = function.getActionClass(); type != null; type = type.getSuperclass()) {
            for (Field field : type.getDeclaredFields()) {
                Option option = field.getAnnotation(Option.class);
                if (option != null) {
                    fields.put(option, field);
                    options.put(option.name(), option);
                    String[] aliases = option.aliases();
                    if (aliases != null) {
                        for (String alias : aliases) {
                            options.put(alias, option);
                        }
                    }
                }
                Argument argument = field.getAnnotation(Argument.class);
                if (argument != null) {
                    Integer key = argument.index();
                    if (arguments.containsKey(key)) {
                        LOGGER.warn("Duplicate @Argument annotations on class " + type.getName() + " for index: " + key + " see: " + field);
                    } else {
                        arguments.put(key, field);
                    }
                }
            }
        }
        options.put(HelpOption.HELP.name(), HelpOption.HELP);
        optionsCompleter = new StringsCompleter(options.keySet());

        // Build arguments completers
        List<Completer> argsCompleters = null;
        Map<String, Completer> optionalCompleters = null;

        if (function instanceof CompletableFunction) {
            Map<String, Completer> focl = ((CompletableFunction) function).getOptionalCompleters();
            List<Completer> fcl = ((CompletableFunction) function).getCompleters();
            if (focl != null || fcl != null) {
                argsCompleters = new ArrayList<Completer>();
                if (fcl != null) {
                    for (Completer c : fcl) {
                        argsCompleters.add(c == null ? NullCompleter.INSTANCE : c);
                    }
                }
                optionalCompleters = focl;
            }
        }
        if (argsCompleters == null) {
            final Map<Integer, Object> values = getCompleterValues(function);
            argsCompleters = new ArrayList<Completer>();
            boolean multi = false;
            for (int key = 0; key < arguments.size(); key++) {
                Completer completer = null;
                Field field = arguments.get(key);
                if (field != null) {
                    Argument argument = field.getAnnotation(Argument.class);
                    multi = (argument != null && argument.multiValued());
                    org.apache.karaf.shell.commands.Completer ann = field.getAnnotation(org.apache.karaf.shell.commands.Completer.class);
                    if (ann != null) {
                        Class clazz = ann.value();
                        String[] value = ann.values();
                        if (clazz != null) {
                            if (value.length > 0 && clazz == StringsCompleter.class) {
                                completer = new StringsCompleter(value, ann.caseSensitive());
                            } else {
                                BundleContext context = FrameworkUtil.getBundle(function.getClass()).getBundleContext();
                                completer = new ProxyServiceCompleter(context, clazz);
                            }
                        }
                    } else if (values.containsKey(key)) {
                        Object value = values.get(key);
                        if (value instanceof String[]) {
                            completer = new StringsCompleter((String[]) value);
                        } else if (value instanceof Collection) {
                            completer = new StringsCompleter((Collection<String>) value);
                        } else {
                            LOGGER.warn("Could not use value " + value + " as set of completions!");
                        }
                    } else {
                        completer = getDefaultCompleter(field);
                    }
                }
                if (completer == null) {
                    completer = NullCompleter.INSTANCE;
                }
                argsCompleters.add(completer);
            }
            if (argsCompleters.isEmpty() || !multi) {
                argsCompleters.add(NullCompleter.INSTANCE);
            }
            optionalCompleters = new HashMap<String, Completer>();
            for (Option option : fields.keySet()) {
                Completer completer = null;
                Field field = fields.get(option);
                if (field != null) {
                    org.apache.karaf.shell.commands.Completer ann = field.getAnnotation(org.apache.karaf.shell.commands.Completer.class);
                    if (ann != null) {
                        Class clazz = ann.value();
                        String[] value = ann.values();
                        if (clazz != null) {
                            if (value.length > 0 && clazz == StringsCompleter.class) {
                                completer = new StringsCompleter(value, ann.caseSensitive());
                            } else {
                                BundleContext context = FrameworkUtil.getBundle(function.getClass()).getBundleContext();
                                completer = new ProxyServiceCompleter(context, clazz);
                            }
                        }
View Full Code Here

        Completer completer = null;
        Class<?> type = field.getType();
        if (type.isAssignableFrom(File.class)) {
            completer = new FileCompleter(null);
        } else if (type.isAssignableFrom(Boolean.class) || type.isAssignableFrom(boolean.class)) {
            completer = new StringsCompleter(new String[] {"false", "true"}, false);
        } else if (type.isAssignableFrom(Enum.class)) {
            Set<String> values = new HashSet<String>();
            for (Object o : EnumSet.allOf((Class<Enum>) type)) {
                values.add(o.toString());
            }
            completer = new StringsCompleter(values, false);
        } else {
            // TODO any other completers we can add?
        }
        return completer;
    }
View Full Code Here

        this.function = function;
        this.scope = scope;
        this.name = name;
        // Command name completer
        String[] names = scoped ? new String[] { name } : new String[] { name, scope + ":" + name };
        commandCompleter = new StringsCompleter(names);
        // Build options completer
        for (Class<?> type = function.getActionClass(); type != null; type = type.getSuperclass()) {
            for (Field field : type.getDeclaredFields()) {
                Option option = field.getAnnotation(Option.class);
                if (option != null) {
                    fields.put(option, field);
                    options.put(option.name(), option);
                    String[] aliases = option.aliases();
                    if (aliases != null) {
                        for (String alias : aliases) {
                            options.put(alias, option);
                        }
                    }
                }
                Argument argument = field.getAnnotation(Argument.class);
                if (argument != null) {
                    Integer key = argument.index();
                    if (arguments.containsKey(key)) {
                        LOGGER.warn("Duplicate @Argument annotations on class " + type.getName() + " for index: " + key + " see: " + field);
                    } else {
                        arguments.put(key, field);
                    }
                }
            }
        }
//        options.put(HelpOption.HELP.name(), HelpOption.HELP);
        optionsCompleter = new StringsCompleter(options.keySet());
        // Build arguments completers
        argsCompleters = new ArrayList<Completer>();

        if (function instanceof CompletableFunction) {
            Map<String, Completer> opt;
            try {
                //
                opt = ((CompletableFunction) function).getOptionalCompleters();
            } catch (Throwable t) {
                opt = new HashMap<String, Completer>();
            }
            optionalCompleters = opt;
            List<Completer> fcl = ((CompletableFunction) function).getCompleters();
            if (fcl != null) {
                for (Completer c : fcl) {
                    argsCompleters.add(c == null ? NullCompleter.INSTANCE : c);
                }
            } else {
                argsCompleters.add(NullCompleter.INSTANCE);
            }
        } else {
            optionalCompleters = new HashMap<String, Completer>();
            final Map<Integer, Method> methods = new HashMap<Integer, Method>();
            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);
                        }
                        if (methods.containsKey(key)) {
                            LOGGER.warn("Duplicate @CompleterMethod annotations on class " + type.getName() + " for index: " + key + " see: " + method);
                        } else {
                            methods.put(key, method);
                        }
                    }
                }
            }
            for (int i = 0, size = arguments.size(); i < size; i++) {
                Completer argCompleter = NullCompleter.INSTANCE;
                Method method = methods.get(i);
                if (method != null) {
                    // lets invoke the method
                    Action action = function.createNewAction();
                    try {
                        Object value = method.invoke(action);
                        if (value instanceof String[]) {
                            argCompleter = new StringsCompleter((String[]) value);
                        } else if (value instanceof Collection) {
                            argCompleter = new StringsCompleter((Collection<String>) value);
                        } else {
                            LOGGER.warn("Could not use value " + value + " as set of completions!");
                        }
                    } 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 {
                        try {
                            function.releaseAction(action);
                        } catch (Exception e) {
                            LOGGER.warn("Failed to release action: " + action + ". " + e, e);
                        }
                    }
                } else {
                    Field field = arguments.get(i);
                    Class<?> type = field.getType();
                    if (type.isAssignableFrom(File.class)) {
                        argCompleter = new FileCompleter(null);
                    } else if (type.isAssignableFrom(Boolean.class) || type.isAssignableFrom(boolean.class)) {
                        argCompleter = new StringsCompleter(new String[] {"false", "true"}, false);
                    } else if (type.isAssignableFrom(Enum.class)) {
                        Set<String> values = new HashSet<String>();
                        for (Object o : EnumSet.allOf((Class<Enum>) type)) {
                            values.add(o.toString());
                        }
                        argCompleter = new StringsCompleter(values, false);
                    } else {
                        // TODO any other completers we can add?
                    }
                }
                argsCompleters.add(argCompleter);
View Full Code Here

TOP

Related Classes of org.apache.karaf.shell.console.completer.StringsCompleter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.