Package org.apache.karaf.shell.commands

Examples of org.apache.karaf.shell.commands.Command


            String line = r.readLine();
            while (line != null) {
                line = line.trim();
                if (line.length() > 0 && line.charAt(0) != '#') {
                    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


            Function function = (Function) session.get(name);
            function = unProxy(function);
            if (function instanceof AbstractCommand) {
                try {
                    Class<? extends Action> actionClass = ((AbstractCommand) function).getActionClass();                   
                    Command ann = new ActionMetaDataFactory().getCommand(actionClass);
                    description = ann.description();
                } catch (Throwable e) {
                }
                if (name.startsWith("*:")) {
                    name = name.substring(2);
                }
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

            for (Class clazz : classes) {
                try {
                    Action action = (Action) clazz.newInstance();
                    ActionMetaData meta = new ActionMetaDataFactory().create(action.getClass());
                    String help = printHelp(action, meta, format, includeHelpOption);
                    Command cmd = meta.getCommand();
                    File output = null;

                    // skip the *-help command
                    if (cmd.scope().equals("*")) continue;

                    if (FORMAT_DOCBX.equals(format)) {
                        output = new File(targetFolder, cmd.scope() + "-" + cmd.name() + ".xml");
                    } else if (FORMAT_CONF.equals(format)) {
                        output = new File(targetFolder, cmd.scope() + "-" + cmd.name() + ".conf");
                    }
                    Writer writer = new OutputStreamWriter(new FileOutputStream(output));
                    writer.write(help);
                    writer.close();

                    Set<String> cmds = commands.get(cmd.scope());
                    if (cmds == null) {
                        cmds = new TreeSet<String>();
                        commands.put(cmd.scope(), cmds);
                    }
                    cmds.add(cmd.name());
                    getLog().info("Found command: " + cmd.scope() + ":" + cmd.name());
                } catch (Exception e) {
                    getLog().warn("Unable to write help for " + clazz.getName(), e);
                }
            }

View Full Code Here

            String line = r.readLine();
            while (line != null) {
                line = line.trim();
                if (line.length() > 0 && line.charAt(0) != '#') {
                    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

    }

    private void printUsageConf(Action action, ActionMetaData actionMeta, PrintStream out, boolean includeHelpOption) {
        Map<Option, Field> optionsMap = actionMeta.getOptions();
        Map<Argument, Field> argsMap = actionMeta.getArguments();
        Command command = action.getClass().getAnnotation(Command.class);
        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());
        if (includeHelpOption)
            options.add(HelpOption.HELP);

        out.println("<section>");
        out.print("  <title>");
        out.print(command.scope());
        out.print(":");
        out.print(command.name());
        out.println("</title>");
        out.println("  <section>");
        out.println("    <title>Description</title>");
        out.println("    <para>");
        out.println(command.description());
        out.println("    </para>");
        out.println("  </section>");

        StringBuffer syntax = new StringBuffer();
        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) {
                syntax.append(String.format(argument.required() ? "%s " : "[%s] ", argument.name()));
            }
        }
        out.println("  <section>");
        out.println("    <title>Syntax</title>");
        out.println("    <para>");
        out.println(syntax.toString());
        out.println("    </para>");
        out.println("  </section>");

        if (arguments.size() > 0) {
            out.println("  <section>");
            out.println("    <title>Arguments</title>");
            out.println("    <informaltable>");
            for (Argument argument : arguments) {
                out.println("    <tr>");
                out.println("      <td>" + argument.name() + "</td>");
                String description = argument.description();
                if (!argument.required()) {
                    try {
                        argsMap.get(argument).setAccessible(true);
                        Object o = argsMap.get(argument).get(action);
                        if (o != null && (!(o instanceof Boolean) || ((Boolean) o))
                                && (!(o instanceof Number) || ((Number) o).doubleValue() != 0.0)) {
                            description += " (defaults to " + o.toString() + ")";
                        }
                    } catch (Exception e) {
                        // Ignore
                    }
                }
                out.println("      <td>" + description + "</td>");
                out.println("    </tr>");
            }

            out.println("    </informaltable>");
            out.println("  </section>");
        }
        if (options.size() > 0) {
            out.println("  <section>");
            out.println("    <title>Options</title>");
            out.println("    <informaltable>");

            for (Option option : options) {
                String opt = option.name();
                String description = option.description();
                for (String alias : option.aliases()) {
                    opt += ", " + alias;
                }
                try {
                    optionsMap.get(option).get(action);
                    Object o = optionsMap.get(option).get(action);
                    if (o != null && (!(o instanceof Boolean) || ((Boolean) o))
                            && (!(o instanceof Number) || ((Number) o).doubleValue() != 0.0)) {
                        description += " (defaults to " + o.toString() + ")";
                    }

                } catch (Exception e) {

                }
                out.println("    <tr>");
                out.println("      <td>" + opt + "</td>");
                out.println("      <td>" + description + "</td>");
                out.println("    </tr>");
            }

            out.println("    </informaltable>");
            out.println("  </section>");

            if (command.detailedDescription() != null && command.detailedDescription().trim().length() > 0) {
                out.println("<section>");
                out.println("   <title>Details</title>");
                String description = loadDescription(action.getClass(), command.detailedDescription());
                out.println("   <para>");

            }
        }
        out.println("</section>");
View Full Code Here

    }

    private void printUsageDocBook(Action action, ActionMetaData actionMeta, PrintStream out, boolean includeHelpOption) {
        Map<Option, Field> optionsMap = actionMeta.getOptions();
        Map<Argument, Field> argsMap = actionMeta.getArguments();
        Command command = actionMeta.getCommand();
        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());
        if (includeHelpOption)
            options.add(HelpOption.HELP);

        out.println("h1. " + command.scope() + ":" + command.name());
        out.println();

        out.println("h2. Description");
        out.println(command.description());
        out.println();

        StringBuffer syntax = new StringBuffer();
        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) {
                syntax.append(String.format(argument.required() ? "%s " : "\\[%s\\] ", argument.name()));
            }
        }
        out.println("h2. Syntax");
        out.println(syntax.toString());
        out.println();

        if (arguments.size() > 0) {
            out.println("h2. Arguments");
            out.println("|| Name || Description ||");
            for (Argument argument : arguments) {
                String description = argument.description();
                if (!argument.required()) {
                    try {
                        argsMap.get(argument).setAccessible(true);
                        Object o = argsMap.get(argument).get(action);
                        if (o != null && (!(o instanceof Boolean) || ((Boolean) o))
                                && (!(o instanceof Number) || ((Number) o).doubleValue() != 0.0)) {
                            description += " (defaults to " + o.toString() + ")";
                        }
                    } catch (Throwable t) {
                        // Ignore
                    }
                }
                out.println("| " + argument.name() + " | " + description + " |");
            }
            out.println();
        }
        if (options.size() > 0) {
            out.println("h2. Options");
            out.println("|| Name || Description ||");
            for (Option option : options) {
                String opt = option.name();
                String desc = option.description();
                for (String alias : option.aliases()) {
                    opt += ", " + alias;
                }
                try {
                    optionsMap.get(option).setAccessible(true);
                    Object o = optionsMap.get(option).get(action);
                    if (o != null && (!(o instanceof Boolean) || ((Boolean) o))
                            && (!(o instanceof Number) || ((Number) o).doubleValue() != 0.0)) {
                        desc += " (defaults to " + o.toString() + ")";
                    }
                } catch (Throwable t) {
                    // Ignore
                }
                out.println("| " + opt + " | " + desc + " |");
            }
            out.println();
        }
        if (command.detailedDescription().length() > 0) {
            out.println("h2. Details");
            String desc = loadDescription(action.getClass(), command.detailedDescription());
            out.println(desc);
        }
        out.println();
    }
