Package org.apache.felix.gogo.commands

Examples of org.apache.felix.gogo.commands.Command


    }

    private static void listCommands() {
        System.out.println("Available commands:");
        for (Map.Entry<String, Class<?>> entry : COMMANDS.entrySet()) {
            Command ann = entry.getValue().getAnnotation(Command.class);
            System.out.printf("  %s - %s\n", entry.getKey(), ann.description());
        }
       
        System.out.println("Type 'command --help' for more help on the specified command.");
    }
View Full Code Here


        return bean;
    }

    private Tuple<String, Object> getName(Object bean, String beanName) {
        Command command = null;
        if (bean.getClass().isAnnotationPresent(Command.class)) {
            command = bean.getClass().getAnnotation(Command.class);
        } else if (bean instanceof Advised) {
            try {
                Object target = ((Advised) bean).getTargetSource().getTarget();
                if (target.getClass().isAnnotationPresent(Command.class)) {
                    command = target.getClass().getAnnotation(Command.class);
                }
            } catch (Exception e) {
                log.warn("Error while trying to determine if Advised bean " + beanName + " is a Command.  This is " +
                        " non-terminal, but this bean will not be exposed as a command.  Error was:", e);
            }
        }

        if (command != null) {
            String scope = command.scope();
            String function = command.name();
            if (scope != null && function != null) {
                Object obj;
                if (bean instanceof Action) {
                    obj = new SimpleSpringBeanCommand(actionFactory, beanName);
                } else {
View Full Code Here

    }

    private static void listCommands() {
        System.out.println("Available commands:");
        for (Map.Entry<String, Class<?>> entry : COMMANDS.entrySet()) {
            Command ann = entry.getValue().getAnnotation(Command.class);
            System.out.printf("  %s - %s\n", entry.getKey(), ann.description());
        }
       
        System.out.println("Type 'command --help' for more help on the specified command.");
    }
View Full Code Here

                    line = line.trim();
                    if (line.isEmpty() || line.charAt(0) == '#') {
                        continue;
                    }
                    final Class<Action> actionClass = (Class<Action>) cl.loadClass(line);
                    Command cmd = actionClass.getAnnotation(Command.class);
                    Function function = new AbstractCommand() {
                        @Override
                        public Action createNewAction() {
                            try {
                                return ((Class<? extends Action>) actionClass).newInstance();
View Full Code Here

                        option = opt;
                        break;
                    }
                }
                if (option == null) {
                    Command command = action.getClass().getAnnotation(Command.class);
                    if (command != null) {
                        throw new CommandException(COLOR_RED
                                                   + "Error executing command "
                                                   + command.scope() + ":"
                                                   + INTENSITY_BOLD + command.name() + INTENSITY_NORMAL
                                                   + " undefined option "
                                                   + INTENSITY_BOLD + param + INTENSITY_NORMAL
                                                   + COLOR_DEFAULT,
                                                   "Undefined option: " + param
                        );
                    } else {
                        throw new CommandException("Undefined option: " + param);
                    }
                }
                Field field = options.get(option);
                if (value == null && (field.getType() == boolean.class || field.getType() == Boolean.class)) {
                    value = Boolean.TRUE;
                }
                if (value == null && it.hasNext()) {
                    value = it.next();
                }
                if (value == null) {
                    Command command = action.getClass().getAnnotation(Command.class);
                    if (command != null) {
                        throw new CommandException(COLOR_RED
                                                   + "Error executing command "
                                                   + command.scope() + ":"
                                                   + INTENSITY_BOLD + command.name() + INTENSITY_NORMAL
                                                   + " missing value for option "
                                                   + INTENSITY_BOLD + param + INTENSITY_NORMAL
                                                   + COLOR_DEFAULT,
                                                   "Missing value for option: " + param
                        );
                    } else {
                        throw new CommandException("Missing value for option: " + param);
                    }
                }
                if (option.multiValued()) {
                    List<Object> l = (List<Object>) optionValues.get(option);
                    if (l == null) {
                        l = new ArrayList<Object>();
                        optionValues.put(option, l);
                    }
                    l.add(value);
                } else {
                    optionValues.put(option, value);
                }
            } else {
                processOptions = false;
                if (argIndex >= orderedArguments.size()) {
                    Command command = action.getClass().getAnnotation(Command.class);
                    if (command != null) {
                        throw new CommandException(COLOR_RED
                                                   + "Error executing command "
                                                   + command.scope() + ":"
                                                   + INTENSITY_BOLD + command.name() + INTENSITY_NORMAL
                                                   + ": too many arguments specified"
                                                   + INTENSITY_BOLD + param + INTENSITY_NORMAL
                                                   + COLOR_DEFAULT,
                                                   "Too many arguments specified"
                        );
                    } else {
                        throw new CommandException("Too many arguments specified");
                    }
                }
                Argument argument = orderedArguments.get(argIndex);
                if (!argument.multiValued()) {
                    argIndex++;
                }
                if (argument.multiValued()) {
                    List<Object> l = (List<Object>) argumentValues.get(argument);
                    if (l == null) {
                        l = new ArrayList<Object>();
                        argumentValues.put(argument, l);
                    }
                    l.add(param);
                } else {
                    argumentValues.put(argument, param);
                }
            }
        }
        // Check required arguments / options
        for (Option option : options.keySet()) {
            if (option.required() && optionValues.get(option) == null) {
                Command command = action.getClass().getAnnotation(Command.class);
                if (command != null) {
                    throw new CommandException(COLOR_RED
                                               + "Error executing command "
                                               + command.scope() + ":"
                                               + INTENSITY_BOLD + command.name() + INTENSITY_NORMAL
                                               + ": option "
                                               + INTENSITY_BOLD + option.name() + INTENSITY_NORMAL
                                               + " is required"
                                               + COLOR_DEFAULT,
                            "Option " + option.name() + " is required"
                    );
                } else {
                    throw new CommandException("Option " + option.name() + " is required");
                }
            }
        }
        for (Argument argument : orderedArguments) {
            if (argument.required() && argumentValues.get(argument) == null) {
                Command command = action.getClass().getAnnotation(Command.class);
                if (command != null) {
                    throw new CommandException(COLOR_RED
                                               + "Error executing command "
                                               + command.scope() + ":"
                                               + INTENSITY_BOLD + command.name() + INTENSITY_NORMAL
                                               + ": argument "
                                               + INTENSITY_BOLD + argument.name() + INTENSITY_NORMAL
                                               + " is required"
                                               + COLOR_DEFAULT,
                                               "Argument " + argument.name() + " is required"
                    );
                } else {
                    throw new CommandException("Argument " + argument.name() + " is required");
                }
            }
        }
        // Convert and inject values
        for (Map.Entry<Option, Object> entry : optionValues.entrySet()) {
            Field field = options.get(entry.getKey());
            Object value;
            try {
                value = convert(action, session, entry.getValue(), field.getGenericType());
            } catch (Exception e) {
                Command command = action.getClass().getAnnotation(Command.class);
                if (command != null) {
                    throw new CommandException(COLOR_RED
                                               + "Error executing command "
                                               + command.scope() + ":"
                                               + INTENSITY_BOLD + command.name() + INTENSITY_NORMAL
                                               + ": unable to convert option "
                                               + INTENSITY_BOLD + entry.getKey().name() + INTENSITY_NORMAL
                                               + " with value '" + entry.getValue() + "' to type "
                                               + new GenericType(field.getGenericType()).toString()
                                               + COLOR_DEFAULT,
                            "Unable to convert option " + entry.getKey().name() + " with value '"
                                    + entry.getValue() + "' to type " + new GenericType(field.getGenericType()).toString(),
                            e
                    );
                } else {
                    throw new CommandException("Unable to convert option " + entry.getKey().name() + " with value '"
                            + entry.getValue() + "' to type " + new GenericType(field.getGenericType()).toString(),
                            e);
                }
            }
            field.setAccessible(true);
            field.set(action, value);
        }
        for (Map.Entry<Argument, Object> entry : argumentValues.entrySet()) {
            Field field = arguments.get(entry.getKey());
            Object value;
            try {
                value = convert(action, session, entry.getValue(), field.getGenericType());
            } catch (Exception e) {
                Command command = action.getClass().getAnnotation(Command.class);
                if (command != null) {
                    throw new CommandException(COLOR_RED
                                               + "Error executing command "
                                               + command.scope() + ":"
                                               + INTENSITY_BOLD + command.name() + INTENSITY_NORMAL
                                               + ": unable to convert argument "
                                               + INTENSITY_BOLD + entry.getKey().name() + INTENSITY_NORMAL
                                               + " with value '" + entry.getValue() + "' to type "
                                               + new GenericType(field.getGenericType()).toString()
                                               + COLOR_DEFAULT,
View Full Code Here

        }
        return true;
    }

    protected void printUsage(CommandSession session, Action action, Map<Option, Field> optionsMap, Map<Argument, Field> argsMap, PrintStream out) {
        Command command = action.getClass().getAnnotation(Command.class);
        if (command != null) {
           
            List<Argument> arguments = new ArrayList<Argument>(argsMap.keySet());
            Collections.sort(arguments, new Comparator<Argument>() {
                public int compare(Argument o1, Argument o2) {
                    return Integer.valueOf(o1.index()).compareTo(Integer.valueOf(o2.index()));
                }
            });
            Set<Option> options = new HashSet<Option>(optionsMap.keySet());
            options.add(HELP);
            boolean globalScope = NameScoping.isGlobalScope(session, command.scope());
            if (command != null && (command.description() != null || command.name() != null)) {
                out.println(INTENSITY_BOLD + "DESCRIPTION" + INTENSITY_NORMAL);
                out.print("        ");
                if (command.name() != null) {
                    if (globalScope) {
                        out.println(INTENSITY_BOLD + command.name() + INTENSITY_NORMAL);
                    } else {
                        out.println(command.scope() + ":" + INTENSITY_BOLD + command.name() + INTENSITY_NORMAL);
                    }
                    out.println();
                }
                out.print("\t");
                out.println(command.description());
                out.println();
            }
            StringBuffer syntax = new StringBuffer();
            if (command != null) {
                if (globalScope) {
                    syntax.append(command.name());
                } else {
                    syntax.append(String.format("%s:%s", command.scope(), command.name()));
                }
            }
            if (options.size() > 0) {
                syntax.append(" [options]");
            }
            if (arguments.size() > 0) {
                syntax.append(' ');
                for (Argument argument : arguments) {
                    if (!argument.required()) {
                        syntax.append(String.format("[%s] ", argument.name()));
                    } else {
                        syntax.append(String.format("%s ", argument.name()));
                    }
                }
            }
            int width = getWidth(session);
            out.println(INTENSITY_BOLD + "SYNTAX" + INTENSITY_NORMAL);
            out.print("        ");
            out.println(syntax.toString());
            out.println();
            if (arguments.size() > 0) {
                out.println(INTENSITY_BOLD + "ARGUMENTS" + INTENSITY_NORMAL);
                for (Argument argument : arguments) {
                    out.print("        ");
                    out.println(INTENSITY_BOLD + argument.name() + INTENSITY_NORMAL);
                   
                    printFormatted("                ", argument.description(), width, out);
                    if (!argument.required()) {
                        if (argument.valueToShowInHelp() != null && argument.valueToShowInHelp().length() != 0) {
                            try {
                                if (Argument.DEFAULT_STRING.equals(argument.valueToShowInHelp())) {
                                    argsMap.get(argument).setAccessible(true);
                                    Object o = argsMap.get(argument).get(action);
                                    printObjectDefaultsTo(out, o);
                                } else {
                                    printDefaultsTo(out, argument.valueToShowInHelp());
                                }
                            } catch (Throwable t) {
                                // Ignore
                            }
                        }
                    }
                }
                out.println();
            }
            if (options.size() > 0) {
                out.println(INTENSITY_BOLD + "OPTIONS" + INTENSITY_NORMAL);
                for (Option option : options) {
                    String opt = option.name();
                    for (String alias : option.aliases()) {
                        opt += ", " + alias;
                    }
                    out.print("        ");
                    out.println(INTENSITY_BOLD + opt + INTENSITY_NORMAL);
                    printFormatted("                ", option.description(), width, out);
                    if (option.valueToShowInHelp() != null && option.valueToShowInHelp().length() != 0) {
                        try {
                            if (Option.DEFAULT_STRING.equals(option.valueToShowInHelp())) {
                                optionsMap.get(option).setAccessible(true);
                                Object o = optionsMap.get(option).get(action);
                                printObjectDefaultsTo(out, o);
                            } else {
                                printDefaultsTo(out, option.valueToShowInHelp());
                            }
                        } catch (Throwable t) {
                            // Ignore
                        }
                    }
                }
                out.println();
            }
            if (command.detailedDescription().length() > 0) {
                out.println(INTENSITY_BOLD + "DETAILS" + INTENSITY_NORMAL);
                String desc = loadDescription(action.getClass(), command.detailedDescription());
                printFormatted("        ", desc, width, out);
            }
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.felix.gogo.commands.Command

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.