Search in sources :

Example 1 with AbstractCommand

use of jug.client.command.api.AbstractCommand in project tomee by apache.

the class Client method main.

public static void main(final String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("Pass the base url as parameter");
        return;
    }
    final ConsoleReader reader = new ConsoleReader(System.in, new OutputStreamWriter(System.out));
    reader.addCompletor(new FileNameCompletor());
    reader.addCompletor(new SimpleCompletor(CommandManager.keys().toArray(new String[CommandManager.size()])));
    String line;
    while ((line = reader.readLine(PROMPT)) != null) {
        if (EXIT_CMD.equals(line)) {
            break;
        }
        Class<?> cmdClass = null;
        for (Map.Entry<String, Class<?>> cmd : CommandManager.getCommands().entrySet()) {
            if (line.startsWith(cmd.getKey())) {
                cmdClass = cmd.getValue();
                break;
            }
        }
        if (cmdClass != null) {
            final ObjectRecipe recipe = new ObjectRecipe(cmdClass);
            recipe.setProperty("url", args[0]);
            recipe.setProperty("command", line);
            recipe.setProperty("commands", CommandManager.getCommands());
            recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
            recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
            recipe.allow(Option.NAMED_PARAMETERS);
            try {
                final AbstractCommand cmdInstance = (AbstractCommand) recipe.create();
                cmdInstance.execute(line);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.err.println("sorry i don't understand '" + line + "'");
        }
    }
}
Also used : ConsoleReader(jline.ConsoleReader) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) FileNameCompletor(jline.FileNameCompletor) SimpleCompletor(jline.SimpleCompletor) AbstractCommand(jug.client.command.api.AbstractCommand) OutputStreamWriter(java.io.OutputStreamWriter) Map(java.util.Map)

Aggregations

OutputStreamWriter (java.io.OutputStreamWriter)1 Map (java.util.Map)1 ConsoleReader (jline.ConsoleReader)1 FileNameCompletor (jline.FileNameCompletor)1 SimpleCompletor (jline.SimpleCompletor)1 AbstractCommand (jug.client.command.api.AbstractCommand)1 ObjectRecipe (org.apache.xbean.recipe.ObjectRecipe)1