View Full Code Here

    public boolean prepare(Action action, CommandSession session, List<Object> params) throws Exception {
        ActionMetaData actionMetaData = new ActionMetaDataFactory().create(action.getClass());
        Map<Option, Field> options = actionMetaData.getOptions();
        Map<Argument, Field> arguments = actionMetaData.getArguments();
        List<Argument> orderedArguments = actionMetaData.getOrderedArguments();
        Command command2 = actionMetaData.getCommand();
        String commandErrorSt = (command2 != null) ? Ansi.ansi()
                .fg(Ansi.Color.RED)
                .a("Error executing command ")
                .a(command2.scope())
                .a(":")
                .a(Ansi.Attribute.INTENSITY_BOLD)
                .a(command2.name())
                .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                .fg(Ansi.Color.DEFAULT)
                .a(": ")
                .toString() : "";
        for (Iterator<Object> it = params.iterator(); it.hasNext(); ) {
View Full Code Here

    private static boolean nodeNameEquals(Node node, String name) {
        return (name.equals(node.getNodeName()) || name.equals(node.getLocalName()));
    }

    public static String getScope(Class<?> action) {
        Command command = action.getAnnotation(Command.class);
        return command.scope();
    }
View Full Code Here

        Command command = action.getAnnotation(Command.class);
        return command.scope();
    }

    public static String getName(Class<?> action) {
        Command command = action.getAnnotation(Command.class);
        return command.name();
    }
View Full Code Here

TOP

Related Classes of org.apache.karaf.shell.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.