Search in sources :

Example 16 with ConsoleReader

use of jline.ConsoleReader in project jackrabbit by apache.

the class JcrClient method runInteractive.

/**
     * Run in interactive mode
     * @throws Exception
     *         if an Exception occurs
     */
public void runInteractive() throws Exception {
    // built jline console reader with history + tab completion
    ConsoleReader reader = new ConsoleReader();
    reader.setHistory(new History());
    reader.setUseHistory(true);
    // get all available commands for command tab completion
    Collection<CommandLine> commands = CommandLineFactory.getInstance().getCommandLines();
    List<String> commandNames = new ArrayList<String>();
    for (CommandLine c : commands) {
        commandNames.add(c.getName());
        for (Object alias : c.getAlias()) {
            commandNames.add((String) alias);
        }
    }
    commandNames.add("exit");
    commandNames.add("quit");
    // first part is the command, then all arguments will get children tab completion
    reader.addCompletor(new ArgumentCompletor(new Completor[] { new SimpleCompletor(commandNames.toArray(new String[] {})), new JcrChildrenCompletor() }));
    while (!exit) {
        try {
            String input = reader.readLine("[" + this.getPrompt() + "] > ");
            if (input == null) {
                input = "exit";
            } else {
                input = input.trim();
            }
            log.debug("running: " + input);
            if (input.equals("exit") || input.equals("quit")) {
                // exit?
                exit = true;
                System.out.println("Good bye...");
            } else if (input.length() > 0) {
                this.runCommand(input);
            }
        } catch (JcrParserException e) {
            System.out.println(e.getLocalizedMessage());
            System.out.println();
        } catch (Exception e) {
            handleException(reader, e);
        }
    }
}
Also used : ConsoleReader(jline.ConsoleReader) ArrayList(java.util.ArrayList) ArgumentCompletor(jline.ArgumentCompletor) History(jline.History) RepositoryException(javax.jcr.RepositoryException) InvalidItemStateException(javax.jcr.InvalidItemStateException) CommandException(org.apache.jackrabbit.standalone.cli.CommandException) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException) SimpleCompletor(jline.SimpleCompletor) SimpleCompletor(jline.SimpleCompletor) ArgumentCompletor(jline.ArgumentCompletor) Completor(jline.Completor)

Example 17 with ConsoleReader

use of jline.ConsoleReader 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

ConsoleReader (jline.ConsoleReader)17 IOException (java.io.IOException)8 DistributedLogClient (com.twitter.distributedlog.service.DistributedLogClient)4 History (jline.History)4 DLSN (com.twitter.distributedlog.DLSN)3 File (java.io.File)3 Map (java.util.Map)3 SimpleCompletor (jline.SimpleCompletor)3 PrintWriter (java.io.PrintWriter)2 Completor (jline.Completor)2 FileNameCompletor (jline.FileNameCompletor)2 ObjectRecipe (org.apache.xbean.recipe.ObjectRecipe)2 WindowingException (com.sap.hadoop.windowing.WindowingException)1 DistributedLogMultiStreamWriter (com.twitter.distributedlog.client.DistributedLogMultiStreamWriter)1 DistributedLogNamespace (com.twitter.distributedlog.namespace.DistributedLogNamespace)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1